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" |
11 #include "src/base/logging.h" | 11 #include "src/base/logging.h" |
12 #include "src/globals.h" | 12 #include "src/globals.h" |
13 #include "src/list.h" | 13 #include "src/list.h" |
14 #include "src/splay-tree.h" | 14 #include "src/splay-tree.h" |
15 #include "src/zone/accounting-allocator.h" | 15 #include "src/zone/accounting-allocator.h" |
16 | 16 |
17 namespace v8 { | 17 namespace v8 { |
18 namespace internal { | 18 namespace internal { |
19 | 19 |
20 // The Zone supports very fast allocation of small chunks of | 20 // The Zone supports very fast allocation of small chunks of |
21 // memory. The chunks cannot be deallocated individually, but instead | 21 // memory. The chunks cannot be deallocated individually, but instead |
22 // the Zone supports deallocating all chunks in one fast | 22 // the Zone supports deallocating all chunks in one fast |
23 // operation. The Zone is used to hold temporary data structures like | 23 // operation. The Zone is used to hold temporary data structures like |
24 // the abstract syntax tree, which is deallocated after compilation. | 24 // the abstract syntax tree, which is deallocated after compilation. |
25 // | 25 // |
26 // Note: There is no need to initialize the Zone; the first time an | 26 // Note: There is no need to initialize the Zone; the first time an |
27 // allocation is attempted, a segment of memory will be requested | 27 // allocation is attempted, a segment of memory will be requested |
28 // through the allocator. | 28 // through a call to malloc(). |
29 // | 29 // |
30 // Note: The implementation is inherently not thread safe. Do not use | 30 // Note: The implementation is inherently not thread safe. Do not use |
31 // from multi-threaded code. | 31 // from multi-threaded code. |
32 class V8_EXPORT_PRIVATE Zone final { | 32 class V8_EXPORT_PRIVATE Zone final { |
33 public: | 33 public: |
34 explicit Zone(AccountingAllocator* allocator); | 34 explicit Zone(AccountingAllocator* allocator); |
35 ~Zone(); | 35 ~Zone(); |
36 | 36 |
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 |
47 // Returns true if more memory has been allocated in zones than | 55 // Returns true if more memory has been allocated in zones than |
48 // the limit allows. | 56 // the limit allows. |
49 bool excess_allocation() const { | 57 bool excess_allocation() const { |
50 return segment_bytes_allocated_ > kExcessLimit; | 58 return segment_bytes_allocated_ > kExcessLimit; |
51 } | 59 } |
52 | 60 |
53 size_t allocation_size() const { return allocation_size_; } | 61 size_t allocation_size() const { return allocation_size_; } |
54 | 62 |
55 AccountingAllocator* allocator() const { return allocator_; } | 63 AccountingAllocator* allocator() const { return allocator_; } |
56 | 64 |
57 private: | 65 private: |
58 // All pointers returned from New() have this alignment. In addition, if the | 66 // All pointers returned from New() have this alignment. In addition, if the |
59 // object being allocated has a size that is divisible by 8 then its alignment | 67 // object being allocated has a size that is divisible by 8 then its alignment |
60 // will be 8. ASan requires 8-byte alignment. MIPS also requires 8-byte | 68 // will be 8. ASan requires 8-byte alignment. MIPS also requires 8-byte |
61 // alignment. | 69 // alignment. |
62 #if defined(V8_USE_ADDRESS_SANITIZER) || defined(V8_TARGET_ARCH_MIPS) | 70 #if defined(V8_USE_ADDRESS_SANITIZER) || defined(V8_TARGET_ARCH_MIPS) |
63 static const size_t kAlignment = 8; | 71 static const size_t kAlignment = 8; |
64 STATIC_ASSERT(kPointerSize <= 8); | 72 STATIC_ASSERT(kPointerSize <= 8); |
65 #else | 73 #else |
66 static const size_t kAlignment = kPointerSize; | 74 static const size_t kAlignment = kPointerSize; |
67 #endif | 75 #endif |
68 | 76 |
69 // Never allocate segments smaller than this size in bytes. | 77 // Never allocate segments smaller than this size in bytes. |
70 static const size_t kMinimumSegmentSize = 8 * KB; | 78 static const size_t kMinimumSegmentSize = 8 * KB; |
71 | 79 |
72 // Never allocate segments larger than this size in bytes. | 80 // Never allocate segments larger than this size in bytes. |
73 static const size_t kMaximumSegmentSize = 1 * MB; | 81 static const size_t kMaximumSegmentSize = 1 * MB; |
74 | 82 |
| 83 // Never keep segments larger than this size in bytes around. |
| 84 static const size_t kMaximumKeptSegmentSize = 64 * KB; |
| 85 |
75 // Report zone excess when allocation exceeds this limit. | 86 // Report zone excess when allocation exceeds this limit. |
76 static const size_t kExcessLimit = 256 * MB; | 87 static const size_t kExcessLimit = 256 * MB; |
77 | 88 |
78 // Deletes all objects and free all memory allocated in the Zone. | |
79 void DeleteAll(); | |
80 | |
81 // The number of bytes allocated in this zone so far. | 89 // The number of bytes allocated in this zone so far. |
82 size_t allocation_size_; | 90 size_t allocation_size_; |
83 | 91 |
84 // The number of bytes allocated in segments. Note that this number | 92 // The number of bytes allocated in segments. Note that this number |
85 // includes memory allocated from the OS but not yet allocated from | 93 // includes memory allocated from the OS but not yet allocated from |
86 // the zone. | 94 // the zone. |
87 size_t segment_bytes_allocated_; | 95 size_t segment_bytes_allocated_; |
88 | 96 |
89 // Expand the Zone to hold at least 'size' more bytes and allocate | 97 // Expand the Zone to hold at least 'size' more bytes and allocate |
90 // the bytes. Returns the address of the newly allocated chunk of | 98 // the bytes. Returns the address of the newly allocated chunk of |
91 // memory in the Zone. Should only be called if there isn't enough | 99 // memory in the Zone. Should only be called if there isn't enough |
92 // room in the Zone already. | 100 // room in the Zone already. |
93 Address NewExpand(size_t size); | 101 Address NewExpand(size_t size); |
94 | 102 |
95 // Creates a new segment, sets it size, and pushes it to the front | 103 // Creates a new segment, sets it size, and pushes it to the front |
96 // of the segment chain. Returns the new segment. | 104 // of the segment chain. Returns the new segment. |
97 inline Segment* NewSegment(size_t requested_size); | 105 inline Segment* NewSegment(size_t size); |
98 | 106 |
99 // The free region in the current (front) segment is represented as | 107 // The free region in the current (front) segment is represented as |
100 // the half-open interval [position, limit). The 'position' variable | 108 // the half-open interval [position, limit). The 'position' variable |
101 // is guaranteed to be aligned as dictated by kAlignment. | 109 // is guaranteed to be aligned as dictated by kAlignment. |
102 Address position_; | 110 Address position_; |
103 Address limit_; | 111 Address limit_; |
104 | 112 |
105 AccountingAllocator* allocator_; | 113 AccountingAllocator* allocator_; |
106 | 114 |
107 Segment* segment_head_; | 115 Segment* segment_head_; |
(...skipping 11 matching lines...) Expand all Loading... |
119 // (unused) destructors for classes derived from ZoneObject, which | 127 // (unused) destructors for classes derived from ZoneObject, which |
120 // require the operator to be visible. MSVC requires the delete | 128 // require the operator to be visible. MSVC requires the delete |
121 // operator to be public. | 129 // operator to be public. |
122 | 130 |
123 // ZoneObjects should never be deleted individually; use | 131 // ZoneObjects should never be deleted individually; use |
124 // Zone::DeleteAll() to delete all zone objects in one go. | 132 // Zone::DeleteAll() to delete all zone objects in one go. |
125 void operator delete(void*, size_t) { UNREACHABLE(); } | 133 void operator delete(void*, size_t) { UNREACHABLE(); } |
126 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } | 134 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } |
127 }; | 135 }; |
128 | 136 |
| 137 // The ZoneScope is used to automatically call DeleteAll() on a |
| 138 // Zone when the ZoneScope is destroyed (i.e. goes out of scope) |
| 139 class ZoneScope final { |
| 140 public: |
| 141 explicit ZoneScope(Zone* zone) : zone_(zone) {} |
| 142 ~ZoneScope() { zone_->DeleteAll(); } |
| 143 |
| 144 Zone* zone() const { return zone_; } |
| 145 |
| 146 private: |
| 147 Zone* zone_; |
| 148 }; |
| 149 |
129 // The ZoneAllocationPolicy is used to specialize generic data | 150 // The ZoneAllocationPolicy is used to specialize generic data |
130 // structures to allocate themselves and their elements in the Zone. | 151 // structures to allocate themselves and their elements in the Zone. |
131 class ZoneAllocationPolicy final { | 152 class ZoneAllocationPolicy final { |
132 public: | 153 public: |
133 explicit ZoneAllocationPolicy(Zone* zone) : zone_(zone) {} | 154 explicit ZoneAllocationPolicy(Zone* zone) : zone_(zone) {} |
134 void* New(size_t size) { return zone()->New(size); } | 155 void* New(size_t size) { return zone()->New(size); } |
135 static void Delete(void* pointer) {} | 156 static void Delete(void* pointer) {} |
136 Zone* zone() const { return zone_; } | 157 Zone* zone() const { return zone_; } |
137 | 158 |
138 private: | 159 private: |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 | 235 |
215 typedef base::PointerTemplateHashMapImpl<ZoneAllocationPolicy> ZoneHashMap; | 236 typedef base::PointerTemplateHashMapImpl<ZoneAllocationPolicy> ZoneHashMap; |
216 | 237 |
217 typedef base::CustomMatcherTemplateHashMapImpl<ZoneAllocationPolicy> | 238 typedef base::CustomMatcherTemplateHashMapImpl<ZoneAllocationPolicy> |
218 CustomMatcherZoneHashMap; | 239 CustomMatcherZoneHashMap; |
219 | 240 |
220 } // namespace internal | 241 } // namespace internal |
221 } // namespace v8 | 242 } // namespace v8 |
222 | 243 |
223 #endif // V8_ZONE_ZONE_H_ | 244 #endif // V8_ZONE_ZONE_H_ |
OLD | NEW |