Flutter Engine
The Flutter Engine
Public Member Functions | Static Public Member Functions | Static Public Attributes | List of all members
dart::WeakTable Class Reference

#include <weak_table.h>

Public Member Functions

 WeakTable ()
 
 WeakTable (intptr_t size)
 
 ~WeakTable ()
 
intptr_t size () const
 
intptr_t used () const
 
intptr_t count () const
 
intptr_t GetValue (ObjectPtr key)
 
void SetValue (ObjectPtr key, intptr_t val)
 
intptr_t SetValueIfNonExistent (ObjectPtr key, intptr_t val)
 
bool IsValidEntryAtExclusive (intptr_t i) const
 
void InvalidateAtExclusive (intptr_t i)
 
ObjectPtr ObjectAtExclusive (intptr_t i) const
 
intptr_t ValueAtExclusive (intptr_t i) const
 
void SetValueExclusive (ObjectPtr key, intptr_t val)
 
bool MarkValueExclusive (ObjectPtr key, intptr_t val)
 
intptr_t GetValueExclusive (ObjectPtr key) const
 
intptr_t RemoveValueExclusive (ObjectPtr key)
 
void Forward (ObjectPointerVisitor *visitor)
 
void ReportSurvivingAllocations (Dart_HeapSamplingReportCallback callback, void *context)
 
void CleanupValues (Dart_HeapSamplingDeleteCallback cleanup)
 
void Reset ()
 

Static Public Member Functions

static WeakTableNewFrom (WeakTable *original)
 

Static Public Attributes

static constexpr intptr_t kNoValue = 0
 

Detailed Description

Definition at line 16 of file weak_table.h.

Constructor & Destructor Documentation

◆ WeakTable() [1/2]

dart::WeakTable::WeakTable ( )
inline

Definition at line 20 of file weak_table.h.

20: WeakTable(kMinSize) {}

◆ WeakTable() [2/2]

dart::WeakTable::WeakTable ( intptr_t  size)
inlineexplicit

Definition at line 21 of file weak_table.h.

21 : used_(0), count_(0) {
22 ASSERT(size >= 0);
23 ASSERT(Utils::IsPowerOfTwo(kMinSize));
24 if (size < kMinSize) {
25 size = kMinSize;
26 }
27 // Get a max size that avoids overflows.
28 const intptr_t kMaxSize =
29 (kIntptrOne << (kBitsPerWord - 2)) / (kEntrySize * kWordSize);
30 ASSERT(Utils::IsPowerOfTwo(kMaxSize));
31 if (size > kMaxSize) {
32 size = kMaxSize;
33 }
34 size_ = size;
36 data_ = reinterpret_cast<intptr_t*>(malloc(size_ * kEntrySize * kWordSize));
37 for (intptr_t i = 0; i < size_; i++) {
38 data_[ObjectIndex(i)] = kNoEntry;
39 data_[ValueIndex(i)] = kNoValue;
40 }
41 }
static constexpr bool IsPowerOfTwo(T x)
Definition: utils.h:76
static constexpr intptr_t kNoValue
Definition: weak_table.h:18
intptr_t size() const
Definition: weak_table.h:49
#define ASSERT(E)
constexpr intptr_t kBitsPerWord
Definition: globals.h:514
void * malloc(size_t size)
Definition: allocation.cc:19
constexpr intptr_t kIntptrOne
Definition: globals.h:555
constexpr intptr_t kWordSize
Definition: globals.h:509

◆ ~WeakTable()

dart::WeakTable::~WeakTable ( )
inline

Definition at line 43 of file weak_table.h.

43{ free(data_); }

Member Function Documentation

◆ CleanupValues()

void dart::WeakTable::CleanupValues ( Dart_HeapSamplingDeleteCallback  cleanup)

Definition at line 156 of file weak_table.cc.

156 {
157 for (intptr_t i = 0; i < size_; i++) {
159 cleanup(reinterpret_cast<void*>(data_[ValueIndex(i)]));
160 }
161 }
162}
bool IsValidEntryAtExclusive(intptr_t i) const
Definition: weak_table.h:80

◆ count()

intptr_t dart::WeakTable::count ( ) const
inline

Definition at line 51 of file weak_table.h.

51{ return count_; }

◆ Forward()

void dart::WeakTable::Forward ( ObjectPointerVisitor visitor)

Definition at line 131 of file weak_table.cc.

131 {
132 if (used_ == 0) return;
133
134 for (intptr_t i = 0; i < size_; i++) {
136 visitor->VisitPointer(ObjectPointerAt(i));
137 }
138 }
139
140 Rehash();
141}

◆ GetValue()

intptr_t dart::WeakTable::GetValue ( ObjectPtr  key)
inline

