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.
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
#ifndef V8_ZONE_H_
|
|
|
|
#define V8_ZONE_H_
|
|
|
|
|
2014-08-08 08:13:06 +00:00
|
|
|
#include <limits>
|
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/allocation.h"
|
2014-06-30 13:25:46 +00:00
|
|
|
#include "src/base/logging.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/globals.h"
|
2014-06-20 08:40:11 +00:00
|
|
|
#include "src/hashmap.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/list.h"
|
|
|
|
#include "src/splay-tree.h"
|
2011-05-06 06:50:20 +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
|
|
|
// Forward declarations.
|
2011-03-18 20:35:07 +00:00
|
|
|
class Segment;
|
2015-01-29 07:41:35 +00:00
|
|
|
|
2008-09-01 06:06:52 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// The Zone supports very fast allocation of small chunks of
|
|
|
|
// memory. The chunks cannot be deallocated individually, but instead
|
|
|
|
// the Zone supports deallocating all chunks in one fast
|
|
|
|
// operation. The Zone is used to hold temporary data structures like
|
|
|
|
// the abstract syntax tree, which is deallocated after compilation.
|
2015-01-29 07:41:35 +00:00
|
|
|
//
|
2008-07-03 15:10:15 +00:00
|
|
|
// Note: There is no need to initialize the Zone; the first time an
|
|
|
|
// allocation is attempted, a segment of memory will be requested
|
|
|
|
// through a call to malloc().
|
2015-01-29 07:41:35 +00:00
|
|
|
//
|
2008-07-03 15:10:15 +00:00
|
|
|
// Note: The implementation is inherently not thread safe. Do not use
|
|
|
|
// from multi-threaded code.
|
2015-04-20 13:08:11 +00:00
|
|
|
class Zone final {
|
2008-07-03 15:10:15 +00:00
|
|
|
public:
|
2015-01-23 15:19:34 +00:00
|
|
|
Zone();
|
2013-06-26 13:36:16 +00:00
|
|
|
~Zone();
|
2015-01-29 07:41:35 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// Allocate 'size' bytes of memory in the Zone; expands the Zone by
|
|
|
|
// allocating new segments of memory on demand using malloc().
|
2015-02-12 12:46:58 +00:00
|
|
|
void* New(size_t size);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-03-27 00:24:49 +00:00
|
|
|
template <typename T>
|
2015-02-12 12:46:58 +00:00
|
|
|
T* NewArray(size_t length) {
|
|
|
|
DCHECK_LT(length, std::numeric_limits<size_t>::max() / sizeof(T));
|
2014-08-08 08:13:06 +00:00
|
|
|
return static_cast<T*>(New(length * sizeof(T)));
|
|
|
|
}
|
2009-03-27 00:24:49 +00:00
|
|
|
|
2013-07-03 11:40:30 +00:00
|
|
|
// Deletes all objects and free all memory allocated in the Zone. Keeps one
|
|
|
|
// small (size <= kMaximumKeptSegmentSize) segment around if it finds one.
|
|
|
|
void DeleteAll();
|
|
|
|
|
|
|
|
// Deletes the last small segment kept around by DeleteAll(). You
|
|
|
|
// may no longer allocate in the Zone after a call to this method.
|
|
|
|
void DeleteKeptSegment();
|
|
|
|
|
2008-12-01 15:32:20 +00:00
|
|
|
// Returns true if more memory has been allocated in zones than
|
|
|
|
// the limit allows.
|
2015-01-29 07:41:35 +00:00
|
|
|
bool excess_allocation() const {
|
|
|
|
return segment_bytes_allocated_ > kExcessLimit;
|
|
|
|
}
|
2008-12-02 14:00:24 +00:00
|
|
|
|
2015-02-12 12:46:58 +00:00
|
|
|
size_t allocation_size() const { return allocation_size_; }
|
2011-07-15 16:57:35 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
private:
|
2011-11-11 11:23:39 +00:00
|
|
|
// All pointers returned from New() have this alignment. In addition, if the
|
|
|
|
// object being allocated has a size that is divisible by 8 then its alignment
|
2014-03-26 10:01:53 +00:00
|
|
|
// will be 8. ASan requires 8-byte alignment.
|
2014-03-26 16:18:28 +00:00
|
|
|
#ifdef V8_USE_ADDRESS_SANITIZER
|
2015-02-12 12:46:58 +00:00
|
|
|
static const size_t kAlignment = 8;
|
2014-03-26 10:01:53 +00:00
|
|
|
STATIC_ASSERT(kPointerSize <= 8);
|
|
|
|
#else
|
2015-02-12 12:46:58 +00:00
|
|
|
static const size_t kAlignment = kPointerSize;
|
2014-03-26 10:01:53 +00:00
|
|
|
#endif
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Never allocate segments smaller than this size in bytes.
|
2015-02-12 12:46:58 +00:00
|
|
|
static const size_t kMinimumSegmentSize = 8 * KB;
|
2008-12-05 17:53:03 +00:00
|
|
|
|
2008-12-05 17:37:12 +00:00
|
|
|
// Never allocate segments larger than this size in bytes.
|
2015-02-12 12:46:58 +00:00
|
|
|
static const size_t kMaximumSegmentSize = 1 * MB;
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2013-07-03 11:40:30 +00:00
|
|
|
// Never keep segments larger than this size in bytes around.
|
2015-02-12 12:46:58 +00:00
|
|
|
static const size_t kMaximumKeptSegmentSize = 64 * KB;
|
2013-07-03 11:40:30 +00:00
|
|
|
|
2008-12-01 15:32:20 +00:00
|
|
|
// Report zone excess when allocation exceeds this limit.
|
2015-02-12 12:46:58 +00:00
|
|
|
static const size_t kExcessLimit = 256 * MB;
|
2008-12-01 15:32:20 +00:00
|
|
|
|
2013-06-19 07:48:41 +00:00
|
|
|
// The number of bytes allocated in this zone so far.
|
2015-02-12 12:46:58 +00:00
|
|
|
size_t allocation_size_;
|
2013-06-19 07:48:41 +00:00
|
|
|
|
2008-12-01 15:32:20 +00:00
|
|
|
// The number of bytes allocated in segments. Note that this number
|
|
|
|
// includes memory allocated from the OS but not yet allocated from
|
|
|
|
// the zone.
|
2015-02-12 12:46:58 +00:00
|
|
|
size_t segment_bytes_allocated_;
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Expand the Zone to hold at least 'size' more bytes and allocate
|
|
|
|
// the bytes. Returns the address of the newly allocated chunk of
|
|
|
|
// memory in the Zone. Should only be called if there isn't enough
|
|
|
|
// room in the Zone already.
|
2015-02-12 12:46:58 +00:00
|
|
|
Address NewExpand(size_t size);
|
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
|
|
|
inline Segment* NewSegment(size_t size);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
// Deletes the given segment. Does not touch the segment chain.
|
2015-02-12 12:46:58 +00:00
|
|
|
inline void DeleteSegment(Segment* segment, size_t size);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// The free region in the current (front) segment is represented as
|
|
|
|
// the half-open interval [position, limit). The 'position' variable
|
|
|
|
// is guaranteed to be aligned as dictated by kAlignment.
|
2011-03-18 20:35:07 +00:00
|
|
|
Address position_;
|
|
|
|
Address limit_;
|
|
|
|
|
|
|
|
Segment* segment_head_;
|
2008-07-03 15:10:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// ZoneObject is an abstraction that helps define classes of objects
|
|
|
|
// allocated in the Zone. Use it as a base class; see ast.h.
|
|
|
|
class ZoneObject {
|
|
|
|
public:
|
|
|
|
// Allocate a new ZoneObject of 'size' bytes in the Zone.
|
2015-02-12 12:46:58 +00:00
|
|
|
void* operator new(size_t size, Zone* zone) { return zone->New(size); }
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Ideally, the delete operator should be private instead of
|
2009-01-15 19:08:34 +00:00
|
|
|
// public, but unfortunately the compiler sometimes synthesizes
|
2008-07-03 15:10:15 +00:00
|
|
|
// (unused) destructors for classes derived from ZoneObject, which
|
|
|
|
// require the operator to be visible. MSVC requires the delete
|
|
|
|
// operator to be public.
|
|
|
|
|
|
|
|
// ZoneObjects should never be deleted individually; use
|
|
|
|
// Zone::DeleteAll() to delete all zone objects in one go.
|
|
|
|
void operator delete(void*, size_t) { UNREACHABLE(); }
|
2011-08-29 09:14:59 +00:00
|
|
|
void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); }
|
2008-07-03 15:10:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-07-03 11:40:30 +00:00
|
|
|
// The ZoneScope is used to automatically call DeleteAll() on a
|
|
|
|
// Zone when the ZoneScope is destroyed (i.e. goes out of scope)
|
2015-04-20 13:08:11 +00:00
|
|
|
class ZoneScope final {
|
2013-07-03 11:40:30 +00:00
|
|
|
public:
|
|
|
|
explicit ZoneScope(Zone* zone) : zone_(zone) { }
|
|
|
|
~ZoneScope() { zone_->DeleteAll(); }
|
|
|
|
|
2015-01-29 07:41:35 +00:00
|
|
|
Zone* zone() const { return zone_; }
|
2013-07-03 11:40:30 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
Zone* zone_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-06-04 14:42:58 +00:00
|
|
|
// The ZoneAllocationPolicy is used to specialize generic data
|
|
|
|
// structures to allocate themselves and their elements in the Zone.
|
2015-04-20 13:08:11 +00:00
|
|
|
class ZoneAllocationPolicy final {
|
2008-07-03 15:10:15 +00:00
|
|
|
public:
|
2012-06-11 12:42:31 +00:00
|
|
|
explicit ZoneAllocationPolicy(Zone* zone) : zone_(zone) { }
|
2015-02-12 12:46:58 +00:00
|
|
|
void* New(size_t size) { return zone()->New(size); }
|
2015-01-29 07:41:35 +00:00
|
|
|
static void Delete(void* pointer) {}
|
|
|
|
Zone* zone() const { return zone_; }
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2012-06-04 14:42:58 +00:00
|
|
|
private:
|
|
|
|
Zone* zone_;
|
2008-07-03 15:10:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// ZoneLists are growable lists with constant-time access to the
|
|
|
|
// elements. The list itself and all its elements are allocated in the
|
|
|
|
// Zone. ZoneLists cannot be deleted individually; you can delete all
|
|
|
|
// objects in the Zone by calling Zone::DeleteAll().
|
2015-01-29 07:41:35 +00:00
|
|
|
template <typename T>
|
2015-04-20 13:08:11 +00:00
|
|
|
class ZoneList final : public List<T, ZoneAllocationPolicy> {
|
2008-07-03 15:10:15 +00:00
|
|
|
public:
|
|
|
|
// Construct a new ZoneList with the given capacity; the length is
|
|
|
|
// always zero. The capacity must be non-negative.
|
2012-06-11 12:42:31 +00:00
|
|
|
ZoneList(int capacity, Zone* zone)
|
2012-06-04 14:42:58 +00:00
|
|
|
: List<T, ZoneAllocationPolicy>(capacity, ZoneAllocationPolicy(zone)) { }
|
|
|
|
|
2015-02-12 12:46:58 +00:00
|
|
|
void* operator new(size_t size, Zone* zone) { return zone->New(size); }
|
2010-12-07 11:31:57 +00:00
|
|
|
|
|
|
|
// Construct a new ZoneList by copying the elements of the given ZoneList.
|
2012-06-11 12:42:31 +00:00
|
|
|
ZoneList(const ZoneList<T>& other, Zone* zone)
|
2012-06-04 14:42:58 +00:00
|
|
|
: List<T, ZoneAllocationPolicy>(other.length(),
|
|
|
|
ZoneAllocationPolicy(zone)) {
|
2013-08-07 11:24:14 +00:00
|
|
|
AddAll(other, zone);
|
2012-06-04 14:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We add some convenience wrappers so that we can pass in a Zone
|
|
|
|
// instead of a (less convenient) ZoneAllocationPolicy.
|
2015-01-29 07:41:35 +00:00
|
|
|
void Add(const T& element, Zone* zone) {
|
2012-06-04 14:42:58 +00:00
|
|
|
List<T, ZoneAllocationPolicy>::Add(element, ZoneAllocationPolicy(zone));
|
|
|
|
}
|
2015-01-29 07:41:35 +00:00
|
|
|
void AddAll(const List<T, ZoneAllocationPolicy>& other, Zone* zone) {
|
2012-06-04 14:42:58 +00:00
|
|
|
List<T, ZoneAllocationPolicy>::AddAll(other, ZoneAllocationPolicy(zone));
|
|
|
|
}
|
2015-01-29 07:41:35 +00:00
|
|
|
void AddAll(const Vector<T>& other, Zone* zone) {
|
2012-06-04 14:42:58 +00:00
|
|
|
List<T, ZoneAllocationPolicy>::AddAll(other, ZoneAllocationPolicy(zone));
|
|
|
|
}
|
2015-01-29 07:41:35 +00:00
|
|
|
void InsertAt(int index, const T& element, Zone* zone) {
|
2012-06-04 14:42:58 +00:00
|
|
|
List<T, ZoneAllocationPolicy>::InsertAt(index, element,
|
|
|
|
ZoneAllocationPolicy(zone));
|
|
|
|
}
|
2015-01-29 07:41:35 +00:00
|
|
|
Vector<T> AddBlock(T value, int count, Zone* zone) {
|
2012-06-04 14:42:58 +00:00
|
|
|
return List<T, ZoneAllocationPolicy>::AddBlock(value, count,
|
|
|
|
ZoneAllocationPolicy(zone));
|
|
|
|
}
|
2015-01-29 07:41:35 +00:00
|
|
|
void Allocate(int length, Zone* zone) {
|
2012-06-04 14:42:58 +00:00
|
|
|
List<T, ZoneAllocationPolicy>::Allocate(length, ZoneAllocationPolicy(zone));
|
|
|
|
}
|
2015-01-29 07:41:35 +00:00
|
|
|
void Initialize(int capacity, Zone* zone) {
|
2012-06-04 14:42:58 +00:00
|
|
|
List<T, ZoneAllocationPolicy>::Initialize(capacity,
|
|
|
|
ZoneAllocationPolicy(zone));
|
2010-12-07 11:31:57 +00:00
|
|
|
}
|
2011-08-29 09:14:59 +00:00
|
|
|
|
|
|
|
void operator delete(void* pointer) { UNREACHABLE(); }
|
|
|
|
void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); }
|
2008-07-03 15:10:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-07-29 08:10:19 +00:00
|
|
|
// A zone splay tree. The config type parameter encapsulates the
|
2010-03-02 10:03:38 +00:00
|
|
|
// different configurations of a concrete splay tree (see splay-tree.h).
|
|
|
|
// The tree itself and all its elements are allocated in the Zone.
|
2009-07-29 08:10:19 +00:00
|
|
|
template <typename Config>
|
2015-04-20 13:08:11 +00:00
|
|
|
class ZoneSplayTree final : public SplayTree<Config, ZoneAllocationPolicy> {
|
2009-07-29 08:10:19 +00:00
|
|
|
public:
|
2012-06-11 12:42:31 +00:00
|
|
|
explicit ZoneSplayTree(Zone* zone)
|
|
|
|
: SplayTree<Config, ZoneAllocationPolicy>(ZoneAllocationPolicy(zone)) {}
|
2015-01-29 07:41:35 +00:00
|
|
|
~ZoneSplayTree() {
|
|
|
|
// Reset the root to avoid unneeded iteration over all tree nodes
|
|
|
|
// in the destructor. For a zone-allocated tree, nodes will be
|
|
|
|
// freed by the Zone.
|
|
|
|
SplayTree<Config, ZoneAllocationPolicy>::ResetRoot();
|
|
|
|
}
|
2013-08-06 12:57:23 +00:00
|
|
|
|
2015-02-12 12:46:58 +00:00
|
|
|
void* operator new(size_t size, Zone* zone) { return zone->New(size); }
|
2013-08-07 08:25:44 +00:00
|
|
|
|
|
|
|
void operator delete(void* pointer) { UNREACHABLE(); }
|
|
|
|
void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); }
|
2009-07-29 08:10:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-06-04 14:42:58 +00:00
|
|
|
typedef TemplateHashMapImpl<ZoneAllocationPolicy> ZoneHashMap;
|
2012-02-23 09:12:57 +00:00
|
|
|
|
2015-01-29 07:41:35 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
#endif // V8_ZONE_H_
|