2014-04-24 11:32:48 +00:00
|
|
|
// Copyright 2014 the V8 project authors. All rights reserved.
|
|
|
|
// 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_
|
|
|
|
|
2011-05-06 06:50:20 +00:00
|
|
|
#include "allocation.h"
|
2012-01-30 10:13:21 +00:00
|
|
|
#include "checks.h"
|
2012-02-23 09:12:57 +00:00
|
|
|
#include "hashmap.h"
|
2012-01-30 10:13:21 +00:00
|
|
|
#include "list.h"
|
|
|
|
#include "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
|
|
|
|
2014-03-26 16:18:28 +00:00
|
|
|
#if defined(__has_feature)
|
|
|
|
#if __has_feature(address_sanitizer)
|
|
|
|
#define V8_USE_ADDRESS_SANITIZER
|
|
|
|
#endif
|
|
|
|
#endif
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
class Segment;
|
2012-01-30 10:13:21 +00:00
|
|
|
class Isolate;
|
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.
|
|
|
|
|
|
|
|
// 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().
|
|
|
|
|
|
|
|
// Note: The implementation is inherently not thread safe. Do not use
|
|
|
|
// from multi-threaded code.
|
|
|
|
|
|
|
|
class Zone {
|
|
|
|
public:
|
2012-06-20 08:58:41 +00:00
|
|
|
explicit Zone(Isolate* isolate);
|
2013-06-26 13:36:16 +00:00
|
|
|
~Zone();
|
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().
|
2011-03-18 20:35:07 +00:00
|
|
|
inline void* New(int size);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-03-27 00:24:49 +00:00
|
|
|
template <typename T>
|
2011-03-18 20:35:07 +00:00
|
|
|
inline T* NewArray(int length);
|
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.
|
2011-03-18 20:35:07 +00:00
|
|
|
inline bool excess_allocation();
|
2008-12-01 15:32:20 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
inline void adjust_segment_bytes_allocated(int delta);
|
2008-12-02 14:00:24 +00:00
|
|
|
|
2013-06-19 07:48:41 +00:00
|
|
|
inline unsigned allocation_size() { return allocation_size_; }
|
2011-07-15 16:57:35 +00:00
|
|
|
|
2013-06-19 07:48:41 +00:00
|
|
|
inline Isolate* isolate() { return isolate_; }
|
2011-01-10 14:16:47 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
private:
|
2011-03-18 20:35:07 +00:00
|
|
|
friend class Isolate;
|
2008-12-01 15:32:20 +00:00
|
|
|
|
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
|
2014-03-26 10:01:53 +00:00
|
|
|
static const int kAlignment = 8;
|
|
|
|
STATIC_ASSERT(kPointerSize <= 8);
|
|
|
|
#else
|
2008-07-03 15:10:15 +00:00
|
|
|
static const int 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.
|
|
|
|
static const int 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.
|
|
|
|
static const int 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.
|
|
|
|
static const int kMaximumKeptSegmentSize = 64 * KB;
|
|
|
|
|
2008-12-01 15:32:20 +00:00
|
|
|
// Report zone excess when allocation exceeds this limit.
|
2013-06-27 13:10:43 +00:00
|
|
|
static const int 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.
|
|
|
|
unsigned allocation_size_;
|
|
|
|
|
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.
|
2011-03-18 20:35:07 +00:00
|
|
|
int 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.
|
2011-03-18 20:35:07 +00:00
|
|
|
Address NewExpand(int size);
|
|
|
|
|
|
|
|
// Creates a new segment, sets it size, and pushes it to the front
|
|
|
|
// of the segment chain. Returns the new segment.
|
2013-07-03 11:40:30 +00:00
|
|
|
INLINE(Segment* NewSegment(int 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.
|
2013-07-03 11:40:30 +00:00
|
|
|
INLINE(void DeleteSegment(Segment* segment, int 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_;
|
|
|
|
Isolate* isolate_;
|
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.
|
2011-05-23 22:23:50 +00:00
|
|
|
INLINE(void* operator new(size_t size, Zone* zone));
|
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)
|
|
|
|
struct ZoneScope {
|
|
|
|
public:
|
|
|
|
explicit ZoneScope(Zone* zone) : zone_(zone) { }
|
|
|
|
~ZoneScope() { zone_->DeleteAll(); }
|
|
|
|
|
|
|
|
Zone* zone() { return zone_; }
|
|
|
|
|
|
|
|
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.
|
|
|
|
struct ZoneAllocationPolicy {
|
2008-07-03 15:10:15 +00:00
|
|
|
public:
|
2012-06-11 12:42:31 +00:00
|
|
|
explicit ZoneAllocationPolicy(Zone* zone) : zone_(zone) { }
|
2012-06-04 14:42:58 +00:00
|
|
|
INLINE(void* New(size_t size));
|
|
|
|
INLINE(static void Delete(void *pointer)) { }
|
2013-08-06 12:57:23 +00:00
|
|
|
Zone* zone() { 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().
|
|
|
|
template<typename T>
|
2012-06-04 14:42:58 +00:00
|
|
|
class ZoneList: 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)) { }
|
|
|
|
|
|
|
|
INLINE(void* operator new(size_t size, Zone* zone));
|
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.
|
2012-06-11 12:42:31 +00:00
|
|
|
INLINE(void Add(const T& element, Zone* zone)) {
|
2012-06-04 14:42:58 +00:00
|
|
|
List<T, ZoneAllocationPolicy>::Add(element, ZoneAllocationPolicy(zone));
|
|
|
|
}
|
2013-08-07 11:24:14 +00:00
|
|
|
INLINE(void AddAll(const List<T, ZoneAllocationPolicy>& other, Zone* zone)) {
|
2012-06-04 14:42:58 +00:00
|
|
|
List<T, ZoneAllocationPolicy>::AddAll(other, ZoneAllocationPolicy(zone));
|
|
|
|
}
|
2012-06-11 12:42:31 +00:00
|
|
|
INLINE(void AddAll(const Vector<T>& other, Zone* zone)) {
|
2012-06-04 14:42:58 +00:00
|
|
|
List<T, ZoneAllocationPolicy>::AddAll(other, ZoneAllocationPolicy(zone));
|
|
|
|
}
|
2012-06-11 12:42:31 +00:00
|
|
|
INLINE(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));
|
|
|
|
}
|
2012-06-11 12:42:31 +00:00
|
|
|
INLINE(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));
|
|
|
|
}
|
2012-06-11 12:42:31 +00:00
|
|
|
INLINE(void Allocate(int length, Zone* zone)) {
|
2012-06-04 14:42:58 +00:00
|
|
|
List<T, ZoneAllocationPolicy>::Allocate(length, ZoneAllocationPolicy(zone));
|
|
|
|
}
|
2012-06-11 12:42:31 +00:00
|
|
|
INLINE(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>
|
2012-06-04 14:42:58 +00:00
|
|
|
class ZoneSplayTree: 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)) {}
|
2010-03-02 10:03:38 +00:00
|
|
|
~ZoneSplayTree();
|
2013-08-06 12:57:23 +00:00
|
|
|
|
|
|
|
INLINE(void* operator new(size_t size, Zone* zone));
|
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
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
} } // namespace v8::internal
|
|
|
|
|
|
|
|
#endif // V8_ZONE_H_
|