Mercurial > hg > graal-jvmci-8
comparison src/share/vm/graal/graalRuntime.cpp @ 21515:1ab7802d35c9
Factor GraalRuntime::parse_lines out of GraalRuntime::get_service_impls
author | Gilles Duboscq <gilles.m.duboscq@oracle.com> |
---|---|
date | Thu, 07 May 2015 16:00:42 +0200 |
parents | 93dc70e47fb0 |
children | fe4a77bec5b7 |
comparison
equal
deleted
inserted
replaced
21514:b426469fadb7 | 21515:1ab7802d35c9 |
---|---|
1074 vm_abort(false); | 1074 vm_abort(false); |
1075 } | 1075 } |
1076 return klass; | 1076 return klass; |
1077 } | 1077 } |
1078 | 1078 |
1079 void GraalRuntime::parse_lines(char* path, ParseClosure* closure, TRAPS) { | |
1080 struct stat st; | |
1081 if (os::stat(path, &st) == 0) { | |
1082 int file_handle = os::open(path, 0, 0); | |
1083 if (file_handle != -1) { | |
1084 char* buffer = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, st.st_size + 1); | |
1085 int num_read = (int) os::read(file_handle, (char*) buffer, st.st_size); | |
1086 if (num_read == -1) { | |
1087 warning("Error reading file %s due to %s", path, strerror(errno)); | |
1088 } else if (num_read != st.st_size) { | |
1089 warning("Only read %d of " SIZE_FORMAT " bytes from %s", num_read, (size_t) st.st_size, path); | |
1090 } | |
1091 os::close(file_handle); | |
1092 if (num_read == st.st_size) { | |
1093 buffer[num_read] = '\0'; | |
1094 | |
1095 char* line = buffer; | |
1096 while (line - buffer < num_read) { | |
1097 // find line end (\r, \n or \r\n) | |
1098 char* nextline = NULL; | |
1099 char* cr = strchr(line, '\r'); | |
1100 char* lf = strchr(line, '\n'); | |
1101 if (cr != NULL && lf != NULL) { | |
1102 char* min = MIN2(cr, lf); | |
1103 *min = '\0'; | |
1104 if (lf == cr + 1) { | |
1105 nextline = lf + 1; | |
1106 } else { | |
1107 nextline = min + 1; | |
1108 } | |
1109 } else if (cr != NULL) { | |
1110 *cr = '\0'; | |
1111 nextline = cr + 1; | |
1112 } else if (lf != NULL) { | |
1113 *lf = '\0'; | |
1114 nextline = lf + 1; | |
1115 } | |
1116 // trim left | |
1117 while (*line == ' ' || *line == '\t') line++; | |
1118 char* end = line + strlen(line); | |
1119 // trim right | |
1120 while (end > line && (*(end -1) == ' ' || *(end -1) == '\t')) end--; | |
1121 *end = '\0'; | |
1122 // skip comments and empty lines | |
1123 if (*line != '#' && strlen(line) > 0) { | |
1124 closure->do_line(line); | |
1125 } | |
1126 if (nextline != NULL) { | |
1127 line = nextline; | |
1128 } else { | |
1129 // File without newline at the end | |
1130 break; | |
1131 } | |
1132 } | |
1133 } | |
1134 } else { | |
1135 warning("Error opening file %s due to %s", path, strerror(errno)); | |
1136 } | |
1137 } else { | |
1138 warning("Error opening file %s due to %s", path, strerror(errno)); | |
1139 } | |
1140 } | |
1141 | |
1142 class ServiceParseClosure : public ParseClosure { | |
1143 GrowableArray<char*> _implNames; | |
1144 public: | |
1145 ServiceParseClosure() : _implNames() {} | |
1146 void do_line(char* line) { | |
1147 // Turn all '.'s into '/'s | |
1148 for (size_t index = 0; line[index] != '\0'; index++) { | |
1149 if (line[index] == '.') { | |
1150 line[index] = '/'; | |
1151 } | |
1152 } | |
1153 _implNames.append(line); | |
1154 } | |
1155 GrowableArray<char*>* implNames() {return &_implNames;} | |
1156 }; | |
1157 | |
1158 | |
1079 Handle GraalRuntime::get_service_impls(KlassHandle serviceKlass, TRAPS) { | 1159 Handle GraalRuntime::get_service_impls(KlassHandle serviceKlass, TRAPS) { |
1080 const char* home = Arguments::get_java_home(); | 1160 const char* home = Arguments::get_java_home(); |
1081 const char* serviceName = serviceKlass->external_name(); | 1161 const char* serviceName = serviceKlass->external_name(); |
1082 size_t path_len = strlen(home) + strlen("/lib/graal/services/") + strlen(serviceName) + 1; | 1162 size_t path_len = strlen(home) + strlen("/lib/graal/services/") + strlen(serviceName) + 1; |
1083 char* path = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, path_len); | 1163 char* path = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, path_len); |
1084 char sep = os::file_separator()[0]; | 1164 char sep = os::file_separator()[0]; |
1085 sprintf(path, "%s%clib%cgraal%cservices%c%s", home, sep, sep, sep, sep, serviceName); | 1165 sprintf(path, "%s%clib%cgraal%cservices%c%s", home, sep, sep, sep, sep, serviceName); |
1086 struct stat st; | 1166 ServiceParseClosure closure; |
1087 if (os::stat(path, &st) == 0) { | 1167 parse_lines(path, &closure, THREAD); |
1088 int file_handle = os::open(path, 0, 0); | 1168 |
1089 if (file_handle != -1) { | 1169 GrowableArray<char*>* implNames = closure.implNames(); |
1090 char* buffer = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, st.st_size + 1); | 1170 objArrayOop servicesOop = oopFactory::new_objArray(serviceKlass(), implNames->length(), CHECK_NH); |
1091 int num_read = (int) os::read(file_handle, (char*) buffer, st.st_size); | 1171 objArrayHandle services(THREAD, servicesOop); |
1092 if (num_read == -1) { | 1172 for (int i = 0; i < implNames->length(); ++i) { |
1093 warning("Error reading file %s due to %s", path, strerror(errno)); | 1173 char* implName = implNames->at(i); |
1094 } else if (num_read != st.st_size) { | 1174 Handle service = create_Service(implName, CHECK_NH); |
1095 warning("Only read %d of " SIZE_FORMAT " bytes from %s", num_read, (size_t) st.st_size, path); | 1175 services->obj_at_put(i, service()); |
1096 } | 1176 } |
1097 os::close(file_handle); | 1177 return services; |
1098 if (num_read == st.st_size) { | |
1099 buffer[num_read] = '\0'; | |
1100 GrowableArray<char*>* implNames = new GrowableArray<char*>(); | |
1101 char* line = buffer; | |
1102 while (line - buffer < num_read) { | |
1103 // find line end (\r, \n or \r\n) | |
1104 char* nextline = NULL; | |
1105 char* cr = strchr(line, '\r'); | |
1106 char* lf = strchr(line, '\n'); | |
1107 if (cr != NULL && lf != NULL) { | |
1108 char* min = MIN2(cr, lf); | |
1109 *min = '\0'; | |
1110 if (lf == cr + 1) { | |
1111 nextline = lf + 1; | |
1112 } else { | |
1113 nextline = min + 1; | |
1114 } | |
1115 } else if (cr != NULL) { | |
1116 *cr = '\0'; | |
1117 nextline = cr + 1; | |
1118 } else if (lf != NULL) { | |
1119 *lf = '\0'; | |
1120 nextline = lf + 1; | |
1121 } | |
1122 // trim left | |
1123 while (*line == ' ' || *line == '\t') line++; | |
1124 char* end = line + strlen(line); | |
1125 // trim right | |
1126 while (end > line && (*(end -1) == ' ' || *(end -1) == '\t')) end--; | |
1127 *end = '\0'; | |
1128 // skip comments and empty lines | |
1129 if (*line != '#' && strlen(line) > 0) { | |
1130 // Turn all '.'s into '/'s | |
1131 for (size_t index = 0; line[index] != '\0'; index++) { | |
1132 if (line[index] == '.') { | |
1133 line[index] = '/'; | |
1134 } | |
1135 } | |
1136 implNames->append(line); | |
1137 } | |
1138 if (nextline != NULL) { | |
1139 line = nextline; | |
1140 } else { | |
1141 // File without newline at the end | |
1142 break; | |
1143 } | |
1144 } | |
1145 | |
1146 objArrayOop servicesOop = oopFactory::new_objArray(serviceKlass(), implNames->length(), CHECK_NH); | |
1147 objArrayHandle services(THREAD, servicesOop); | |
1148 for (int i = 0; i < implNames->length(); ++i) { | |
1149 char* implName = implNames->at(i); | |
1150 Handle service = create_Service(implName, CHECK_NH); | |
1151 services->obj_at_put(i, service()); | |
1152 } | |
1153 return services; | |
1154 } | |
1155 } else { | |
1156 warning("Error opening file %s due to %s", path, strerror(errno)); | |
1157 } | |
1158 } else { | |
1159 warning("Error opening file %s due to %s", path, strerror(errno)); | |
1160 } | |
1161 return Handle(); | |
1162 } | 1178 } |
1163 | 1179 |
1164 #include "graalRuntime.inline.hpp" | 1180 #include "graalRuntime.inline.hpp" |