2012-01-30 10:49:25 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
2008-07-03 15:10:15 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
2012-01-30 10:49:25 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
#include "v8.h"
|
|
|
|
#include "zone-inl.h"
|
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
|
|
|
|
|
|
|
|
|
|
|
// 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:
|
2011-08-17 08:48:54 +00:00
|
|
|
void Initialize(Segment* next, int size) {
|
|
|
|
next_ = next;
|
|
|
|
size_ = size;
|
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
Segment* next() const { return next_; }
|
|
|
|
void clear_next() { next_ = NULL; }
|
|
|
|
|
|
|
|
int size() const { return size_; }
|
|
|
|
int capacity() const { return size_ - sizeof(Segment); }
|
|
|
|
|
|
|
|
Address start() const { return address(sizeof(Segment)); }
|
|
|
|
Address end() const { return address(size_); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Computes the address of the nth byte in this segment.
|
|
|
|
Address address(int n) const {
|
|
|
|
return Address(this) + n;
|
|
|
|
}
|
|
|
|
|
|
|
|
Segment* next_;
|
|
|
|
int size_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-06-20 08:58:41 +00:00
|
|
|
Zone::Zone(Isolate* isolate)
|
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),
|
2012-06-20 08:58:41 +00:00
|
|
|
segment_head_(NULL),
|
|
|
|
isolate_(isolate) {
|
2011-08-17 08:48:54 +00:00
|
|
|
}
|
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() {
|
|
|
|
#ifdef DEBUG
|
|
|
|
// Constant byte value used for zapping dead memory in debug mode.
|
|
|
|
static const unsigned char kZapDeadByte = 0xcd;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Traverse the chained list of segments, zapping
|
|
|
|
// (in debug mode) and freeing every segment
|
|
|
|
Segment* current = segment_head_;
|
|
|
|
while (current != NULL) {
|
|
|
|
Segment* next = current->next();
|
|
|
|
int size = current->size();
|
|
|
|
#ifdef DEBUG
|
|
|
|
// Zap the entire current segment (including the header).
|
|
|
|
memset(current, kZapDeadByte, size);
|
|
|
|
#endif
|
|
|
|
DeleteSegment(current, size);
|
|
|
|
current = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We must clear the position and limit to force
|
|
|
|
// a new segment to be allocated on demand.
|
|
|
|
position_ = limit_ = 0;
|
|
|
|
|
|
|
|
// Update the head segment.
|
|
|
|
segment_head_ = NULL;
|
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.
|
|
|
|
Segment* Zone::NewSegment(int size) {
|
|
|
|
Segment* result = reinterpret_cast<Segment*>(Malloced::New(size));
|
|
|
|
adjust_segment_bytes_allocated(size);
|
|
|
|
if (result != NULL) {
|
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.
|
|
|
|
void Zone::DeleteSegment(Segment* segment, int size) {
|
|
|
|
adjust_segment_bytes_allocated(-size);
|
|
|
|
Malloced::Delete(segment);
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
Address Zone::NewExpand(int size) {
|
|
|
|
// Make sure the requested size is already properly aligned and that
|
|
|
|
// there isn't enough room in the Zone to satisfy the request.
|
|
|
|
ASSERT(size == RoundDown(size, kAlignment));
|
2011-09-09 12:41:58 +00:00
|
|
|
ASSERT(size > limit_ - position_);
|
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_;
|
2008-07-03 15:10:15 +00:00
|
|
|
int old_size = (head == NULL) ? 0 : head->size();
|
2008-12-05 17:37:12 +00:00
|
|
|
static const int kSegmentOverhead = sizeof(Segment) + kAlignment;
|
2011-09-09 12:41:58 +00:00
|
|
|
int new_size_no_overhead = size + (old_size << 1);
|
|
|
|
int new_size = kSegmentOverhead + new_size_no_overhead;
|
|
|
|
// Guard against integer overflow.
|
|
|
|
if (new_size_no_overhead < size || new_size < kSegmentOverhead) {
|
|
|
|
V8::FatalProcessOutOfMemory("Zone");
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-12-05 17:37:12 +00:00
|
|
|
if (new_size < kMinimumSegmentSize) {
|
|
|
|
new_size = kMinimumSegmentSize;
|
|
|
|
} else if (new_size > kMaximumSegmentSize) {
|
|
|
|
// 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.
|
|
|
|
new_size = Max(kSegmentOverhead + size, kMaximumSegmentSize);
|
2008-12-05 17:37:12 +00:00
|
|
|
}
|
2011-03-18 20:35:07 +00:00
|
|
|
Segment* segment = NewSegment(new_size);
|
2009-07-22 11:29:38 +00:00
|
|
|
if (segment == NULL) {
|
|
|
|
V8::FatalProcessOutOfMemory("Zone");
|
|
|
|
return NULL;
|
|
|
|
}
|
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.
|
|
|
|
if (position_ < result) {
|
|
|
|
V8::FatalProcessOutOfMemory("Zone");
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
limit_ = segment->end();
|
|
|
|
ASSERT(position_ <= limit_);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} } // namespace v8::internal
|