| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef VM_ZONE_H_ | 5 #ifndef VM_ZONE_H_ |
| 6 #define VM_ZONE_H_ | 6 #define VM_ZONE_H_ |
| 7 | 7 |
| 8 #include "platform/utils.h" | 8 #include "platform/utils.h" |
| 9 #include "vm/allocation.h" | 9 #include "vm/allocation.h" |
| 10 #include "vm/handles.h" | 10 #include "vm/handles.h" |
| 11 #include "vm/memory_region.h" | 11 #include "vm/memory_region.h" |
| 12 | 12 |
| 13 namespace dart { | 13 namespace dart { |
| 14 | 14 |
| 15 // Zones support very fast allocation of small chunks of memory. The | 15 // Zones support very fast allocation of small chunks of memory. The |
| 16 // chunks cannot be deallocated individually, but instead zones | 16 // chunks cannot be deallocated individually, but instead zones |
| 17 // support deallocating all chunks in one fast operation. | 17 // support deallocating all chunks in one fast operation. |
| 18 | 18 |
| 19 class Zone { | 19 class Zone { |
| 20 private: | 20 public: |
| 21 Zone(); | |
| 22 ~Zone(); // Delete all memory associated with the zone. | |
| 23 | |
| 24 // Allocate an array sized to hold 'len' elements of type | 21 // Allocate an array sized to hold 'len' elements of type |
| 25 // 'ElementType'. Checks for integer overflow when performing the | 22 // 'ElementType'. Checks for integer overflow when performing the |
| 26 // size computation. | 23 // size computation. |
| 27 template <class ElementType> | 24 template <class ElementType> |
| 28 inline ElementType* Alloc(intptr_t len); | 25 inline ElementType* Alloc(intptr_t len); |
| 29 | 26 |
| 30 // Allocates an array sized to hold 'len' elements of type | 27 // Allocates an array sized to hold 'len' elements of type |
| 31 // 'ElementType'. The new array is initialized from the memory of | 28 // 'ElementType'. The new array is initialized from the memory of |
| 32 // 'old_array' up to 'old_len'. | 29 // 'old_array' up to 'old_len'. |
| 33 template <class ElementType> | 30 template <class ElementType> |
| 34 inline ElementType* Realloc(ElementType* old_array, | 31 inline ElementType* Realloc(ElementType* old_array, |
| 35 intptr_t old_len, | 32 intptr_t old_len, |
| 36 intptr_t new_len); | 33 intptr_t new_len); |
| 37 | 34 |
| 38 // Allocates 'size' bytes of memory in the zone; expands the zone by | 35 // Allocates 'size' bytes of memory in the zone; expands the zone by |
| 39 // allocating new segments of memory on demand using 'new'. | 36 // allocating new segments of memory on demand using 'new'. |
| 40 // | 37 // |
| 41 // It is preferred to use Alloc<T>() instead, as that function can | 38 // It is preferred to use Alloc<T>() instead, as that function can |
| 42 // check for integer overflow. If you use AllocUnsafe, you are | 39 // check for integer overflow. If you use AllocUnsafe, you are |
| 43 // responsible for avoiding integer overflow yourself. | 40 // responsible for avoiding integer overflow yourself. |
| 44 inline uword AllocUnsafe(intptr_t size); | 41 inline uword AllocUnsafe(intptr_t size); |
| 45 | 42 |
| 43 |
| 44 // Make a copy of the string in the zone allocated area. |
| 45 char* MakeCopyOfString(const char* str); |
| 46 |
| 47 // Make a zone-allocated string based on printf format and args. |
| 48 char* PrintToString(const char* format, ...) PRINTF_ATTRIBUTE(2, 3); |
| 49 |
| 46 // Compute the total size of this zone. This includes wasted space that is | 50 // Compute the total size of this zone. This includes wasted space that is |
| 47 // due to internal fragmentation in the segments. | 51 // due to internal fragmentation in the segments. |
| 48 intptr_t SizeInBytes() const; | 52 intptr_t SizeInBytes() const; |
| 49 | 53 |
| 50 // Make a copy of the string in the zone allocated area. | 54 // Structure for managing handles allocation. |
| 51 char* MakeCopyOfString(const char* str); | 55 VMHandles* handles() { return &handles_; } |
| 56 |
| 57 void VisitObjectPointers(ObjectPointerVisitor* visitor); |
| 58 |
| 59 private: |
| 60 Zone(); |
| 61 ~Zone(); // Delete all memory associated with the zone. |
| 52 | 62 |
| 53 // All pointers returned from AllocateUnsafe() and New() have this alignment. | 63 // All pointers returned from AllocateUnsafe() and New() have this alignment. |
| 54 static const intptr_t kAlignment = kWordSize; | 64 static const intptr_t kAlignment = kWordSize; |
| 55 | 65 |
| 56 // Default initial chunk size. | 66 // Default initial chunk size. |
| 57 static const intptr_t kInitialChunkSize = 1 * KB; | 67 static const intptr_t kInitialChunkSize = 1 * KB; |
| 58 | 68 |
| 59 // Default segment size. | 69 // Default segment size. |
| 60 static const intptr_t kSegmentSize = 64 * KB; | 70 static const intptr_t kSegmentSize = 64 * KB; |
| 61 | 71 |
| 62 // Zap value used to indicate deleted zone area (debug purposes). | 72 // Zap value used to indicate deleted zone area (debug purposes). |
| 63 static const unsigned char kZapDeletedByte = 0x42; | 73 static const unsigned char kZapDeletedByte = 0x42; |
| 64 | 74 |
| 65 // Zap value used to indicate uninitialized zone area (debug purposes). | 75 // Zap value used to indicate uninitialized zone area (debug purposes). |
| 66 static const unsigned char kZapUninitializedByte = 0xab; | 76 static const unsigned char kZapUninitializedByte = 0xab; |
| 67 | 77 |
| 68 // Expand the zone to accommodate an allocation of 'size' bytes. | 78 // Expand the zone to accommodate an allocation of 'size' bytes. |
| 69 uword AllocateExpand(intptr_t size); | 79 uword AllocateExpand(intptr_t size); |
| 70 | 80 |
| 71 // Allocate a large segment. | 81 // Allocate a large segment. |
| 72 uword AllocateLargeSegment(intptr_t size); | 82 uword AllocateLargeSegment(intptr_t size); |
| 73 | 83 |
| 84 // Insert zone into zone chain, after current_zone. |
| 85 void Link(Zone* current_zone) { |
| 86 previous_ = current_zone; |
| 87 } |
| 88 |
| 74 // Delete all objects and free all memory allocated in the zone. | 89 // Delete all objects and free all memory allocated in the zone. |
| 75 void DeleteAll(); | 90 void DeleteAll(); |
| 76 | 91 |
| 77 #if defined(DEBUG) | 92 #if defined(DEBUG) |
| 78 // Dump the current allocated sizes in the zone object. | 93 // Dump the current allocated sizes in the zone object. |
| 79 void DumpZoneSizes(); | 94 void DumpZoneSizes(); |
| 80 #endif | 95 #endif |
| 81 | 96 |
| 82 // This buffer is used for allocation before any segments. | 97 // This buffer is used for allocation before any segments. |
| 83 // This would act as the initial stack allocated chunk so that we don't | 98 // This would act as the initial stack allocated chunk so that we don't |
| (...skipping 14 matching lines...) Expand all Loading... |
| 98 class Segment; | 113 class Segment; |
| 99 | 114 |
| 100 // The current head segment; may be NULL. | 115 // The current head segment; may be NULL. |
| 101 Segment* head_; | 116 Segment* head_; |
| 102 | 117 |
| 103 // List of large segments allocated in this zone; may be NULL. | 118 // List of large segments allocated in this zone; may be NULL. |
| 104 Segment* large_segments_; | 119 Segment* large_segments_; |
| 105 | 120 |
| 106 // Structure for managing handles allocation. | 121 // Structure for managing handles allocation. |
| 107 VMHandles handles_; | 122 VMHandles handles_; |
| 108 VMHandles* handles() { return &handles_; } | 123 |
| 124 // Used for chaining zones in order to allow unwinding of stacks. |
| 125 Zone* previous_; |
| 109 | 126 |
| 110 friend class StackZone; | 127 friend class StackZone; |
| 111 friend class ApiZone; | 128 friend class ApiZone; |
| 112 template<typename T, typename B> friend class BaseGrowableArray; | 129 template<typename T, typename B> friend class BaseGrowableArray; |
| 113 DISALLOW_COPY_AND_ASSIGN(Zone); | 130 DISALLOW_COPY_AND_ASSIGN(Zone); |
| 114 }; | 131 }; |
| 115 | 132 |
| 116 | 133 |
| 117 class StackZone : public StackResource { | 134 class StackZone : public StackResource { |
| 118 public: | 135 public: |
| 119 // Create an empty zone and set is at the current zone for the Isolate. | 136 // Create an empty zone and set is at the current zone for the Isolate. |
| 120 explicit StackZone(BaseIsolate* isolate); | 137 explicit StackZone(BaseIsolate* isolate); |
| 121 | 138 |
| 122 // Delete all memory associated with the zone. | 139 // Delete all memory associated with the zone. |
| 123 ~StackZone(); | 140 ~StackZone(); |
| 124 | 141 |
| 125 // Allocates an array sized to hold 'len' elements of type | |
| 126 // 'ElementType'. Checks for integer overflow when performing the | |
| 127 // size computation. | |
| 128 template <class ElementType> | |
| 129 ElementType* Alloc(intptr_t len) { return zone_.Alloc<ElementType>(len); } | |
| 130 | |
| 131 // Allocates an array sized to hold 'len' elements of type | |
| 132 // 'ElementType'. The new array is initialized from the memory of | |
| 133 // 'old_array' up to 'old_len'. | |
| 134 template <class ElementType> | |
| 135 ElementType* Realloc(ElementType* old_array, | |
| 136 intptr_t old_len, | |
| 137 intptr_t new_len) { | |
| 138 return zone_.Realloc<ElementType>(old_array, old_len, new_len); | |
| 139 } | |
| 140 | |
| 141 // Allocates 'size' bytes of memory in the zone; expands the zone by | |
| 142 // allocating new segments of memory on demand using 'new'. | |
| 143 // | |
| 144 // It is preferred to use Alloc<T>() instead, as that function can | |
| 145 // check for integer overflow. If you use AllocUnsafe, you are | |
| 146 // responsible for avoiding integer overflow yourself. | |
| 147 uword AllocUnsafe(intptr_t size) { return zone_.AllocUnsafe(size); } | |
| 148 | |
| 149 // Compute the total size of this zone. This includes wasted space that is | 142 // Compute the total size of this zone. This includes wasted space that is |
| 150 // due to internal fragmentation in the segments. | 143 // due to internal fragmentation in the segments. |
| 151 intptr_t SizeInBytes() const { return zone_.SizeInBytes(); } | 144 intptr_t SizeInBytes() const { return zone_.SizeInBytes(); } |
| 152 | 145 |
| 153 // Make a copy of the string in the zone allocated area. | 146 Zone* GetZone() { return &zone_; } |
| 154 char* MakeCopyOfString(const char* str) { | |
| 155 return zone_.MakeCopyOfString(str); | |
| 156 } | |
| 157 | |
| 158 // Make a zone-allocated string based on printf format and args. | |
| 159 char* PrintToString(const char* format, ...) PRINTF_ATTRIBUTE(2, 3); | |
| 160 | |
| 161 // TODO(tball): remove once zone refactoring is finished. | |
| 162 VMHandles* handles() { return zone_.handles(); } | |
| 163 | |
| 164 void VisitObjectPointers(ObjectPointerVisitor* visitor); | |
| 165 | 147 |
| 166 private: | 148 private: |
| 167 Zone* GetBaseZone() { return &zone_; } | |
| 168 | |
| 169 Zone zone_; | 149 Zone zone_; |
| 170 | 150 |
| 171 // Used for chaining zones in order to allow unwinding of stacks. | |
| 172 StackZone* previous_; | |
| 173 | |
| 174 template<typename T> friend class GrowableArray; | 151 template<typename T> friend class GrowableArray; |
| 175 template<typename T> friend class ZoneGrowableArray; | 152 template<typename T> friend class ZoneGrowableArray; |
| 176 | 153 |
| 177 DISALLOW_IMPLICIT_CONSTRUCTORS(StackZone); | 154 DISALLOW_IMPLICIT_CONSTRUCTORS(StackZone); |
| 178 }; | 155 }; |
| 179 | 156 |
| 180 inline uword Zone::AllocUnsafe(intptr_t size) { | 157 inline uword Zone::AllocUnsafe(intptr_t size) { |
| 181 ASSERT(size >= 0); | 158 ASSERT(size >= 0); |
| 182 | 159 |
| 183 // Round up the requested size to fit the alignment. | 160 // Round up the requested size to fit the alignment. |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 reinterpret_cast<void*>(old_data), | 198 reinterpret_cast<void*>(old_data), |
| 222 Utils::Minimum(old_len * sizeof(ElementType), | 199 Utils::Minimum(old_len * sizeof(ElementType), |
| 223 new_len * sizeof(ElementType))); | 200 new_len * sizeof(ElementType))); |
| 224 } | 201 } |
| 225 return new_data; | 202 return new_data; |
| 226 } | 203 } |
| 227 | 204 |
| 228 } // namespace dart | 205 } // namespace dart |
| 229 | 206 |
| 230 #endif // VM_ZONE_H_ | 207 #endif // VM_ZONE_H_ |
| OLD | NEW |