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

Side by Side Diff: runtime/vm/object.cc

Issue 428273002: Handle-like interface for HashTable. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 6 years, 4 months 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
« no previous file with comments | « runtime/vm/hash_table_test.cc ('k') | runtime/vm/symbols.cc » ('j') | 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/object.h" 5 #include "vm/object.h"
6 6
7 #include "include/dart_api.h" 7 #include "include/dart_api.h"
8 #include "platform/assert.h" 8 #include "platform/assert.h"
9 #include "vm/assembler.h" 9 #include "vm/assembler.h"
10 #include "vm/cpu.h" 10 #include "vm/cpu.h"
(...skipping 7706 matching lines...) Expand 10 before | Expand all | Expand 10 after
7717 typedef UnorderedHashMap<CompressedTokenTraits> CompressedTokenMap; 7717 typedef UnorderedHashMap<CompressedTokenTraits> CompressedTokenMap;
7718 7718
7719 7719
7720 // Helper class for creation of compressed token stream data. 7720 // Helper class for creation of compressed token stream data.
7721 class CompressedTokenStreamData : public ValueObject { 7721 class CompressedTokenStreamData : public ValueObject {
7722 public: 7722 public:
7723 static const intptr_t kInitialBufferSize = 16 * KB; 7723 static const intptr_t kInitialBufferSize = 16 * KB;
7724 CompressedTokenStreamData() : 7724 CompressedTokenStreamData() :
7725 buffer_(NULL), 7725 buffer_(NULL),
7726 stream_(&buffer_, Reallocate, kInitialBufferSize), 7726 stream_(&buffer_, Reallocate, kInitialBufferSize),
7727 tokens_(Array::Handle( 7727 tokens_(HashTables::New<CompressedTokenMap>(kInitialTableSize)) {
7728 HashTables::New<CompressedTokenMap>(kInitialTableSize))) {
7729 } 7728 }
7730 ~CompressedTokenStreamData() { 7729 ~CompressedTokenStreamData() {
7731 // Safe to discard the hash table now. 7730 // Safe to discard the hash table now.
7732 tokens_.Release(); 7731 tokens_.Release();
7733 } 7732 }
7734 7733
7735 // Add an IDENT token into the stream and the token hash map. 7734 // Add an IDENT token into the stream and the token hash map.
7736 void AddIdentToken(const String* ident) { 7735 void AddIdentToken(const String* ident) {
7737 ASSERT(ident->IsSymbol()); 7736 ASSERT(ident->IsSymbol());
7738 const intptr_t fresh_index = tokens_.NumOccupied(); 7737 const intptr_t fresh_index = tokens_.NumOccupied();
(...skipping 1004 matching lines...) Expand 10 before | Expand all | Expand 10 after
8743 } 8742 }
8744 }; 8743 };
8745 typedef UnorderedHashMap<StringEqualsTraits> ResolvedNamesMap; 8744 typedef UnorderedHashMap<StringEqualsTraits> ResolvedNamesMap;
8746 8745
8747 8746
8748 // Returns true if the name is found in the cache, false no cache hit. 8747 // Returns true if the name is found in the cache, false no cache hit.
8749 // obj is set to the cached entry. It may be null, indicating that the 8748 // obj is set to the cached entry. It may be null, indicating that the
8750 // name does not resolve to anything in this library. 8749 // name does not resolve to anything in this library.
8751 bool Library::LookupResolvedNamesCache(const String& name, 8750 bool Library::LookupResolvedNamesCache(const String& name,
8752 Object* obj) const { 8751 Object* obj) const {
8753 ResolvedNamesMap cache(Array::Handle(resolved_names())); 8752 ResolvedNamesMap cache(resolved_names());
8754 bool present = false; 8753 bool present = false;
8755 *obj = cache.GetOrNull(name, &present); 8754 *obj = cache.GetOrNull(name, &present);
8756 RawArray* array = cache.Release(); 8755 ASSERT(cache.Release().raw() == resolved_names());
8757 ASSERT(array == resolved_names());
8758 return present; 8756 return present;
8759 } 8757 }
8760 8758
8761 8759
8762 // Add a name to the resolved name cache. This name resolves to the 8760 // Add a name to the resolved name cache. This name resolves to the
8763 // given object in this library scope. obj may be null, which means 8761 // given object in this library scope. obj may be null, which means
8764 // the name does not resolve to anything in this library scope. 8762 // the name does not resolve to anything in this library scope.
8765 void Library::AddToResolvedNamesCache(const String& name, 8763 void Library::AddToResolvedNamesCache(const String& name,
8766 const Object& obj) const { 8764 const Object& obj) const {
8767 if (!FLAG_use_lib_cache) { 8765 if (!FLAG_use_lib_cache) {
8768 return; 8766 return;
8769 } 8767 }
8770 ResolvedNamesMap cache(Array::Handle(resolved_names())); 8768 ResolvedNamesMap cache(resolved_names());
8771 cache.UpdateOrInsert(name, obj); 8769 cache.UpdateOrInsert(name, obj);
8772 StorePointer(&raw_ptr()->resolved_names_, cache.Release()); 8770 StorePointer(&raw_ptr()->resolved_names_, cache.Release().raw());
8773 } 8771 }
8774 8772
8775 8773
8776 void Library::InvalidateResolvedName(const String& name) const { 8774 void Library::InvalidateResolvedName(const String& name) const {
8777 Object& entry = Object::Handle(); 8775 Object& entry = Object::Handle();
8778 if (LookupResolvedNamesCache(name, &entry)) { 8776 if (LookupResolvedNamesCache(name, &entry)) {
8779 // TODO(koda): Support deleted sentinel in snapshots and remove only 'name'. 8777 // TODO(koda): Support deleted sentinel in snapshots and remove only 'name'.
8780 InvalidateResolvedNamesCache(); 8778 InvalidateResolvedNamesCache();
8781 } 8779 }
8782 } 8780 }
(...skipping 9360 matching lines...) Expand 10 before | Expand all | Expand 10 after
18143 Integer::Cast(hash_code).AsTruncatedUint32Value()); 18141 Integer::Cast(hash_code).AsTruncatedUint32Value());
18144 } else { 18142 } else {
18145 return 0; 18143 return 0;
18146 } 18144 }
18147 } 18145 }
18148 }; 18146 };
18149 typedef EnumIndexHashMap<DefaultHashTraits> EnumIndexDefaultMap; 18147 typedef EnumIndexHashMap<DefaultHashTraits> EnumIndexDefaultMap;
18150 18148
18151 18149
18152 intptr_t LinkedHashMap::Length() const { 18150 intptr_t LinkedHashMap::Length() const {
18153 EnumIndexDefaultMap map(Array::Handle(data())); 18151 EnumIndexDefaultMap map(data());
18154 intptr_t result = map.NumOccupied(); 18152 intptr_t result = map.NumOccupied();
18155 { 18153 ASSERT(map.Release().raw() == data());
18156 RawArray* array = map.Release();
18157 ASSERT(array == data());
18158 }
18159 return result; 18154 return result;
18160 } 18155 }
18161 18156
18162 18157
18163 void LinkedHashMap::InsertOrUpdate(const Object& key, 18158 void LinkedHashMap::InsertOrUpdate(const Object& key,
18164 const Object& value) const { 18159 const Object& value) const {
18165 ASSERT(!IsNull()); 18160 ASSERT(!IsNull());
18166 EnumIndexDefaultMap map(Array::Handle(data())); 18161 EnumIndexDefaultMap map(data());
18167 if (!map.UpdateOrInsert(key, value)) { 18162 if (!map.UpdateOrInsert(key, value)) {
18168 SetModified(); 18163 SetModified();
18169 } 18164 }
18170 StorePointer(&raw_ptr()->data_, map.Release()); 18165 StorePointer(&raw_ptr()->data_, map.Release().raw());
18171 } 18166 }
18172 18167
18173 18168
18174 RawObject* LinkedHashMap::LookUp(const Object& key) const { 18169 RawObject* LinkedHashMap::LookUp(const Object& key) const {
18175 ASSERT(!IsNull()); 18170 ASSERT(!IsNull());
18176 EnumIndexDefaultMap map(Array::Handle(data())); 18171 EnumIndexDefaultMap map(data());
18177 const Object& result = Object::Handle(map.GetOrNull(key)); 18172 const Object& result = Object::Handle(map.GetOrNull(key));
18178 { 18173 ASSERT(map.Release().raw() == data());
18179 RawArray* array = map.Release();
18180 ASSERT(array == data());
18181 }
18182 return result.raw(); 18174 return result.raw();
18183 } 18175 }
18184 18176
18185 18177
18186 bool LinkedHashMap::Contains(const Object& key) const { 18178 bool LinkedHashMap::Contains(const Object& key) const {
18187 ASSERT(!IsNull()); 18179 ASSERT(!IsNull());
18188 EnumIndexDefaultMap map(Array::Handle(data())); 18180 EnumIndexDefaultMap map(data());
18189 bool result = map.ContainsKey(key); 18181 bool result = map.ContainsKey(key);
18190 { 18182 ASSERT(map.Release().raw() == data());
18191 RawArray* array = map.Release();
18192 ASSERT(array == data());
18193 }
18194 return result; 18183 return result;
18195 } 18184 }
18196 18185
18197 18186
18198 RawObject* LinkedHashMap::Remove(const Object& key) const { 18187 RawObject* LinkedHashMap::Remove(const Object& key) const {
18199 ASSERT(!IsNull()); 18188 ASSERT(!IsNull());
18200 EnumIndexDefaultMap map(Array::Handle(data())); 18189 EnumIndexDefaultMap map(data());
18201 // TODO(koda): Make 'Remove' also return the old value. 18190 // TODO(koda): Make 'Remove' also return the old value.
18202 const Object& result = Object::Handle(map.GetOrNull(key)); 18191 const Object& result = Object::Handle(map.GetOrNull(key));
18203 if (map.Remove(key)) { 18192 if (map.Remove(key)) {
18204 SetModified(); 18193 SetModified();
18205 } 18194 }
18206 StorePointer(&raw_ptr()->data_, map.Release()); 18195 StorePointer(&raw_ptr()->data_, map.Release().raw());
18207 return result.raw(); 18196 return result.raw();
18208 } 18197 }
18209 18198
18210 18199
18211 void LinkedHashMap::Clear() const { 18200 void LinkedHashMap::Clear() const {
18212 ASSERT(!IsNull()); 18201 ASSERT(!IsNull());
18213 if (Length() != 0) { 18202 if (Length() != 0) {
18214 EnumIndexDefaultMap map(Array::Handle(data())); 18203 EnumIndexDefaultMap map(data());
18215 map.Initialize(); 18204 map.Initialize();
18216 SetModified(); 18205 SetModified();
18217 StorePointer(&raw_ptr()->data_, map.Release()); 18206 StorePointer(&raw_ptr()->data_, map.Release().raw());
18218 } 18207 }
18219 } 18208 }
18220 18209
18221 18210
18222 RawArray* LinkedHashMap::ToArray() const { 18211 RawArray* LinkedHashMap::ToArray() const {
18223 EnumIndexDefaultMap map(Array::Handle(data())); 18212 EnumIndexDefaultMap map(data());
18224 const Array& result = Array::Handle(HashTables::ToArray(map, true)); 18213 const Array& result = Array::Handle(HashTables::ToArray(map, true));
18225 RawArray* array = map.Release(); 18214 ASSERT(map.Release().raw() == data());
18226 ASSERT(array == data());
18227 return result.raw(); 18215 return result.raw();
18228 } 18216 }
18229 18217
18230 18218
18231 void LinkedHashMap::SetModified() const { 18219 void LinkedHashMap::SetModified() const {
18232 StorePointer(&raw_ptr()->cme_mark_, Instance::null()); 18220 StorePointer(&raw_ptr()->cme_mark_, Instance::null());
18233 } 18221 }
18234 18222
18235 18223
18236 RawInstance* LinkedHashMap::GetModificationMark(bool create) const { 18224 RawInstance* LinkedHashMap::GetModificationMark(bool create) const {
(...skipping 1107 matching lines...) Expand 10 before | Expand all | Expand 10 after
19344 return tag_label.ToCString(); 19332 return tag_label.ToCString();
19345 } 19333 }
19346 19334
19347 19335
19348 void UserTag::PrintJSONImpl(JSONStream* stream, bool ref) const { 19336 void UserTag::PrintJSONImpl(JSONStream* stream, bool ref) const {
19349 Instance::PrintJSONImpl(stream, ref); 19337 Instance::PrintJSONImpl(stream, ref);
19350 } 19338 }
19351 19339
19352 19340
19353 } // namespace dart 19341 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/hash_table_test.cc ('k') | runtime/vm/symbols.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698