| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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/freelist.h" | 5 #include "vm/freelist.h" |
| 6 | 6 |
| 7 #include "vm/bit_set.h" | 7 #include "vm/bit_set.h" |
| 8 #include "vm/object.h" | 8 #include "vm/object.h" |
| 9 #include "vm/raw_object.h" | 9 #include "vm/raw_object.h" |
| 10 | 10 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 // Nothing to release. | 47 // Nothing to release. |
| 48 } | 48 } |
| 49 | 49 |
| 50 | 50 |
| 51 uword FreeList::TryAllocate(intptr_t size) { | 51 uword FreeList::TryAllocate(intptr_t size) { |
| 52 int index = IndexForSize(size); | 52 int index = IndexForSize(size); |
| 53 if ((index != kNumLists) && free_map_.Test(index)) { | 53 if ((index != kNumLists) && free_map_.Test(index)) { |
| 54 return reinterpret_cast<uword>(DequeueElement(index)); | 54 return reinterpret_cast<uword>(DequeueElement(index)); |
| 55 } | 55 } |
| 56 | 56 |
| 57 if (index < kNumLists) { | 57 if ((index + 1) < kNumLists) { |
| 58 index++; | 58 intptr_t next_index = free_map_.Next(index + 1); |
| 59 while (index < kNumLists) { | 59 if (next_index != -1) { |
| 60 if (free_map_.Test(index)) { | 60 // Dequeue an element from the list, split and enqueue the remainder in |
| 61 // Dequeue an element from the list, split and enqueue the remainder in | 61 // the appropriate list. |
| 62 // the appropriate list. | 62 FreeListElement* element = DequeueElement(next_index); |
| 63 FreeListElement* element = DequeueElement(index); | 63 SplitElementAfterAndEnqueue(element, size); |
| 64 SplitElementAfterAndEnqueue(element, size); | 64 return reinterpret_cast<uword>(element); |
| 65 return reinterpret_cast<uword>(element); | |
| 66 } | |
| 67 index++; | |
| 68 } | 65 } |
| 69 } | 66 } |
| 70 | 67 |
| 71 FreeListElement* previous = NULL; | 68 FreeListElement* previous = NULL; |
| 72 FreeListElement* current = free_lists_[kNumLists]; | 69 FreeListElement* current = free_lists_[kNumLists]; |
| 73 while (current != NULL) { | 70 while (current != NULL) { |
| 74 if (current->Size() >= size) { | 71 if (current->Size() >= size) { |
| 75 // Found an element large enough to hold the requested size. Dequeue, | 72 // Found an element large enough to hold the requested size. Dequeue, |
| 76 // split and enqueue the remainder. | 73 // split and enqueue the remainder. |
| 77 if (previous == NULL) { | 74 if (previous == NULL) { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 intptr_t index = size / kObjectAlignment; | 108 intptr_t index = size / kObjectAlignment; |
| 112 if (index >= kNumLists) { | 109 if (index >= kNumLists) { |
| 113 index = kNumLists; | 110 index = kNumLists; |
| 114 } | 111 } |
| 115 return index; | 112 return index; |
| 116 } | 113 } |
| 117 | 114 |
| 118 | 115 |
| 119 void FreeList::EnqueueElement(FreeListElement* element, intptr_t index) { | 116 void FreeList::EnqueueElement(FreeListElement* element, intptr_t index) { |
| 120 FreeListElement* next = free_lists_[index]; | 117 FreeListElement* next = free_lists_[index]; |
| 121 if (next == NULL) { | 118 if (next == NULL && index != kNumLists) { |
| 122 free_map_.Set(index, true); | 119 free_map_.Set(index, true); |
| 123 } | 120 } |
| 124 element->set_next(next); | 121 element->set_next(next); |
| 125 free_lists_[index] = element; | 122 free_lists_[index] = element; |
| 126 } | 123 } |
| 127 | 124 |
| 128 | 125 |
| 129 FreeListElement* FreeList::DequeueElement(intptr_t index) { | 126 FreeListElement* FreeList::DequeueElement(intptr_t index) { |
| 130 FreeListElement* result = free_lists_[index]; | 127 FreeListElement* result = free_lists_[index]; |
| 131 FreeListElement* next = result->next(); | 128 FreeListElement* next = result->next(); |
| 132 if (next == NULL) { | 129 if (next == NULL && index != kNumLists) { |
| 133 free_map_.Set(index, false); | 130 free_map_.Set(index, false); |
| 134 } | 131 } |
| 135 free_lists_[index] = next; | 132 free_lists_[index] = next; |
| 136 return result; | 133 return result; |
| 137 } | 134 } |
| 138 | 135 |
| 139 | 136 |
| 140 intptr_t FreeList::Length(int index) const { | 137 intptr_t FreeList::Length(int index) const { |
| 141 ASSERT(index >= 0); | 138 ASSERT(index >= 0); |
| 142 ASSERT(index < kNumLists); | 139 ASSERT(index < kNumLists); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 intptr_t remainder_size = element->Size() - size; | 174 intptr_t remainder_size = element->Size() - size; |
| 178 if (remainder_size == 0) return; | 175 if (remainder_size == 0) return; |
| 179 | 176 |
| 180 element = FreeListElement::AsElement(reinterpret_cast<uword>(element) + size, | 177 element = FreeListElement::AsElement(reinterpret_cast<uword>(element) + size, |
| 181 remainder_size); | 178 remainder_size); |
| 182 intptr_t remainder_index = IndexForSize(remainder_size); | 179 intptr_t remainder_index = IndexForSize(remainder_size); |
| 183 EnqueueElement(element, remainder_index); | 180 EnqueueElement(element, remainder_index); |
| 184 } | 181 } |
| 185 | 182 |
| 186 } // namespace dart | 183 } // namespace dart |
| OLD | NEW |