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
3// BSD-style license that can be found in the LICENSE file.
4// Defines growable array classes, that differ where they are allocated:
5// - GrowableArray: allocated on stack.
6// - ZoneGrowableArray: allocated in the zone.
7// - MallocGrowableArray: allocates using malloc/realloc; free is only called
8// at destruction.
9
10#ifndef RUNTIME_VM_GROWABLE_ARRAY_H_
11#define RUNTIME_VM_GROWABLE_ARRAY_H_
12
13#include "platform/growable_array.h"
14#include "vm/thread_state.h"
15#include "vm/zone.h"
16
17namespace dart {
18
19template <typename T>
20class GrowableArray : public BaseGrowableArray<T, ValueObject, Zone> {
21 public:
22 GrowableArray(Zone* zone, intptr_t initial_capacity)
23 : BaseGrowableArray<T, ValueObject, Zone>(initial_capacity,
24 ASSERT_NOTNULL(zone)) {}
25 explicit GrowableArray(intptr_t initial_capacity)
26 : BaseGrowableArray<T, ValueObject, Zone>(
27 initial_capacity,
28 ASSERT_NOTNULL(ThreadState::Current()->zone())) {}
29 GrowableArray()
30 : BaseGrowableArray<T, ValueObject, Zone>(
31 ASSERT_NOTNULL(ThreadState::Current()->zone())) {}
32};
33
34template <typename T>
35class ZoneGrowableArray : public BaseGrowableArray<T, ZoneAllocated, Zone> {
36 public:
37 ZoneGrowableArray(Zone* zone, intptr_t initial_capacity)
38 : BaseGrowableArray<T, ZoneAllocated, Zone>(initial_capacity,
39 ASSERT_NOTNULL(zone)) {}
40 explicit ZoneGrowableArray(intptr_t initial_capacity)
41 : BaseGrowableArray<T, ZoneAllocated, Zone>(
42 initial_capacity,
43 ASSERT_NOTNULL(ThreadState::Current()->zone())) {}
44 ZoneGrowableArray()
45 : BaseGrowableArray<T, ZoneAllocated, Zone>(
46 ASSERT_NOTNULL(ThreadState::Current()->zone())) {}
47};
48
49// T must be a Handle type.
50template <typename T, typename B>
51class BaseGrowableHandlePtrArray : public B {
52 public:
53 BaseGrowableHandlePtrArray(Zone* zone, intptr_t initial_capacity)
54 : zone_(zone), array_(zone, initial_capacity) {}
55
56 // Use unique zone handles to store objects.
57 void Add(const T& t) { array_.Add(&T::ZoneHandle(zone_, t.raw())); }
58
59 T& operator[](intptr_t index) const { return *array_[index]; }
60
61 const T& At(intptr_t index) const { return operator[](index); }
62
63 void SetAt(intptr_t index, const T& t) {
64 array_[index] = &T::ZoneHandle(zone_, t.raw());
65 }
66
67 intptr_t length() const { return array_.length(); }
68
69 const GrowableArray<T*>& growable_array() const { return array_; }
70
71 private:
72 Zone* zone_;
73 GrowableArray<T*> array_;
74
75 DISALLOW_COPY_AND_ASSIGN(BaseGrowableHandlePtrArray);
76};
77
78template <typename T>
79class GrowableHandlePtrArray
80 : public BaseGrowableHandlePtrArray<T, ValueObject> {
81 public:
82 GrowableHandlePtrArray(Zone* zone, intptr_t initial_capacity)
83 : BaseGrowableHandlePtrArray<T, ValueObject>(zone, initial_capacity) {}
84};
85
86template <typename T>
87class ZoneGrowableHandlePtrArray
88 : public BaseGrowableHandlePtrArray<T, ZoneAllocated> {
89 public:
90 ZoneGrowableHandlePtrArray(Zone* zone, intptr_t initial_capacity)
91 : BaseGrowableHandlePtrArray<T, ZoneAllocated>(zone, initial_capacity) {}
92};
93
94} // namespace dart
95
96#endif // RUNTIME_VM_GROWABLE_ARRAY_H_
97