2014-04-24 11:44:22 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2014-04-24 11:44:22 +00:00
|
|
|
|
2015-01-29 07:41:35 +00:00
|
|
|
#include "src/zone.h"
|
2012-01-30 10:49:25 +00:00
|
|
|
|
2015-01-29 07:41:35 +00:00
|
|
|
#include <cstring>
|
|
|
|
|
2015-02-11 09:25:37 +00:00
|
|
|
#include "src/v8.h"
|
|
|
|
|
2015-01-29 07:41:35 +00:00
|
|
|
#ifdef V8_USE_ADDRESS_SANITIZER
|
|
|
|
#include <sanitizer/asan_interface.h>
|
|
|
|
#endif // V8_USE_ADDRESS_SANITIZER
|
2012-01-30 10:13:21 +00:00
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2015-01-29 07:41:35 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
#if V8_USE_ADDRESS_SANITIZER
|
|
|
|
|
2015-02-12 12:46:58 +00:00
|
|
|
const size_t kASanRedzoneBytes = 24; // Must be a multiple of 8.
|
2015-01-29 07:41:35 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define ASAN_POISON_MEMORY_REGION(start, size) \
|
|
|
|
do { \
|
|
|
|
USE(start); \
|
|
|
|
USE(size); \
|
|
|
|
} while (false)
|
|
|
|
|
|
|
|
#define ASAN_UNPOISON_MEMORY_REGION(start, size) \
|
|
|
|
do { \
|
|
|
|
USE(start); \
|
|
|
|
USE(size); \
|
|
|
|
} while (false)
|
|
|
|
|
2015-02-12 12:46:58 +00:00
|
|
|
const size_t kASanRedzoneBytes = 0;
|
2015-01-29 07:41:35 +00:00
|
|
|
|
|
|
|
#endif // V8_USE_ADDRESS_SANITIZER
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2014-04-24 11:44:22 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// Segments represent chunks of memory: They have starting address
|
|
|
|
// (encoded in the this pointer) and a size in bytes. Segments are
|
|
|
|
// chained together forming a LIFO structure with the newest segment
|
2011-03-18 20:35:07 +00:00
|
|
|
// available as segment_head_. Segments are allocated using malloc()
|
2008-07-03 15:10:15 +00:00
|
|
|
// and de-allocated using free().
|
|
|
|
|
|
|
|
class Segment {
|
|
|
|
public:
|
2015-02-12 12:46:58 +00:00
|
|
|
void Initialize(Segment* next, size_t size) {
|
2011-08-17 08:48:54 +00:00
|
|
|
next_ = next;
|
|
|
|
size_ = size;
|
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
Segment* next() const { return next_; }
|
2015-01-29 07:41:35 +00:00
|
|
|
void clear_next() { next_ = nullptr; }
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2015-02-12 12:46:58 +00:00
|
|
|
size_t size() const { return size_; }
|
|
|
|
size_t capacity() const { return size_ - sizeof(Segment); }
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
Address start() const { return address(sizeof(Segment)); }
|
|
|
|
Address end() const { return address(size_); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Computes the address of the nth byte in this segment.
|
2015-02-12 12:46:58 +00:00
|
|
|
Address address(size_t n) const { return Address(this) + n; }
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
Segment* next_;
|
2015-02-12 12:46:58 +00:00
|
|
|
size_t size_;
|
2008-07-03 15:10:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-01-23 15:19:34 +00:00
|
|
|
Zone::Zone()
|
2013-06-27 13:10:43 +00:00
|
|
|
: allocation_size_(0),
|
2011-08-17 08:48:54 +00:00
|
|
|
segment_bytes_allocated_(0),
|
|
|
|
position_(0),
|
|
|
|
limit_(0),
|
2015-01-29 07:41:35 +00:00
|
|
|
segment_head_(nullptr) {}
|
2013-06-19 07:48:41 +00:00
|
|
|
|
2011-08-17 08:48:54 +00:00
|
|
|
|
2013-06-26 13:36:16 +00:00
|
|
|
Zone::~Zone() {
|
2013-07-03 11:40:30 +00:00
|
|
|
DeleteAll();
|
|
|
|
DeleteKeptSegment();
|
|
|
|
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(segment_bytes_allocated_ == 0);
|
2013-07-03 11:40:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-12 12:46:58 +00:00
|
|
|
void* Zone::New(size_t size) {
|
2014-08-08 08:13:06 +00:00
|
|
|
// Round up the requested size to fit the alignment.
|
|
|
|
size = RoundUp(size, kAlignment);
|
|
|
|
|
|
|
|
// If the allocation size is divisible by 8 then we return an 8-byte aligned
|
|
|
|
// address.
|
|
|
|
if (kPointerSize == 4 && kAlignment == 4) {
|
|
|
|
position_ += ((~size) & 4) & (reinterpret_cast<intptr_t>(position_) & 4);
|
|
|
|
} else {
|
|
|
|
DCHECK(kAlignment >= kPointerSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the requested size is available without expanding.
|
|
|
|
Address result = position_;
|
|
|
|
|
2015-02-12 12:46:58 +00:00
|
|
|
const size_t size_with_redzone = size + kASanRedzoneBytes;
|
|
|
|
if (limit_ < position_ + size_with_redzone) {
|
2015-01-29 07:41:35 +00:00
|
|
|
result = NewExpand(size_with_redzone);
|
2014-08-08 08:13:06 +00:00
|
|
|
} else {
|
2015-01-29 07:41:35 +00:00
|
|
|
position_ += size_with_redzone;
|
2014-08-08 08:13:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Address redzone_position = result + size;
|
|
|
|
DCHECK(redzone_position + kASanRedzoneBytes == position_);
|
|
|
|
ASAN_POISON_MEMORY_REGION(redzone_position, kASanRedzoneBytes);
|
|
|
|
|
|
|
|
// Check that the result has the proper alignment and return it.
|
|
|
|
DCHECK(IsAddressAligned(result, kAlignment, 0));
|
|
|
|
allocation_size_ += size;
|
|
|
|
return reinterpret_cast<void*>(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-03 11:40:30 +00:00
|
|
|
void Zone::DeleteAll() {
|
2013-06-26 13:36:16 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
// Constant byte value used for zapping dead memory in debug mode.
|
|
|
|
static const unsigned char kZapDeadByte = 0xcd;
|
|
|
|
#endif
|
|
|
|
|
2013-07-03 11:40:30 +00:00
|
|
|
// Find a segment with a suitable size to keep around.
|
2015-01-29 07:41:35 +00:00
|
|
|
Segment* keep = nullptr;
|
2013-07-03 11:40:30 +00:00
|
|
|
// Traverse the chained list of segments, zapping (in debug mode)
|
|
|
|
// and freeing every segment except the one we wish to keep.
|
2015-01-29 07:41:35 +00:00
|
|
|
for (Segment* current = segment_head_; current;) {
|
2013-06-26 13:36:16 +00:00
|
|
|
Segment* next = current->next();
|
2015-01-29 07:41:35 +00:00
|
|
|
if (!keep && current->size() <= kMaximumKeptSegmentSize) {
|
2013-07-03 11:40:30 +00:00
|
|
|
// Unlink the segment we wish to keep from the list.
|
2013-07-05 12:57:38 +00:00
|
|
|
keep = current;
|
|
|
|
keep->clear_next();
|
2013-07-03 11:40:30 +00:00
|
|
|
} else {
|
2015-02-12 12:46:58 +00:00
|
|
|
size_t size = current->size();
|
2013-06-26 13:36:16 +00:00
|
|
|
#ifdef DEBUG
|
2014-03-26 10:01:53 +00:00
|
|
|
// Un-poison first so the zapping doesn't trigger ASan complaints.
|
|
|
|
ASAN_UNPOISON_MEMORY_REGION(current, size);
|
2013-07-03 11:40:30 +00:00
|
|
|
// Zap the entire current segment (including the header).
|
|
|
|
memset(current, kZapDeadByte, size);
|
2013-06-26 13:36:16 +00:00
|
|
|
#endif
|
2013-07-03 11:40:30 +00:00
|
|
|
DeleteSegment(current, size);
|
|
|
|
}
|
2013-06-26 13:36:16 +00:00
|
|
|
current = next;
|
|
|
|
}
|
|
|
|
|
2013-07-03 11:40:30 +00:00
|
|
|
// If we have found a segment we want to keep, we must recompute the
|
|
|
|
// variables 'position' and 'limit' to prepare for future allocate
|
|
|
|
// attempts. Otherwise, we must clear the position and limit to
|
|
|
|
// force a new segment to be allocated on demand.
|
2015-01-29 07:41:35 +00:00
|
|
|
if (keep) {
|
2013-07-03 11:40:30 +00:00
|
|
|
Address start = keep->start();
|
|
|
|
position_ = RoundUp(start, kAlignment);
|
|
|
|
limit_ = keep->end();
|
2014-03-26 10:01:53 +00:00
|
|
|
// Un-poison so we can re-use the segment later.
|
|
|
|
ASAN_UNPOISON_MEMORY_REGION(start, keep->capacity());
|
2013-07-03 11:40:30 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
// Zap the contents of the kept segment (but not the header).
|
|
|
|
memset(start, kZapDeadByte, keep->capacity());
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
position_ = limit_ = 0;
|
|
|
|
}
|
|
|
|
|
2014-10-21 12:38:46 +00:00
|
|
|
allocation_size_ = 0;
|
2013-07-03 11:40:30 +00:00
|
|
|
// Update the head segment to be the kept segment (if any).
|
|
|
|
segment_head_ = keep;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Zone::DeleteKeptSegment() {
|
|
|
|
#ifdef DEBUG
|
|
|
|
// Constant byte value used for zapping dead memory in debug mode.
|
|
|
|
static const unsigned char kZapDeadByte = 0xcd;
|
|
|
|
#endif
|
|
|
|
|
2015-01-29 07:41:35 +00:00
|
|
|
DCHECK(segment_head_ == nullptr || segment_head_->next() == nullptr);
|
|
|
|
if (segment_head_ != nullptr) {
|
2015-02-12 12:46:58 +00:00
|
|
|
size_t size = segment_head_->size();
|
2013-07-03 11:40:30 +00:00
|
|
|
#ifdef DEBUG
|
2014-03-26 10:01:53 +00:00
|
|
|
// Un-poison first so the zapping doesn't trigger ASan complaints.
|
|
|
|
ASAN_UNPOISON_MEMORY_REGION(segment_head_, size);
|
2013-07-03 11:40:30 +00:00
|
|
|
// Zap the entire kept segment (including the header).
|
|
|
|
memset(segment_head_, kZapDeadByte, size);
|
|
|
|
#endif
|
|
|
|
DeleteSegment(segment_head_, size);
|
2015-01-29 07:41:35 +00:00
|
|
|
segment_head_ = nullptr;
|
2013-07-03 11:40:30 +00:00
|
|
|
}
|
2013-06-26 13:36:16 +00:00
|
|
|
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(segment_bytes_allocated_ == 0);
|
2011-08-17 08:48:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
// Creates a new segment, sets it size, and pushes it to the front
|
|
|
|
// of the segment chain. Returns the new segment.
|
2015-02-12 12:46:58 +00:00
|
|
|
Segment* Zone::NewSegment(size_t size) {
|
2011-03-18 20:35:07 +00:00
|
|
|
Segment* result = reinterpret_cast<Segment*>(Malloced::New(size));
|
2015-01-29 07:41:35 +00:00
|
|
|
segment_bytes_allocated_ += size;
|
|
|
|
if (result != nullptr) {
|
2011-08-17 08:48:54 +00:00
|
|
|
result->Initialize(segment_head_, size);
|
2011-03-18 20:35:07 +00:00
|
|
|
segment_head_ = result;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Deletes the given segment. Does not touch the segment chain.
|
2015-02-12 12:46:58 +00:00
|
|
|
void Zone::DeleteSegment(Segment* segment, size_t size) {
|
2015-01-29 07:41:35 +00:00
|
|
|
segment_bytes_allocated_ -= size;
|
2011-03-18 20:35:07 +00:00
|
|
|
Malloced::Delete(segment);
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2015-02-12 12:46:58 +00:00
|
|
|
Address Zone::NewExpand(size_t size) {
|
2008-07-03 15:10:15 +00:00
|
|
|
// Make sure the requested size is already properly aligned and that
|
|
|
|
// there isn't enough room in the Zone to satisfy the request.
|
2015-02-12 12:46:58 +00:00
|
|
|
DCHECK_EQ(size, RoundDown(size, kAlignment));
|
|
|
|
DCHECK_LT(limit_, position_ + size);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Compute the new segment size. We use a 'high water mark'
|
|
|
|
// strategy, where we increase the segment size every time we expand
|
|
|
|
// except that we employ a maximum segment size when we delete. This
|
|
|
|
// is to avoid excessive malloc() and free() overhead.
|
2011-03-18 20:35:07 +00:00
|
|
|
Segment* head = segment_head_;
|
2015-01-29 07:41:35 +00:00
|
|
|
const size_t old_size = (head == nullptr) ? 0 : head->size();
|
2014-01-13 13:00:09 +00:00
|
|
|
static const size_t kSegmentOverhead = sizeof(Segment) + kAlignment;
|
|
|
|
const size_t new_size_no_overhead = size + (old_size << 1);
|
|
|
|
size_t new_size = kSegmentOverhead + new_size_no_overhead;
|
2015-02-12 12:46:58 +00:00
|
|
|
const size_t min_new_size = kSegmentOverhead + size;
|
2011-09-09 12:41:58 +00:00
|
|
|
// Guard against integer overflow.
|
2015-02-12 12:46:58 +00:00
|
|
|
if (new_size_no_overhead < size || new_size < kSegmentOverhead) {
|
2015-02-11 09:25:37 +00:00
|
|
|
V8::FatalProcessOutOfMemory("Zone");
|
2015-01-29 07:41:35 +00:00
|
|
|
return nullptr;
|
2011-09-09 12:41:58 +00:00
|
|
|
}
|
2015-02-12 12:46:58 +00:00
|
|
|
if (new_size < kMinimumSegmentSize) {
|
2008-12-05 17:37:12 +00:00
|
|
|
new_size = kMinimumSegmentSize;
|
2015-02-12 12:46:58 +00:00
|
|
|
} else if (new_size > kMaximumSegmentSize) {
|
2008-12-05 17:37:12 +00:00
|
|
|
// Limit the size of new segments to avoid growing the segment size
|
2008-12-05 21:51:46 +00:00
|
|
|
// exponentially, thus putting pressure on contiguous virtual address space.
|
|
|
|
// All the while making sure to allocate a segment large enough to hold the
|
|
|
|
// requested size.
|
2015-02-12 12:46:58 +00:00
|
|
|
new_size = Max(min_new_size, kMaximumSegmentSize);
|
2008-12-05 17:37:12 +00:00
|
|
|
}
|
2014-01-13 13:00:09 +00:00
|
|
|
if (new_size > INT_MAX) {
|
2015-02-11 09:25:37 +00:00
|
|
|
V8::FatalProcessOutOfMemory("Zone");
|
2015-01-29 07:41:35 +00:00
|
|
|
return nullptr;
|
2014-01-13 13:00:09 +00:00
|
|
|
}
|
2015-02-12 12:46:58 +00:00
|
|
|
Segment* segment = NewSegment(new_size);
|
2015-01-29 07:41:35 +00:00
|
|
|
if (segment == nullptr) {
|
2015-02-11 09:25:37 +00:00
|
|
|
V8::FatalProcessOutOfMemory("Zone");
|
2015-01-29 07:41:35 +00:00
|
|
|
return nullptr;
|
2009-07-22 11:29:38 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Recompute 'top' and 'limit' based on the new segment.
|
|
|
|
Address result = RoundUp(segment->start(), kAlignment);
|
|
|
|
position_ = result + size;
|
2011-09-09 12:41:58 +00:00
|
|
|
// Check for address overflow.
|
2014-01-13 13:00:09 +00:00
|
|
|
// (Should not happen since the segment is guaranteed to accomodate
|
|
|
|
// size bytes + header and alignment padding)
|
2015-01-29 07:41:35 +00:00
|
|
|
DCHECK_GE(reinterpret_cast<uintptr_t>(position_),
|
|
|
|
reinterpret_cast<uintptr_t>(result));
|
2008-07-03 15:10:15 +00:00
|
|
|
limit_ = segment->end();
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(position_ <= limit_);
|
2008-07-03 15:10:15 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-01-29 07:41:35 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|