| 1 | // Copyright (c) 2020, 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 | |
| 5 | #ifndef RUNTIME_VM_FIELD_TABLE_H_ |
| 6 | #define RUNTIME_VM_FIELD_TABLE_H_ |
| 7 | |
| 8 | #include "platform/assert.h" |
| 9 | #include "platform/atomic.h" |
| 10 | |
| 11 | #include "vm/bitfield.h" |
| 12 | #include "vm/class_id.h" |
| 13 | #include "vm/globals.h" |
| 14 | #include "vm/growable_array.h" |
| 15 | #include "vm/tagged_pointer.h" |
| 16 | |
| 17 | namespace dart { |
| 18 | |
| 19 | class Field; |
| 20 | |
| 21 | class FieldTable { |
| 22 | public: |
| 23 | FieldTable() |
| 24 | : top_(0), |
| 25 | capacity_(0), |
| 26 | free_head_(-1), |
| 27 | table_(nullptr), |
| 28 | old_tables_(new MallocGrowableArray<InstancePtr*>()) {} |
| 29 | |
| 30 | ~FieldTable(); |
| 31 | |
| 32 | intptr_t NumFieldIds() const { return top_; } |
| 33 | intptr_t Capacity() const { return capacity_; } |
| 34 | |
| 35 | InstancePtr* table() { return table_; } |
| 36 | |
| 37 | void FreeOldTables(); |
| 38 | |
| 39 | // Used by the generated code. |
| 40 | static intptr_t FieldOffsetFor(intptr_t field_id); |
| 41 | |
| 42 | bool IsValidIndex(intptr_t index) const { return index >= 0 && index < top_; } |
| 43 | |
| 44 | void Register(const Field& field); |
| 45 | void AllocateIndex(intptr_t index); |
| 46 | |
| 47 | // Static field elements are being freed only during isolate reload |
| 48 | // when initially created static field have to get remapped to point |
| 49 | // to an existing static field value. |
| 50 | void Free(intptr_t index); |
| 51 | |
| 52 | InstancePtr At(intptr_t index) const { |
| 53 | ASSERT(IsValidIndex(index)); |
| 54 | return table_[index]; |
| 55 | } |
| 56 | void SetAt(intptr_t index, InstancePtr raw_instance); |
| 57 | |
| 58 | FieldTable* Clone(); |
| 59 | |
| 60 | void VisitObjectPointers(ObjectPointerVisitor* visitor); |
| 61 | |
| 62 | static const int kInitialCapacity = 512; |
| 63 | static const int kCapacityIncrement = 256; |
| 64 | |
| 65 | private: |
| 66 | friend class GCMarker; |
| 67 | friend class MarkingWeakVisitor; |
| 68 | friend class Scavenger; |
| 69 | friend class ScavengerWeakVisitor; |
| 70 | |
| 71 | void Grow(intptr_t new_capacity); |
| 72 | |
| 73 | intptr_t top_; |
| 74 | intptr_t capacity_; |
| 75 | // -1 if free list is empty, otherwise index of first empty element. Empty |
| 76 | // elements are organized into linked list - they contain index of next |
| 77 | // element, last element contains -1. |
| 78 | intptr_t free_head_; |
| 79 | |
| 80 | InstancePtr* table_; |
| 81 | // When table_ grows and have to reallocated, keep the old one here |
| 82 | // so it will get freed when its are no longer in use. |
| 83 | MallocGrowableArray<InstancePtr*>* old_tables_; |
| 84 | |
| 85 | DISALLOW_COPY_AND_ASSIGN(FieldTable); |
| 86 | }; |
| 87 | |
| 88 | } // namespace dart |
| 89 | |
| 90 | #endif // RUNTIME_VM_FIELD_TABLE_H_ |
| 91 |