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_ZONE_H_ | 5 #ifndef V8_ZONE_ZONE_H_ |
6 #define V8_ZONE_ZONE_H_ | 6 #define V8_ZONE_ZONE_H_ |
7 | 7 |
8 #include <limits> | 8 #include <limits> |
9 | 9 |
10 #include "src/base/hashmap.h" | 10 #include "src/base/hashmap.h" |
(...skipping 26 matching lines...) Expand all Loading... |
37 // Allocate 'size' bytes of memory in the Zone; expands the Zone by | 37 // Allocate 'size' bytes of memory in the Zone; expands the Zone by |
38 // allocating new segments of memory on demand using malloc(). | 38 // allocating new segments of memory on demand using malloc(). |
39 void* New(size_t size); | 39 void* New(size_t size); |
40 | 40 |
41 template <typename T> | 41 template <typename T> |
42 T* NewArray(size_t length) { | 42 T* NewArray(size_t length) { |
43 DCHECK_LT(length, std::numeric_limits<size_t>::max() / sizeof(T)); | 43 DCHECK_LT(length, std::numeric_limits<size_t>::max() / sizeof(T)); |
44 return static_cast<T*>(New(length * sizeof(T))); | 44 return static_cast<T*>(New(length * sizeof(T))); |
45 } | 45 } |
46 | 46 |
47 // Deletes all objects and free all memory allocated in the Zone. Keeps one | |
48 // small (size <= kMaximumKeptSegmentSize) segment around if it finds one. | |
49 void DeleteAll(); | |
50 | |
51 // Deletes the last small segment kept around by DeleteAll(). You | |
52 // may no longer allocate in the Zone after a call to this method. | |
53 void DeleteKeptSegment(); | |
54 | |
55 // Returns true if more memory has been allocated in zones than | 47 // Returns true if more memory has been allocated in zones than |
56 // the limit allows. | 48 // the limit allows. |
57 bool excess_allocation() const { | 49 bool excess_allocation() const { |
58 return segment_bytes_allocated_ > kExcessLimit; | 50 return segment_bytes_allocated_ > kExcessLimit; |
59 } | 51 } |
60 | 52 |
61 size_t allocation_size() const { return allocation_size_; } | 53 size_t allocation_size() const { return allocation_size_; } |
62 | 54 |
63 AccountingAllocator* allocator() const { return allocator_; } | 55 AccountingAllocator* allocator() const { return allocator_; } |
64 | 56 |
| 57 // Deletes all objects and free all memory allocated in the Zone. |
| 58 void DeleteAll(); |
| 59 |
65 private: | 60 private: |
66 // All pointers returned from New() have this alignment. In addition, if the | 61 // All pointers returned from New() have this alignment. In addition, if the |
67 // object being allocated has a size that is divisible by 8 then its alignment | 62 // object being allocated has a size that is divisible by 8 then its alignment |
68 // will be 8. ASan requires 8-byte alignment. | 63 // will be 8. ASan requires 8-byte alignment. |
69 #ifdef V8_USE_ADDRESS_SANITIZER | 64 #ifdef V8_USE_ADDRESS_SANITIZER |
70 static const size_t kAlignment = 8; | 65 static const size_t kAlignment = 8; |
71 STATIC_ASSERT(kPointerSize <= 8); | 66 STATIC_ASSERT(kPointerSize <= 8); |
72 #else | 67 #else |
73 static const size_t kAlignment = kPointerSize; | 68 static const size_t kAlignment = kPointerSize; |
74 #endif | 69 #endif |
75 | 70 |
76 // Never allocate segments smaller than this size in bytes. | 71 // Never allocate segments smaller than this size in bytes. |
77 static const size_t kMinimumSegmentSize = 8 * KB; | 72 static const size_t kMinimumSegmentSize = 8 * KB; |
78 | 73 |
79 // Never allocate segments larger than this size in bytes. | 74 // Never allocate segments larger than this size in bytes. |
80 static const size_t kMaximumSegmentSize = 1 * MB; | 75 static const size_t kMaximumSegmentSize = 1 * MB; |
81 | 76 |
82 // Never keep segments larger than this size in bytes around. | |
83 static const size_t kMaximumKeptSegmentSize = 64 * KB; | |
84 | |
85 // Report zone excess when allocation exceeds this limit. | 77 // Report zone excess when allocation exceeds this limit. |
86 static const size_t kExcessLimit = 256 * MB; | 78 static const size_t kExcessLimit = 256 * MB; |
87 | 79 |
88 // The number of bytes allocated in this zone so far. | 80 // The number of bytes allocated in this zone so far. |
89 size_t allocation_size_; | 81 size_t allocation_size_; |
90 | 82 |
91 // The number of bytes allocated in segments. Note that this number | 83 // The number of bytes allocated in segments. Note that this number |
92 // includes memory allocated from the OS but not yet allocated from | 84 // includes memory allocated from the OS but not yet allocated from |
93 // the zone. | 85 // the zone. |
94 size_t segment_bytes_allocated_; | 86 size_t segment_bytes_allocated_; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 // (unused) destructors for classes derived from ZoneObject, which | 118 // (unused) destructors for classes derived from ZoneObject, which |
127 // require the operator to be visible. MSVC requires the delete | 119 // require the operator to be visible. MSVC requires the delete |
128 // operator to be public. | 120 // operator to be public. |
129 | 121 |
130 // ZoneObjects should never be deleted individually; use | 122 // ZoneObjects should never be deleted individually; use |
131 // Zone::DeleteAll() to delete all zone objects in one go. | 123 // Zone::DeleteAll() to delete all zone objects in one go. |
132 void operator delete(void*, size_t) { UNREACHABLE(); } | 124 void operator delete(void*, size_t) { UNREACHABLE(); } |
133 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } | 125 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } |
134 }; | 126 }; |
135 | 127 |
136 // The ZoneScope is used to automatically call DeleteAll() on a | |
137 // Zone when the ZoneScope is destroyed (i.e. goes out of scope) | |
138 class ZoneScope final { | |
139 public: | |
140 explicit ZoneScope(Zone* zone) : zone_(zone) {} | |
141 ~ZoneScope() { zone_->DeleteAll(); } | |
142 | |
143 Zone* zone() const { return zone_; } | |
144 | |
145 private: | |
146 Zone* zone_; | |
147 }; | |
148 | |
149 // The ZoneAllocationPolicy is used to specialize generic data | 128 // The ZoneAllocationPolicy is used to specialize generic data |
150 // structures to allocate themselves and their elements in the Zone. | 129 // structures to allocate themselves and their elements in the Zone. |
151 class ZoneAllocationPolicy final { | 130 class ZoneAllocationPolicy final { |
152 public: | 131 public: |
153 explicit ZoneAllocationPolicy(Zone* zone) : zone_(zone) {} | 132 explicit ZoneAllocationPolicy(Zone* zone) : zone_(zone) {} |
154 void* New(size_t size) { return zone()->New(size); } | 133 void* New(size_t size) { return zone()->New(size); } |
155 static void Delete(void* pointer) {} | 134 static void Delete(void* pointer) {} |
156 Zone* zone() const { return zone_; } | 135 Zone* zone() const { return zone_; } |
157 | 136 |
158 private: | 137 private: |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 void operator delete(void* pointer) { UNREACHABLE(); } | 210 void operator delete(void* pointer) { UNREACHABLE(); } |
232 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } | 211 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } |
233 }; | 212 }; |
234 | 213 |
235 typedef base::TemplateHashMapImpl<ZoneAllocationPolicy> ZoneHashMap; | 214 typedef base::TemplateHashMapImpl<ZoneAllocationPolicy> ZoneHashMap; |
236 | 215 |
237 } // namespace internal | 216 } // namespace internal |
238 } // namespace v8 | 217 } // namespace v8 |
239 | 218 |
240 #endif // V8_ZONE_ZONE_H_ | 219 #endif // V8_ZONE_ZONE_H_ |
OLD | NEW |