Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(287)

Side by Side Diff: runtime/vm/zone.h

Issue 10836061: Change the zone allocation api. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/vm/symbols.cc ('k') | runtime/vm/zone.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 BaseZone { 19 class BaseZone {
20 private: 20 private:
21 BaseZone(); 21 BaseZone();
22 ~BaseZone(); // Delete all memory associated with the zone. 22 ~BaseZone(); // Delete all memory associated with the zone.
23 23
24 // Allocate 'size' bytes of memory in the zone; expands the zone by 24 // Allocate an array sized to hold 'len' elements of type
25 // 'ElementType'. Checks for integer overflow when performing the
26 // size computation.
27 template <class ElementType>
28 inline ElementType* Alloc(intptr_t len);
29
30 // Allocates an array sized to hold 'len' elements of type
31 // 'ElementType'. The new array is initialized from the memory of
32 // 'old_array' up to 'old_len'.
33 template <class ElementType>
34 inline ElementType* Realloc(ElementType* old_array,
35 intptr_t old_len,
36 intptr_t new_len);
37
38 // Allocates 'size' bytes of memory in the zone; expands the zone by
25 // allocating new segments of memory on demand using 'new'. 39 // allocating new segments of memory on demand using 'new'.
26 inline uword Allocate(intptr_t size); 40 //
27 41 // It is preferred to use Alloc<T>() instead, as that function can
28 // Allocate 'new_size' bytes of memory and copies 'old_size' bytes from 42 // check for integer overflow. If you use AllocUnsafe, you are
29 // 'data' into new allocated memory. Uses current zone. 43 // responsible for avoiding integer overflow yourself.
30 uword Reallocate(uword data, intptr_t old_size, intptr_t new_size); 44 inline uword AllocUnsafe(intptr_t size);
31 45
32 // Compute the total size of this zone. This includes wasted space that is 46 // Compute the total size of this zone. This includes wasted space that is
33 // due to internal fragmentation in the segments. 47 // due to internal fragmentation in the segments.
34 intptr_t SizeInBytes() const; 48 intptr_t SizeInBytes() const;
35 49
36 // Make a copy of the string in the zone allocated area. 50 // Make a copy of the string in the zone allocated area.
37 char* MakeCopyOfString(const char* str); 51 char* MakeCopyOfString(const char* str);
38 52
39 // All pointers returned from Allocate() and New() have this alignment. 53 // All pointers returned from AllocateUnsafe() and New() have this alignment.
40 static const intptr_t kAlignment = kWordSize; 54 static const intptr_t kAlignment = kWordSize;
41 55
42 // Default initial chunk size. 56 // Default initial chunk size.
43 static const intptr_t kInitialChunkSize = 1 * KB; 57 static const intptr_t kInitialChunkSize = 1 * KB;
44 58
45 // Default segment size. 59 // Default segment size.
46 static const intptr_t kSegmentSize = 64 * KB; 60 static const intptr_t kSegmentSize = 64 * KB;
47 61
48 // Zap value used to indicate deleted zone area (debug purposes). 62 // Zap value used to indicate deleted zone area (debug purposes).
49 static const unsigned char kZapDeletedByte = 0x42; 63 static const unsigned char kZapDeletedByte = 0x42;
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 111
98 112
99 class Zone : public StackResource { 113 class Zone : public StackResource {
100 public: 114 public:
101 // Create an empty zone and set is at the current zone for the Isolate. 115 // Create an empty zone and set is at the current zone for the Isolate.
102 explicit Zone(BaseIsolate* isolate); 116 explicit Zone(BaseIsolate* isolate);
103 117
104 // Delete all memory associated with the zone. 118 // Delete all memory associated with the zone.
105 ~Zone(); 119 ~Zone();
106 120
107 // Allocate 'size' bytes of memory in the zone; expands the zone by 121 // Allocates an array sized to hold 'len' elements of type
122 // 'ElementType'. Checks for integer overflow when performing the
123 // size computation.
124 template <class ElementType>
125 ElementType* Alloc(intptr_t len) { return zone_.Alloc<ElementType>(len); }
126
127 // Allocates an array sized to hold 'len' elements of type
128 // 'ElementType'. The new array is initialized from the memory of
129 // 'old_array' up to 'old_len'.
130 template <class ElementType>
131 ElementType* Realloc(ElementType* old_array,
132 intptr_t old_len,
133 intptr_t new_len) {
134 return zone_.Realloc<ElementType>(old_array, old_len, new_len);
135 }
136
137 // Allocates 'size' bytes of memory in the zone; expands the zone by
108 // allocating new segments of memory on demand using 'new'. 138 // allocating new segments of memory on demand using 'new'.
109 uword Allocate(intptr_t size) { return zone_.Allocate(size); } 139 //
110 140 // It is preferred to use Alloc<T>() instead, as that function can
111 // Allocate 'new_size' bytes of memory and copies 'old_size' bytes from 141 // check for integer overflow. If you use AllocUnsafe, you are
112 // 'data' into new allocated memory. Uses current zone. 142 // responsible for avoiding integer overflow yourself.
113 uword Reallocate(uword data, intptr_t old_size, intptr_t new_size) { 143 uword AllocUnsafe(intptr_t size) { return zone_.AllocUnsafe(size); }
114 return zone_.Reallocate(data, old_size, new_size);
115 }
116 144
117 // Compute the total size of this zone. This includes wasted space that is 145 // Compute the total size of this zone. This includes wasted space that is
118 // due to internal fragmentation in the segments. 146 // due to internal fragmentation in the segments.
119 intptr_t SizeInBytes() const { return zone_.SizeInBytes(); } 147 intptr_t SizeInBytes() const { return zone_.SizeInBytes(); }
120 148
121 // Make a copy of the string in the zone allocated area. 149 // Make a copy of the string in the zone allocated area.
122 char* MakeCopyOfString(const char* str) { 150 char* MakeCopyOfString(const char* str) {
123 return zone_.MakeCopyOfString(str); 151 return zone_.MakeCopyOfString(str);
124 } 152 }
125 153
(...skipping 14 matching lines...) Expand all
140 168
141 // Used for chaining zones in order to allow unwinding of stacks. 169 // Used for chaining zones in order to allow unwinding of stacks.
142 Zone* previous_; 170 Zone* previous_;
143 171
144 template<typename T> friend class GrowableArray; 172 template<typename T> friend class GrowableArray;
145 template<typename T> friend class ZoneGrowableArray; 173 template<typename T> friend class ZoneGrowableArray;
146 174
147 DISALLOW_IMPLICIT_CONSTRUCTORS(Zone); 175 DISALLOW_IMPLICIT_CONSTRUCTORS(Zone);
148 }; 176 };
149 177
150 178 inline uword BaseZone::AllocUnsafe(intptr_t size) {
151 inline uword BaseZone::Allocate(intptr_t size) {
152 ASSERT(size >= 0); 179 ASSERT(size >= 0);
153 180
154 // Round up the requested size to fit the alignment. 181 // Round up the requested size to fit the alignment.
182 if (size > (kIntptrMax - kAlignment)) {
183 FATAL1("BaseZone::Alloc: 'size' is too large: size=%ld", size);
184 }
155 size = Utils::RoundUp(size, kAlignment); 185 size = Utils::RoundUp(size, kAlignment);
156 186
157 // Check if the requested size is available without expanding. 187 // Check if the requested size is available without expanding.
158 uword result; 188 uword result;
159 intptr_t free_size = (limit_ - position_); 189 intptr_t free_size = (limit_ - position_);
160 if (free_size >= size) { 190 if (free_size >= size) {
161 result = position_; 191 result = position_;
162 position_ += size; 192 position_ += size;
163 } else { 193 } else {
164 result = AllocateExpand(size); 194 result = AllocateExpand(size);
165 } 195 }
166 196
167 // Check that the result has the proper alignment and return it. 197 // Check that the result has the proper alignment and return it.
168 ASSERT(Utils::IsAligned(result, kAlignment)); 198 ASSERT(Utils::IsAligned(result, kAlignment));
169 return result; 199 return result;
170 } 200 }
171 201
202 template <class ElementType>
203 inline ElementType* BaseZone::Alloc(intptr_t len) {
204 const intptr_t element_size = sizeof(ElementType);
205 if (len > (kIntptrMax / element_size)) {
206 FATAL2("BaseZone::Alloc: 'len' is too large: len=%ld, element_size=%ld",
207 len, element_size);
208 }
209 return reinterpret_cast<ElementType*>(AllocUnsafe(len * element_size));
210 }
211
212 template <class ElementType>
213 inline ElementType* BaseZone::Realloc(ElementType* old_data,
214 intptr_t old_len,
215 intptr_t new_len) {
216 ElementType* new_data = Alloc<ElementType>(new_len);
217 if (old_data != 0) {
218 memmove(reinterpret_cast<void*>(new_data),
219 reinterpret_cast<void*>(old_data),
220 Utils::Minimum(old_len * sizeof(ElementType),
221 new_len * sizeof(ElementType)));
222 }
223 return new_data;
224 }
225
172 } // namespace dart 226 } // namespace dart
173 227
174 #endif // VM_ZONE_H_ 228 #endif // VM_ZONE_H_
OLDNEW
« no previous file with comments | « runtime/vm/symbols.cc ('k') | runtime/vm/zone.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698