Skip to content

Commit

Permalink
8230168: Use ClasspathStream for FileMapInfo::create_path_array
Browse files Browse the repository at this point in the history
Reviewed-by: lfoltan, fparain
  • Loading branch information
iklam committed Aug 28, 2019
1 parent f85fe3a commit 11ca73d
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 68 deletions.
36 changes: 1 addition & 35 deletions src/hotspot/share/classfile/classLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
#include "runtime/vm_version.hpp"
#include "services/management.hpp"
#include "services/threadService.hpp"
#include "utilities/classpathStream.hpp"
#include "utilities/events.hpp"
#include "utilities/hashtable.inline.hpp"
#include "utilities/macros.hpp"
Expand Down Expand Up @@ -168,41 +169,6 @@ static const char* get_jimage_version_string() {
return (const char*)version_string;
}

class ClasspathStream : public StackObj {
const char* _class_path;
int _len;
int _start;
int _end;

public:
ClasspathStream(const char* class_path) {
_class_path = class_path;
_len = (int)strlen(class_path);
_start = 0;
_end = 0;
}

bool has_next() {
return _start < _len;
}

const char* get_next() {
while (_class_path[_end] != '\0' && _class_path[_end] != os::path_separator()[0]) {
_end++;
}
int path_len = _end - _start;
char* path = NEW_RESOURCE_ARRAY(char, path_len + 1);
strncpy(path, &_class_path[_start], path_len);
path[path_len] = '\0';

while (_class_path[_end] == os::path_separator()[0]) {
_end++;
}
_start = _end;
return path;
}
};

bool ClassLoader::string_ends_with(const char* str, const char* str_to_find) {
size_t str_len = strlen(str);
size_t str_to_find_len = strlen(str_to_find);
Expand Down
43 changes: 14 additions & 29 deletions src/hotspot/share/memory/filemap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
#include "runtime/vm_version.hpp"
#include "services/memTracker.hpp"
#include "utilities/align.hpp"
#include "utilities/classpathStream.hpp"
#include "utilities/defaultStream.hpp"
#if INCLUDE_G1GC
#include "gc/g1/g1CollectedHeap.hpp"
Expand Down Expand Up @@ -566,32 +567,16 @@ int FileMapInfo::num_paths(const char* path) {
return npaths;
}

GrowableArray<char*>* FileMapInfo::create_path_array(const char* path) {
GrowableArray<char*>* path_array = new(ResourceObj::RESOURCE_AREA, mtInternal)
GrowableArray<char*>(10);
char* begin_ptr = (char*)path;
char* end_ptr = strchr((char*)path, os::path_separator()[0]);
if (end_ptr == NULL) {
end_ptr = strchr((char*)path, '\0');
}
while (end_ptr != NULL) {
if ((end_ptr - begin_ptr) > 1) {
struct stat st;
char* temp_name = NEW_RESOURCE_ARRAY(char, (size_t)(end_ptr - begin_ptr + 1));
strncpy(temp_name, begin_ptr, end_ptr - begin_ptr);
temp_name[end_ptr - begin_ptr] = '\0';
if (os::stat(temp_name, &st) == 0) {
path_array->append(temp_name);
}
}
if (end_ptr < (path + strlen(path))) {
begin_ptr = ++end_ptr;
end_ptr = strchr(begin_ptr, os::path_separator()[0]);
if (end_ptr == NULL) {
end_ptr = strchr(begin_ptr, '\0');
}
} else {
break;
GrowableArray<const char*>* FileMapInfo::create_path_array(const char* paths) {
GrowableArray<const char*>* path_array = new(ResourceObj::RESOURCE_AREA, mtInternal)
GrowableArray<const char*>(10);

ClasspathStream cp_stream(paths);
while (cp_stream.has_next()) {
const char* path = cp_stream.get_next();
struct stat st;
if (os::stat(path, &st) == 0) {
path_array->append(path);
}
}
return path_array;
Expand All @@ -603,7 +588,7 @@ bool FileMapInfo::fail(const char* msg, const char* name) {
return false;
}

bool FileMapInfo::check_paths(int shared_path_start_idx, int num_paths, GrowableArray<char*>* rp_array) {
bool FileMapInfo::check_paths(int shared_path_start_idx, int num_paths, GrowableArray<const char*>* rp_array) {
int i = 0;
int j = shared_path_start_idx;
bool mismatch = false;
Expand Down Expand Up @@ -657,7 +642,7 @@ bool FileMapInfo::validate_boot_class_paths() {
} else if (dp_len > 0 && rp != NULL) {
int num;
ResourceMark rm;
GrowableArray<char*>* rp_array = create_path_array(rp);
GrowableArray<const char*>* rp_array = create_path_array(rp);
int rp_len = rp_array->length();
if (rp_len >= dp_len) {
if (relaxed_check) {
Expand Down Expand Up @@ -690,7 +675,7 @@ bool FileMapInfo::validate_app_class_paths(int shared_app_paths_len) {
if (shared_app_paths_len != 0 && rp_len != 0) {
// Prefix is OK: E.g., dump with -cp foo.jar, but run with -cp foo.jar:bar.jar.
ResourceMark rm;
GrowableArray<char*>* rp_array = create_path_array(appcp);
GrowableArray<const char*>* rp_array = create_path_array(appcp);
if (rp_array->length() == 0) {
// None of the jar file specified in the runtime -cp exists.
return fail("None of the jar file specified in the runtime -cp exists: -Djava.class.path=", appcp);
Expand Down
7 changes: 3 additions & 4 deletions src/hotspot/share/memory/filemap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,11 +379,10 @@ class FileMapInfo : public CHeapObj<mtInternal> {
private:
char* skip_first_path_entry(const char* path) NOT_CDS_RETURN_(NULL);
int num_paths(const char* path) NOT_CDS_RETURN_(0);
GrowableArray<char*>* create_path_array(const char* path) NOT_CDS_RETURN_(NULL);
GrowableArray<const char*>* create_path_array(const char* path) NOT_CDS_RETURN_(NULL);
bool fail(const char* msg, const char* name) NOT_CDS_RETURN_(false);
bool check_paths(int shared_path_start_idx,
int num_paths,
GrowableArray<char*>* rp_array) NOT_CDS_RETURN_(false);
bool check_paths(int shared_path_start_idx, int num_paths,
GrowableArray<const char*>* rp_array) NOT_CDS_RETURN_(false);
bool validate_boot_class_paths() NOT_CDS_RETURN_(false);
bool validate_app_class_paths(int shared_app_paths_len) NOT_CDS_RETURN_(false);
bool map_heap_data(MemRegion **heap_mem, int first, int max, int* num,
Expand Down
44 changes: 44 additions & 0 deletions src/hotspot/share/utilities/classpathStream.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/

#include "precompiled.hpp"
#include "memory/allocation.inline.hpp"
#include "runtime/os.hpp"
#include "utilities/classpathStream.hpp"

const char* ClasspathStream::get_next() {
while (_class_path[_end] != '\0' && _class_path[_end] != os::path_separator()[0]) {
_end++;
}
int path_len = _end - _start;
char* path = NEW_RESOURCE_ARRAY(char, path_len + 1);
strncpy(path, &_class_path[_start], path_len);
path[path_len] = '\0';

while (_class_path[_end] == os::path_separator()[0]) {
_end++;
}
_start = _end;
return path;
}
49 changes: 49 additions & 0 deletions src/hotspot/share/utilities/classpathStream.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/

#ifndef SHARE_UTILITIES_CLASSPATHSTREAM_HPP
#define SHARE_UTILITIES_CLASSPATHSTREAM_HPP

class ClasspathStream : public StackObj {
const char* _class_path;
int _len;
int _start;
int _end;

public:
ClasspathStream(const char* class_path) {
_class_path = class_path;
_len = (int)strlen(class_path);
_start = 0;
_end = 0;
}

bool has_next() {
return _start < _len;
}

const char* get_next();
};

#endif // SHARE_UTILITIES_CLASSPATHSTREAM_HPP

0 comments on commit 11ca73d

Please sign in to comment.