Definition at line 55 of file weak_table.h.

55 {
56 MutexLocker ml(&mutex_);
57 return GetValueExclusive(key);
58 }
intptr_t GetValueExclusive(ObjectPtr key) const
Definition: weak_table.h:109

◆ GetValueExclusive()

intptr_t dart::WeakTable::GetValueExclusive ( ObjectPtr  key) const
inline

Definition at line 109 of file weak_table.h.

109 {
110 intptr_t mask = size() - 1;
111 intptr_t idx = Hash(key) & mask;
112 ObjectPtr obj = ObjectAtExclusive(idx);
113 while (obj != static_cast<ObjectPtr>(kNoEntry)) {
114 if (obj == key) {
115 return ValueAtExclusive(idx);
116 }
117 idx = (idx + 1) & mask;
118 obj = ObjectAtExclusive(idx);
119 }
120 ASSERT(ValueAtExclusive(idx) == 0);
121 return kNoValue;
122 }
intptr_t ValueAtExclusive(intptr_t i) const
Definition: weak_table.h:100
ObjectPtr ObjectAtExclusive(intptr_t i) const
Definition: weak_table.h:94

◆ InvalidateAtExclusive()

void dart::WeakTable::InvalidateAtExclusive ( intptr_t  i)
inline

Definition at line 89 of file weak_table.h.

89 {
91 SetValueAt(i, 0);
92 }

◆ IsValidEntryAtExclusive()

bool dart::WeakTable::IsValidEntryAtExclusive ( intptr_t  i) const
inline

Definition at line 80 of file weak_table.h.

80 {
81 ASSERT((ValueAtExclusive(i) == 0 &&
82 (data_[ObjectIndex(i)] == kNoEntry ||
83 data_[ObjectIndex(i)] == kDeletedEntry)) ||
84 (ValueAtExclusive(i) != 0 && data_[ObjectIndex(i)] != kNoEntry &&
85 data_[ObjectIndex(i)] != kDeletedEntry));
86 return (data_[ValueIndex(i)] != 0);
87 }

◆ MarkValueExclusive()

bool dart::WeakTable::MarkValueExclusive ( ObjectPtr  key,
intptr_t  val 
)

Definition at line 78 of file weak_table.cc.

78 {
79 const intptr_t mask = size() - 1;
80 intptr_t idx = Hash(key) & mask;
81 intptr_t empty_idx = -1;
82 ObjectPtr obj = ObjectAtExclusive(idx);
83
84 while (obj != static_cast<ObjectPtr>(kNoEntry)) {
85 if (obj == key) {
86 return false;
87 } else if ((empty_idx < 0) &&
88 (static_cast<intptr_t>(obj) == kDeletedEntry)) {
89 empty_idx = idx; // Insert at this location if not found.
90 }
91 idx = (idx + 1) & mask;
92 obj = ObjectAtExclusive(idx);
93 }
94
95 ASSERT(val != 0);
96
97 if (empty_idx >= 0) {
98 // We will be reusing a slot below.
99 set_used(used() - 1);
100 idx = empty_idx;
101 }
102
104 // Set the key and value.
105 SetObjectAt(idx, key);
106 SetValueAt(idx, val);
107 // Update the counts.
108 set_used(used() + 1);
109 set_count(count() + 1);
110
111 // Rehash if needed to ensure that there are empty slots available.
112 if (used_ >= limit()) {
113 Rehash();
114 }
115 return true;
116}
intptr_t used() const
Definition: weak_table.h:50
intptr_t count() const
Definition: weak_table.h:51

◆ NewFrom()

static WeakTable * dart::WeakTable::NewFrom ( WeakTable original)
inlinestatic

Definition at line 45 of file weak_table.h.

45 {
46 return new WeakTable(SizeFor(original->count(), original->size()));
47 }

◆ ObjectAtExclusive()

ObjectPtr dart::WeakTable::ObjectAtExclusive ( intptr_t  i) const
inline

Definition at line 94 of file weak_table.h.

94 {
95 ASSERT(i >= 0);
96 ASSERT(i < size());
97 return static_cast<ObjectPtr>(data_[ObjectIndex(i)]);
98 }

◆ RemoveValueExclusive()

intptr_t dart::WeakTable::RemoveValueExclusive ( ObjectPtr  key)
inline

Definition at line 126 of file weak_table.h.

