Skip to content

Commit 11ca73d

Browse files
committed
8230168: Use ClasspathStream for FileMapInfo::create_path_array
Reviewed-by: lfoltan, fparain
1 parent f85fe3a commit 11ca73d

File tree

5 files changed

+111
-68
lines changed

5 files changed

+111
-68
lines changed

src/hotspot/share/classfile/classLoader.cpp

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
#include "runtime/vm_version.hpp"
7070
#include "services/management.hpp"
7171
#include "services/threadService.hpp"
72+
#include "utilities/classpathStream.hpp"
7273
#include "utilities/events.hpp"
7374
#include "utilities/hashtable.inline.hpp"
7475
#include "utilities/macros.hpp"
@@ -168,41 +169,6 @@ static const char* get_jimage_version_string() {
168169
return (const char*)version_string;
169170
}
170171

171-
class ClasspathStream : public StackObj {
172-
const char* _class_path;
173-
int _len;
174-
int _start;
175-
int _end;
176-
177-
public:
178-
ClasspathStream(const char* class_path) {
179-
_class_path = class_path;
180-
_len = (int)strlen(class_path);
181-
_start = 0;
182-
_end = 0;
183-
}
184-
185-
bool has_next() {
186-
return _start < _len;
187-
}
188-
189-
const char* get_next() {
190-
while (_class_path[_end] != '\0' && _class_path[_end] != os::path_separator()[0]) {
191-
_end++;
192-
}
193-
int path_len = _end - _start;
194-
char* path = NEW_RESOURCE_ARRAY(char, path_len + 1);
195-
strncpy(path, &_class_path[_start], path_len);
196-
path[path_len] = '\0';
197-
198-
while (_class_path[_end] == os::path_separator()[0]) {
199-
_end++;
200-
}
201-
_start = _end;
202-
return path;
203-
}
204-
};
205-
206172
bool ClassLoader::string_ends_with(const char* str, const char* str_to_find) {
207173
size_t str_len = strlen(str);
208174
size_t str_to_find_len = strlen(str_to_find);

src/hotspot/share/memory/filemap.cpp

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
#include "runtime/vm_version.hpp"
5656
#include "services/memTracker.hpp"
5757
#include "utilities/align.hpp"
58+
#include "utilities/classpathStream.hpp"
5859
#include "utilities/defaultStream.hpp"
5960
#if INCLUDE_G1GC
6061
#include "gc/g1/g1CollectedHeap.hpp"
@@ -566,32 +567,16 @@ int FileMapInfo::num_paths(const char* path) {
566567
return npaths;
567568
}
568569

569-
GrowableArray<char*>* FileMapInfo::create_path_array(const char* path) {
570-
GrowableArray<char*>* path_array = new(ResourceObj::RESOURCE_AREA, mtInternal)
571-
GrowableArray<char*>(10);
572-
char* begin_ptr = (char*)path;
573-
char* end_ptr = strchr((char*)path, os::path_separator()[0]);
574-
if (end_ptr == NULL) {
575-
end_ptr = strchr((char*)path, '\0');
576-
}
577-
while (end_ptr != NULL) {
578-
if ((end_ptr - begin_ptr) > 1) {
579-
struct stat st;
580-
char* temp_name = NEW_RESOURCE_ARRAY(char, (size_t)(end_ptr - begin_ptr + 1));
581-
strncpy(temp_name, begin_ptr, end_ptr - begin_ptr);
582-
temp_name[end_ptr - begin_ptr] = '\0';
583-
if (os::stat(temp_name, &st) == 0) {
584-
path_array->append(temp_name);
585-
}
586-
}
587-
if (end_ptr < (path + strlen(path))) {
588-
begin_ptr = ++end_ptr;
589-
end_ptr = strchr(begin_ptr, os::path_separator()[0]);
590-
if (end_ptr == NULL) {
591-
end_ptr = strchr(begin_ptr, '\0');
592-
}
593-
} else {
594-
break;
570+
GrowableArray<const char*>* FileMapInfo::create_path_array(const char* paths) {
571+
GrowableArray<const char*>* path_array = new(ResourceObj::RESOURCE_AREA, mtInternal)
572+
GrowableArray<const char*>(10);
573+
574+
ClasspathStream cp_stream(paths);
575+
while (cp_stream.has_next()) {
576+
const char* path = cp_stream.get_next();
577+
struct stat st;
578+
if (os::stat(path, &st) == 0) {
579+
path_array->append(path);
595580
}
596581
}
597582
return path_array;
@@ -603,7 +588,7 @@ bool FileMapInfo::fail(const char* msg, const char* name) {
603588
return false;
604589
}
605590

606-
bool FileMapInfo::check_paths(int shared_path_start_idx, int num_paths, GrowableArray<char*>* rp_array) {
591+
bool FileMapInfo::check_paths(int shared_path_start_idx, int num_paths, GrowableArray<const char*>* rp_array) {
607592
int i = 0;
608593
int j = shared_path_start_idx;
609594
bool mismatch = false;
@@ -657,7 +642,7 @@ bool FileMapInfo::validate_boot_class_paths() {
657642
} else if (dp_len > 0 && rp != NULL) {
658643
int num;
659644
ResourceMark rm;
660-
GrowableArray<char*>* rp_array = create_path_array(rp);
645+
GrowableArray<const char*>* rp_array = create_path_array(rp);
661646
int rp_len = rp_array->length();
662647
if (rp_len >= dp_len) {
663648
if (relaxed_check) {
@@ -690,7 +675,7 @@ bool FileMapInfo::validate_app_class_paths(int shared_app_paths_len) {
690675
if (shared_app_paths_len != 0 && rp_len != 0) {
691676
// Prefix is OK: E.g., dump with -cp foo.jar, but run with -cp foo.jar:bar.jar.
692677
ResourceMark rm;
693-
GrowableArray<char*>* rp_array = create_path_array(appcp);
678+
GrowableArray<const char*>* rp_array = create_path_array(appcp);
694679
if (rp_array->length() == 0) {
695680
// None of the jar file specified in the runtime -cp exists.
696681
return fail("None of the jar file specified in the runtime -cp exists: -Djava.class.path=", appcp);

src/hotspot/share/memory/filemap.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -379,11 +379,10 @@ class FileMapInfo : public CHeapObj<mtInternal> {
379379
private:
380380
char* skip_first_path_entry(const char* path) NOT_CDS_RETURN_(NULL);
381381
int num_paths(const char* path) NOT_CDS_RETURN_(0);
382-
GrowableArray<char*>* create_path_array(const char* path) NOT_CDS_RETURN_(NULL);
382+
GrowableArray<const char*>* create_path_array(const char* path) NOT_CDS_RETURN_(NULL);
383383
bool fail(const char* msg, const char* name) NOT_CDS_RETURN_(false);
384-
bool check_paths(int shared_path_start_idx,
385-
int num_paths,
386-
GrowableArray<char*>* rp_array) NOT_CDS_RETURN_(false);
384+
bool check_paths(int shared_path_start_idx, int num_paths,
385+
GrowableArray<const char*>* rp_array) NOT_CDS_RETURN_(false);
387386
bool validate_boot_class_paths() NOT_CDS_RETURN_(false);
388387
bool validate_app_class_paths(int shared_app_paths_len) NOT_CDS_RETURN_(false);
389388
bool map_heap_data(MemRegion **heap_mem, int first, int max, int* num,
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*
23+
*/
24+
25+
#include "precompiled.hpp"
26+
#include "memory/allocation.inline.hpp"
27+
#include "runtime/os.hpp"
28+
#include "utilities/classpathStream.hpp"
29+
30+
const char* ClasspathStream::get_next() {
31+
while (_class_path[_end] != '\0' && _class_path[_end] != os::path_separator()[0]) {
32+
_end++;
33+
}
34+
int path_len = _end - _start;
35+
char* path = NEW_RESOURCE_ARRAY(char, path_len + 1);
36+
strncpy(path, &_class_path[_start], path_len);
37+
path[path_len] = '\0';
38+
39+
while (_class_path[_end] == os::path_separator()[0]) {
40+
_end++;
41+
}
42+
_start = _end;
43+
return path;
44+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*
23+
*/
24+
25+
#ifndef SHARE_UTILITIES_CLASSPATHSTREAM_HPP
26+
#define SHARE_UTILITIES_CLASSPATHSTREAM_HPP
27+
28+
class ClasspathStream : public StackObj {
29+
const char* _class_path;
30+
int _len;
31+
int _start;
32+
int _end;
33+
34+
public:
35+
ClasspathStream(const char* class_path) {
36+
_class_path = class_path;
37+
_len = (int)strlen(class_path);
38+
_start = 0;
39+
_end = 0;
40+
}
41+
42+
bool has_next() {
43+
return _start < _len;
44+
}
45+
46+
const char* get_next();
47+
};
48+
49+
#endif // SHARE_UTILITIES_CLASSPATHSTREAM_HPP

0 commit comments

Comments
 (0)