| 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 a call to malloc(). | 28 // through the allocator. |
| 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 Zone final { | 32 class 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 | 47 // Deletes all objects and free all memory allocated in the Zone. |
| 48 // small (size <= kMaximumKeptSegmentSize) segment around if it finds one. | |
| 49 void DeleteAll(); | 48 void DeleteAll(); |
| 50 | 49 |
| 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 | 50 // Returns true if more memory has been allocated in zones than |
| 56 // the limit allows. | 51 // the limit allows. |
| 57 bool excess_allocation() const { | 52 bool excess_allocation() const { |
| 58 return segment_bytes_allocated_ > kExcessLimit; | 53 return segment_bytes_allocated_ > kExcessLimit; |
| 59 } | 54 } |
| 60 | 55 |
| 61 size_t allocation_size() const { return allocation_size_; } | 56 size_t allocation_size() const { return allocation_size_; } |
| 62 | 57 |
| 63 AccountingAllocator* allocator() const { return allocator_; } | 58 AccountingAllocator* allocator() const { return allocator_; } |
| 64 | 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 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } | 224 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } |
| 233 }; | 225 }; |
| 234 | 226 |
| 235 typedef base::TemplateHashMapImpl<void*, void*, ZoneAllocationPolicy> | 227 typedef base::TemplateHashMapImpl<void*, void*, ZoneAllocationPolicy> |
| 236 ZoneHashMap; | 228 ZoneHashMap; |
| 237 | 229 |
| 238 } // namespace internal | 230 } // namespace internal |
| 239 } // namespace v8 | 231 } // namespace v8 |
| 240 | 232 |
| 241 #endif // V8_ZONE_ZONE_H_ | 233 #endif // V8_ZONE_ZONE_H_ |
| OLD | NEW |