OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_ZONE_H_ | 5 #ifndef V8_ZONE_H_ |
6 #define V8_ZONE_H_ | 6 #define V8_ZONE_H_ |
7 | 7 |
8 #include <limits> | 8 #include <limits> |
9 | 9 |
10 #include "src/allocation.h" | 10 #include "src/allocation.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 // | 33 // |
34 // Note: The implementation is inherently not thread safe. Do not use | 34 // Note: The implementation is inherently not thread safe. Do not use |
35 // from multi-threaded code. | 35 // from multi-threaded code. |
36 class Zone FINAL { | 36 class Zone FINAL { |
37 public: | 37 public: |
38 Zone(); | 38 Zone(); |
39 ~Zone(); | 39 ~Zone(); |
40 | 40 |
41 // Allocate 'size' bytes of memory in the Zone; expands the Zone by | 41 // Allocate 'size' bytes of memory in the Zone; expands the Zone by |
42 // allocating new segments of memory on demand using malloc(). | 42 // allocating new segments of memory on demand using malloc(). |
43 void* New(int size); | 43 void* New(size_t size); |
44 | 44 |
45 template <typename T> | 45 template <typename T> |
46 T* NewArray(int length) { | 46 T* NewArray(size_t length) { |
47 DCHECK(std::numeric_limits<int>::max() / static_cast<int>(sizeof(T)) > | 47 DCHECK_LT(length, std::numeric_limits<size_t>::max() / sizeof(T)); |
48 length); | |
49 return static_cast<T*>(New(length * sizeof(T))); | 48 return static_cast<T*>(New(length * sizeof(T))); |
50 } | 49 } |
51 | 50 |
52 // Deletes all objects and free all memory allocated in the Zone. Keeps one | 51 // Deletes all objects and free all memory allocated in the Zone. Keeps one |
53 // small (size <= kMaximumKeptSegmentSize) segment around if it finds one. | 52 // small (size <= kMaximumKeptSegmentSize) segment around if it finds one. |
54 void DeleteAll(); | 53 void DeleteAll(); |
55 | 54 |
56 // Deletes the last small segment kept around by DeleteAll(). You | 55 // Deletes the last small segment kept around by DeleteAll(). You |
57 // may no longer allocate in the Zone after a call to this method. | 56 // may no longer allocate in the Zone after a call to this method. |
58 void DeleteKeptSegment(); | 57 void DeleteKeptSegment(); |
59 | 58 |
60 // Returns true if more memory has been allocated in zones than | 59 // Returns true if more memory has been allocated in zones than |
61 // the limit allows. | 60 // the limit allows. |
62 bool excess_allocation() const { | 61 bool excess_allocation() const { |
63 return segment_bytes_allocated_ > kExcessLimit; | 62 return segment_bytes_allocated_ > kExcessLimit; |
64 } | 63 } |
65 | 64 |
66 unsigned allocation_size() const { return allocation_size_; } | 65 size_t allocation_size() const { return allocation_size_; } |
67 | 66 |
68 private: | 67 private: |
69 // All pointers returned from New() have this alignment. In addition, if the | 68 // All pointers returned from New() have this alignment. In addition, if the |
70 // object being allocated has a size that is divisible by 8 then its alignment | 69 // object being allocated has a size that is divisible by 8 then its alignment |
71 // will be 8. ASan requires 8-byte alignment. | 70 // will be 8. ASan requires 8-byte alignment. |
72 #ifdef V8_USE_ADDRESS_SANITIZER | 71 #ifdef V8_USE_ADDRESS_SANITIZER |
73 static const int kAlignment = 8; | 72 static const size_t kAlignment = 8; |
74 STATIC_ASSERT(kPointerSize <= 8); | 73 STATIC_ASSERT(kPointerSize <= 8); |
75 #else | 74 #else |
76 static const int kAlignment = kPointerSize; | 75 static const size_t kAlignment = kPointerSize; |
77 #endif | 76 #endif |
78 | 77 |
79 // Never allocate segments smaller than this size in bytes. | 78 // Never allocate segments smaller than this size in bytes. |
80 static const int kMinimumSegmentSize = 8 * KB; | 79 static const size_t kMinimumSegmentSize = 8 * KB; |
81 | 80 |
82 // Never allocate segments larger than this size in bytes. | 81 // Never allocate segments larger than this size in bytes. |
83 static const int kMaximumSegmentSize = 1 * MB; | 82 static const size_t kMaximumSegmentSize = 1 * MB; |
84 | 83 |
85 // Never keep segments larger than this size in bytes around. | 84 // Never keep segments larger than this size in bytes around. |
86 static const int kMaximumKeptSegmentSize = 64 * KB; | 85 static const size_t kMaximumKeptSegmentSize = 64 * KB; |
87 | 86 |
88 // Report zone excess when allocation exceeds this limit. | 87 // Report zone excess when allocation exceeds this limit. |
89 static const int kExcessLimit = 256 * MB; | 88 static const size_t kExcessLimit = 256 * MB; |
90 | 89 |
91 // The number of bytes allocated in this zone so far. | 90 // The number of bytes allocated in this zone so far. |
92 unsigned allocation_size_; | 91 size_t allocation_size_; |
93 | 92 |
94 // The number of bytes allocated in segments. Note that this number | 93 // The number of bytes allocated in segments. Note that this number |
95 // includes memory allocated from the OS but not yet allocated from | 94 // includes memory allocated from the OS but not yet allocated from |
96 // the zone. | 95 // the zone. |
97 int segment_bytes_allocated_; | 96 size_t segment_bytes_allocated_; |
98 | 97 |
99 // Expand the Zone to hold at least 'size' more bytes and allocate | 98 // Expand the Zone to hold at least 'size' more bytes and allocate |
100 // the bytes. Returns the address of the newly allocated chunk of | 99 // the bytes. Returns the address of the newly allocated chunk of |
101 // memory in the Zone. Should only be called if there isn't enough | 100 // memory in the Zone. Should only be called if there isn't enough |
102 // room in the Zone already. | 101 // room in the Zone already. |
103 Address NewExpand(int size); | 102 Address NewExpand(size_t size); |
104 | 103 |
105 // Creates a new segment, sets it size, and pushes it to the front | 104 // Creates a new segment, sets it size, and pushes it to the front |
106 // of the segment chain. Returns the new segment. | 105 // of the segment chain. Returns the new segment. |
107 inline Segment* NewSegment(int size); | 106 inline Segment* NewSegment(size_t size); |
108 | 107 |
109 // Deletes the given segment. Does not touch the segment chain. | 108 // Deletes the given segment. Does not touch the segment chain. |
110 inline void DeleteSegment(Segment* segment, int size); | 109 inline void DeleteSegment(Segment* segment, size_t size); |
111 | 110 |
112 // The free region in the current (front) segment is represented as | 111 // The free region in the current (front) segment is represented as |
113 // the half-open interval [position, limit). The 'position' variable | 112 // the half-open interval [position, limit). The 'position' variable |
114 // is guaranteed to be aligned as dictated by kAlignment. | 113 // is guaranteed to be aligned as dictated by kAlignment. |
115 Address position_; | 114 Address position_; |
116 Address limit_; | 115 Address limit_; |
117 | 116 |
118 Segment* segment_head_; | 117 Segment* segment_head_; |
119 }; | 118 }; |
120 | 119 |
121 | 120 |
122 // ZoneObject is an abstraction that helps define classes of objects | 121 // ZoneObject is an abstraction that helps define classes of objects |
123 // allocated in the Zone. Use it as a base class; see ast.h. | 122 // allocated in the Zone. Use it as a base class; see ast.h. |
124 class ZoneObject { | 123 class ZoneObject { |
125 public: | 124 public: |
126 // Allocate a new ZoneObject of 'size' bytes in the Zone. | 125 // Allocate a new ZoneObject of 'size' bytes in the Zone. |
127 void* operator new(size_t size, Zone* zone) { | 126 void* operator new(size_t size, Zone* zone) { return zone->New(size); } |
128 return zone->New(static_cast<int>(size)); | |
129 } | |
130 | 127 |
131 // Ideally, the delete operator should be private instead of | 128 // Ideally, the delete operator should be private instead of |
132 // public, but unfortunately the compiler sometimes synthesizes | 129 // public, but unfortunately the compiler sometimes synthesizes |
133 // (unused) destructors for classes derived from ZoneObject, which | 130 // (unused) destructors for classes derived from ZoneObject, which |
134 // require the operator to be visible. MSVC requires the delete | 131 // require the operator to be visible. MSVC requires the delete |
135 // operator to be public. | 132 // operator to be public. |
136 | 133 |
137 // ZoneObjects should never be deleted individually; use | 134 // ZoneObjects should never be deleted individually; use |
138 // Zone::DeleteAll() to delete all zone objects in one go. | 135 // Zone::DeleteAll() to delete all zone objects in one go. |
139 void operator delete(void*, size_t) { UNREACHABLE(); } | 136 void operator delete(void*, size_t) { UNREACHABLE(); } |
(...skipping 13 matching lines...) Expand all Loading... |
153 private: | 150 private: |
154 Zone* zone_; | 151 Zone* zone_; |
155 }; | 152 }; |
156 | 153 |
157 | 154 |
158 // The ZoneAllocationPolicy is used to specialize generic data | 155 // The ZoneAllocationPolicy is used to specialize generic data |
159 // structures to allocate themselves and their elements in the Zone. | 156 // structures to allocate themselves and their elements in the Zone. |
160 class ZoneAllocationPolicy FINAL { | 157 class ZoneAllocationPolicy FINAL { |
161 public: | 158 public: |
162 explicit ZoneAllocationPolicy(Zone* zone) : zone_(zone) { } | 159 explicit ZoneAllocationPolicy(Zone* zone) : zone_(zone) { } |
163 void* New(size_t size) { return zone()->New(static_cast<int>(size)); } | 160 void* New(size_t size) { return zone()->New(size); } |
164 static void Delete(void* pointer) {} | 161 static void Delete(void* pointer) {} |
165 Zone* zone() const { return zone_; } | 162 Zone* zone() const { return zone_; } |
166 | 163 |
167 private: | 164 private: |
168 Zone* zone_; | 165 Zone* zone_; |
169 }; | 166 }; |
170 | 167 |
171 | 168 |
172 // ZoneLists are growable lists with constant-time access to the | 169 // ZoneLists are growable lists with constant-time access to the |
173 // elements. The list itself and all its elements are allocated in the | 170 // elements. The list itself and all its elements are allocated in the |
174 // Zone. ZoneLists cannot be deleted individually; you can delete all | 171 // Zone. ZoneLists cannot be deleted individually; you can delete all |
175 // objects in the Zone by calling Zone::DeleteAll(). | 172 // objects in the Zone by calling Zone::DeleteAll(). |
176 template <typename T> | 173 template <typename T> |
177 class ZoneList FINAL : public List<T, ZoneAllocationPolicy> { | 174 class ZoneList FINAL : public List<T, ZoneAllocationPolicy> { |
178 public: | 175 public: |
179 // Construct a new ZoneList with the given capacity; the length is | 176 // Construct a new ZoneList with the given capacity; the length is |
180 // always zero. The capacity must be non-negative. | 177 // always zero. The capacity must be non-negative. |
181 ZoneList(int capacity, Zone* zone) | 178 ZoneList(int capacity, Zone* zone) |
182 : List<T, ZoneAllocationPolicy>(capacity, ZoneAllocationPolicy(zone)) { } | 179 : List<T, ZoneAllocationPolicy>(capacity, ZoneAllocationPolicy(zone)) { } |
183 | 180 |
184 void* operator new(size_t size, Zone* zone) { | 181 void* operator new(size_t size, Zone* zone) { return zone->New(size); } |
185 return zone->New(static_cast<int>(size)); | |
186 } | |
187 | 182 |
188 // Construct a new ZoneList by copying the elements of the given ZoneList. | 183 // Construct a new ZoneList by copying the elements of the given ZoneList. |
189 ZoneList(const ZoneList<T>& other, Zone* zone) | 184 ZoneList(const ZoneList<T>& other, Zone* zone) |
190 : List<T, ZoneAllocationPolicy>(other.length(), | 185 : List<T, ZoneAllocationPolicy>(other.length(), |
191 ZoneAllocationPolicy(zone)) { | 186 ZoneAllocationPolicy(zone)) { |
192 AddAll(other, zone); | 187 AddAll(other, zone); |
193 } | 188 } |
194 | 189 |
195 // We add some convenience wrappers so that we can pass in a Zone | 190 // We add some convenience wrappers so that we can pass in a Zone |
196 // instead of a (less convenient) ZoneAllocationPolicy. | 191 // instead of a (less convenient) ZoneAllocationPolicy. |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 public: | 227 public: |
233 explicit ZoneSplayTree(Zone* zone) | 228 explicit ZoneSplayTree(Zone* zone) |
234 : SplayTree<Config, ZoneAllocationPolicy>(ZoneAllocationPolicy(zone)) {} | 229 : SplayTree<Config, ZoneAllocationPolicy>(ZoneAllocationPolicy(zone)) {} |
235 ~ZoneSplayTree() { | 230 ~ZoneSplayTree() { |
236 // Reset the root to avoid unneeded iteration over all tree nodes | 231 // Reset the root to avoid unneeded iteration over all tree nodes |
237 // in the destructor. For a zone-allocated tree, nodes will be | 232 // in the destructor. For a zone-allocated tree, nodes will be |
238 // freed by the Zone. | 233 // freed by the Zone. |
239 SplayTree<Config, ZoneAllocationPolicy>::ResetRoot(); | 234 SplayTree<Config, ZoneAllocationPolicy>::ResetRoot(); |
240 } | 235 } |
241 | 236 |
242 void* operator new(size_t size, Zone* zone) { | 237 void* operator new(size_t size, Zone* zone) { return zone->New(size); } |
243 return zone->New(static_cast<int>(size)); | |
244 } | |
245 | 238 |
246 void operator delete(void* pointer) { UNREACHABLE(); } | 239 void operator delete(void* pointer) { UNREACHABLE(); } |
247 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } | 240 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } |
248 }; | 241 }; |
249 | 242 |
250 | 243 |
251 typedef TemplateHashMapImpl<ZoneAllocationPolicy> ZoneHashMap; | 244 typedef TemplateHashMapImpl<ZoneAllocationPolicy> ZoneHashMap; |
252 | 245 |
253 } // namespace internal | 246 } // namespace internal |
254 } // namespace v8 | 247 } // namespace v8 |
255 | 248 |
256 #endif // V8_ZONE_H_ | 249 #endif // V8_ZONE_H_ |
OLD | NEW |