| 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 #ifdef DEBUG | 94 // Traverse the chained list of segments and return them all to the allocator. |
| 96 // Constant byte value used for zapping dead memory in debug mode. | |
| 97 static const unsigned char kZapDeadByte = 0xcd; | |
| 98 #endif | |
| 99 | |
| 100 // Find a segment with a suitable size to keep around. | |
| 101 Segment* keep = nullptr; | |
| 102 // Traverse the chained list of segments, zapping (in debug mode) | |
| 103 // and freeing every segment except the one we wish to keep. | |
| 104 for (Segment* current = segment_head_; current;) { | 95 for (Segment* current = segment_head_; current;) { |
| 105 Segment* next = current->next(); | 96 Segment* next = current->next(); |
| 106 if (!keep && current->size() <= kMaximumKeptSegmentSize) { | 97 size_t size = current->size(); |
| 107 // Unlink the segment we wish to keep from the list. | 98 |
| 108 keep = current; | 99 // Un-poison the segment content so we can re-use or zap it later. |
| 109 keep->set_next(nullptr); | 100 ASAN_UNPOISON_MEMORY_REGION(current->start(), current->capacity()); |
| 110 } else { | 101 |
| 111 size_t size = current->size(); | 102 segment_bytes_allocated_ -= size; |
| 112 #ifdef DEBUG | 103 allocator_->ReturnSegment(current); |
| 113 // Un-poison first so the zapping doesn't trigger ASan complaints. | |
| 114 ASAN_UNPOISON_MEMORY_REGION(current, size); | |
| 115 // Zap the entire current segment (including the header). | |
| 116 memset(current, kZapDeadByte, size); | |
| 117 #endif | |
| 118 segment_bytes_allocated_ -= size; | |
| 119 allocator_->FreeSegment(current); | |
| 120 } | |
| 121 current = next; | 104 current = next; |
| 122 } | 105 } |
| 123 | 106 |
| 124 // If we have found a segment we want to keep, we must recompute the | 107 position_ = limit_ = 0; |
| 125 // variables 'position' and 'limit' to prepare for future allocate | |
| 126 // attempts. Otherwise, we must clear the position and limit to | |
| 127 // force a new segment to be allocated on demand. | |
| 128 if (keep) { | |
| 129 Address start = keep->start(); | |
| 130 position_ = RoundUp(start, kAlignment); | |
| 131 limit_ = keep->end(); | |
| 132 // Un-poison so we can re-use the segment later. | |
| 133 ASAN_UNPOISON_MEMORY_REGION(start, keep->capacity()); | |
| 134 #ifdef DEBUG | |
| 135 // Zap the contents of the kept segment (but not the header). | |
| 136 memset(start, kZapDeadByte, keep->capacity()); | |
| 137 #endif | |
| 138 } else { | |
| 139 position_ = limit_ = 0; | |
| 140 } | |
| 141 | 108 |
| 142 allocation_size_ = 0; | 109 allocation_size_ = 0; |
| 143 // Update the head segment to be the kept segment (if any). | 110 // Update the head segment to be the kept segment (if any). |
| 144 segment_head_ = keep; | 111 segment_head_ = nullptr; |
| 145 } | |
| 146 | |
| 147 void Zone::DeleteKeptSegment() { | |
| 148 #ifdef DEBUG | |
| 149 // Constant byte value used for zapping dead memory in debug mode. | |
| 150 static const unsigned char kZapDeadByte = 0xcd; | |
| 151 #endif | |
| 152 | |
| 153 DCHECK(segment_head_ == nullptr || segment_head_->next() == nullptr); | |
| 154 if (segment_head_ != nullptr) { | |
| 155 size_t size = segment_head_->size(); | |
| 156 #ifdef DEBUG | |
| 157 // Un-poison first so the zapping doesn't trigger ASan complaints. | |
| 158 ASAN_UNPOISON_MEMORY_REGION(segment_head_, size); | |
| 159 // Zap the entire kept segment (including the header). | |
| 160 memset(segment_head_, kZapDeadByte, size); | |
| 161 #endif | |
| 162 segment_bytes_allocated_ -= size; | |
| 163 allocator_->FreeSegment(segment_head_); | |
| 164 segment_head_ = nullptr; | |
| 165 } | |
| 166 | |
| 167 DCHECK(segment_bytes_allocated_ == 0); | |
| 168 } | 112 } |
| 169 | 113 |
| 170 // 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 |
| 171 // of the segment chain. Returns the new segment. | 115 // of the segment chain. Returns the new segment. |
| 172 Segment* Zone::NewSegment(size_t size) { | 116 Segment* Zone::NewSegment(size_t size) { |
| 173 Segment* result = allocator_->AllocateSegment(size); | 117 Segment* result = allocator_->GetSegment(size); |
| 174 segment_bytes_allocated_ += size; | 118 segment_bytes_allocated_ += size; |
| 175 if (result != nullptr) { | 119 if (result != nullptr) { |
| 176 result->Initialize(segment_head_, size, this); | 120 result->Initialize(segment_head_, size, this); |
| 177 segment_head_ = result; | 121 segment_head_ = result; |
| 178 } | 122 } |
| 179 return result; | 123 return result; |
| 180 } | 124 } |
| 181 | 125 |
| 182 Address Zone::NewExpand(size_t size) { | 126 Address Zone::NewExpand(size_t size) { |
| 183 // Make sure the requested size is already properly aligned and that | 127 // Make sure the requested size is already properly aligned and that |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 // size bytes + header and alignment padding) | 174 // size bytes + header and alignment padding) |
| 231 DCHECK(reinterpret_cast<uintptr_t>(position_) >= | 175 DCHECK(reinterpret_cast<uintptr_t>(position_) >= |
| 232 reinterpret_cast<uintptr_t>(result)); | 176 reinterpret_cast<uintptr_t>(result)); |
| 233 limit_ = segment->end(); | 177 limit_ = segment->end(); |
| 234 DCHECK(position_ <= limit_); | 178 DCHECK(position_ <= limit_); |
| 235 return result; | 179 return result; |
| 236 } | 180 } |
| 237 | 181 |
| 238 } // namespace internal | 182 } // namespace internal |
| 239 } // namespace v8 | 183 } // namespace v8 |
| OLD | NEW |