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. | 47 // Deletes all objects and free all memory allocated in the Zone. Keeps one |
| 48 // small (size <= kMaximumKeptSegmentSize) segment around if it finds one. |
48 void DeleteAll(); | 49 void DeleteAll(); |
49 | 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 |
50 // Returns true if more memory has been allocated in zones than | 55 // Returns true if more memory has been allocated in zones than |
51 // the limit allows. | 56 // the limit allows. |
52 bool excess_allocation() const { | 57 bool excess_allocation() const { |
53 return segment_bytes_allocated_ > kExcessLimit; | 58 return segment_bytes_allocated_ > kExcessLimit; |
54 } | 59 } |
55 | 60 |
56 size_t allocation_size() const { return allocation_size_; } | 61 size_t allocation_size() const { return allocation_size_; } |
57 | 62 |
58 AccountingAllocator* allocator() const { return allocator_; } | 63 AccountingAllocator* allocator() const { return allocator_; } |
59 | 64 |
60 private: | 65 private: |
61 // 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 |
62 // 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 |
63 // will be 8. ASan requires 8-byte alignment. | 68 // will be 8. ASan requires 8-byte alignment. |
64 #ifdef V8_USE_ADDRESS_SANITIZER | 69 #ifdef V8_USE_ADDRESS_SANITIZER |
65 static const size_t kAlignment = 8; | 70 static const size_t kAlignment = 8; |
66 STATIC_ASSERT(kPointerSize <= 8); | 71 STATIC_ASSERT(kPointerSize <= 8); |
67 #else | 72 #else |
68 static const size_t kAlignment = kPointerSize; | 73 static const size_t kAlignment = kPointerSize; |
69 #endif | 74 #endif |
70 | 75 |
71 // Never allocate segments smaller than this size in bytes. | 76 // Never allocate segments smaller than this size in bytes. |
72 static const size_t kMinimumSegmentSize = 8 * KB; | 77 static const size_t kMinimumSegmentSize = 8 * KB; |
73 | 78 |
74 // Never allocate segments larger than this size in bytes. | 79 // Never allocate segments larger than this size in bytes. |
75 static const size_t kMaximumSegmentSize = 1 * MB; | 80 static const size_t kMaximumSegmentSize = 1 * MB; |
76 | 81 |
| 82 // Never keep segments larger than this size in bytes around. |
| 83 static const size_t kMaximumKeptSegmentSize = 64 * KB; |
| 84 |
77 // Report zone excess when allocation exceeds this limit. | 85 // Report zone excess when allocation exceeds this limit. |
78 static const size_t kExcessLimit = 256 * MB; | 86 static const size_t kExcessLimit = 256 * MB; |
79 | 87 |
80 // The number of bytes allocated in this zone so far. | 88 // The number of bytes allocated in this zone so far. |
81 size_t allocation_size_; | 89 size_t allocation_size_; |
82 | 90 |
83 // The number of bytes allocated in segments. Note that this number | 91 // The number of bytes allocated in segments. Note that this number |
84 // includes memory allocated from the OS but not yet allocated from | 92 // includes memory allocated from the OS but not yet allocated from |
85 // the zone. | 93 // the zone. |
86 size_t segment_bytes_allocated_; | 94 size_t segment_bytes_allocated_; |
87 | 95 |
88 // Expand the Zone to hold at least 'size' more bytes and allocate | 96 // Expand the Zone to hold at least 'size' more bytes and allocate |
89 // the bytes. Returns the address of the newly allocated chunk of | 97 // the bytes. Returns the address of the newly allocated chunk of |
90 // memory in the Zone. Should only be called if there isn't enough | 98 // memory in the Zone. Should only be called if there isn't enough |
91 // room in the Zone already. | 99 // room in the Zone already. |
92 Address NewExpand(size_t size); | 100 Address NewExpand(size_t size); |
93 | 101 |
94 // Creates a new segment, sets it size, and pushes it to the front | 102 // Creates a new segment, sets it size, and pushes it to the front |
95 // of the segment chain. Returns the new segment. | 103 // of the segment chain. Returns the new segment. |
96 inline Segment* NewSegment(size_t requested_size); | 104 inline Segment* NewSegment(size_t size); |
97 | 105 |
98 // The free region in the current (front) segment is represented as | 106 // The free region in the current (front) segment is represented as |
99 // the half-open interval [position, limit). The 'position' variable | 107 // the half-open interval [position, limit). The 'position' variable |
100 // is guaranteed to be aligned as dictated by kAlignment. | 108 // is guaranteed to be aligned as dictated by kAlignment. |
101 Address position_; | 109 Address position_; |
102 Address limit_; | 110 Address limit_; |
103 | 111 |
104 AccountingAllocator* allocator_; | 112 AccountingAllocator* allocator_; |
105 | 113 |
106 Segment* segment_head_; | 114 Segment* segment_head_; |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 | 234 |
227 typedef base::PointerTemplateHashMapImpl<ZoneAllocationPolicy> ZoneHashMap; | 235 typedef base::PointerTemplateHashMapImpl<ZoneAllocationPolicy> ZoneHashMap; |
228 | 236 |
229 typedef base::CustomMatcherTemplateHashMapImpl<ZoneAllocationPolicy> | 237 typedef base::CustomMatcherTemplateHashMapImpl<ZoneAllocationPolicy> |
230 CustomMatcherZoneHashMap; | 238 CustomMatcherZoneHashMap; |
231 | 239 |
232 } // namespace internal | 240 } // namespace internal |
233 } // namespace v8 | 241 } // namespace v8 |
234 | 242 |
235 #endif // V8_ZONE_ZONE_H_ | 243 #endif // V8_ZONE_ZONE_H_ |
OLD | NEW |