OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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/weak_table.h" | 5 #include "vm/weak_table.h" |
6 | 6 |
7 #include "platform/assert.h" | 7 #include "platform/assert.h" |
8 #include "vm/raw_object.h" | 8 #include "vm/raw_object.h" |
9 | 9 |
10 namespace dart { | 10 namespace dart { |
11 | 11 |
12 intptr_t WeakTable::SizeFor(intptr_t count, intptr_t size) { | 12 intptr_t WeakTable::SizeFor(intptr_t count, intptr_t size) { |
13 intptr_t result = size; | 13 intptr_t result = size; |
14 if (count <= (size / 4)) { | 14 if (count <= (size / 4)) { |
15 // Reduce the capacity. | 15 // Reduce the capacity. |
16 result = size / 2; | 16 result = size / 2; |
17 } else { | 17 } else { |
18 // Increase the capacity. | 18 // Increase the capacity. |
19 result = size * 2; | 19 result = size * 2; |
20 if (result < size) { | 20 if (result < size) { |
21 FATAL("Reached impossible state of having more weak table entries" | 21 FATAL( |
22 " than memory available for heap objects."); | 22 "Reached impossible state of having more weak table entries" |
| 23 " than memory available for heap objects."); |
23 } | 24 } |
24 } | 25 } |
25 if (result < kMinSize) { | 26 if (result < kMinSize) { |
26 result = kMinSize; | 27 result = kMinSize; |
27 } | 28 } |
28 return result; | 29 return result; |
29 } | 30 } |
30 | 31 |
31 | 32 |
32 void WeakTable::SetValue(RawObject* key, intptr_t val) { | 33 void WeakTable::SetValue(RawObject* key, intptr_t val) { |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 free(old_data); | 85 free(old_data); |
85 } | 86 } |
86 | 87 |
87 | 88 |
88 void WeakTable::Rehash() { | 89 void WeakTable::Rehash() { |
89 intptr_t old_size = size(); | 90 intptr_t old_size = size(); |
90 intptr_t* old_data = data_; | 91 intptr_t* old_data = data_; |
91 | 92 |
92 intptr_t new_size = SizeFor(count(), size()); | 93 intptr_t new_size = SizeFor(count(), size()); |
93 ASSERT(Utils::IsPowerOfTwo(new_size)); | 94 ASSERT(Utils::IsPowerOfTwo(new_size)); |
94 intptr_t* new_data = reinterpret_cast<intptr_t*>( | 95 intptr_t* new_data = |
95 calloc(new_size, kEntrySize * kWordSize)); | 96 reinterpret_cast<intptr_t*>(calloc(new_size, kEntrySize * kWordSize)); |
96 | 97 |
97 intptr_t mask = new_size - 1; | 98 intptr_t mask = new_size - 1; |
98 set_used(0); | 99 set_used(0); |
99 for (intptr_t i = 0; i < old_size; i++) { | 100 for (intptr_t i = 0; i < old_size; i++) { |
100 if (IsValidEntryAt(i)) { | 101 if (IsValidEntryAt(i)) { |
101 // Find the new hash location for this entry. | 102 // Find the new hash location for this entry. |
102 RawObject* key = ObjectAt(i); | 103 RawObject* key = ObjectAt(i); |
103 intptr_t idx = Hash(key) & mask; | 104 intptr_t idx = Hash(key) & mask; |
104 RawObject* obj = reinterpret_cast<RawObject*>(new_data[ObjectIndex(idx)]); | 105 RawObject* obj = reinterpret_cast<RawObject*>(new_data[ObjectIndex(idx)]); |
105 while (obj != NULL) { | 106 while (obj != NULL) { |
(...skipping 10 matching lines...) Expand all Loading... |
116 // We should only have used valid entries. | 117 // We should only have used valid entries. |
117 ASSERT(used() == count()); | 118 ASSERT(used() == count()); |
118 | 119 |
119 // Switch to using the newly allocated backing store. | 120 // Switch to using the newly allocated backing store. |
120 size_ = new_size; | 121 size_ = new_size; |
121 data_ = new_data; | 122 data_ = new_data; |
122 free(old_data); | 123 free(old_data); |
123 } | 124 } |
124 | 125 |
125 } // namespace dart | 126 } // namespace dart |
OLD | NEW |