5 #ifndef V8_ZONE_ZONE_ALLOCATOR_H_ 6 #define V8_ZONE_ZONE_ALLOCATOR_H_ 9 #include "src/zone/zone.h" 23 typedef ptrdiff_t difference_type;
34 explicit ZoneAllocator(
const ZoneAllocator& other)
throw()
35 : ZoneAllocator<T>(other.zone_) {}
37 ZoneAllocator(
const ZoneAllocator<U>& other)
throw()
38 : ZoneAllocator<T>(other.zone_) {}
40 friend class ZoneAllocator;
42 T* address(T& x)
const {
return &x; }
43 const T* address(
const T& x)
const {
return &x; }
45 T* allocate(
size_t n,
const void* hint =
nullptr) {
46 return static_cast<T*
>(zone_->NewArray<T>(
static_cast<int>(n)));
48 void deallocate(T* p,
size_t) {
51 size_t max_size()
const throw() {
52 return std::numeric_limits<int>::max() /
sizeof(T);
54 template <
typename U,
typename... Args>
55 void construct(U* p, Args&&... args) {
56 void* v_p =
const_cast<void*
>(
static_cast<const void*
>(p));
57 new (v_p) U(std::forward<Args>(args)...);
64 bool operator==(ZoneAllocator
const& other)
const {
65 return zone_ == other.zone_;
67 bool operator!=(ZoneAllocator
const& other)
const {
68 return zone_ != other.zone_;
71 Zone* zone() {
return zone_; }
98 explicit RecyclingZoneAllocator(
const RecyclingZoneAllocator& other)
throw()
99 : ZoneAllocator<T>(other), free_list_(
nullptr) {}
100 template <
typename U>
101 RecyclingZoneAllocator(
const RecyclingZoneAllocator<U>& other)
throw()
102 : ZoneAllocator<T>(other), free_list_(
nullptr) {}
103 template <
typename U>
104 friend class RecyclingZoneAllocator;
106 T* allocate(
size_t n,
const void* hint =
nullptr) {
109 if (free_list_ && free_list_->size >= n) {
110 T* return_val =
reinterpret_cast<T*
>(free_list_);
111 free_list_ = free_list_->next;
114 return ZoneAllocator<T>::allocate(n, hint);
118 void deallocate(T* p,
size_t n) {
119 if ((
sizeof(T) * n <
sizeof(FreeBlock)))
return;
123 if (!free_list_ || free_list_->size <= n) {
125 DCHECK((
sizeof(T) * n >=
sizeof(FreeBlock)));
126 FreeBlock* new_free_block =
reinterpret_cast<FreeBlock*
>(p);
128 new_free_block->size = n;
129 new_free_block->next = free_list_;
130 free_list_ = new_free_block;
140 FreeBlock* free_list_;
143 typedef ZoneAllocator<bool> ZoneBoolAllocator;
144 typedef ZoneAllocator<int> ZoneIntAllocator;
149 #endif // V8_ZONE_ZONE_ALLOCATOR_H_