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 if (current_zone != NULL) { | |
88 ASSERT(current_zone->next_ == NULL); | |
89 current_zone->next_ = this; | |
90 } | |
91 } | |
92 | |
93 // Remove zone from zone chain. | |
94 void Unlink() { | |
95 Zone* next = next_; | |
96 Zone* previous = previous_; | |
97 if (next != NULL) { | |
98 next->previous_ = previous; | |
99 } | |
100 if (previous != NULL) { | |
101 previous->next_ = next; | |
102 } | |
103 } | |
104 | |
74 // Delete all objects and free all memory allocated in the zone. | 105 // Delete all objects and free all memory allocated in the zone. |
75 void DeleteAll(); | 106 void DeleteAll(); |
76 | 107 |
77 #if defined(DEBUG) | 108 #if defined(DEBUG) |
78 // Dump the current allocated sizes in the zone object. | 109 // Dump the current allocated sizes in the zone object. |
79 void DumpZoneSizes(); | 110 void DumpZoneSizes(); |
80 #endif | 111 #endif |
81 | 112 |
82 // This buffer is used for allocation before any segments. | 113 // This buffer is used for allocation before any segments. |
83 // This would act as the initial stack allocated chunk so that we don't | 114 // This would act as the initial stack allocated chunk so that we don't |
(...skipping 14 matching lines...) Expand all Loading... | |
98 class Segment; | 129 class Segment; |
99 | 130 |
100 // The current head segment; may be NULL. | 131 // The current head segment; may be NULL. |
101 Segment* head_; | 132 Segment* head_; |
102 | 133 |
103 // List of large segments allocated in this zone; may be NULL. | 134 // List of large segments allocated in this zone; may be NULL. |
104 Segment* large_segments_; | 135 Segment* large_segments_; |
105 | 136 |
106 // Structure for managing handles allocation. | 137 // Structure for managing handles allocation. |
107 VMHandles handles_; | 138 VMHandles handles_; |
108 VMHandles* handles() { return &handles_; } | 139 |
140 // Used for chaining zones in order to allow unwinding of stacks. | |
141 Zone* previous_; | |
142 Zone* next_; | |
siva
2012/10/12 20:40:21
As discussed offline, I wanted to understand the t
Tom Ball
2012/10/12 22:56:51
The double-linked list is no longer necessary, now
| |
109 | 143 |
110 friend class StackZone; | 144 friend class StackZone; |
111 friend class ApiZone; | 145 friend class ApiZone; |
112 template<typename T, typename B> friend class BaseGrowableArray; | 146 template<typename T, typename B> friend class BaseGrowableArray; |
113 DISALLOW_COPY_AND_ASSIGN(Zone); | 147 DISALLOW_COPY_AND_ASSIGN(Zone); |
114 }; | 148 }; |
115 | 149 |
116 | 150 |
117 class StackZone : public StackResource { | 151 class StackZone : public StackResource { |
118 public: | 152 public: |
119 // Create an empty zone and set is at the current zone for the Isolate. | 153 // Create an empty zone and set is at the current zone for the Isolate. |
120 explicit StackZone(BaseIsolate* isolate); | 154 explicit StackZone(BaseIsolate* isolate); |
121 | 155 |
122 // Delete all memory associated with the zone. | 156 // Delete all memory associated with the zone. |
123 ~StackZone(); | 157 ~StackZone(); |
124 | 158 |
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 | 159 // Compute the total size of this zone. This includes wasted space that is |
150 // due to internal fragmentation in the segments. | 160 // due to internal fragmentation in the segments. |
151 intptr_t SizeInBytes() const { return zone_.SizeInBytes(); } | 161 intptr_t SizeInBytes() const { return zone_.SizeInBytes(); } |
152 | 162 |
153 // Make a copy of the string in the zone allocated area. | 163 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 | 164 |
166 private: | 165 private: |
167 Zone* GetBaseZone() { return &zone_; } | |
168 | |
169 Zone zone_; | 166 Zone zone_; |
170 | 167 |
171 // Used for chaining zones in order to allow unwinding of stacks. | |
172 StackZone* previous_; | |
173 | |
174 template<typename T> friend class GrowableArray; | 168 template<typename T> friend class GrowableArray; |
175 template<typename T> friend class ZoneGrowableArray; | 169 template<typename T> friend class ZoneGrowableArray; |
176 | 170 |
177 DISALLOW_IMPLICIT_CONSTRUCTORS(StackZone); | 171 DISALLOW_IMPLICIT_CONSTRUCTORS(StackZone); |
178 }; | 172 }; |
179 | 173 |
180 inline uword Zone::AllocUnsafe(intptr_t size) { | 174 inline uword Zone::AllocUnsafe(intptr_t size) { |
181 ASSERT(size >= 0); | 175 ASSERT(size >= 0); |
182 | 176 |
183 // Round up the requested size to fit the alignment. | 177 // 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), | 215 reinterpret_cast<void*>(old_data), |
222 Utils::Minimum(old_len * sizeof(ElementType), | 216 Utils::Minimum(old_len * sizeof(ElementType), |
223 new_len * sizeof(ElementType))); | 217 new_len * sizeof(ElementType))); |
224 } | 218 } |
225 return new_data; | 219 return new_data; |
226 } | 220 } |
227 | 221 |
228 } // namespace dart | 222 } // namespace dart |
229 | 223 |
230 #endif // VM_ZONE_H_ | 224 #endif // VM_ZONE_H_ |
OLD | NEW |