Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(77)

Side by Side Diff: vm/symbols.cc

Issue 11443005: Issue - 7123 (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/runtime/
Patch Set: Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« vm/symbols.h ('K') | « vm/symbols.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/symbols.h" 5 #include "vm/symbols.h"
6 6
7 #include "vm/isolate.h" 7 #include "vm/isolate.h"
8 #include "vm/object.h" 8 #include "vm/object.h"
9 #include "vm/object_store.h" 9 #include "vm/object_store.h"
10 #include "vm/raw_object.h" 10 #include "vm/raw_object.h"
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 RawString* Symbols::New(const char* cstr) { 100 RawString* Symbols::New(const char* cstr) {
101 ASSERT(cstr != NULL); 101 ASSERT(cstr != NULL);
102 intptr_t array_len = strlen(cstr); 102 intptr_t array_len = strlen(cstr);
103 const uint8_t* utf8_array = reinterpret_cast<const uint8_t*>(cstr); 103 const uint8_t* utf8_array = reinterpret_cast<const uint8_t*>(cstr);
104 return Symbols::FromUTF8(utf8_array, array_len); 104 return Symbols::FromUTF8(utf8_array, array_len);
105 } 105 }
106 106
107 107
108 RawString* Symbols::FromUTF8(const uint8_t* utf8_array, intptr_t array_len) { 108 RawString* Symbols::FromUTF8(const uint8_t* utf8_array, intptr_t array_len) {
109 if (array_len == 0 || utf8_array == NULL) { 109 if (array_len == 0 || utf8_array == NULL) {
110 return NewSymbol(reinterpret_cast<uint8_t*>(NULL), 0); 110 return FromLatin1(reinterpret_cast<uint8_t*>(NULL), 0);
111 } 111 }
112 Utf8::Type type; 112 Utf8::Type type;
113 intptr_t len = Utf8::CodeUnitCount(utf8_array, array_len, &type); 113 intptr_t len = Utf8::CodeUnitCount(utf8_array, array_len, &type);
114 ASSERT(len != 0); 114 ASSERT(len != 0);
115 Zone* zone = Isolate::Current()->current_zone(); 115 Zone* zone = Isolate::Current()->current_zone();
116 if (type == Utf8::kLatin1) { 116 if (type == Utf8::kLatin1) {
117 uint8_t* characters = zone->Alloc<uint8_t>(len); 117 uint8_t* characters = zone->Alloc<uint8_t>(len);
118 Utf8::DecodeToLatin1(utf8_array, array_len, characters, len); 118 Utf8::DecodeToLatin1(utf8_array, array_len, characters, len);
119 return NewSymbol(characters, len); 119 return FromLatin1(characters, len);
120 } 120 }
121 ASSERT((type == Utf8::kBMP) || (type == Utf8::kSupplementary)); 121 ASSERT((type == Utf8::kBMP) || (type == Utf8::kSupplementary));
122 uint16_t* characters = zone->Alloc<uint16_t>(len); 122 uint16_t* characters = zone->Alloc<uint16_t>(len);
123 Utf8::DecodeToUTF16(utf8_array, array_len, characters, len); 123 Utf8::DecodeToUTF16(utf8_array, array_len, characters, len);
124 return NewSymbol(characters, len); 124 return FromUTF16(characters, len);
125 } 125 }
126 126
127 127
128 RawString* Symbols::FromLatin1(const uint8_t* latin1_array, intptr_t len) { 128 RawString* Symbols::FromLatin1(const uint8_t* latin1_array, intptr_t len) {
129 return NewSymbol(latin1_array, len); 129 return NewSymbol(latin1_array, len, String::FromLatin1);
130 } 130 }
131 131
132 132
133 RawString* Symbols::FromUTF16(const uint16_t* utf16_array, intptr_t len) { 133 RawString* Symbols::FromUTF16(const uint16_t* utf16_array, intptr_t len) {
134 return NewSymbol(utf16_array, len); 134 return NewSymbol(utf16_array, len, String::FromUTF16);
135 } 135 }
136 136
137 137
138 RawString* Symbols::FromUTF32(const int32_t* utf32_array, intptr_t len) { 138 RawString* Symbols::FromUTF32(const int32_t* utf32_array, intptr_t len) {
139 return NewSymbol(utf32_array, len); 139 return NewSymbol(utf32_array, len, String::FromUTF32);
140 } 140 }
141 141
142 142
143 template<typename T> 143 template<typename T>
144 RawString* Symbols::NewSymbol(const T* characters, intptr_t len) { 144 RawString* Symbols::NewSymbol(const T* characters,
145 intptr_t len,
146 RawString* (*New)(const T* chars,
cshapiro 2012/12/05 20:40:16 rename New, possibly to new_string
siva 2012/12/05 23:33:14 Done.
147 intptr_t len,
148 Heap::Space space)) {
145 Isolate* isolate = Isolate::Current(); 149 Isolate* isolate = Isolate::Current();
146 String& symbol = String::Handle(isolate, String::null()); 150 String& symbol = String::Handle(isolate, String::null());
147 Array& symbol_table = Array::Handle(isolate, Array::null()); 151 Array& symbol_table = Array::Handle(isolate, Array::null());
148 152
149 // Calculate the String hash for this sequence of characters. 153 // Calculate the String hash for this sequence of characters.
150 intptr_t hash = String::Hash(characters, len); 154 intptr_t hash = String::Hash(characters, len);
151 155
152 // First check if a symbol exists in the vm isolate for these characters. 156 // First check if a symbol exists in the vm isolate for these characters.
153 symbol_table = Dart::vm_isolate()->object_store()->symbol_table(); 157 symbol_table = Dart::vm_isolate()->object_store()->symbol_table();
154 intptr_t index = FindIndex(symbol_table, characters, len, hash); 158 intptr_t index = FindIndex(symbol_table, characters, len, hash);
155 symbol ^= symbol_table.At(index); 159 symbol ^= symbol_table.At(index);
156 if (symbol.IsNull()) { 160 if (symbol.IsNull()) {
157 // Now try in the symbol table of the current isolate. 161 // Now try in the symbol table of the current isolate.
158 symbol_table = isolate->object_store()->symbol_table(); 162 symbol_table = isolate->object_store()->symbol_table();
159 index = FindIndex(symbol_table, characters, len, hash); 163 index = FindIndex(symbol_table, characters, len, hash);
160 // Since we leave enough room in the table to guarantee, that we find an 164 // Since we leave enough room in the table to guarantee, that we find an
161 // empty spot, index is the insertion point if symbol is null. 165 // empty spot, index is the insertion point if symbol is null.
162 symbol ^= symbol_table.At(index); 166 symbol ^= symbol_table.At(index);
163 if (symbol.IsNull()) { 167 if (symbol.IsNull()) {
164 // Allocate new result string. 168 // Allocate new result string.
165 symbol = String::New(characters, len, Heap::kOld); 169 symbol = (*New)(characters, len, Heap::kOld);
166 symbol.SetHash(hash); // Remember the calculated hash value. 170 symbol.SetHash(hash); // Remember the calculated hash value.
167 InsertIntoSymbolTable(symbol_table, symbol, index); 171 InsertIntoSymbolTable(symbol_table, symbol, index);
168 } 172 }
169 } 173 }
170 ASSERT(symbol.IsSymbol()); 174 ASSERT(symbol.IsSymbol());
171 return symbol.raw(); 175 return symbol.raw();
172 } 176 }
173 177
174 178
175 template RawString* Symbols::NewSymbol(const uint8_t* characters, 179 template RawString* Symbols::NewSymbol(const uint8_t* characters,
176 intptr_t len); 180 intptr_t len,
181 RawString* (*New)(const uint8_t* chars,
cshapiro 2012/12/05 20:40:16 ditto on the rename here and in the two cases belo
siva 2012/12/05 23:33:14 Done.
182 intptr_t len,
183 Heap::Space space));
177 template RawString* Symbols::NewSymbol(const uint16_t* characters, 184 template RawString* Symbols::NewSymbol(const uint16_t* characters,
178 intptr_t len); 185 intptr_t len,
186 RawString* (*New)(const uint16_t* chars,
187 intptr_t len,
188 Heap::Space space));
179 template RawString* Symbols::NewSymbol(const int32_t* characters, 189 template RawString* Symbols::NewSymbol(const int32_t* characters,
180 intptr_t len); 190 intptr_t len,
191 RawString* (*New)(const int32_t* chars,
192 intptr_t len,
193 Heap::Space space));
181 194
182 195
183 RawString* Symbols::New(const String& str) { 196 RawString* Symbols::New(const String& str) {
184 if (str.IsSymbol()) { 197 if (str.IsSymbol()) {
185 return str.raw(); 198 return str.raw();
186 } 199 }
187 return New(str, 0, str.Length()); 200 return New(str, 0, str.Length());
188 } 201 }
189 202
190 203
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
348 } 361 }
349 362
350 363
351 RawObject* Symbols::GetVMSymbol(intptr_t object_id) { 364 RawObject* Symbols::GetVMSymbol(intptr_t object_id) {
352 ASSERT(IsVMSymbolId(object_id)); 365 ASSERT(IsVMSymbolId(object_id));
353 intptr_t i = (object_id - kMaxPredefinedObjectIds); 366 intptr_t i = (object_id - kMaxPredefinedObjectIds);
354 return (i > 0 && i < Symbols::kMaxId) ? predefined_[i] : Object::null(); 367 return (i > 0 && i < Symbols::kMaxId) ? predefined_[i] : Object::null();
355 } 368 }
356 369
357 } // namespace dart 370 } // namespace dart
OLDNEW
« vm/symbols.h ('K') | « vm/symbols.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698