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 22 matching lines...) Expand all Loading... | |
33 #include "debug.h" | 33 #include "debug.h" |
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 bool StringsStorage::StringsMatch(void* key1, void* key2) { | |
44 return strcmp(reinterpret_cast<char*>(key1), | |
45 reinterpret_cast<char*>(key2)) == 0; | |
46 } | |
43 | 47 |
alph
2013/10/17 12:19:09
nit: add extra lines around the function.
yurys
2013/10/17 12:56:41
Done.
| |
44 StringsStorage::StringsStorage(Heap* heap) | 48 StringsStorage::StringsStorage(Heap* heap) |
45 : hash_seed_(heap->HashSeed()), names_(StringsMatch) { | 49 : hash_seed_(heap->HashSeed()), names_(StringsMatch) { |
46 } | 50 } |
47 | 51 |
48 | 52 |
49 StringsStorage::~StringsStorage() { | 53 StringsStorage::~StringsStorage() { |
50 for (HashMap::Entry* p = names_.Start(); | 54 for (HashMap::Entry* p = names_.Start(); |
51 p != NULL; | 55 p != NULL; |
52 p = names_.Next(p)) { | 56 p = names_.Next(p)) { |
53 DeleteArray(reinterpret_cast<const char*>(p->value)); | 57 DeleteArray(reinterpret_cast<const char*>(p->value)); |
54 } | 58 } |
55 } | 59 } |
56 | 60 |
57 | 61 |
58 const char* StringsStorage::GetCopy(const char* src) { | 62 const char* StringsStorage::GetCopy(const char* src) { |
59 int len = static_cast<int>(strlen(src)); | 63 int len = static_cast<int>(strlen(src)); |
60 Vector<char> dst = Vector<char>::New(len + 1); | 64 HashMap::Entry* entry = GetEntry(src, len); |
61 OS::StrNCpy(dst, src, len); | 65 if (entry->value == NULL) { |
62 dst[len] = '\0'; | 66 Vector<char> dst = Vector<char>::New(len + 1); |
63 uint32_t hash = | 67 OS::StrNCpy(dst, src, len); |
64 StringHasher::HashSequentialString(dst.start(), len, hash_seed_); | 68 dst[len] = '\0'; |
65 return AddOrDisposeString(dst.start(), hash); | 69 entry->key = dst.start(); |
70 entry->value = entry->key; | |
71 } | |
72 return reinterpret_cast<const char*>(entry->value); | |
66 } | 73 } |
67 | 74 |
68 | 75 |
69 const char* StringsStorage::GetFormatted(const char* format, ...) { | 76 const char* StringsStorage::GetFormatted(const char* format, ...) { |
70 va_list args; | 77 va_list args; |
71 va_start(args, format); | 78 va_start(args, format); |
72 const char* result = GetVFormatted(format, args); | 79 const char* result = GetVFormatted(format, args); |
73 va_end(args); | 80 va_end(args); |
74 return result; | 81 return result; |
75 } | 82 } |
76 | 83 |
77 | 84 |
78 const char* StringsStorage::AddOrDisposeString(char* str, uint32_t hash) { | 85 const char* StringsStorage::AddOrDisposeString(char* str, int len) { |
79 HashMap::Entry* cache_entry = names_.Lookup(str, hash, true); | 86 HashMap::Entry* entry = GetEntry(str, len); |
80 if (cache_entry->value == NULL) { | 87 if (entry->value == NULL) { |
81 // New entry added. | 88 // New entry added. |
82 cache_entry->value = str; | 89 entry->key = str; |
90 entry->value = str; | |
83 } else { | 91 } else { |
84 DeleteArray(str); | 92 DeleteArray(str); |
85 } | 93 } |
86 return reinterpret_cast<const char*>(cache_entry->value); | 94 return reinterpret_cast<const char*>(entry->value); |
87 } | 95 } |
88 | 96 |
89 | 97 |
90 const char* StringsStorage::GetVFormatted(const char* format, va_list args) { | 98 const char* StringsStorage::GetVFormatted(const char* format, va_list args) { |
91 Vector<char> str = Vector<char>::New(1024); | 99 Vector<char> str = Vector<char>::New(1024); |
92 int len = OS::VSNPrintF(str, format, args); | 100 int len = OS::VSNPrintF(str, format, args); |
93 if (len == -1) { | 101 if (len == -1) { |
94 DeleteArray(str.start()); | 102 DeleteArray(str.start()); |
95 return format; | 103 return GetCopy(format); |
96 } | 104 } |
97 uint32_t hash = StringHasher::HashSequentialString( | 105 return AddOrDisposeString(str.start(), len); |
98 str.start(), len, hash_seed_); | |
99 return AddOrDisposeString(str.start(), hash); | |
100 } | 106 } |
101 | 107 |
102 | 108 |
103 const char* StringsStorage::GetName(Name* name) { | 109 const char* StringsStorage::GetName(Name* name) { |
104 if (name->IsString()) { | 110 if (name->IsString()) { |
105 String* str = String::cast(name); | 111 String* str = String::cast(name); |
106 int length = Min(kMaxNameSize, str->length()); | 112 int length = Min(kMaxNameSize, str->length()); |
113 int actual_length = 0; | |
107 SmartArrayPointer<char> data = | 114 SmartArrayPointer<char> data = |
108 str->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL, 0, length); | 115 str->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL, 0, length, |
109 uint32_t hash = StringHasher::HashSequentialString( | 116 &actual_length); |
110 *data, length, name->GetHeap()->HashSeed()); | 117 return AddOrDisposeString(data.Detach(), actual_length); |
111 return AddOrDisposeString(data.Detach(), hash); | |
112 } else if (name->IsSymbol()) { | 118 } else if (name->IsSymbol()) { |
113 return "<symbol>"; | 119 return "<symbol>"; |
114 } | 120 } |
115 return ""; | 121 return ""; |
116 } | 122 } |
117 | 123 |
118 | 124 |
119 const char* StringsStorage::GetName(int index) { | 125 const char* StringsStorage::GetName(int index) { |
120 return GetFormatted("%d", index); | 126 return GetFormatted("%d", index); |
121 } | 127 } |
122 | 128 |
123 | 129 |
130 const char* StringsStorage::GetFunctionName(Name* name) { | |
131 return BeautifyFunctionName(GetName(name)); | |
132 } | |
133 | |
134 | |
135 const char* StringsStorage::GetFunctionName(const char* name) { | |
136 return BeautifyFunctionName(GetCopy(name)); | |
137 } | |
138 | |
139 | |
140 const char* StringsStorage::BeautifyFunctionName(const char* name) { | |
141 return (*name == 0) ? ProfileGenerator::kAnonymousFunctionName : name; | |
142 } | |
143 | |
144 | |
124 size_t StringsStorage::GetUsedMemorySize() const { | 145 size_t StringsStorage::GetUsedMemorySize() const { |
125 size_t size = sizeof(*this); | 146 size_t size = sizeof(*this); |
126 size += sizeof(HashMap::Entry) * names_.capacity(); | 147 size += sizeof(HashMap::Entry) * names_.capacity(); |
127 for (HashMap::Entry* p = names_.Start(); p != NULL; p = names_.Next(p)) { | 148 for (HashMap::Entry* p = names_.Start(); p != NULL; p = names_.Next(p)) { |
128 size += strlen(reinterpret_cast<const char*>(p->value)) + 1; | 149 size += strlen(reinterpret_cast<const char*>(p->value)) + 1; |
129 } | 150 } |
130 return size; | 151 return size; |
131 } | 152 } |
132 | 153 |
133 | 154 |
155 HashMap::Entry* StringsStorage::GetEntry(const char* str, int len) { | |
156 uint32_t hash = StringHasher::HashSequentialString(str, len, hash_seed_); | |
157 return names_.Lookup(const_cast<char*>(str), hash, true); | |
158 } | |
159 | |
160 | |
134 const char* const CodeEntry::kEmptyNamePrefix = ""; | 161 const char* const CodeEntry::kEmptyNamePrefix = ""; |
135 const char* const CodeEntry::kEmptyResourceName = ""; | 162 const char* const CodeEntry::kEmptyResourceName = ""; |
136 const char* const CodeEntry::kEmptyBailoutReason = ""; | 163 const char* const CodeEntry::kEmptyBailoutReason = ""; |
137 | 164 |
138 | 165 |
139 CodeEntry::~CodeEntry() { | 166 CodeEntry::~CodeEntry() { |
140 delete no_frame_ranges_; | 167 delete no_frame_ranges_; |
141 } | 168 } |
142 | 169 |
143 | 170 |
(...skipping 521 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
665 case OTHER: | 692 case OTHER: |
666 case EXTERNAL: | 693 case EXTERNAL: |
667 return program_entry_; | 694 return program_entry_; |
668 case IDLE: | 695 case IDLE: |
669 return idle_entry_; | 696 return idle_entry_; |
670 default: return NULL; | 697 default: return NULL; |
671 } | 698 } |
672 } | 699 } |
673 | 700 |
674 } } // namespace v8::internal | 701 } } // namespace v8::internal |
OLD | NEW |