| OLD | NEW |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/profiler/strings-storage.h" | 5 #include "src/profiler/strings-storage.h" |
| 6 | 6 |
| 7 #include "src/base/smart-pointers.h" | 7 #include "src/base/smart-pointers.h" |
| 8 #include "src/objects-inl.h" | 8 #include "src/objects-inl.h" |
| 9 | 9 |
| 10 namespace v8 { | 10 namespace v8 { |
| 11 namespace internal { | 11 namespace internal { |
| 12 | 12 |
| 13 | 13 |
| 14 bool StringsStorage::StringsMatch(void* key1, void* key2) { | 14 bool StringsStorage::StringsMatch(void* key1, void* key2) { |
| 15 return strcmp(reinterpret_cast<char*>(key1), reinterpret_cast<char*>(key2)) == | 15 return strcmp(reinterpret_cast<char*>(key1), reinterpret_cast<char*>(key2)) == |
| 16 0; | 16 0; |
| 17 } | 17 } |
| 18 | 18 |
| 19 | 19 |
| 20 StringsStorage::StringsStorage(Heap* heap) | 20 StringsStorage::StringsStorage(Heap* heap) |
| 21 : hash_seed_(heap->HashSeed()), names_(StringsMatch) {} | 21 : hash_seed_(heap->HashSeed()), names_(StringsMatch) {} |
| 22 | 22 |
| 23 | 23 |
| 24 StringsStorage::~StringsStorage() { | 24 StringsStorage::~StringsStorage() { |
| 25 for (HashMap::Entry* p = names_.Start(); p != NULL; p = names_.Next(p)) { | 25 for (base::HashMap::Entry* p = names_.Start(); p != NULL; |
| 26 p = names_.Next(p)) { |
| 26 DeleteArray(reinterpret_cast<const char*>(p->value)); | 27 DeleteArray(reinterpret_cast<const char*>(p->value)); |
| 27 } | 28 } |
| 28 } | 29 } |
| 29 | 30 |
| 30 | 31 |
| 31 const char* StringsStorage::GetCopy(const char* src) { | 32 const char* StringsStorage::GetCopy(const char* src) { |
| 32 int len = static_cast<int>(strlen(src)); | 33 int len = static_cast<int>(strlen(src)); |
| 33 HashMap::Entry* entry = GetEntry(src, len); | 34 base::HashMap::Entry* entry = GetEntry(src, len); |
| 34 if (entry->value == NULL) { | 35 if (entry->value == NULL) { |
| 35 Vector<char> dst = Vector<char>::New(len + 1); | 36 Vector<char> dst = Vector<char>::New(len + 1); |
| 36 StrNCpy(dst, src, len); | 37 StrNCpy(dst, src, len); |
| 37 dst[len] = '\0'; | 38 dst[len] = '\0'; |
| 38 entry->key = dst.start(); | 39 entry->key = dst.start(); |
| 39 entry->value = entry->key; | 40 entry->value = entry->key; |
| 40 } | 41 } |
| 41 return reinterpret_cast<const char*>(entry->value); | 42 return reinterpret_cast<const char*>(entry->value); |
| 42 } | 43 } |
| 43 | 44 |
| 44 | 45 |
| 45 const char* StringsStorage::GetFormatted(const char* format, ...) { | 46 const char* StringsStorage::GetFormatted(const char* format, ...) { |
| 46 va_list args; | 47 va_list args; |
| 47 va_start(args, format); | 48 va_start(args, format); |
| 48 const char* result = GetVFormatted(format, args); | 49 const char* result = GetVFormatted(format, args); |
| 49 va_end(args); | 50 va_end(args); |
| 50 return result; | 51 return result; |
| 51 } | 52 } |
| 52 | 53 |
| 53 | 54 |
| 54 const char* StringsStorage::AddOrDisposeString(char* str, int len) { | 55 const char* StringsStorage::AddOrDisposeString(char* str, int len) { |
| 55 HashMap::Entry* entry = GetEntry(str, len); | 56 base::HashMap::Entry* entry = GetEntry(str, len); |
| 56 if (entry->value == NULL) { | 57 if (entry->value == NULL) { |
| 57 // New entry added. | 58 // New entry added. |
| 58 entry->key = str; | 59 entry->key = str; |
| 59 entry->value = str; | 60 entry->value = str; |
| 60 } else { | 61 } else { |
| 61 DeleteArray(str); | 62 DeleteArray(str); |
| 62 } | 63 } |
| 63 return reinterpret_cast<const char*>(entry->value); | 64 return reinterpret_cast<const char*>(entry->value); |
| 64 } | 65 } |
| 65 | 66 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 } | 101 } |
| 101 | 102 |
| 102 | 103 |
| 103 const char* StringsStorage::GetFunctionName(const char* name) { | 104 const char* StringsStorage::GetFunctionName(const char* name) { |
| 104 return GetCopy(name); | 105 return GetCopy(name); |
| 105 } | 106 } |
| 106 | 107 |
| 107 | 108 |
| 108 size_t StringsStorage::GetUsedMemorySize() const { | 109 size_t StringsStorage::GetUsedMemorySize() const { |
| 109 size_t size = sizeof(*this); | 110 size_t size = sizeof(*this); |
| 110 size += sizeof(HashMap::Entry) * names_.capacity(); | 111 size += sizeof(base::HashMap::Entry) * names_.capacity(); |
| 111 for (HashMap::Entry* p = names_.Start(); p != NULL; p = names_.Next(p)) { | 112 for (base::HashMap::Entry* p = names_.Start(); p != NULL; |
| 113 p = names_.Next(p)) { |
| 112 size += strlen(reinterpret_cast<const char*>(p->value)) + 1; | 114 size += strlen(reinterpret_cast<const char*>(p->value)) + 1; |
| 113 } | 115 } |
| 114 return size; | 116 return size; |
| 115 } | 117 } |
| 116 | 118 |
| 117 | 119 base::HashMap::Entry* StringsStorage::GetEntry(const char* str, int len) { |
| 118 HashMap::Entry* StringsStorage::GetEntry(const char* str, int len) { | |
| 119 uint32_t hash = StringHasher::HashSequentialString(str, len, hash_seed_); | 120 uint32_t hash = StringHasher::HashSequentialString(str, len, hash_seed_); |
| 120 return names_.LookupOrInsert(const_cast<char*>(str), hash); | 121 return names_.LookupOrInsert(const_cast<char*>(str), hash); |
| 121 } | 122 } |
| 122 } // namespace internal | 123 } // namespace internal |
| 123 } // namespace v8 | 124 } // namespace v8 |
| OLD | NEW |