| 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_H_ | 5 #ifndef V8_ZONE_H_ |
| 6 #define V8_ZONE_H_ | 6 #define V8_ZONE_H_ |
| 7 | 7 |
| 8 #include <limits> | 8 #include <limits> |
| 9 | 9 |
| 10 #include "src/allocation.h" | 10 #include "src/base/accounting-allocator.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/hashmap.h" | 13 #include "src/hashmap.h" |
| 14 #include "src/list.h" | 14 #include "src/list.h" |
| 15 #include "src/splay-tree.h" | 15 #include "src/splay-tree.h" |
| 16 | 16 |
| 17 namespace v8 { | 17 namespace v8 { |
| 18 namespace internal { | 18 namespace internal { |
| 19 | 19 |
| 20 // Forward declarations. | 20 // Forward declarations. |
| 21 class Segment; | 21 class Segment; |
| 22 | 22 |
| 23 | 23 |
| 24 // The Zone supports very fast allocation of small chunks of | 24 // The Zone supports very fast allocation of small chunks of |
| 25 // memory. The chunks cannot be deallocated individually, but instead | 25 // memory. The chunks cannot be deallocated individually, but instead |
| 26 // the Zone supports deallocating all chunks in one fast | 26 // the Zone supports deallocating all chunks in one fast |
| 27 // operation. The Zone is used to hold temporary data structures like | 27 // operation. The Zone is used to hold temporary data structures like |
| 28 // the abstract syntax tree, which is deallocated after compilation. | 28 // the abstract syntax tree, which is deallocated after compilation. |
| 29 // | 29 // |
| 30 // Note: There is no need to initialize the Zone; the first time an | 30 // Note: There is no need to initialize the Zone; the first time an |
| 31 // allocation is attempted, a segment of memory will be requested | 31 // allocation is attempted, a segment of memory will be requested |
| 32 // through a call to malloc(). | 32 // through a call to malloc(). |
| 33 // | 33 // |
| 34 // Note: The implementation is inherently not thread safe. Do not use | 34 // Note: The implementation is inherently not thread safe. Do not use |
| 35 // from multi-threaded code. | 35 // from multi-threaded code. |
| 36 class Zone final { | 36 class Zone final { |
| 37 public: | 37 public: |
| 38 Zone(); | 38 explicit Zone(base::AccountingAllocator* allocator); |
| 39 ~Zone(); | 39 ~Zone(); |
| 40 | 40 |
| 41 // Allocate 'size' bytes of memory in the Zone; expands the Zone by | 41 // Allocate 'size' bytes of memory in the Zone; expands the Zone by |
| 42 // allocating new segments of memory on demand using malloc(). | 42 // allocating new segments of memory on demand using malloc(). |
| 43 void* New(size_t size); | 43 void* New(size_t size); |
| 44 | 44 |
| 45 template <typename T> | 45 template <typename T> |
| 46 T* NewArray(size_t length) { | 46 T* NewArray(size_t length) { |
| 47 DCHECK_LT(length, std::numeric_limits<size_t>::max() / sizeof(T)); | 47 DCHECK_LT(length, std::numeric_limits<size_t>::max() / sizeof(T)); |
| 48 return static_cast<T*>(New(length * sizeof(T))); | 48 return static_cast<T*>(New(length * sizeof(T))); |
| 49 } | 49 } |
| 50 | 50 |
| 51 // Deletes all objects and free all memory allocated in the Zone. Keeps one | 51 // Deletes all objects and free all memory allocated in the Zone. Keeps one |
| 52 // small (size <= kMaximumKeptSegmentSize) segment around if it finds one. | 52 // small (size <= kMaximumKeptSegmentSize) segment around if it finds one. |
| 53 void DeleteAll(); | 53 void DeleteAll(); |
| 54 | 54 |
| 55 // Deletes the last small segment kept around by DeleteAll(). You | 55 // Deletes the last small segment kept around by DeleteAll(). You |
| 56 // may no longer allocate in the Zone after a call to this method. | 56 // may no longer allocate in the Zone after a call to this method. |
| 57 void DeleteKeptSegment(); | 57 void DeleteKeptSegment(); |
| 58 | 58 |
| 59 // Returns true if more memory has been allocated in zones than | 59 // Returns true if more memory has been allocated in zones than |
| 60 // the limit allows. | 60 // the limit allows. |
| 61 bool excess_allocation() const { | 61 bool excess_allocation() const { |
| 62 return segment_bytes_allocated_ > kExcessLimit; | 62 return segment_bytes_allocated_ > kExcessLimit; |
| 63 } | 63 } |
| 64 | 64 |
| 65 size_t allocation_size() const { return allocation_size_; } | 65 size_t allocation_size() const { return allocation_size_; } |
| 66 | 66 |
| 67 base::AccountingAllocator* allocator() const { return allocator_; } |
| 68 |
| 67 private: | 69 private: |
| 68 // All pointers returned from New() have this alignment. In addition, if the | 70 // All pointers returned from New() have this alignment. In addition, if the |
| 69 // object being allocated has a size that is divisible by 8 then its alignment | 71 // object being allocated has a size that is divisible by 8 then its alignment |
| 70 // will be 8. ASan requires 8-byte alignment. | 72 // will be 8. ASan requires 8-byte alignment. |
| 71 #ifdef V8_USE_ADDRESS_SANITIZER | 73 #ifdef V8_USE_ADDRESS_SANITIZER |
| 72 static const size_t kAlignment = 8; | 74 static const size_t kAlignment = 8; |
| 73 STATIC_ASSERT(kPointerSize <= 8); | 75 STATIC_ASSERT(kPointerSize <= 8); |
| 74 #else | 76 #else |
| 75 static const size_t kAlignment = kPointerSize; | 77 static const size_t kAlignment = kPointerSize; |
| 76 #endif | 78 #endif |
| (...skipping 30 matching lines...) Expand all Loading... |
| 107 | 109 |
| 108 // Deletes the given segment. Does not touch the segment chain. | 110 // Deletes the given segment. Does not touch the segment chain. |
| 109 inline void DeleteSegment(Segment* segment, size_t size); | 111 inline void DeleteSegment(Segment* segment, size_t size); |
| 110 | 112 |
| 111 // The free region in the current (front) segment is represented as | 113 // The free region in the current (front) segment is represented as |
| 112 // the half-open interval [position, limit). The 'position' variable | 114 // the half-open interval [position, limit). The 'position' variable |
| 113 // is guaranteed to be aligned as dictated by kAlignment. | 115 // is guaranteed to be aligned as dictated by kAlignment. |
| 114 Address position_; | 116 Address position_; |
| 115 Address limit_; | 117 Address limit_; |
| 116 | 118 |
| 119 base::AccountingAllocator* allocator_; |
| 120 |
| 117 Segment* segment_head_; | 121 Segment* segment_head_; |
| 118 }; | 122 }; |
| 119 | 123 |
| 120 | 124 |
| 121 // ZoneObject is an abstraction that helps define classes of objects | 125 // ZoneObject is an abstraction that helps define classes of objects |
| 122 // allocated in the Zone. Use it as a base class; see ast.h. | 126 // allocated in the Zone. Use it as a base class; see ast.h. |
| 123 class ZoneObject { | 127 class ZoneObject { |
| 124 public: | 128 public: |
| 125 // Allocate a new ZoneObject of 'size' bytes in the Zone. | 129 // Allocate a new ZoneObject of 'size' bytes in the Zone. |
| 126 void* operator new(size_t size, Zone* zone) { return zone->New(size); } | 130 void* operator new(size_t size, Zone* zone) { return zone->New(size); } |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } | 244 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } |
| 241 }; | 245 }; |
| 242 | 246 |
| 243 | 247 |
| 244 typedef TemplateHashMapImpl<ZoneAllocationPolicy> ZoneHashMap; | 248 typedef TemplateHashMapImpl<ZoneAllocationPolicy> ZoneHashMap; |
| 245 | 249 |
| 246 } // namespace internal | 250 } // namespace internal |
| 247 } // namespace v8 | 251 } // namespace v8 |
| 248 | 252 |
| 249 #endif // V8_ZONE_H_ | 253 #endif // V8_ZONE_H_ |
| OLD | NEW |