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

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

Issue 17992002: - Add a WeakTable to the VM. This is used to remember the (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 5 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/weak_table.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
(Empty)
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
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.
4
5 #include "vm/weak_table.h"
6
7 #include "platform/assert.h"
8 #include "vm/raw_object.h"
9
10 namespace dart {
11
12 WeakTable* WeakTable::SetValue(RawObject* key, intptr_t val) {
13 intptr_t sz = size();
14 intptr_t idx = Hash(key) % sz;
15 intptr_t empty_idx = -1;
16 RawObject* obj = ObjectAt(idx);
17
18 while (obj != NULL) {
19 if (obj == key) {
20 SetValueAt(idx, val);
21 return this;
22 } else if ((empty_idx < 0) &&
23 (reinterpret_cast<intptr_t>(obj) == kDeletedEntry)) {
24 empty_idx = idx; // Insert at this location if not found.
25 }
26 idx = (idx + 1) % sz;
27 obj = ObjectAt(idx);
28 }
29
30 if (val == 0) {
31 // Do not enter an invalid value. Associating 0 with a key deletes it from
32 // this weak table above in SetValueAt. If the key was not present in the
33 // weak table we are done.
34 return this;
35 }
36
37 if (empty_idx >= 0) {
38 // We will be reusing a slot below.
39 set_used(used() - 1);
40 idx = empty_idx;
41 }
42
43 ASSERT(!IsValidEntryAt(idx));
44 // Set the key and value.
45 SetObjectAt(idx, key);
46 SetValueAt(idx, val);
47 // Update the counts.
48 set_used(used() + 1);
49 set_count(count() + 1);
50
51 // Rehash if needed to ensure that there are empty slots available.
52 if (used_ >= limit()) {
53 return Rehash();
54 }
55 return this;
56 }
57
58
59 WeakTable* WeakTable::Rehash() {
60 intptr_t sz = size();
61 WeakTable* result = NewFrom(this);
62
63 for (intptr_t i = 0; i < sz; i++) {
64 if (IsValidEntryAt(i)) {
65 WeakTable* temp = result->SetValue(ObjectAt(i), ValueAt(i));
66 ASSERT(temp == result);
67 }
68 }
69 return result;
70 }
71
72 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/weak_table.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698