| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 #include "sampler.h" | 34 #include "sampler.h" |
| 35 #include "global-handles.h" | 35 #include "global-handles.h" |
| 36 #include "scopeinfo.h" | 36 #include "scopeinfo.h" |
| 37 #include "unicode.h" | 37 #include "unicode.h" |
| 38 #include "zone-inl.h" | 38 #include "zone-inl.h" |
| 39 | 39 |
| 40 namespace v8 { | 40 namespace v8 { |
| 41 namespace internal { | 41 namespace internal { |
| 42 | 42 |
| 43 | 43 |
| 44 bool StringsStorage::StringsMatch(void* key1, void* key2) { |
| 45 return strcmp(reinterpret_cast<char*>(key1), |
| 46 reinterpret_cast<char*>(key2)) == 0; |
| 47 } |
| 48 |
| 49 |
| 44 StringsStorage::StringsStorage(Heap* heap) | 50 StringsStorage::StringsStorage(Heap* heap) |
| 45 : hash_seed_(heap->HashSeed()), names_(StringsMatch) { | 51 : hash_seed_(heap->HashSeed()), names_(StringsMatch) { |
| 46 } | 52 } |
| 47 | 53 |
| 48 | 54 |
| 49 StringsStorage::~StringsStorage() { | 55 StringsStorage::~StringsStorage() { |
| 50 for (HashMap::Entry* p = names_.Start(); | 56 for (HashMap::Entry* p = names_.Start(); |
| 51 p != NULL; | 57 p != NULL; |
| 52 p = names_.Next(p)) { | 58 p = names_.Next(p)) { |
| 53 DeleteArray(reinterpret_cast<const char*>(p->value)); | 59 DeleteArray(reinterpret_cast<const char*>(p->value)); |
| 54 } | 60 } |
| 55 } | 61 } |
| 56 | 62 |
| 57 | 63 |
| 58 const char* StringsStorage::GetCopy(const char* src) { | 64 const char* StringsStorage::GetCopy(const char* src) { |
| 59 int len = static_cast<int>(strlen(src)); | 65 int len = static_cast<int>(strlen(src)); |
| 60 Vector<char> dst = Vector<char>::New(len + 1); | 66 HashMap::Entry* entry = GetEntry(src, len); |
| 61 OS::StrNCpy(dst, src, len); | 67 if (entry->value == NULL) { |
| 62 dst[len] = '\0'; | 68 Vector<char> dst = Vector<char>::New(len + 1); |
| 63 uint32_t hash = | 69 OS::StrNCpy(dst, src, len); |
| 64 StringHasher::HashSequentialString(dst.start(), len, hash_seed_); | 70 dst[len] = '\0'; |
| 65 return AddOrDisposeString(dst.start(), hash); | 71 entry->key = dst.start(); |
| 72 entry->value = entry->key; |
| 73 } |
| 74 return reinterpret_cast<const char*>(entry->value); |
| 66 } | 75 } |
| 67 | 76 |
| 68 | 77 |
| 69 const char* StringsStorage::GetFormatted(const char* format, ...) { | 78 const char* StringsStorage::GetFormatted(const char* format, ...) { |
| 70 va_list args; | 79 va_list args; |
| 71 va_start(args, format); | 80 va_start(args, format); |
| 72 const char* result = GetVFormatted(format, args); | 81 const char* result = GetVFormatted(format, args); |
| 73 va_end(args); | 82 va_end(args); |
| 74 return result; | 83 return result; |
| 75 } | 84 } |
| 76 | 85 |
| 77 | 86 |
| 78 const char* StringsStorage::AddOrDisposeString(char* str, uint32_t hash) { | 87 const char* StringsStorage::AddOrDisposeString(char* str, int len) { |
| 79 HashMap::Entry* cache_entry = names_.Lookup(str, hash, true); | 88 HashMap::Entry* entry = GetEntry(str, len); |
| 80 if (cache_entry->value == NULL) { | 89 if (entry->value == NULL) { |
| 81 // New entry added. | 90 // New entry added. |
| 82 cache_entry->value = str; | 91 entry->key = str; |
| 92 entry->value = str; |
| 83 } else { | 93 } else { |
| 84 DeleteArray(str); | 94 DeleteArray(str); |
| 85 } | 95 } |
| 86 return reinterpret_cast<const char*>(cache_entry->value); | 96 return reinterpret_cast<const char*>(entry->value); |
| 87 } | 97 } |
| 88 | 98 |
| 89 | 99 |
| 90 const char* StringsStorage::GetVFormatted(const char* format, va_list args) { | 100 const char* StringsStorage::GetVFormatted(const char* format, va_list args) { |
| 91 Vector<char> str = Vector<char>::New(1024); | 101 Vector<char> str = Vector<char>::New(1024); |
| 92 int len = OS::VSNPrintF(str, format, args); | 102 int len = OS::VSNPrintF(str, format, args); |
| 93 if (len == -1) { | 103 if (len == -1) { |
| 94 DeleteArray(str.start()); | 104 DeleteArray(str.start()); |
| 95 return format; | 105 return GetCopy(format); |
| 96 } | 106 } |
| 97 uint32_t hash = StringHasher::HashSequentialString( | 107 return AddOrDisposeString(str.start(), len); |
| 98 str.start(), len, hash_seed_); | |
| 99 return AddOrDisposeString(str.start(), hash); | |
| 100 } | 108 } |
| 101 | 109 |
| 102 | 110 |
| 103 const char* StringsStorage::GetName(Name* name) { | 111 const char* StringsStorage::GetName(Name* name) { |
| 104 if (name->IsString()) { | 112 if (name->IsString()) { |
| 105 String* str = String::cast(name); | 113 String* str = String::cast(name); |
| 106 int length = Min(kMaxNameSize, str->length()); | 114 int length = Min(kMaxNameSize, str->length()); |
| 115 int actual_length = 0; |
| 107 SmartArrayPointer<char> data = | 116 SmartArrayPointer<char> data = |
| 108 str->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL, 0, length); | 117 str->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL, 0, length, |
| 109 uint32_t hash = StringHasher::HashSequentialString( | 118 &actual_length); |
| 110 *data, length, name->GetHeap()->HashSeed()); | 119 return AddOrDisposeString(data.Detach(), actual_length); |
| 111 return AddOrDisposeString(data.Detach(), hash); | |
| 112 } else if (name->IsSymbol()) { | 120 } else if (name->IsSymbol()) { |
| 113 return "<symbol>"; | 121 return "<symbol>"; |
| 114 } | 122 } |
| 115 return ""; | 123 return ""; |
| 116 } | 124 } |
| 117 | 125 |
| 118 | 126 |
| 119 const char* StringsStorage::GetName(int index) { | 127 const char* StringsStorage::GetName(int index) { |
| 120 return GetFormatted("%d", index); | 128 return GetFormatted("%d", index); |
| 121 } | 129 } |
| 122 | 130 |
| 123 | 131 |
| 132 const char* StringsStorage::GetFunctionName(Name* name) { |
| 133 return BeautifyFunctionName(GetName(name)); |
| 134 } |
| 135 |
| 136 |
| 137 const char* StringsStorage::GetFunctionName(const char* name) { |
| 138 return BeautifyFunctionName(GetCopy(name)); |
| 139 } |
| 140 |
| 141 |
| 142 const char* StringsStorage::BeautifyFunctionName(const char* name) { |
| 143 return (*name == 0) ? ProfileGenerator::kAnonymousFunctionName : name; |
| 144 } |
| 145 |
| 146 |
| 124 size_t StringsStorage::GetUsedMemorySize() const { | 147 size_t StringsStorage::GetUsedMemorySize() const { |
| 125 size_t size = sizeof(*this); | 148 size_t size = sizeof(*this); |
| 126 size += sizeof(HashMap::Entry) * names_.capacity(); | 149 size += sizeof(HashMap::Entry) * names_.capacity(); |
| 127 for (HashMap::Entry* p = names_.Start(); p != NULL; p = names_.Next(p)) { | 150 for (HashMap::Entry* p = names_.Start(); p != NULL; p = names_.Next(p)) { |
| 128 size += strlen(reinterpret_cast<const char*>(p->value)) + 1; | 151 size += strlen(reinterpret_cast<const char*>(p->value)) + 1; |
| 129 } | 152 } |
| 130 return size; | 153 return size; |
| 131 } | 154 } |
| 132 | 155 |
| 133 | 156 |
| 157 HashMap::Entry* StringsStorage::GetEntry(const char* str, int len) { |
| 158 uint32_t hash = StringHasher::HashSequentialString(str, len, hash_seed_); |
| 159 return names_.Lookup(const_cast<char*>(str), hash, true); |
| 160 } |
| 161 |
| 162 |
| 134 const char* const CodeEntry::kEmptyNamePrefix = ""; | 163 const char* const CodeEntry::kEmptyNamePrefix = ""; |
| 135 const char* const CodeEntry::kEmptyResourceName = ""; | 164 const char* const CodeEntry::kEmptyResourceName = ""; |
| 136 const char* const CodeEntry::kEmptyBailoutReason = ""; | 165 const char* const CodeEntry::kEmptyBailoutReason = ""; |
| 137 | 166 |
| 138 | 167 |
| 139 CodeEntry::~CodeEntry() { | 168 CodeEntry::~CodeEntry() { |
| 140 delete no_frame_ranges_; | 169 delete no_frame_ranges_; |
| 141 } | 170 } |
| 142 | 171 |
| 143 | 172 |
| (...skipping 521 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 665 case OTHER: | 694 case OTHER: |
| 666 case EXTERNAL: | 695 case EXTERNAL: |
| 667 return program_entry_; | 696 return program_entry_; |
| 668 case IDLE: | 697 case IDLE: |
| 669 return idle_entry_; | 698 return idle_entry_; |
| 670 default: return NULL; | 699 default: return NULL; |
| 671 } | 700 } |
| 672 } | 701 } |
| 673 | 702 |
| 674 } } // namespace v8::internal | 703 } } // namespace v8::internal |
| OLD | NEW |