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
3 // found in the LICENSE file.
14 // 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
17 // available as segment_head_. Segments are allocated using malloc()
18 // and de-allocated using free().
22 void Initialize(Segment* next, int size) {
27 Segment* next() const { return next_; }
28 void clear_next() { next_ = NULL; }
30 int size() const { return size_; }
31 int capacity() const { return size_ - sizeof(Segment); }
33 Address start() const { return address(sizeof(Segment)); }
34 Address end() const { return address(size_); }
37 // Computes the address of the nth byte in this segment.
38 Address address(int n) const {
39 return Address(this) + n;
47 Zone::Zone(Isolate* isolate)
48 : allocation_size_(0),
49 segment_bytes_allocated_(0),
61 ASSERT(segment_bytes_allocated_ == 0);
65 void Zone::DeleteAll() {
67 // Constant byte value used for zapping dead memory in debug mode.
68 static const unsigned char kZapDeadByte = 0xcd;
71 // Find a segment with a suitable size to keep around.
73 // Traverse the chained list of segments, zapping (in debug mode)
74 // and freeing every segment except the one we wish to keep.
75 for (Segment* current = segment_head_; current != NULL; ) {
76 Segment* next = current->next();
77 if (keep == NULL && current->size() <= kMaximumKeptSegmentSize) {
78 // Unlink the segment we wish to keep from the list.
82 int size = current->size();
84 // Un-poison first so the zapping doesn't trigger ASan complaints.
85 ASAN_UNPOISON_MEMORY_REGION(current, size);
86 // Zap the entire current segment (including the header).
87 memset(current, kZapDeadByte, size);
89 DeleteSegment(current, size);
94 // If we have found a segment we want to keep, we must recompute the
95 // variables 'position' and 'limit' to prepare for future allocate
96 // attempts. Otherwise, we must clear the position and limit to
97 // force a new segment to be allocated on demand.
99 Address start = keep->start();
100 position_ = RoundUp(start, kAlignment);
101 limit_ = keep->end();
102 // Un-poison so we can re-use the segment later.
103 ASAN_UNPOISON_MEMORY_REGION(start, keep->capacity());
105 // Zap the contents of the kept segment (but not the header).
106 memset(start, kZapDeadByte, keep->capacity());
109 position_ = limit_ = 0;
112 // Update the head segment to be the kept segment (if any).
113 segment_head_ = keep;
117 void Zone::DeleteKeptSegment() {
119 // Constant byte value used for zapping dead memory in debug mode.
120 static const unsigned char kZapDeadByte = 0xcd;
123 ASSERT(segment_head_ == NULL || segment_head_->next() == NULL);
124 if (segment_head_ != NULL) {
125 int size = segment_head_->size();
127 // Un-poison first so the zapping doesn't trigger ASan complaints.
128 ASAN_UNPOISON_MEMORY_REGION(segment_head_, size);
129 // Zap the entire kept segment (including the header).
130 memset(segment_head_, kZapDeadByte, size);
132 DeleteSegment(segment_head_, size);
133 segment_head_ = NULL;
136 ASSERT(segment_bytes_allocated_ == 0);
140 // Creates a new segment, sets it size, and pushes it to the front
141 // of the segment chain. Returns the new segment.
142 Segment* Zone::NewSegment(int size) {
143 Segment* result = reinterpret_cast<Segment*>(Malloced::New(size));
144 adjust_segment_bytes_allocated(size);
145 if (result != NULL) {
146 result->Initialize(segment_head_, size);
147 segment_head_ = result;
153 // Deletes the given segment. Does not touch the segment chain.
154 void Zone::DeleteSegment(Segment* segment, int size) {
155 adjust_segment_bytes_allocated(-size);
156 Malloced::Delete(segment);
160 Address Zone::NewExpand(int size) {
161 // Make sure the requested size is already properly aligned and that
162 // there isn't enough room in the Zone to satisfy the request.
163 ASSERT(size == RoundDown(size, kAlignment));
164 ASSERT(size > limit_ - position_);
166 // Compute the new segment size. We use a 'high water mark'
167 // strategy, where we increase the segment size every time we expand
168 // except that we employ a maximum segment size when we delete. This
169 // is to avoid excessive malloc() and free() overhead.
170 Segment* head = segment_head_;
171 const size_t old_size = (head == NULL) ? 0 : head->size();
172 static const size_t kSegmentOverhead = sizeof(Segment) + kAlignment;
173 const size_t new_size_no_overhead = size + (old_size << 1);
174 size_t new_size = kSegmentOverhead + new_size_no_overhead;
175 const size_t min_new_size = kSegmentOverhead + static_cast<size_t>(size);
176 // Guard against integer overflow.
177 if (new_size_no_overhead < static_cast<size_t>(size) ||
178 new_size < static_cast<size_t>(kSegmentOverhead)) {
179 V8::FatalProcessOutOfMemory("Zone");
182 if (new_size < static_cast<size_t>(kMinimumSegmentSize)) {
183 new_size = kMinimumSegmentSize;
184 } else if (new_size > static_cast<size_t>(kMaximumSegmentSize)) {
185 // Limit the size of new segments to avoid growing the segment size
186 // exponentially, thus putting pressure on contiguous virtual address space.
187 // All the while making sure to allocate a segment large enough to hold the
189 new_size = Max(min_new_size, static_cast<size_t>(kMaximumSegmentSize));
191 if (new_size > INT_MAX) {
192 V8::FatalProcessOutOfMemory("Zone");
195 Segment* segment = NewSegment(static_cast<int>(new_size));
196 if (segment == NULL) {
197 V8::FatalProcessOutOfMemory("Zone");
201 // Recompute 'top' and 'limit' based on the new segment.
202 Address result = RoundUp(segment->start(), kAlignment);
203 position_ = result + size;
204 // Check for address overflow.
205 // (Should not happen since the segment is guaranteed to accomodate
206 // size bytes + header and alignment padding)
207 if (reinterpret_cast<uintptr_t>(position_)
208 < reinterpret_cast<uintptr_t>(result)) {
209 V8::FatalProcessOutOfMemory("Zone");
212 limit_ = segment->end();
213 ASSERT(position_ <= limit_);
218 } } // namespace v8::internal