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_DART_API_STATE_H_ | 5 #ifndef VM_DART_API_STATE_H_ |
6 #define VM_DART_API_STATE_H_ | 6 #define VM_DART_API_STATE_H_ |
7 | 7 |
8 #include "include/dart_api.h" | 8 #include "include/dart_api.h" |
9 | 9 |
10 #include "platform/thread.h" | 10 #include "platform/thread.h" |
(...skipping 15 matching lines...) Expand all Loading... |
26 // zones support deallocating all chunks in one fast operation when the | 26 // zones support deallocating all chunks in one fast operation when the |
27 // scope is exited. | 27 // scope is exited. |
28 class ApiZone { | 28 class ApiZone { |
29 public: | 29 public: |
30 // Create an empty zone. | 30 // Create an empty zone. |
31 ApiZone() : zone_() { } | 31 ApiZone() : zone_() { } |
32 | 32 |
33 // Delete all memory associated with the zone. | 33 // Delete all memory associated with the zone. |
34 ~ApiZone() { } | 34 ~ApiZone() { } |
35 | 35 |
36 // Allocate 'size' bytes of memory in the zone; expands the zone by | 36 // Allocates an array sized to hold 'len' elements of type |
| 37 // 'ElementType'. Checks for integer overflow when performing the |
| 38 // size computation. |
| 39 template <class ElementType> |
| 40 ElementType* Alloc(intptr_t len) { return zone_.Alloc<ElementType>(len); } |
| 41 |
| 42 // Allocates an array sized to hold 'len' elements of type |
| 43 // 'ElementType'. The new array is initialized from the memory of |
| 44 // 'old_array' up to 'old_len'. |
| 45 template <class ElementType> |
| 46 ElementType* Realloc(ElementType* old_array, |
| 47 intptr_t old_len, |
| 48 intptr_t new_len) { |
| 49 return zone_.Realloc<ElementType>(old_array, old_len, new_len); |
| 50 } |
| 51 |
| 52 // Allocates 'size' bytes of memory in the zone; expands the zone by |
37 // allocating new segments of memory on demand using 'new'. | 53 // allocating new segments of memory on demand using 'new'. |
38 uword Allocate(intptr_t size) { return zone_.Allocate(size); } | 54 // |
39 | 55 // It is preferred to use Alloc<T>() instead, as that function can |
40 // Allocate 'new_size' bytes of memory and copies 'old_size' bytes from | 56 // check for integer overflow. If you use AllocUnsafe, you are |
41 // 'data' into new allocated memory. Uses current zone. | 57 // responsible for avoiding integer overflow yourself. |
42 uword Reallocate(uword data, intptr_t old_size, intptr_t new_size) { | 58 uword AllocUnsafe(intptr_t size) { return zone_.AllocUnsafe(size); } |
43 return zone_.Reallocate(data, old_size, new_size); | |
44 } | |
45 | 59 |
46 // Compute the total size of this zone. This includes wasted space that is | 60 // Compute the total size of this zone. This includes wasted space that is |
47 // due to internal fragmentation in the segments. | 61 // due to internal fragmentation in the segments. |
48 intptr_t SizeInBytes() const { return zone_.SizeInBytes(); } | 62 intptr_t SizeInBytes() const { return zone_.SizeInBytes(); } |
49 | 63 |
50 private: | 64 private: |
51 BaseZone* GetBaseZone() { return &zone_; } | 65 BaseZone* GetBaseZone() { return &zone_; } |
52 | 66 |
53 BaseZone zone_; | 67 BaseZone zone_; |
54 | 68 |
(...skipping 602 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
657 ApiNativeScope::Current()->zone()->GetBaseZone()) {} | 671 ApiNativeScope::Current()->zone()->GetBaseZone()) {} |
658 ApiGrowableArray() | 672 ApiGrowableArray() |
659 : BaseGrowableArray<T, ValueObject>( | 673 : BaseGrowableArray<T, ValueObject>( |
660 ApiNativeScope::Current()->zone()->GetBaseZone()) {} | 674 ApiNativeScope::Current()->zone()->GetBaseZone()) {} |
661 }; | 675 }; |
662 | 676 |
663 | 677 |
664 } // namespace dart | 678 } // namespace dart |
665 | 679 |
666 #endif // VM_DART_API_STATE_H_ | 680 #endif // VM_DART_API_STATE_H_ |
OLD | NEW |