Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
field_table.h
Go to the documentation of this file.
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
17namespace dart {
18
19class Isolate;
20class Field;
21class FieldInvalidator;
22
24 public:
25 explicit FieldTable(Isolate* isolate)
26 : top_(0),
27 capacity_(0),
28 free_head_(-1),
29 table_(nullptr),
30 old_tables_(new MallocGrowableArray<ObjectPtr*>()),
31 isolate_(isolate),
32 is_ready_to_use_(isolate == nullptr) {}
33
35
36 bool IsReadyToUse() const;
37 void MarkReadyToUse();
38
39 intptr_t NumFieldIds() const { return top_; }
40 intptr_t Capacity() const { return capacity_; }
41
42 ObjectPtr* table() { return table_; }
43
44 void FreeOldTables();
45
46 // Used by the generated code.
47 static intptr_t FieldOffsetFor(intptr_t field_id);
48
49 bool IsValidIndex(intptr_t index) const { return index >= 0 && index < top_; }
50
51 // Returns whether registering this field caused a growth in the backing
52 // store.
53 bool Register(const Field& field, intptr_t expected_field_id = -1);
54 void AllocateIndex(intptr_t index);
55
56 // Static field elements are being freed only during isolate reload
57 // when initially created static field have to get remapped to point
58 // to an existing static field value.
59 void Free(intptr_t index);
60
61 ObjectPtr At(intptr_t index, bool concurrent_use = false) const {
62 ASSERT(IsValidIndex(index));
63 if (concurrent_use) {
65 reinterpret_cast<const AcqRelAtomic<ObjectPtr*>*>(&table_)->load();
66 return reinterpret_cast<AcqRelAtomic<ObjectPtr>*>(&table[index])->load();
67 } else {
68 // There is no concurrent access expected for this field, so we avoid
69 // using atomics. This will allow us to detect via TSAN if there are
70 // racy uses.
71 return table_[index];
72 }
73 }
74
75 void SetAt(intptr_t index,
76 ObjectPtr raw_instance,
77 bool concurrent_use = false) {
78 ASSERT(index < capacity_);
79 ObjectPtr* slot = &table_[index];
80 if (concurrent_use) {
81 reinterpret_cast<AcqRelAtomic<ObjectPtr>*>(slot)->store(raw_instance);
82 } else {
83 // There is no concurrent access expected for this field, so we avoid
84 // using atomics. This will allow us to detect via TSAN if there are
85 // racy uses.
86 *slot = raw_instance;
87 }
88 }
89
90 FieldTable* Clone(Isolate* for_isolate);
91
93
94 static constexpr int kInitialCapacity = 512;
95 static constexpr int kCapacityIncrement = 256;
96
97 private:
98 void Grow(intptr_t new_capacity);
99
100 intptr_t top_;
101 intptr_t capacity_;
102 // -1 if free list is empty, otherwise index of first empty element. Empty
103 // elements are organized into linked list - they contain index of next
104 // element, last element contains -1.
105 intptr_t free_head_;
106
107 ObjectPtr* table_;
108 // When table_ grows and have to reallocated, keep the old one here
109 // so it will get freed when its are no longer in use.
111
112 // If non-null, it will specify the isolate this field table belongs to.
113 // Growing the field table will keep the cached field table on the isolate's
114 // mutator thread up-to-date.
115 Isolate* isolate_;
116
117 // Whether this field table is ready to use by e.g. registering new static
118 // fields.
119 bool is_ready_to_use_ = false;
120
122};
123
124} // namespace dart
125
126#endif // RUNTIME_VM_FIELD_TABLE_H_
SI void store(P *ptr, const T &val)
SI T load(const P *ptr)
static constexpr int kInitialCapacity
Definition field_table.h:94
void Free(intptr_t index)
void SetAt(intptr_t index, ObjectPtr raw_instance, bool concurrent_use=false)
Definition field_table.h:75
ObjectPtr At(intptr_t index, bool concurrent_use=false) const
Definition field_table.h:61
void AllocateIndex(intptr_t index)
bool IsReadyToUse() const
ObjectPtr * table()
Definition field_table.h:42
intptr_t NumFieldIds() const
Definition field_table.h:39
FieldTable * Clone(Isolate *for_isolate)
bool IsValidIndex(intptr_t index) const
Definition field_table.h:49
static constexpr int kCapacityIncrement
Definition field_table.h:95
static intptr_t FieldOffsetFor(intptr_t field_id)
FieldTable(Isolate *isolate)
Definition field_table.h:25
intptr_t Capacity() const
Definition field_table.h:40
void VisitObjectPointers(ObjectPointerVisitor *visitor)
#define ASSERT(E)
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition globals.h:581