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 <map> | 7 #include <map> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "vm/bit_set.h" | 10 #include "vm/bit_set.h" |
11 #include "vm/object.h" | 11 #include "vm/object.h" |
12 #include "vm/raw_object.h" | 12 #include "vm/raw_object.h" |
13 | 13 |
14 namespace dart { | 14 namespace dart { |
15 | 15 |
16 | 16 |
17 FreeListElement* FreeListElement::AsElement(uword addr, intptr_t size) { | 17 FreeListElement* FreeListElement::AsElement(uword addr, intptr_t size) { |
| 18 // Precondition: the (page containing the) header of the element is |
| 19 // writable. |
18 ASSERT(size >= kObjectAlignment); | 20 ASSERT(size >= kObjectAlignment); |
19 ASSERT(Utils::IsAligned(size, kObjectAlignment)); | 21 ASSERT(Utils::IsAligned(size, kObjectAlignment)); |
20 | 22 |
21 FreeListElement* result = reinterpret_cast<FreeListElement*>(addr); | 23 FreeListElement* result = reinterpret_cast<FreeListElement*>(addr); |
22 | 24 |
23 uword tags = 0; | 25 uword tags = 0; |
24 tags = RawObject::SizeTag::update(size, tags); | 26 tags = RawObject::SizeTag::update(size, tags); |
25 tags = RawObject::ClassIdTag::update(kFreeListElement, tags); | 27 tags = RawObject::ClassIdTag::update(kFreeListElement, tags); |
26 | 28 |
27 result->tags_ = tags; | 29 result->tags_ = tags; |
28 if (size > RawObject::SizeTag::kMaxSizeTag) { | 30 if (size > RawObject::SizeTag::kMaxSizeTag) { |
29 *result->SizeAddress() = size; | 31 *result->SizeAddress() = size; |
30 } | 32 } |
31 result->set_next(NULL); | 33 result->set_next(NULL); |
32 | |
33 return result; | 34 return result; |
| 35 // Postcondition: the (page containing the) header of the element is |
| 36 // writable. |
34 } | 37 } |
35 | 38 |
36 | 39 |
37 void FreeListElement::InitOnce() { | 40 void FreeListElement::InitOnce() { |
38 ASSERT(sizeof(FreeListElement) == kObjectAlignment); | 41 ASSERT(sizeof(FreeListElement) == kObjectAlignment); |
39 ASSERT(OFFSET_OF(FreeListElement, tags_) == Object::tags_offset()); | 42 ASSERT(OFFSET_OF(FreeListElement, tags_) == Object::tags_offset()); |
40 } | 43 } |
41 | 44 |
42 | 45 |
43 FreeList::FreeList() { | 46 FreeList::FreeList() { |
44 Reset(); | 47 Reset(); |
45 } | 48 } |
46 | 49 |
47 | 50 |
48 FreeList::~FreeList() { | 51 FreeList::~FreeList() { |
49 // Nothing to release. | 52 // Nothing to release. |
50 } | 53 } |
51 | 54 |
52 | 55 |
53 uword FreeList::TryAllocate(intptr_t size) { | 56 uword FreeList::TryAllocate(intptr_t size, bool is_protected) { |
| 57 // Precondition: is_protected is false or else all free list elements are |
| 58 // in non-writable pages. |
| 59 |
| 60 // Postcondition: if allocation succeeds, the allocated block is writable. |
54 int index = IndexForSize(size); | 61 int index = IndexForSize(size); |
55 if ((index != kNumLists) && free_map_.Test(index)) { | 62 if ((index != kNumLists) && free_map_.Test(index)) { |
56 return reinterpret_cast<uword>(DequeueElement(index)); | 63 FreeListElement* element = DequeueElement(index); |
| 64 if (is_protected) { |
| 65 bool status = |
| 66 VirtualMemory::Protect(reinterpret_cast<void*>(element), |
| 67 size, |
| 68 VirtualMemory::kReadWriteExecute); |
| 69 ASSERT(status); |
| 70 } |
| 71 return reinterpret_cast<uword>(element); |
57 } | 72 } |
58 | 73 |
59 if ((index + 1) < kNumLists) { | 74 if ((index + 1) < kNumLists) { |
60 intptr_t next_index = free_map_.Next(index + 1); | 75 intptr_t next_index = free_map_.Next(index + 1); |
61 if (next_index != -1) { | 76 if (next_index != -1) { |
62 // Dequeue an element from the list, split and enqueue the remainder in | 77 // Dequeue an element from the list, split and enqueue the remainder in |
63 // the appropriate list. | 78 // the appropriate list. |
64 FreeListElement* element = DequeueElement(next_index); | 79 FreeListElement* element = DequeueElement(next_index); |
65 SplitElementAfterAndEnqueue(element, size); | 80 if (is_protected) { |
| 81 // Make the allocated block and the header of the remainder element |
| 82 // writable. The remainder will be non-writable if necessary after |
| 83 // the call to SplitElementAfterAndEnqueue. |
| 84 bool status = |
| 85 VirtualMemory::Protect(reinterpret_cast<void*>(element), |
| 86 size + FreeListElement::kHeaderSize, |
| 87 VirtualMemory::kReadWriteExecute); |
| 88 ASSERT(status); |
| 89 } |
| 90 SplitElementAfterAndEnqueue(element, size, is_protected); |
66 return reinterpret_cast<uword>(element); | 91 return reinterpret_cast<uword>(element); |
67 } | 92 } |
68 } | 93 } |
69 | 94 |
70 FreeListElement* previous = NULL; | 95 FreeListElement* previous = NULL; |
71 FreeListElement* current = free_lists_[kNumLists]; | 96 FreeListElement* current = free_lists_[kNumLists]; |
72 while (current != NULL) { | 97 while (current != NULL) { |
73 if (current->Size() >= size) { | 98 if (current->Size() >= size) { |
74 // Found an element large enough to hold the requested size. Dequeue, | 99 // Found an element large enough to hold the requested size. Dequeue, |
75 // split and enqueue the remainder. | 100 // split and enqueue the remainder. |
| 101 if (is_protected) { |
| 102 // Make the allocated block and the header of the remainder element |
| 103 // writable. The remainder will be non-writable if necessary after |
| 104 // the call to SplitElementAfterAndEnqueue. |
| 105 bool status = |
| 106 VirtualMemory::Protect(reinterpret_cast<void*>(current), |
| 107 size + FreeListElement::kHeaderSize, |
| 108 VirtualMemory::kReadWriteExecute); |
| 109 ASSERT(status); |
| 110 } |
76 if (previous == NULL) { | 111 if (previous == NULL) { |
77 free_lists_[kNumLists] = current->next(); | 112 free_lists_[kNumLists] = current->next(); |
78 } else { | 113 } else { |
| 114 // If the previous free list element's next field is protected, it |
| 115 // needs to be unprotected before storing to it and reprotected |
| 116 // after. |
| 117 bool target_is_protected = false; |
| 118 uword target_address = NULL; |
| 119 if (is_protected) { |
| 120 uword writable_start = reinterpret_cast<uword>(current); |
| 121 uword writable_end = |
| 122 writable_start + size + FreeListElement::kHeaderSize - 1; |
| 123 target_address = previous->next_address(); |
| 124 target_is_protected = |
| 125 !VirtualMemory::InSamePage(target_address, writable_start) && |
| 126 !VirtualMemory::InSamePage(target_address, writable_end); |
| 127 } |
| 128 if (target_is_protected) { |
| 129 bool status = |
| 130 VirtualMemory::Protect(reinterpret_cast<void*>(target_address), |
| 131 kWordSize, |
| 132 VirtualMemory::kReadWriteExecute); |
| 133 ASSERT(status); |
| 134 } |
79 previous->set_next(current->next()); | 135 previous->set_next(current->next()); |
| 136 if (target_is_protected) { |
| 137 bool status = |
| 138 VirtualMemory::Protect(reinterpret_cast<void*>(target_address), |
| 139 kWordSize, |
| 140 VirtualMemory::kReadExecute); |
| 141 ASSERT(status); |
| 142 } |
80 } | 143 } |
81 SplitElementAfterAndEnqueue(current, size); | 144 SplitElementAfterAndEnqueue(current, size, is_protected); |
82 return reinterpret_cast<uword>(current); | 145 return reinterpret_cast<uword>(current); |
83 } | 146 } |
84 previous = current; | 147 previous = current; |
85 current = current->next(); | 148 current = current->next(); |
86 } | 149 } |
87 return 0; | 150 return 0; |
88 } | 151 } |
89 | 152 |
90 | 153 |
91 void FreeList::Free(uword addr, intptr_t size) { | 154 void FreeList::Free(uword addr, intptr_t size) { |
| 155 // Precondition required by AsElement and EnqueueElement: the (page |
| 156 // containing the) header of the freed block should be writable. This is |
| 157 // the case when called for newly allocated pages because they are |
| 158 // allocated as writable. It is the case when called during GC sweeping |
| 159 // because the entire heap is writable. |
92 intptr_t index = IndexForSize(size); | 160 intptr_t index = IndexForSize(size); |
93 FreeListElement* element = FreeListElement::AsElement(addr, size); | 161 FreeListElement* element = FreeListElement::AsElement(addr, size); |
94 EnqueueElement(element, index); | 162 EnqueueElement(element, index); |
| 163 // Postcondition: the (page containing the) header is left writable. |
95 } | 164 } |
96 | 165 |
97 | 166 |
98 void FreeList::Reset() { | 167 void FreeList::Reset() { |
99 free_map_.Reset(); | 168 free_map_.Reset(); |
100 for (int i = 0; i < (kNumLists + 1); i++) { | 169 for (int i = 0; i < (kNumLists + 1); i++) { |
101 free_lists_[i] = NULL; | 170 free_lists_[i] = NULL; |
102 } | 171 } |
103 } | 172 } |
104 | 173 |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 } | 275 } |
207 | 276 |
208 | 277 |
209 void FreeList::Print() const { | 278 void FreeList::Print() const { |
210 PrintSmall(); | 279 PrintSmall(); |
211 PrintLarge(); | 280 PrintLarge(); |
212 } | 281 } |
213 | 282 |
214 | 283 |
215 void FreeList::SplitElementAfterAndEnqueue(FreeListElement* element, | 284 void FreeList::SplitElementAfterAndEnqueue(FreeListElement* element, |
216 intptr_t size) { | 285 intptr_t size, |
| 286 bool is_protected) { |
| 287 // Precondition required by AsElement and EnqueueElement: either |
| 288 // element->Size() == size, or else the (page containing the) header of |
| 289 // the remainder element starting at element + size is writable. |
217 intptr_t remainder_size = element->Size() - size; | 290 intptr_t remainder_size = element->Size() - size; |
218 if (remainder_size == 0) return; | 291 if (remainder_size == 0) return; |
219 | 292 |
220 element = FreeListElement::AsElement(reinterpret_cast<uword>(element) + size, | 293 uword remainder_address = reinterpret_cast<uword>(element) + size; |
221 remainder_size); | 294 element = FreeListElement::AsElement(remainder_address, remainder_size); |
222 intptr_t remainder_index = IndexForSize(remainder_size); | 295 intptr_t remainder_index = IndexForSize(remainder_size); |
223 EnqueueElement(element, remainder_index); | 296 EnqueueElement(element, remainder_index); |
| 297 |
| 298 // Postcondition: when allocating in a protected page, the remainder |
| 299 // element is no longer writable unless it is in the same page as the |
| 300 // allocated element. (The allocated element is still writable, and the |
| 301 // remainder element will be protected when the allocated one is). |
| 302 if (is_protected && |
| 303 !VirtualMemory::InSamePage(remainder_address - 1, remainder_address)) { |
| 304 bool status = |
| 305 VirtualMemory::Protect(reinterpret_cast<void*>(remainder_address), |
| 306 FreeListElement::kHeaderSize, |
| 307 VirtualMemory::kReadExecute); |
| 308 ASSERT(status); |
| 309 } |
224 } | 310 } |
225 | 311 |
226 } // namespace dart | 312 } // namespace dart |
OLD | NEW |