blob: eb2e532a228d0390dd6ea62cf545aee428ac7bf0 [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005#include <string.h>
Ben Murdoch85b71792012-04-11 18:30:58 +01006
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#include "src/v8.h"
8#include "src/zone-inl.h"
Steve Blocka7e24c12009-10-30 11:49:00 +00009
10namespace v8 {
11namespace internal {
12
13
Steve Blocka7e24c12009-10-30 11:49:00 +000014// Segments represent chunks of memory: They have starting address
15// (encoded in the this pointer) and a size in bytes. Segments are
16// chained together forming a LIFO structure with the newest segment
Steve Block44f0eee2011-05-26 01:26:41 +010017// available as segment_head_. Segments are allocated using malloc()
Steve Blocka7e24c12009-10-30 11:49:00 +000018// and de-allocated using free().
19
20class Segment {
21 public:
Ben Murdoch69a99ed2011-11-30 16:03:39 +000022 void Initialize(Segment* next, int size) {
23 next_ = next;
24 size_ = size;
25 }
26
Steve Blocka7e24c12009-10-30 11:49:00 +000027 Segment* next() const { return next_; }
28 void clear_next() { next_ = NULL; }
29
30 int size() const { return size_; }
31 int capacity() const { return size_ - sizeof(Segment); }
32
33 Address start() const { return address(sizeof(Segment)); }
34 Address end() const { return address(size_); }
35
Steve Blocka7e24c12009-10-30 11:49:00 +000036 private:
37 // Computes the address of the nth byte in this segment.
38 Address address(int n) const {
39 return Address(this) + n;
40 }
41
Steve Blocka7e24c12009-10-30 11:49:00 +000042 Segment* next_;
43 int size_;
44};
45
46
Ben Murdochb8a8cc12014-11-26 15:28:44 +000047Zone::Zone(Isolate* isolate)
48 : allocation_size_(0),
Ben Murdoch69a99ed2011-11-30 16:03:39 +000049 segment_bytes_allocated_(0),
50 position_(0),
51 limit_(0),
Ben Murdochb8a8cc12014-11-26 15:28:44 +000052 segment_head_(NULL),
53 isolate_(isolate) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +000054}
Ben Murdoch69a99ed2011-11-30 16:03:39 +000055
Ben Murdochb8a8cc12014-11-26 15:28:44 +000056
57Zone::~Zone() {
58 DeleteAll();
59 DeleteKeptSegment();
60
61 DCHECK(segment_bytes_allocated_ == 0);
62}
63
64
65void* Zone::New(int size) {
66 // Round up the requested size to fit the alignment.
67 size = RoundUp(size, kAlignment);
68
69 // If the allocation size is divisible by 8 then we return an 8-byte aligned
70 // address.
71 if (kPointerSize == 4 && kAlignment == 4) {
72 position_ += ((~size) & 4) & (reinterpret_cast<intptr_t>(position_) & 4);
73 } else {
74 DCHECK(kAlignment >= kPointerSize);
75 }
76
77 // Check if the requested size is available without expanding.
78 Address result = position_;
79
80 int size_with_redzone =
81#ifdef V8_USE_ADDRESS_SANITIZER
82 size + kASanRedzoneBytes;
83#else
84 size;
85#endif
86
87 if (size_with_redzone > limit_ - position_) {
88 result = NewExpand(size_with_redzone);
89 } else {
90 position_ += size_with_redzone;
91 }
92
93#ifdef V8_USE_ADDRESS_SANITIZER
94 Address redzone_position = result + size;
95 DCHECK(redzone_position + kASanRedzoneBytes == position_);
96 ASAN_POISON_MEMORY_REGION(redzone_position, kASanRedzoneBytes);
97#endif
98
99 // Check that the result has the proper alignment and return it.
100 DCHECK(IsAddressAligned(result, kAlignment, 0));
101 allocation_size_ += size;
102 return reinterpret_cast<void*>(result);
103}
104
105
106void Zone::DeleteAll() {
107#ifdef DEBUG
108 // Constant byte value used for zapping dead memory in debug mode.
109 static const unsigned char kZapDeadByte = 0xcd;
110#endif
111
112 // Find a segment with a suitable size to keep around.
113 Segment* keep = NULL;
114 // Traverse the chained list of segments, zapping (in debug mode)
115 // and freeing every segment except the one we wish to keep.
116 for (Segment* current = segment_head_; current != NULL; ) {
117 Segment* next = current->next();
118 if (keep == NULL && current->size() <= kMaximumKeptSegmentSize) {
119 // Unlink the segment we wish to keep from the list.
120 keep = current;
121 keep->clear_next();
122 } else {
123 int size = current->size();
124#ifdef DEBUG
125 // Un-poison first so the zapping doesn't trigger ASan complaints.
126 ASAN_UNPOISON_MEMORY_REGION(current, size);
127 // Zap the entire current segment (including the header).
128 memset(current, kZapDeadByte, size);
129#endif
130 DeleteSegment(current, size);
131 }
132 current = next;
133 }
134
135 // If we have found a segment we want to keep, we must recompute the
136 // variables 'position' and 'limit' to prepare for future allocate
137 // attempts. Otherwise, we must clear the position and limit to
138 // force a new segment to be allocated on demand.
139 if (keep != NULL) {
140 Address start = keep->start();
141 position_ = RoundUp(start, kAlignment);
142 limit_ = keep->end();
143 // Un-poison so we can re-use the segment later.
144 ASAN_UNPOISON_MEMORY_REGION(start, keep->capacity());
145#ifdef DEBUG
146 // Zap the contents of the kept segment (but not the header).
147 memset(start, kZapDeadByte, keep->capacity());
148#endif
149 } else {
150 position_ = limit_ = 0;
151 }
152
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400153 allocation_size_ = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000154 // Update the head segment to be the kept segment (if any).
155 segment_head_ = keep;
156}
157
158
159void Zone::DeleteKeptSegment() {
160#ifdef DEBUG
161 // Constant byte value used for zapping dead memory in debug mode.
162 static const unsigned char kZapDeadByte = 0xcd;
163#endif
164
165 DCHECK(segment_head_ == NULL || segment_head_->next() == NULL);
166 if (segment_head_ != NULL) {
167 int size = segment_head_->size();
168#ifdef DEBUG
169 // Un-poison first so the zapping doesn't trigger ASan complaints.
170 ASAN_UNPOISON_MEMORY_REGION(segment_head_, size);
171 // Zap the entire kept segment (including the header).
172 memset(segment_head_, kZapDeadByte, size);
173#endif
174 DeleteSegment(segment_head_, size);
175 segment_head_ = NULL;
176 }
177
178 DCHECK(segment_bytes_allocated_ == 0);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000179}
180
181
Steve Block44f0eee2011-05-26 01:26:41 +0100182// Creates a new segment, sets it size, and pushes it to the front
183// of the segment chain. Returns the new segment.
184Segment* Zone::NewSegment(int size) {
185 Segment* result = reinterpret_cast<Segment*>(Malloced::New(size));
186 adjust_segment_bytes_allocated(size);
187 if (result != NULL) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000188 result->Initialize(segment_head_, size);
Steve Block44f0eee2011-05-26 01:26:41 +0100189 segment_head_ = result;
190 }
191 return result;
192}
193
194
195// Deletes the given segment. Does not touch the segment chain.
196void Zone::DeleteSegment(Segment* segment, int size) {
197 adjust_segment_bytes_allocated(-size);
198 Malloced::Delete(segment);
199}
Steve Blocka7e24c12009-10-30 11:49:00 +0000200
201
Steve Blocka7e24c12009-10-30 11:49:00 +0000202Address Zone::NewExpand(int size) {
203 // Make sure the requested size is already properly aligned and that
204 // there isn't enough room in the Zone to satisfy the request.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000205 DCHECK(size == RoundDown(size, kAlignment));
206 DCHECK(size > limit_ - position_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000207
208 // Compute the new segment size. We use a 'high water mark'
209 // strategy, where we increase the segment size every time we expand
210 // except that we employ a maximum segment size when we delete. This
211 // is to avoid excessive malloc() and free() overhead.
Steve Block44f0eee2011-05-26 01:26:41 +0100212 Segment* head = segment_head_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000213 const size_t old_size = (head == NULL) ? 0 : head->size();
214 static const size_t kSegmentOverhead = sizeof(Segment) + kAlignment;
215 const size_t new_size_no_overhead = size + (old_size << 1);
216 size_t new_size = kSegmentOverhead + new_size_no_overhead;
217 const size_t min_new_size = kSegmentOverhead + static_cast<size_t>(size);
Ben Murdoch589d6972011-11-30 16:04:58 +0000218 // Guard against integer overflow.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000219 if (new_size_no_overhead < static_cast<size_t>(size) ||
220 new_size < static_cast<size_t>(kSegmentOverhead)) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000221 V8::FatalProcessOutOfMemory("Zone");
222 return NULL;
223 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000224 if (new_size < static_cast<size_t>(kMinimumSegmentSize)) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000225 new_size = kMinimumSegmentSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000226 } else if (new_size > static_cast<size_t>(kMaximumSegmentSize)) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000227 // Limit the size of new segments to avoid growing the segment size
228 // exponentially, thus putting pressure on contiguous virtual address space.
229 // All the while making sure to allocate a segment large enough to hold the
230 // requested size.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000231 new_size = Max(min_new_size, static_cast<size_t>(kMaximumSegmentSize));
Steve Blocka7e24c12009-10-30 11:49:00 +0000232 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000233 if (new_size > INT_MAX) {
234 V8::FatalProcessOutOfMemory("Zone");
235 return NULL;
236 }
237 Segment* segment = NewSegment(static_cast<int>(new_size));
Steve Blocka7e24c12009-10-30 11:49:00 +0000238 if (segment == NULL) {
239 V8::FatalProcessOutOfMemory("Zone");
240 return NULL;
241 }
242
243 // Recompute 'top' and 'limit' based on the new segment.
244 Address result = RoundUp(segment->start(), kAlignment);
245 position_ = result + size;
Ben Murdoch589d6972011-11-30 16:04:58 +0000246 // Check for address overflow.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000247 // (Should not happen since the segment is guaranteed to accomodate
248 // size bytes + header and alignment padding)
249 if (reinterpret_cast<uintptr_t>(position_)
250 < reinterpret_cast<uintptr_t>(result)) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000251 V8::FatalProcessOutOfMemory("Zone");
252 return NULL;
253 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000254 limit_ = segment->end();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000255 DCHECK(position_ <= limit_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000256 return result;
257}
258
259
260} } // namespace v8::internal