# HG changeset patch # User Gilles Duboscq # Date 1431007242 -7200 # Node ID 1ab7802d35c9e2b187612777ec7005bf0dc7aee3 # Parent b426469fadb79b0f98dedc9f5f8a7be72a3ee7d0 Factor GraalRuntime::parse_lines out of GraalRuntime::get_service_impls diff -r b426469fadb7 -r 1ab7802d35c9 src/share/vm/graal/graalRuntime.cpp --- a/src/share/vm/graal/graalRuntime.cpp Wed May 06 17:04:06 2015 +0200 +++ b/src/share/vm/graal/graalRuntime.cpp Thu May 07 16:00:42 2015 +0200 @@ -1076,6 +1076,86 @@ return klass; } +void GraalRuntime::parse_lines(char* path, ParseClosure* closure, TRAPS) { + struct stat st; + if (os::stat(path, &st) == 0) { + int file_handle = os::open(path, 0, 0); + if (file_handle != -1) { + char* buffer = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, st.st_size + 1); + int num_read = (int) os::read(file_handle, (char*) buffer, st.st_size); + if (num_read == -1) { + warning("Error reading file %s due to %s", path, strerror(errno)); + } else if (num_read != st.st_size) { + warning("Only read %d of " SIZE_FORMAT " bytes from %s", num_read, (size_t) st.st_size, path); + } + os::close(file_handle); + if (num_read == st.st_size) { + buffer[num_read] = '\0'; + + char* line = buffer; + while (line - buffer < num_read) { + // find line end (\r, \n or \r\n) + char* nextline = NULL; + char* cr = strchr(line, '\r'); + char* lf = strchr(line, '\n'); + if (cr != NULL && lf != NULL) { + char* min = MIN2(cr, lf); + *min = '\0'; + if (lf == cr + 1) { + nextline = lf + 1; + } else { + nextline = min + 1; + } + } else if (cr != NULL) { + *cr = '\0'; + nextline = cr + 1; + } else if (lf != NULL) { + *lf = '\0'; + nextline = lf + 1; + } + // trim left + while (*line == ' ' || *line == '\t') line++; + char* end = line + strlen(line); + // trim right + while (end > line && (*(end -1) == ' ' || *(end -1) == '\t')) end--; + *end = '\0'; + // skip comments and empty lines + if (*line != '#' && strlen(line) > 0) { + closure->do_line(line); + } + if (nextline != NULL) { + line = nextline; + } else { + // File without newline at the end + break; + } + } + } + } else { + warning("Error opening file %s due to %s", path, strerror(errno)); + } + } else { + warning("Error opening file %s due to %s", path, strerror(errno)); + } +} + +class ServiceParseClosure : public ParseClosure { + GrowableArray _implNames; +public: + ServiceParseClosure() : _implNames() {} + void do_line(char* line) { + // Turn all '.'s into '/'s + for (size_t index = 0; line[index] != '\0'; index++) { + if (line[index] == '.') { + line[index] = '/'; + } + } + _implNames.append(line); + } + GrowableArray* implNames() {return &_implNames;} +}; + + Handle GraalRuntime::get_service_impls(KlassHandle serviceKlass, TRAPS) { const char* home = Arguments::get_java_home(); const char* serviceName = serviceKlass->external_name(); @@ -1083,82 +1163,18 @@ char* path = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, path_len); char sep = os::file_separator()[0]; sprintf(path, "%s%clib%cgraal%cservices%c%s", home, sep, sep, sep, sep, serviceName); - struct stat st; - if (os::stat(path, &st) == 0) { - int file_handle = os::open(path, 0, 0); - if (file_handle != -1) { - char* buffer = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, st.st_size + 1); - int num_read = (int) os::read(file_handle, (char*) buffer, st.st_size); - if (num_read == -1) { - warning("Error reading file %s due to %s", path, strerror(errno)); - } else if (num_read != st.st_size) { - warning("Only read %d of " SIZE_FORMAT " bytes from %s", num_read, (size_t) st.st_size, path); - } - os::close(file_handle); - if (num_read == st.st_size) { - buffer[num_read] = '\0'; - GrowableArray* implNames = new GrowableArray(); - char* line = buffer; - while (line - buffer < num_read) { - // find line end (\r, \n or \r\n) - char* nextline = NULL; - char* cr = strchr(line, '\r'); - char* lf = strchr(line, '\n'); - if (cr != NULL && lf != NULL) { - char* min = MIN2(cr, lf); - *min = '\0'; - if (lf == cr + 1) { - nextline = lf + 1; - } else { - nextline = min + 1; - } - } else if (cr != NULL) { - *cr = '\0'; - nextline = cr + 1; - } else if (lf != NULL) { - *lf = '\0'; - nextline = lf + 1; - } - // trim left - while (*line == ' ' || *line == '\t') line++; - char* end = line + strlen(line); - // trim right - while (end > line && (*(end -1) == ' ' || *(end -1) == '\t')) end--; - *end = '\0'; - // skip comments and empty lines - if (*line != '#' && strlen(line) > 0) { - // Turn all '.'s into '/'s - for (size_t index = 0; line[index] != '\0'; index++) { - if (line[index] == '.') { - line[index] = '/'; - } - } - implNames->append(line); - } - if (nextline != NULL) { - line = nextline; - } else { - // File without newline at the end - break; - } - } + ServiceParseClosure closure; + parse_lines(path, &closure, THREAD); - objArrayOop servicesOop = oopFactory::new_objArray(serviceKlass(), implNames->length(), CHECK_NH); - objArrayHandle services(THREAD, servicesOop); - for (int i = 0; i < implNames->length(); ++i) { - char* implName = implNames->at(i); - Handle service = create_Service(implName, CHECK_NH); - services->obj_at_put(i, service()); - } - return services; - } - } else { - warning("Error opening file %s due to %s", path, strerror(errno)); - } - } else { - warning("Error opening file %s due to %s", path, strerror(errno)); + GrowableArray* implNames = closure.implNames(); + objArrayOop servicesOop = oopFactory::new_objArray(serviceKlass(), implNames->length(), CHECK_NH); + objArrayHandle services(THREAD, servicesOop); + for (int i = 0; i < implNames->length(); ++i) { + char* implName = implNames->at(i); + Handle service = create_Service(implName, CHECK_NH); + services->obj_at_put(i, service()); } - return Handle(); + return services; } #include "graalRuntime.inline.hpp" diff -r b426469fadb7 -r 1ab7802d35c9 src/share/vm/graal/graalRuntime.hpp --- a/src/share/vm/graal/graalRuntime.hpp Wed May 06 17:04:06 2015 +0200 +++ b/src/share/vm/graal/graalRuntime.hpp Thu May 07 16:00:42 2015 +0200 @@ -28,6 +28,11 @@ #include "memory/allocation.hpp" #include "runtime/deoptimization.hpp" +class ParseClosure : public StackObj { + public: + virtual void do_line(char* line) = 0; +}; + class GraalRuntime: public CHeapObj { private: @@ -177,6 +182,8 @@ */ static Handle get_service_impls(KlassHandle serviceKlass, TRAPS); + static void parse_lines(char* path, ParseClosure* closure, TRAPS); + /** * Aborts the VM due to an unexpected exception. */