v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
zone-allocator.h
Go to the documentation of this file.
1// Copyright 2014 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_ZONE_ZONE_ALLOCATOR_H_
6#define V8_ZONE_ZONE_ALLOCATOR_H_
7
8#include <limits>
9
10#include "src/zone/zone.h"
11
12namespace v8 {
13namespace internal {
14
15template <typename T>
17 public:
18 using value_type = T;
19
20#ifdef V8_OS_WIN
21 // The exported class ParallelMove derives from ZoneVector, which derives
22 // from std::vector. On Windows, the semantics of dllexport mean that
23 // a class's superclasses that are not explicitly exported themselves get
24 // implicitly exported together with the subclass, and exporting a class
25 // exports all its functions -- including the std::vector() constructors
26 // that don't take an explicit allocator argument, which in turn reference
27 // the vector allocator's default constructor. So this constructor needs
28 // to exist for linking purposes, even if it's never called.
29 // Other fixes would be to disallow subclasses of ZoneVector (etc) to be
30 // exported, or using composition instead of inheritance for either
31 // ZoneVector and friends or for ParallelMove.
32 ZoneAllocator() : ZoneAllocator(nullptr) { UNREACHABLE(); }
33#endif
35 // If we are going to allocate compressed pointers in the zone it must
36 // support compression.
39 }
40 template <typename U>
42 : ZoneAllocator<T>(other.zone()) {
43 // If we are going to allocate compressed pointers in the zone it must
44 // support compression.
47 }
48
49 T* allocate(size_t length) { return zone_->AllocateArray<T>(length); }
50 void deallocate(T* p, size_t length) { zone_->DeleteArray<T>(p, length); }
51
52 bool operator==(ZoneAllocator const& other) const {
53 return zone_ == other.zone_;
54 }
55 bool operator!=(ZoneAllocator const& other) const {
56 return zone_ != other.zone_;
57 }
58
59 Zone* zone() const { return zone_; }
60
61 private:
63};
64
65// A recycling zone allocator maintains a free list of deallocated chunks
66// to reuse on subsequent allocations. The free list management is purposely
67// very simple and works best for data-structures which regularly allocate and
68// free blocks of similar sized memory (such as std::deque).
69template <typename T>
71 public:
73 : ZoneAllocator<T>(zone), free_list_(nullptr) {}
74 template <typename U>
78
79 T* allocate(size_t n) {
80 // Only check top block in free list, since this will be equal to or larger
81 // than the other blocks in the free list.
82 if (free_list_ && free_list_->size >= n) {
83 T* return_val = reinterpret_cast<T*>(free_list_);
85 return return_val;
86 }
88 }
89
90 void deallocate(T* p, size_t n) {
91 if ((sizeof(T) * n < sizeof(FreeBlock))) return;
92
93 // Only add block to free_list if it is equal or larger than previous block
94 // so that allocation stays O(1) only having to look at the top block.
95 if (!free_list_ || free_list_->size <= n) {
96 // Store the free-list within the block being deallocated.
97 DCHECK((sizeof(T) * n >= sizeof(FreeBlock)));
98 FreeBlock* new_free_block = reinterpret_cast<FreeBlock*>(p);
99
100 new_free_block->size = n;
101 new_free_block->next = free_list_;
102 free_list_ = new_free_block;
103 }
104 }
105
106 private:
107 struct FreeBlock {
109 size_t size;
110 };
111
113};
114
117
118} // namespace internal
119} // namespace v8
120
121#endif // V8_ZONE_ZONE_ALLOCATOR_H_
#define T
RecyclingZoneAllocator(const RecyclingZoneAllocator< U > &other) V8_NOEXCEPT
T * allocate(size_t length)
bool operator!=(ZoneAllocator const &other) const
void deallocate(T *p, size_t length)
bool operator==(ZoneAllocator const &other) const
ZoneAllocator(const ZoneAllocator< U > &other) V8_NOEXCEPT
bool supports_compression() const
Definition zone.h:50
T * AllocateArray(size_t length)
Definition zone.h:127
void DeleteArray(T *pointer, size_t length)
Definition zone.h:171
int n
Definition mul-fft.cc:296
#define V8_NOEXCEPT
#define DCHECK_IMPLIES(v1, v2)
Definition logging.h:493
#define DCHECK(condition)
Definition logging.h:482