content/base/src/nsPropertyTable.cpp

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

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

mercurial