Chromium Code Reviews| 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 25 matching lines...) Expand all Loading... | |
| 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 #ifdef DEBUG |
| 96 // Constant byte value used for zapping dead memory in debug mode. | 95 // Constant byte value used for zapping dead memory in debug mode. |
| 97 static const unsigned char kZapDeadByte = 0xcd; | 96 static const unsigned char kZapDeadByte = 0xcd; |
| 98 #endif | 97 #endif |
| 99 | 98 |
| 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) | 99 // Traverse the chained list of segments, zapping (in debug mode) |
| 103 // and freeing every segment except the one we wish to keep. | 100 // and freeing every segment except the one we wish to keep. |
|
Michael Starzinger
2016/09/20 10:35:39
nit: Lets drop the "except the one we wish to keep
| |
| 104 for (Segment* current = segment_head_; current;) { | 101 for (Segment* current = segment_head_; current;) { |
| 105 Segment* next = current->next(); | 102 Segment* next = current->next(); |
| 106 if (!keep && current->size() <= kMaximumKeptSegmentSize) { | 103 size_t size = current->size(); |
| 107 // Unlink the segment we wish to keep from the list. | |
| 108 keep = current; | |
| 109 keep->set_next(nullptr); | |
| 110 } else { | |
| 111 size_t size = current->size(); | |
| 112 #ifdef DEBUG | 104 #ifdef DEBUG |
| 113 // Un-poison first so the zapping doesn't trigger ASan complaints. | 105 // Un-poison first so the zapping doesn't trigger ASan complaints. |
| 114 ASAN_UNPOISON_MEMORY_REGION(current, size); | 106 ASAN_UNPOISON_MEMORY_REGION(current, size); |
| 115 // Zap the entire current segment (including the header). | 107 // Zap the entire current segment (including the header). |
| 116 memset(current, kZapDeadByte, size); | 108 memset(current, kZapDeadByte, size); |
| 117 #endif | 109 #endif |
| 118 segment_bytes_allocated_ -= size; | 110 segment_bytes_allocated_ -= size; |
| 119 allocator_->ReturnSegment(current); | 111 allocator_->ReturnSegment(current); |
| 120 } | 112 |
| 121 current = next; | 113 current = next; |
| 122 } | 114 } |
| 123 | 115 |
| 124 // If we have found a segment we want to keep, we must recompute the | 116 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 | |
| 142 allocation_size_ = 0; | 117 allocation_size_ = 0; |
| 143 // Update the head segment to be the kept segment (if any). | 118 segment_head_ = nullptr; |
| 144 segment_head_ = keep; | |
| 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_->ReturnSegment(segment_head_); | |
| 164 segment_head_ = nullptr; | |
| 165 } | |
| 166 | |
| 167 DCHECK(segment_bytes_allocated_ == 0); | |
| 168 } | 119 } |
| 169 | 120 |
| 170 // Creates a new segment, sets it size, and pushes it to the front | 121 // Creates a new segment, sets it size, and pushes it to the front |
| 171 // of the segment chain. Returns the new segment. | 122 // of the segment chain. Returns the new segment. |
| 172 Segment* Zone::NewSegment(size_t size) { | 123 Segment* Zone::NewSegment(size_t size) { |
| 173 Segment* result = allocator_->GetSegment(size); | 124 Segment* result = allocator_->GetSegment(size); |
| 174 segment_bytes_allocated_ += size; | 125 segment_bytes_allocated_ += size; |
| 175 if (result != nullptr) { | 126 if (result != nullptr) { |
| 176 result->Initialize(segment_head_, size, this); | 127 result->Initialize(segment_head_, size, this); |
| 177 segment_head_ = result; | 128 segment_head_ = result; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 230 // size bytes + header and alignment padding) | 181 // size bytes + header and alignment padding) |
| 231 DCHECK(reinterpret_cast<uintptr_t>(position_) >= | 182 DCHECK(reinterpret_cast<uintptr_t>(position_) >= |
| 232 reinterpret_cast<uintptr_t>(result)); | 183 reinterpret_cast<uintptr_t>(result)); |
| 233 limit_ = segment->end(); | 184 limit_ = segment->end(); |
| 234 DCHECK(position_ <= limit_); | 185 DCHECK(position_ <= limit_); |
| 235 return result; | 186 return result; |
| 236 } | 187 } |
| 237 | 188 |
| 238 } // namespace internal | 189 } // namespace internal |
| 239 } // namespace v8 | 190 } // namespace v8 |
| OLD | NEW |