/src/mozilla-central/dom/base/nsPropertyTable.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | /** |
8 | | * nsPropertyTable allows a set of arbitrary key/value pairs to be stored |
9 | | * for any number of nodes, in a global hashtable rather than on the nodes |
10 | | * themselves. Nodes can be any type of object; the hashtable keys are |
11 | | * nsAtom pointers, and the values are void pointers. |
12 | | */ |
13 | | |
14 | | #include "nsPropertyTable.h" |
15 | | |
16 | | #include "mozilla/MemoryReporting.h" |
17 | | |
18 | | #include "PLDHashTable.h" |
19 | | #include "nsError.h" |
20 | | #include "nsAtom.h" |
21 | | |
22 | | struct PropertyListMapEntry : public PLDHashEntryHdr { |
23 | | const void *key; |
24 | | void *value; |
25 | | }; |
26 | | |
27 | | //---------------------------------------------------------------------- |
28 | | |
29 | | class nsPropertyTable::PropertyList { |
30 | | public: |
31 | | PropertyList(nsAtom* aName, |
32 | | NSPropertyDtorFunc aDtorFunc, |
33 | | void* aDtorData, |
34 | | bool aTransfer); |
35 | | ~PropertyList(); |
36 | | |
37 | | // Removes the property associated with the given object, and destroys |
38 | | // the property value |
39 | | bool DeletePropertyFor(nsPropertyOwner aObject); |
40 | | |
41 | | // Destroy all remaining properties (without removing them) |
42 | | void Destroy(); |
43 | | |
44 | | bool Equals(nsAtom *aPropertyName) |
45 | 0 | { |
46 | 0 | return mName == aPropertyName; |
47 | 0 | } |
48 | | |
49 | | size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf); |
50 | | |
51 | | RefPtr<nsAtom> mName; // property name |
52 | | PLDHashTable mObjectValueMap; // map of object/value pairs |
53 | | NSPropertyDtorFunc mDtorFunc; // property specific value dtor function |
54 | | void* mDtorData; // pointer to pass to dtor |
55 | | bool mTransfer; // whether to transfer in |
56 | | // TransferOrDeleteAllPropertiesFor |
57 | | |
58 | | PropertyList* mNext; |
59 | | }; |
60 | | |
61 | | void |
62 | | nsPropertyTable::DeleteAllProperties() |
63 | 0 | { |
64 | 0 | while (mPropertyList) { |
65 | 0 | PropertyList* tmp = mPropertyList; |
66 | 0 |
|
67 | 0 | mPropertyList = mPropertyList->mNext; |
68 | 0 | tmp->Destroy(); |
69 | 0 | delete tmp; |
70 | 0 | } |
71 | 0 | } |
72 | | |
73 | | void |
74 | | nsPropertyTable::DeleteAllPropertiesFor(nsPropertyOwner aObject) |
75 | 0 | { |
76 | 0 | for (PropertyList* prop = mPropertyList; prop; prop = prop->mNext) { |
77 | 0 | prop->DeletePropertyFor(aObject); |
78 | 0 | } |
79 | 0 | } |
80 | | |
81 | | nsresult |
82 | | nsPropertyTable::TransferOrDeleteAllPropertiesFor(nsPropertyOwner aObject, |
83 | | nsPropertyTable& aOtherTable) |
84 | 0 | { |
85 | 0 | nsresult rv = NS_OK; |
86 | 0 | for (PropertyList* prop = mPropertyList; prop; prop = prop->mNext) { |
87 | 0 | if (prop->mTransfer) { |
88 | 0 | auto entry = static_cast<PropertyListMapEntry*> |
89 | 0 | (prop->mObjectValueMap.Search(aObject)); |
90 | 0 | if (entry) { |
91 | 0 | rv = aOtherTable.SetProperty(aObject, prop->mName, |
92 | 0 | entry->value, prop->mDtorFunc, |
93 | 0 | prop->mDtorData, prop->mTransfer); |
94 | 0 | if (NS_FAILED(rv)) { |
95 | 0 | DeleteAllPropertiesFor(aObject); |
96 | 0 | aOtherTable.DeleteAllPropertiesFor(aObject); |
97 | 0 | break; |
98 | 0 | } |
99 | 0 | |
100 | 0 | prop->mObjectValueMap.RemoveEntry(entry); |
101 | 0 | } |
102 | 0 | } else { |
103 | 0 | prop->DeletePropertyFor(aObject); |
104 | 0 | } |
105 | 0 | } |
106 | 0 |
|
107 | 0 | return rv; |
108 | 0 | } |
109 | | |
110 | | void |
111 | | nsPropertyTable::Enumerate(nsPropertyOwner aObject, |
112 | | NSPropertyFunc aCallback, void *aData) |
113 | 0 | { |
114 | 0 | PropertyList* prop; |
115 | 0 | for (prop = mPropertyList; prop; prop = prop->mNext) { |
116 | 0 | auto entry = static_cast<PropertyListMapEntry*> |
117 | 0 | (prop->mObjectValueMap.Search(aObject)); |
118 | 0 | if (entry) { |
119 | 0 | aCallback(const_cast<void*>(aObject.get()), prop->mName, entry->value, |
120 | 0 | aData); |
121 | 0 | } |
122 | 0 | } |
123 | 0 | } |
124 | | |
125 | | void |
126 | | nsPropertyTable::EnumerateAll(NSPropertyFunc aCallBack, void* aData) |
127 | 0 | { |
128 | 0 | for (PropertyList* prop = mPropertyList; prop; prop = prop->mNext) { |
129 | 0 | for (auto iter = prop->mObjectValueMap.Iter(); !iter.Done(); iter.Next()) { |
130 | 0 | auto entry = static_cast<PropertyListMapEntry*>(iter.Get()); |
131 | 0 | aCallBack(const_cast<void*>(entry->key), prop->mName, entry->value, |
132 | 0 | aData); |
133 | 0 | } |
134 | 0 | } |
135 | 0 | } |
136 | | |
137 | | void* |
138 | | nsPropertyTable::GetPropertyInternal(nsPropertyOwner aObject, |
139 | | nsAtom* aPropertyName, |
140 | | bool aRemove, |
141 | | nsresult* aResult) |
142 | 0 | { |
143 | 0 | MOZ_ASSERT(aPropertyName && aObject, "unexpected null param"); |
144 | 0 | nsresult rv = NS_PROPTABLE_PROP_NOT_THERE; |
145 | 0 | void *propValue = nullptr; |
146 | 0 |
|
147 | 0 | PropertyList* propertyList = GetPropertyListFor(aPropertyName); |
148 | 0 | if (propertyList) { |
149 | 0 | auto entry = static_cast<PropertyListMapEntry*> |
150 | 0 | (propertyList->mObjectValueMap.Search(aObject)); |
151 | 0 | if (entry) { |
152 | 0 | propValue = entry->value; |
153 | 0 | if (aRemove) { |
154 | 0 | // don't call propertyList->mDtorFunc. That's the caller's job now. |
155 | 0 | propertyList->mObjectValueMap.RemoveEntry(entry); |
156 | 0 | } |
157 | 0 | rv = NS_OK; |
158 | 0 | } |
159 | 0 | } |
160 | 0 |
|
161 | 0 | if (aResult) |
162 | 0 | *aResult = rv; |
163 | 0 |
|
164 | 0 | return propValue; |
165 | 0 | } |
166 | | |
167 | | nsresult |
168 | | nsPropertyTable::SetPropertyInternal(nsPropertyOwner aObject, |
169 | | nsAtom* aPropertyName, |
170 | | void* aPropertyValue, |
171 | | NSPropertyDtorFunc aPropDtorFunc, |
172 | | void* aPropDtorData, |
173 | | bool aTransfer) |
174 | 0 | { |
175 | 0 | MOZ_ASSERT(aPropertyName && aObject, "unexpected null param"); |
176 | 0 |
|
177 | 0 | PropertyList* propertyList = GetPropertyListFor(aPropertyName); |
178 | 0 |
|
179 | 0 | if (propertyList) { |
180 | 0 | // Make sure the dtor function and data and the transfer flag match |
181 | 0 | if (aPropDtorFunc != propertyList->mDtorFunc || |
182 | 0 | aPropDtorData != propertyList->mDtorData || |
183 | 0 | aTransfer != propertyList->mTransfer) { |
184 | 0 | NS_WARNING("Destructor/data mismatch while setting property"); |
185 | 0 | return NS_ERROR_INVALID_ARG; |
186 | 0 | } |
187 | 0 |
|
188 | 0 | } else { |
189 | 0 | propertyList = new PropertyList(aPropertyName, aPropDtorFunc, |
190 | 0 | aPropDtorData, aTransfer); |
191 | 0 | propertyList->mNext = mPropertyList; |
192 | 0 | mPropertyList = propertyList; |
193 | 0 | } |
194 | 0 |
|
195 | 0 | // The current property value (if there is one) is replaced and the current |
196 | 0 | // value is destroyed |
197 | 0 | nsresult result = NS_OK; |
198 | 0 | auto entry = static_cast<PropertyListMapEntry*> |
199 | 0 | (propertyList->mObjectValueMap.Add(aObject, mozilla::fallible)); |
200 | 0 | if (!entry) |
201 | 0 | return NS_ERROR_OUT_OF_MEMORY; |
202 | 0 | // A nullptr entry->key is the sign that the entry has just been allocated |
203 | 0 | // for us. If it's non-nullptr then we have an existing entry. |
204 | 0 | if (entry->key) { |
205 | 0 | if (propertyList->mDtorFunc) { |
206 | 0 | propertyList->mDtorFunc(const_cast<void*>(entry->key), aPropertyName, |
207 | 0 | entry->value, propertyList->mDtorData); |
208 | 0 | } |
209 | 0 | result = NS_PROPTABLE_PROP_OVERWRITTEN; |
210 | 0 | } |
211 | 0 | entry->key = aObject; |
212 | 0 | entry->value = aPropertyValue; |
213 | 0 |
|
214 | 0 | return result; |
215 | 0 | } |
216 | | |
217 | | nsresult |
218 | | nsPropertyTable::DeleteProperty(nsPropertyOwner aObject, |
219 | | nsAtom* aPropertyName) |
220 | 0 | { |
221 | 0 | MOZ_ASSERT(aPropertyName && aObject, "unexpected null param"); |
222 | 0 |
|
223 | 0 | PropertyList* propertyList = GetPropertyListFor(aPropertyName); |
224 | 0 | if (propertyList) { |
225 | 0 | if (propertyList->DeletePropertyFor(aObject)) |
226 | 0 | return NS_OK; |
227 | 0 | } |
228 | 0 | |
229 | 0 | return NS_PROPTABLE_PROP_NOT_THERE; |
230 | 0 | } |
231 | | |
232 | | nsPropertyTable::PropertyList* |
233 | | nsPropertyTable::GetPropertyListFor(nsAtom* aPropertyName) const |
234 | 0 | { |
235 | 0 | PropertyList* result; |
236 | 0 |
|
237 | 0 | for (result = mPropertyList; result; result = result->mNext) { |
238 | 0 | if (result->Equals(aPropertyName)) { |
239 | 0 | break; |
240 | 0 | } |
241 | 0 | } |
242 | 0 |
|
243 | 0 | return result; |
244 | 0 | } |
245 | | |
246 | | //---------------------------------------------------------------------- |
247 | | |
248 | | nsPropertyTable::PropertyList::PropertyList(nsAtom *aName, |
249 | | NSPropertyDtorFunc aDtorFunc, |
250 | | void *aDtorData, |
251 | | bool aTransfer) |
252 | | : mName(aName), |
253 | | mObjectValueMap(PLDHashTable::StubOps(), sizeof(PropertyListMapEntry)), |
254 | | mDtorFunc(aDtorFunc), |
255 | | mDtorData(aDtorData), |
256 | | mTransfer(aTransfer), |
257 | | mNext(nullptr) |
258 | 0 | { |
259 | 0 | } |
260 | | |
261 | | nsPropertyTable::PropertyList::~PropertyList() |
262 | 0 | { |
263 | 0 | } |
264 | | |
265 | | void |
266 | | nsPropertyTable::PropertyList::Destroy() |
267 | 0 | { |
268 | 0 | // Enumerate any remaining object/value pairs and destroy the value object. |
269 | 0 | if (mDtorFunc) { |
270 | 0 | for (auto iter = mObjectValueMap.Iter(); !iter.Done(); iter.Next()) { |
271 | 0 | auto entry = static_cast<PropertyListMapEntry*>(iter.Get()); |
272 | 0 | mDtorFunc(const_cast<void*>(entry->key), mName, entry->value, mDtorData); |
273 | 0 | } |
274 | 0 | } |
275 | 0 | } |
276 | | |
277 | | bool |
278 | | nsPropertyTable::PropertyList::DeletePropertyFor(nsPropertyOwner aObject) |
279 | 0 | { |
280 | 0 | auto entry = |
281 | 0 | static_cast<PropertyListMapEntry*>(mObjectValueMap.Search(aObject)); |
282 | 0 | if (!entry) |
283 | 0 | return false; |
284 | 0 | |
285 | 0 | void* value = entry->value; |
286 | 0 | mObjectValueMap.RemoveEntry(entry); |
287 | 0 |
|
288 | 0 | if (mDtorFunc) |
289 | 0 | mDtorFunc(const_cast<void*>(aObject.get()), mName, value, mDtorData); |
290 | 0 |
|
291 | 0 | return true; |
292 | 0 | } |
293 | | |
294 | | size_t |
295 | | nsPropertyTable::PropertyList::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) |
296 | 0 | { |
297 | 0 | size_t n = aMallocSizeOf(this); |
298 | 0 | n += mObjectValueMap.ShallowSizeOfExcludingThis(aMallocSizeOf); |
299 | 0 | return n; |
300 | 0 | } |
301 | | |
302 | | size_t |
303 | | nsPropertyTable::SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const |
304 | 0 | { |
305 | 0 | size_t n = 0; |
306 | 0 |
|
307 | 0 | for (PropertyList *prop = mPropertyList; prop; prop = prop->mNext) { |
308 | 0 | n += prop->SizeOfIncludingThis(aMallocSizeOf); |
309 | 0 | } |
310 | 0 |
|
311 | 0 | return n; |
312 | 0 | } |
313 | | |
314 | | size_t |
315 | | nsPropertyTable::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const |
316 | 0 | { |
317 | 0 | return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf); |
318 | 0 | } |
319 | | |
320 | | /* static */ |
321 | | void |
322 | | nsPropertyTable::SupportsDtorFunc(void *aObject, nsAtom *aPropertyName, |
323 | | void *aPropertyValue, void *aData) |
324 | 0 | { |
325 | 0 | nsISupports *propertyValue = static_cast<nsISupports*>(aPropertyValue); |
326 | 0 | NS_IF_RELEASE(propertyValue); |
327 | 0 | } |