8230168: Use ClasspathStream for FileMapInfo::create_path_array
authoriklam
Tue, 27 Aug 2019 22:14:15 -0700
changeset 57897 e2e315f1aa63
parent 57896 48f52ad5a2c3
child 57898 5ddb746d45e0
8230168: Use ClasspathStream for FileMapInfo::create_path_array Reviewed-by: lfoltan, fparain
src/hotspot/share/classfile/classLoader.cpp
src/hotspot/share/memory/filemap.cpp
src/hotspot/share/memory/filemap.hpp
src/hotspot/share/utilities/classpathStream.cpp
src/hotspot/share/utilities/classpathStream.hpp
--- a/src/hotspot/share/classfile/classLoader.cpp	Wed Aug 28 11:00:55 2019 +0800
+++ b/src/hotspot/share/classfile/classLoader.cpp	Tue Aug 27 22:14:15 2019 -0700
@@ -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"
@@ -168,41 +169,6 @@
   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);
--- a/src/hotspot/share/memory/filemap.cpp	Wed Aug 28 11:00:55 2019 +0800
+++ b/src/hotspot/share/memory/filemap.cpp	Tue Aug 27 22:14:15 2019 -0700
@@ -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"
@@ -566,32 +567,16 @@
   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;
@@ -603,7 +588,7 @@
   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;
@@ -657,7 +642,7 @@
   } 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) {
@@ -690,7 +675,7 @@
   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);
--- a/src/hotspot/share/memory/filemap.hpp	Wed Aug 28 11:00:55 2019 +0800
+++ b/src/hotspot/share/memory/filemap.hpp	Tue Aug 27 22:14:15 2019 -0700
@@ -379,11 +379,10 @@
  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,
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/hotspot/share/utilities/classpathStream.cpp	Tue Aug 27 22:14:15 2019 -0700
@@ -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;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/hotspot/share/utilities/classpathStream.hpp	Tue Aug 27 22:14:15 2019 -0700
@@ -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