| 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. | |
| 48 void DeleteAll(); | |
| 49 | |
| 50 // Returns true if more memory has been allocated in zones than | 47 // Returns true if more memory has been allocated in zones than |
| 51 // the limit allows. | 48 // the limit allows. |
| 52 bool excess_allocation() const { | 49 bool excess_allocation() const { |
| 53 return segment_bytes_allocated_ > kExcessLimit; | 50 return segment_bytes_allocated_ > kExcessLimit; |
| 54 } | 51 } |
| 55 | 52 |
| 56 size_t allocation_size() const { return allocation_size_; } | 53 size_t allocation_size() const { return allocation_size_; } |
| 57 | 54 |
| 58 AccountingAllocator* allocator() const { return allocator_; } | 55 AccountingAllocator* allocator() const { return allocator_; } |
| 59 | 56 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 70 | 67 |
| 71 // Never allocate segments smaller than this size in bytes. | 68 // Never allocate segments smaller than this size in bytes. |
| 72 static const size_t kMinimumSegmentSize = 8 * KB; | 69 static const size_t kMinimumSegmentSize = 8 * KB; |
| 73 | 70 |
| 74 // Never allocate segments larger than this size in bytes. | 71 // Never allocate segments larger than this size in bytes. |
| 75 static const size_t kMaximumSegmentSize = 1 * MB; | 72 static const size_t kMaximumSegmentSize = 1 * MB; |
| 76 | 73 |
| 77 // Report zone excess when allocation exceeds this limit. | 74 // Report zone excess when allocation exceeds this limit. |
| 78 static const size_t kExcessLimit = 256 * MB; | 75 static const size_t kExcessLimit = 256 * MB; |
| 79 | 76 |
| 77 // Deletes all objects and free all memory allocated in the Zone. |
| 78 void DeleteAll(); |
| 79 |
| 80 // The number of bytes allocated in this zone so far. | 80 // The number of bytes allocated in this zone so far. |
| 81 size_t allocation_size_; | 81 size_t allocation_size_; |
| 82 | 82 |
| 83 // The number of bytes allocated in segments. Note that this number | 83 // The number of bytes allocated in segments. Note that this number |
| 84 // includes memory allocated from the OS but not yet allocated from | 84 // includes memory allocated from the OS but not yet allocated from |
| 85 // the zone. | 85 // the zone. |
| 86 size_t segment_bytes_allocated_; | 86 size_t segment_bytes_allocated_; |
| 87 | 87 |
| 88 // Expand the Zone to hold at least 'size' more bytes and allocate | 88 // Expand the Zone to hold at least 'size' more bytes and allocate |
| 89 // the bytes. Returns the address of the newly allocated chunk of | 89 // the bytes. Returns the address of the newly allocated chunk of |
| (...skipping 28 matching lines...) Expand all Loading... |
| 118 // (unused) destructors for classes derived from ZoneObject, which | 118 // (unused) destructors for classes derived from ZoneObject, which |
| 119 // require the operator to be visible. MSVC requires the delete | 119 // require the operator to be visible. MSVC requires the delete |
| 120 // operator to be public. | 120 // operator to be public. |
| 121 | 121 |
| 122 // ZoneObjects should never be deleted individually; use | 122 // ZoneObjects should never be deleted individually; use |
| 123 // Zone::DeleteAll() to delete all zone objects in one go. | 123 // Zone::DeleteAll() to delete all zone objects in one go. |
| 124 void operator delete(void*, size_t) { UNREACHABLE(); } | 124 void operator delete(void*, size_t) { UNREACHABLE(); } |
| 125 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } | 125 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } |
| 126 }; | 126 }; |
| 127 | 127 |
| 128 // The ZoneScope is used to automatically call DeleteAll() on a | |
| 129 // Zone when the ZoneScope is destroyed (i.e. goes out of scope) | |
| 130 class ZoneScope final { | |
| 131 public: | |
| 132 explicit ZoneScope(Zone* zone) : zone_(zone) {} | |
| 133 ~ZoneScope() { zone_->DeleteAll(); } | |
| 134 | |
| 135 Zone* zone() const { return zone_; } | |
| 136 | |
| 137 private: | |
| 138 Zone* zone_; | |
| 139 }; | |
| 140 | |
| 141 // The ZoneAllocationPolicy is used to specialize generic data | 128 // The ZoneAllocationPolicy is used to specialize generic data |
| 142 // structures to allocate themselves and their elements in the Zone. | 129 // structures to allocate themselves and their elements in the Zone. |
| 143 class ZoneAllocationPolicy final { | 130 class ZoneAllocationPolicy final { |
| 144 public: | 131 public: |
| 145 explicit ZoneAllocationPolicy(Zone* zone) : zone_(zone) {} | 132 explicit ZoneAllocationPolicy(Zone* zone) : zone_(zone) {} |
| 146 void* New(size_t size) { return zone()->New(size); } | 133 void* New(size_t size) { return zone()->New(size); } |
| 147 static void Delete(void* pointer) {} | 134 static void Delete(void* pointer) {} |
| 148 Zone* zone() const { return zone_; } | 135 Zone* zone() const { return zone_; } |
| 149 | 136 |
| 150 private: | 137 private: |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } | 211 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } |
| 225 }; | 212 }; |
| 226 | 213 |
| 227 typedef base::TemplateHashMapImpl<void*, void*, ZoneAllocationPolicy> | 214 typedef base::TemplateHashMapImpl<void*, void*, ZoneAllocationPolicy> |
| 228 ZoneHashMap; | 215 ZoneHashMap; |
| 229 | 216 |
| 230 } // namespace internal | 217 } // namespace internal |
| 231 } // namespace v8 | 218 } // namespace v8 |
| 232 | 219 |
| 233 #endif // V8_ZONE_ZONE_H_ | 220 #endif // V8_ZONE_ZONE_H_ |
| OLD | NEW |