OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/zone/zone.h" | 5 #include "src/zone/zone.h" |
6 | 6 |
7 #include <cstring> | 7 #include <cstring> |
8 | 8 |
9 #include "src/v8.h" | 9 #include "src/v8.h" |
10 | 10 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 Zone::Zone(AccountingAllocator* allocator) | 44 Zone::Zone(AccountingAllocator* allocator) |
45 : allocation_size_(0), | 45 : allocation_size_(0), |
46 segment_bytes_allocated_(0), | 46 segment_bytes_allocated_(0), |
47 position_(0), | 47 position_(0), |
48 limit_(0), | 48 limit_(0), |
49 allocator_(allocator), | 49 allocator_(allocator), |
50 segment_head_(nullptr) {} | 50 segment_head_(nullptr) {} |
51 | 51 |
52 Zone::~Zone() { | 52 Zone::~Zone() { |
53 DeleteAll(); | 53 DeleteAll(); |
54 DeleteKeptSegment(); | |
55 | 54 |
56 DCHECK(segment_bytes_allocated_ == 0); | 55 DCHECK(segment_bytes_allocated_ == 0); |
57 } | 56 } |
58 | 57 |
59 void* Zone::New(size_t size) { | 58 void* Zone::New(size_t size) { |
60 // Round up the requested size to fit the alignment. | 59 // Round up the requested size to fit the alignment. |
61 size = RoundUp(size, kAlignment); | 60 size = RoundUp(size, kAlignment); |
62 | 61 |
63 // If the allocation size is divisible by 8 then we return an 8-byte aligned | 62 // If the allocation size is divisible by 8 then we return an 8-byte aligned |
64 // address. | 63 // address. |
(...skipping 20 matching lines...) Expand all Loading... |
85 DCHECK(redzone_position + kASanRedzoneBytes == position_); | 84 DCHECK(redzone_position + kASanRedzoneBytes == position_); |
86 ASAN_POISON_MEMORY_REGION(redzone_position, kASanRedzoneBytes); | 85 ASAN_POISON_MEMORY_REGION(redzone_position, kASanRedzoneBytes); |
87 | 86 |
88 // Check that the result has the proper alignment and return it. | 87 // Check that the result has the proper alignment and return it. |
89 DCHECK(IsAddressAligned(result, kAlignment, 0)); | 88 DCHECK(IsAddressAligned(result, kAlignment, 0)); |
90 allocation_size_ += size; | 89 allocation_size_ += size; |
91 return reinterpret_cast<void*>(result); | 90 return reinterpret_cast<void*>(result); |
92 } | 91 } |
93 | 92 |
94 void Zone::DeleteAll() { | 93 void Zone::DeleteAll() { |
95 // Find a segment with a suitable size to keep around. | 94 // Traverse the chained list of segments and return them all to the allocator. |
96 Segment* keep = nullptr; | |
97 // Traverse the chained list of segments, zapping (in debug mode) | |
98 // and freeing every segment except the one we wish to keep. | |
99 for (Segment* current = segment_head_; current;) { | 95 for (Segment* current = segment_head_; current;) { |
100 Segment* next = current->next(); | 96 Segment* next = current->next(); |
101 if (!keep && current->size() <= kMaximumKeptSegmentSize) { | 97 size_t size = current->size(); |
102 // Unlink the segment we wish to keep from the list. | 98 |
103 keep = current; | 99 // Un-poison the segment content so we can re-use or zap it later. |
104 keep->set_next(nullptr); | 100 ASAN_UNPOISON_MEMORY_REGION(current->start(), current->capacity()); |
105 } else { | 101 |
106 size_t size = current->size(); | 102 segment_bytes_allocated_ -= size; |
107 #ifdef DEBUG | 103 allocator_->ReturnSegment(current); |
108 // Un-poison first so the zapping doesn't trigger ASan complaints. | |
109 ASAN_UNPOISON_MEMORY_REGION(current, size); | |
110 #endif | |
111 current->ZapContents(); | |
112 segment_bytes_allocated_ -= size; | |
113 allocator_->FreeSegment(current); | |
114 } | |
115 current = next; | 104 current = next; |
116 } | 105 } |
117 | 106 |
118 // If we have found a segment we want to keep, we must recompute the | 107 position_ = limit_ = 0; |
119 // variables 'position' and 'limit' to prepare for future allocate | |
120 // attempts. Otherwise, we must clear the position and limit to | |
121 // force a new segment to be allocated on demand. | |
122 if (keep) { | |
123 Address start = keep->start(); | |
124 position_ = RoundUp(start, kAlignment); | |
125 limit_ = keep->end(); | |
126 // Un-poison so we can re-use the segment later. | |
127 ASAN_UNPOISON_MEMORY_REGION(start, keep->capacity()); | |
128 keep->ZapContents(); | |
129 } else { | |
130 position_ = limit_ = 0; | |
131 } | |
132 | 108 |
133 allocation_size_ = 0; | 109 allocation_size_ = 0; |
134 // Update the head segment to be the kept segment (if any). | 110 // Update the head segment to be the kept segment (if any). |
135 segment_head_ = keep; | 111 segment_head_ = nullptr; |
136 } | |
137 | |
138 void Zone::DeleteKeptSegment() { | |
139 DCHECK(segment_head_ == nullptr || segment_head_->next() == nullptr); | |
140 if (segment_head_ != nullptr) { | |
141 size_t size = segment_head_->size(); | |
142 #ifdef DEBUG | |
143 // Un-poison first so the zapping doesn't trigger ASan complaints. | |
144 ASAN_UNPOISON_MEMORY_REGION(segment_head_, size); | |
145 #endif | |
146 segment_head_->ZapContents(); | |
147 segment_bytes_allocated_ -= size; | |
148 allocator_->FreeSegment(segment_head_); | |
149 segment_head_ = nullptr; | |
150 } | |
151 | |
152 DCHECK(segment_bytes_allocated_ == 0); | |
153 } | 112 } |
154 | 113 |
155 // Creates a new segment, sets it size, and pushes it to the front | 114 // Creates a new segment, sets it size, and pushes it to the front |
156 // of the segment chain. Returns the new segment. | 115 // of the segment chain. Returns the new segment. |
157 Segment* Zone::NewSegment(size_t size) { | 116 Segment* Zone::NewSegment(size_t requested_size) { |
158 Segment* result = allocator_->AllocateSegment(size); | 117 Segment* result = allocator_->GetSegment(requested_size); |
159 segment_bytes_allocated_ += size; | 118 DCHECK_GE(result->size(), requested_size); |
| 119 segment_bytes_allocated_ += result->size(); |
160 if (result != nullptr) { | 120 if (result != nullptr) { |
161 result->Initialize(segment_head_, size, this); | 121 result->set_zone(this); |
| 122 result->set_next(segment_head_); |
162 segment_head_ = result; | 123 segment_head_ = result; |
163 } | 124 } |
164 return result; | 125 return result; |
165 } | 126 } |
166 | 127 |
167 Address Zone::NewExpand(size_t size) { | 128 Address Zone::NewExpand(size_t size) { |
168 // Make sure the requested size is already properly aligned and that | 129 // Make sure the requested size is already properly aligned and that |
169 // there isn't enough room in the Zone to satisfy the request. | 130 // there isn't enough room in the Zone to satisfy the request. |
170 DCHECK_EQ(size, RoundDown(size, kAlignment)); | 131 DCHECK_EQ(size, RoundDown(size, kAlignment)); |
171 DCHECK(limit_ < position_ || | 132 DCHECK(limit_ < position_ || |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 // size bytes + header and alignment padding) | 176 // size bytes + header and alignment padding) |
216 DCHECK(reinterpret_cast<uintptr_t>(position_) >= | 177 DCHECK(reinterpret_cast<uintptr_t>(position_) >= |
217 reinterpret_cast<uintptr_t>(result)); | 178 reinterpret_cast<uintptr_t>(result)); |
218 limit_ = segment->end(); | 179 limit_ = segment->end(); |
219 DCHECK(position_ <= limit_); | 180 DCHECK(position_ <= limit_); |
220 return result; | 181 return result; |
221 } | 182 } |
222 | 183 |
223 } // namespace internal | 184 } // namespace internal |
224 } // namespace v8 | 185 } // namespace v8 |
OLD | NEW |