126 {
127 intptr_t mask = size() - 1;
128 intptr_t idx = Hash(key) & mask;
129 ObjectPtr obj = ObjectAtExclusive(idx);
130 while (obj != static_cast<ObjectPtr>(kNoEntry)) {
131 if (obj == key) {
132 intptr_t result = ValueAtExclusive(idx);
134 return result;
135 }
136 idx = (idx + 1) & mask;
137 obj = ObjectAtExclusive(idx);
138 }
139 ASSERT(ValueAtExclusive(idx) == 0);
140 return kNoValue;
141 }
void InvalidateAtExclusive(intptr_t i)
Definition: weak_table.h:89
GAsyncResult * result

◆ ReportSurvivingAllocations()

void dart::WeakTable::ReportSurvivingAllocations ( Dart_HeapSamplingReportCallback  callback,
void *  context 
)

Definition at line 144 of file weak_table.cc.

146 {
147 MutexLocker ml(&mutex_);
148 for (intptr_t i = 0; i < size_; i++) {
150 void* data = reinterpret_cast<void*>(data_[ValueIndex(i)]);
151 callback(context, data);
152 }
153 }
154}
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
static int8_t data[kExtLength]

◆ Reset()

void dart::WeakTable::Reset ( )

Definition at line 118 of file weak_table.cc.

118 {
119 intptr_t* old_data = data_;
120 used_ = 0;
121 count_ = 0;
122 size_ = kMinSize;
123 free(old_data);
124 data_ = reinterpret_cast<intptr_t*>(malloc(size_ * kEntrySize * kWordSize));
125 for (intptr_t i = 0; i < size_; i++) {
126 data_[ObjectIndex(i)] = kNoEntry;
127 data_[ValueIndex(i)] = kNoValue;
128 }
129}

◆ SetValue()

void dart::WeakTable::SetValue ( ObjectPtr  key,
intptr_t  val 
)
inline

Definition at line 60 of file weak_table.h.

60 {
61 MutexLocker ml(&mutex_);
62 return SetValueExclusive(key, val);
63 }
void SetValueExclusive(ObjectPtr key, intptr_t val)
Definition: weak_table.cc:33

◆ SetValueExclusive()

void dart::WeakTable::SetValueExclusive ( ObjectPtr  key,
intptr_t  val 
)

Definition at line 33 of file weak_table.cc.

33 {
34 const intptr_t mask = size() - 1;
35 intptr_t idx = Hash(key) & mask;
36 intptr_t empty_idx = -1;
37 ObjectPtr obj = ObjectAtExclusive(idx);
38
39 while (obj != static_cast<ObjectPtr>(kNoEntry)) {
40 if (obj == key) {
41 SetValueAt(idx, val);
42 return;
43 } else if ((empty_idx < 0) &&
44 (static_cast<intptr_t>(obj) == kDeletedEntry)) {
45 empty_idx = idx; // Insert at this location if not found.
46 }
47 idx = (idx + 1) & mask;
48 obj = ObjectAtExclusive(idx);
49 }
50
51 if (val == 0) {
52 // Do not enter an invalid value. Associating 0 with a key deletes it from
53 // this weak table above in SetValueAt. If the key was not present in the
54 // weak table we are done.
55 return;
56 }
57
58 if (empty_idx >= 0) {
59 // We will be reusing a slot below.
60 set_used(used() - 1);
61 idx = empty_idx;
62 }
63
65 // Set the key and value.
66 SetObjectAt(idx, key);
67 SetValueAt(idx, val);
68 // Update the counts.
69 set_used(used() + 1);
70 set_count(count() + 1);
71
72 // Rehash if needed to ensure that there are empty slots available.
73 if (used_ >= limit()) {
74 Rehash();
75 }
76}

◆ SetValueIfNonExistent()

intptr_t dart::WeakTable::SetValueIfNonExistent ( ObjectPtr  key,
intptr_t  val 
)
inline

Definition at line 65 of file weak_table.h.

65 {
66 MutexLocker ml(&mutex_);
67 const auto old_value = GetValueExclusive(key);
68 if (old_value == kNoValue) {
70 return val;
71 }
72 return old_value;
73 }

◆ size()

intptr_t dart::WeakTable::size ( ) const
inline

Definition at line 49 of file weak_table.h.

49{ return size_; }

◆ used()

intptr_t dart::WeakTable::used ( ) const
inline

Definition at line 50 of file weak_table.h.

50{ return used_; }

◆ ValueAtExclusive()

intptr_t dart::WeakTable::ValueAtExclusive ( intptr_t  i) const
inline

Definition at line 100 of file weak_table.h.

100 {
101 ASSERT(i >= 0);
102 ASSERT(i < size());
103 return data_[ValueIndex(i)];
104 }

Member Data Documentation

◆ kNoValue

constexpr intptr_t dart::WeakTable::kNoValue = 0
staticconstexpr

Definition at line 18 of file weak_table.h.


The documentation for this class was generated from the following files: