| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "vm/globals.h" | 5 #include "vm/globals.h" |
| 6 #if defined(TARGET_OS_LINUX) || defined(TARGET_OS_ANDROID) | 6 #if defined(TARGET_OS_LINUX) || defined(TARGET_OS_ANDROID) |
| 7 | 7 |
| 8 #include "vm/proccpuinfo.h" | 8 #include "vm/proccpuinfo.h" |
| 9 | 9 |
| 10 #include <ctype.h> // NOLINT | 10 #include <ctype.h> // NOLINT |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 size_t n = fread(buffer, 1, sizeof(buffer), fp); | 31 size_t n = fread(buffer, 1, sizeof(buffer), fp); |
| 32 if (n == 0) { | 32 if (n == 0) { |
| 33 break; | 33 break; |
| 34 } | 34 } |
| 35 datalen_ += n; | 35 datalen_ += n; |
| 36 } | 36 } |
| 37 fclose(fp); | 37 fclose(fp); |
| 38 } | 38 } |
| 39 | 39 |
| 40 // Read the contents of the cpuinfo file. | 40 // Read the contents of the cpuinfo file. |
| 41 data_ = new char[datalen_ + 1]; | 41 data_ = reinterpret_cast<char*>(malloc(datalen_ + 1)); |
| 42 fp = fopen(PATHNAME, "r"); | 42 fp = fopen(PATHNAME, "r"); |
| 43 if (fp != NULL) { | 43 if (fp != NULL) { |
| 44 for (intptr_t offset = 0; offset < datalen_; ) { | 44 for (intptr_t offset = 0; offset < datalen_; ) { |
| 45 size_t n = fread(data_ + offset, 1, datalen_ - offset, fp); | 45 size_t n = fread(data_ + offset, 1, datalen_ - offset, fp); |
| 46 if (n == 0) { | 46 if (n == 0) { |
| 47 break; | 47 break; |
| 48 } | 48 } |
| 49 offset += n; | 49 offset += n; |
| 50 } | 50 } |
| 51 fclose(fp); | 51 fclose(fp); |
| 52 } | 52 } |
| 53 | 53 |
| 54 // Zero-terminate the data. | 54 // Zero-terminate the data. |
| 55 data_[datalen_] = '\0'; | 55 data_[datalen_] = '\0'; |
| 56 } | 56 } |
| 57 | 57 |
| 58 | 58 |
| 59 void ProcCpuInfo::Cleanup() { | 59 void ProcCpuInfo::Cleanup() { |
| 60 ASSERT(data_); | 60 ASSERT(data_); |
| 61 delete[] data_; | 61 free(data_); |
| 62 data_ = NULL; | 62 data_ = NULL; |
| 63 } | 63 } |
| 64 | 64 |
| 65 | 65 |
| 66 char* ProcCpuInfo::FieldStart(const char* field) { | 66 char* ProcCpuInfo::FieldStart(const char* field) { |
| 67 // Look for first field occurrence, and ensure it starts the line. | 67 // Look for first field occurrence, and ensure it starts the line. |
| 68 size_t fieldlen = strlen(field); | 68 size_t fieldlen = strlen(field); |
| 69 char* p = data_; | 69 char* p = data_; |
| 70 for (;;) { | 70 for (;;) { |
| 71 p = strstr(p, field); | 71 p = strstr(p, field); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 *q = '\0'; | 108 *q = '\0'; |
| 109 bool ret = (strcasestr(p, search_string) != NULL); | 109 bool ret = (strcasestr(p, search_string) != NULL); |
| 110 *q = saved_end; | 110 *q = saved_end; |
| 111 | 111 |
| 112 return ret; | 112 return ret; |
| 113 } | 113 } |
| 114 | 114 |
| 115 | 115 |
| 116 // Extract the content of a the first occurrence of a given field in | 116 // Extract the content of a the first occurrence of a given field in |
| 117 // the content of the cpuinfo file and return it as a heap-allocated | 117 // the content of the cpuinfo file and return it as a heap-allocated |
| 118 // string that must be freed by the caller using delete[]. | 118 // string that must be freed by the caller using free. |
| 119 // Return NULL if not found. | 119 // Return NULL if not found. |
| 120 const char* ProcCpuInfo::ExtractField(const char* field) { | 120 const char* ProcCpuInfo::ExtractField(const char* field) { |
| 121 ASSERT(field != NULL); | 121 ASSERT(field != NULL); |
| 122 ASSERT(data_ != NULL); | 122 ASSERT(data_ != NULL); |
| 123 | 123 |
| 124 char* p = FieldStart(field); | 124 char* p = FieldStart(field); |
| 125 if (p == NULL) { | 125 if (p == NULL) { |
| 126 return NULL; | 126 return NULL; |
| 127 } | 127 } |
| 128 | 128 |
| 129 // Find the end of the line. | 129 // Find the end of the line. |
| 130 char* q = strchr(p, '\n'); | 130 char* q = strchr(p, '\n'); |
| 131 if (q == NULL) { | 131 if (q == NULL) { |
| 132 q = data_ + datalen_; | 132 q = data_ + datalen_; |
| 133 } | 133 } |
| 134 | 134 |
| 135 intptr_t len = q - p; | 135 intptr_t len = q - p; |
| 136 char* result = new char[len + 1]; // plus one for null-terminator. | 136 char* result = reinterpret_cast<char*>(malloc(len + 1)); |
| 137 // Copy the line into result, leaving enough room for a null-terminator. | 137 // Copy the line into result, leaving enough room for a null-terminator. |
| 138 char saved_end = *q; | 138 char saved_end = *q; |
| 139 *q = '\0'; | 139 *q = '\0'; |
| 140 strncpy(result, p, len); | 140 strncpy(result, p, len); |
| 141 result[len] = '\0'; | 141 result[len] = '\0'; |
| 142 *q = saved_end; | 142 *q = saved_end; |
| 143 | 143 |
| 144 return result; | 144 return result; |
| 145 } | 145 } |
| 146 | 146 |
| 147 | 147 |
| 148 bool ProcCpuInfo::HasField(const char* field) { | 148 bool ProcCpuInfo::HasField(const char* field) { |
| 149 ASSERT(field != NULL); | 149 ASSERT(field != NULL); |
| 150 ASSERT(data_ != NULL); | 150 ASSERT(data_ != NULL); |
| 151 return (FieldStart(field) != NULL); | 151 return (FieldStart(field) != NULL); |
| 152 } | 152 } |
| 153 | 153 |
| 154 } // namespace dart | 154 } // namespace dart |
| 155 | 155 |
| 156 #endif // defined(TARGET_OS_LINUX) || defined(TARGET_OS_ANDROID) | 156 #endif // defined(TARGET_OS_LINUX) || defined(TARGET_OS_ANDROID) |
| OLD | NEW |