2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-21 15:12:37 +04:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
2017-08-17 00:10:56 +03:00
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2004-08-24 22:50:29 +04:00
|
|
|
|
2006-03-31 12:00:42 +04:00
|
|
|
/**
|
|
|
|
* nsPropertyTable allows a set of arbitrary key/value pairs to be stored
|
|
|
|
* for any number of nodes, in a global hashtable rather than on the nodes
|
|
|
|
* themselves. Nodes can be any type of object; the hashtable keys are
|
2017-10-03 01:05:19 +03:00
|
|
|
* nsAtom pointers, and the values are void pointers.
|
2006-03-31 12:00:42 +04:00
|
|
|
*/
|
|
|
|
|
2004-08-24 22:50:29 +04:00
|
|
|
#include "nsPropertyTable.h"
|
2013-06-30 20:26:39 +04:00
|
|
|
|
|
|
|
#include "mozilla/MemoryReporting.h"
|
|
|
|
|
2015-09-16 06:49:53 +03:00
|
|
|
#include "PLDHashTable.h"
|
2012-07-27 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
2017-10-03 01:05:19 +03:00
|
|
|
#include "nsAtom.h"
|
2004-08-24 22:50:29 +04:00
|
|
|
|
|
|
|
struct PropertyListMapEntry : public PLDHashEntryHdr {
|
|
|
|
const void* key;
|
|
|
|
void* value;
|
|
|
|
};
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2005-10-27 18:09:23 +04:00
|
|
|
class nsPropertyTable::PropertyList {
|
|
|
|
public:
|
2005-01-21 20:00:07 +03:00
|
|
|
PropertyList(nsAtom* aName, NSPropertyDtorFunc aDtorFunc, void* aDtorData,
|
2014-06-02 16:08:24 +04:00
|
|
|
bool aTransfer);
|
|
|
|
~PropertyList();
|
2004-08-24 22:50:29 +04:00
|
|
|
|
|
|
|
// Removes the property associated with the given object, and destroys
|
|
|
|
// the property value
|
2014-06-02 16:08:21 +04:00
|
|
|
bool DeletePropertyFor(nsPropertyOwner aObject);
|
2004-08-24 22:50:29 +04:00
|
|
|
|
|
|
|
// Destroy all remaining properties (without removing them)
|
2014-06-02 16:08:21 +04:00
|
|
|
void Destroy();
|
2005-10-27 18:09:23 +04:00
|
|
|
|
2018-11-13 15:48:21 +03:00
|
|
|
bool Equals(const nsAtom* aPropertyName) { return mName == aPropertyName; }
|
2005-10-27 18:09:23 +04:00
|
|
|
|
2013-06-23 16:03:39 +04:00
|
|
|
size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf);
|
2012-06-15 08:10:08 +04:00
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
RefPtr<nsAtom> mName; // property name
|
2015-05-20 02:46:17 +03:00
|
|
|
PLDHashTable mObjectValueMap; // map of object/value pairs
|
2005-10-27 18:09:23 +04:00
|
|
|
NSPropertyDtorFunc mDtorFunc; // property specific value dtor function
|
2006-09-16 21:15:38 +04:00
|
|
|
void* mDtorData; // pointer to pass to dtor
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mTransfer; // whether to transfer in
|
2006-09-16 21:15:38 +04:00
|
|
|
// TransferOrDeleteAllPropertiesFor
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2005-10-27 18:09:23 +04:00
|
|
|
PropertyList* mNext;
|
2004-08-24 22:50:29 +04:00
|
|
|
};
|
|
|
|
|
2005-12-06 07:53:03 +03:00
|
|
|
void nsPropertyTable::DeleteAllProperties() {
|
|
|
|
while (mPropertyList) {
|
|
|
|
PropertyList* tmp = mPropertyList;
|
2004-08-24 22:50:29 +04:00
|
|
|
|
2005-12-06 07:53:03 +03:00
|
|
|
mPropertyList = mPropertyList->mNext;
|
|
|
|
tmp->Destroy();
|
|
|
|
delete tmp;
|
2004-08-24 22:50:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-01-27 02:40:49 +03:00
|
|
|
void nsPropertyTable::DeleteAllPropertiesFor(nsPropertyOwner aObject) {
|
2004-08-24 22:50:29 +04:00
|
|
|
for (PropertyList* prop = mPropertyList; prop; prop = prop->mNext) {
|
|
|
|
prop->DeletePropertyFor(aObject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-16 21:15:38 +04:00
|
|
|
nsresult nsPropertyTable::TransferOrDeleteAllPropertiesFor(
|
2018-03-17 07:30:49 +03:00
|
|
|
nsPropertyOwner aObject, nsPropertyTable& aOtherTable) {
|
2006-09-16 21:15:38 +04:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
for (PropertyList* prop = mPropertyList; prop; prop = prop->mNext) {
|
|
|
|
if (prop->mTransfer) {
|
2015-05-21 10:34:25 +03:00
|
|
|
auto entry = static_cast<PropertyListMapEntry*>(
|
|
|
|
prop->mObjectValueMap.Search(aObject));
|
2015-01-23 08:06:55 +03:00
|
|
|
if (entry) {
|
2018-03-17 07:30:49 +03:00
|
|
|
rv = aOtherTable.SetProperty(aObject, prop->mName, entry->value,
|
|
|
|
prop->mDtorFunc, prop->mDtorData,
|
|
|
|
prop->mTransfer);
|
2006-09-16 21:15:38 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
DeleteAllPropertiesFor(aObject);
|
2018-03-17 07:30:49 +03:00
|
|
|
aOtherTable.DeleteAllPropertiesFor(aObject);
|
2006-09-16 21:15:38 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-09-08 05:20:16 +03:00
|
|
|
prop->mObjectValueMap.RemoveEntry(entry);
|
2006-09-16 21:15:38 +04:00
|
|
|
}
|
2018-03-17 07:30:49 +03:00
|
|
|
} else {
|
2006-09-16 21:15:38 +04:00
|
|
|
prop->DeletePropertyFor(aObject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2010-04-23 06:41:38 +04:00
|
|
|
void nsPropertyTable::Enumerate(nsPropertyOwner aObject,
|
2005-10-27 18:09:23 +04:00
|
|
|
NSPropertyFunc aCallback, void* aData) {
|
|
|
|
PropertyList* prop;
|
|
|
|
for (prop = mPropertyList; prop; prop = prop->mNext) {
|
2015-05-21 10:34:25 +03:00
|
|
|
auto entry = static_cast<PropertyListMapEntry*>(
|
|
|
|
prop->mObjectValueMap.Search(aObject));
|
2015-01-23 08:06:55 +03:00
|
|
|
if (entry) {
|
2010-04-23 06:41:38 +04:00
|
|
|
aCallback(const_cast<void*>(aObject.get()), prop->mName, entry->value,
|
2015-01-23 08:06:55 +03:00
|
|
|
aData);
|
2005-10-27 18:09:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-15 23:02:06 +04:00
|
|
|
void nsPropertyTable::EnumerateAll(NSPropertyFunc aCallBack, void* aData) {
|
|
|
|
for (PropertyList* prop = mPropertyList; prop; prop = prop->mNext) {
|
2015-06-12 07:19:53 +03:00
|
|
|
for (auto iter = prop->mObjectValueMap.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
auto entry = static_cast<PropertyListMapEntry*>(iter.Get());
|
2015-06-09 02:01:23 +03:00
|
|
|
aCallBack(const_cast<void*>(entry->key), prop->mName, entry->value,
|
|
|
|
aData);
|
|
|
|
}
|
2012-01-15 23:02:06 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-01-27 02:40:49 +03:00
|
|
|
void* nsPropertyTable::GetPropertyInternal(nsPropertyOwner aObject,
|
2018-11-13 15:48:21 +03:00
|
|
|
const nsAtom* aPropertyName,
|
2018-03-17 07:30:49 +03:00
|
|
|
bool aRemove, nsresult* aResult) {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(aPropertyName && aObject, "unexpected null param");
|
2004-08-24 22:50:29 +04:00
|
|
|
nsresult rv = NS_PROPTABLE_PROP_NOT_THERE;
|
2012-07-30 18:20:58 +04:00
|
|
|
void* propValue = nullptr;
|
2004-08-24 22:50:29 +04:00
|
|
|
|
2010-04-23 06:41:38 +04:00
|
|
|
PropertyList* propertyList = GetPropertyListFor(aPropertyName);
|
2004-08-24 22:50:29 +04:00
|
|
|
if (propertyList) {
|
2015-05-21 10:34:25 +03:00
|
|
|
auto entry = static_cast<PropertyListMapEntry*>(
|
|
|
|
propertyList->mObjectValueMap.Search(aObject));
|
2015-01-23 08:06:55 +03:00
|
|
|
if (entry) {
|
2004-08-24 22:50:29 +04:00
|
|
|
propValue = entry->value;
|
|
|
|
if (aRemove) {
|
|
|
|
// don't call propertyList->mDtorFunc. That's the caller's job now.
|
2015-09-08 05:20:16 +03:00
|
|
|
propertyList->mObjectValueMap.RemoveEntry(entry);
|
2004-08-24 22:50:29 +04:00
|
|
|
}
|
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aResult) *aResult = rv;
|
|
|
|
|
|
|
|
return propValue;
|
|
|
|
}
|
|
|
|
|
2018-03-17 07:30:49 +03:00
|
|
|
nsresult nsPropertyTable::SetPropertyInternal(
|
|
|
|
nsPropertyOwner aObject, nsAtom* aPropertyName, void* aPropertyValue,
|
|
|
|
NSPropertyDtorFunc aPropDtorFunc, void* aPropDtorData, bool aTransfer) {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(aPropertyName && aObject, "unexpected null param");
|
2004-08-24 22:50:29 +04:00
|
|
|
|
2010-04-23 06:41:38 +04:00
|
|
|
PropertyList* propertyList = GetPropertyListFor(aPropertyName);
|
2004-08-24 22:50:29 +04:00
|
|
|
|
|
|
|
if (propertyList) {
|
2006-09-16 21:15:38 +04:00
|
|
|
// Make sure the dtor function and data and the transfer flag match
|
2005-01-21 20:00:07 +03:00
|
|
|
if (aPropDtorFunc != propertyList->mDtorFunc ||
|
2006-09-16 21:15:38 +04:00
|
|
|
aPropDtorData != propertyList->mDtorData ||
|
|
|
|
aTransfer != propertyList->mTransfer) {
|
2006-09-20 01:39:33 +04:00
|
|
|
NS_WARNING("Destructor/data mismatch while setting property");
|
2004-08-24 22:50:29 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2010-04-23 06:41:38 +04:00
|
|
|
propertyList = new PropertyList(aPropertyName, aPropDtorFunc, aPropDtorData,
|
2006-09-16 21:15:38 +04:00
|
|
|
aTransfer);
|
2004-08-24 22:50:29 +04:00
|
|
|
propertyList->mNext = mPropertyList;
|
|
|
|
mPropertyList = propertyList;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The current property value (if there is one) is replaced and the current
|
|
|
|
// value is destroyed
|
|
|
|
nsresult result = NS_OK;
|
2015-09-15 00:23:12 +03:00
|
|
|
auto entry = static_cast<PropertyListMapEntry*>(
|
|
|
|
propertyList->mObjectValueMap.Add(aObject, mozilla::fallible));
|
2004-08-24 22:50:29 +04:00
|
|
|
if (!entry) return NS_ERROR_OUT_OF_MEMORY;
|
2013-04-03 05:14:24 +04:00
|
|
|
// A nullptr entry->key is the sign that the entry has just been allocated
|
|
|
|
// for us. If it's non-nullptr then we have an existing entry.
|
2005-10-27 18:09:23 +04:00
|
|
|
if (entry->key) {
|
2018-03-17 07:30:49 +03:00
|
|
|
if (propertyList->mDtorFunc) {
|
2007-07-08 11:08:04 +04:00
|
|
|
propertyList->mDtorFunc(const_cast<void*>(entry->key), aPropertyName,
|
2006-09-16 21:15:38 +04:00
|
|
|
entry->value, propertyList->mDtorData);
|
2018-03-17 07:30:49 +03:00
|
|
|
}
|
2004-08-24 22:50:29 +04:00
|
|
|
result = NS_PROPTABLE_PROP_OVERWRITTEN;
|
|
|
|
}
|
|
|
|
entry->key = aObject;
|
|
|
|
entry->value = aPropertyValue;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2006-01-27 02:40:49 +03:00
|
|
|
nsresult nsPropertyTable::DeleteProperty(nsPropertyOwner aObject,
|
2018-11-13 15:48:21 +03:00
|
|
|
const nsAtom* aPropertyName) {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(aPropertyName && aObject, "unexpected null param");
|
2004-08-24 22:50:29 +04:00
|
|
|
|
2010-04-23 06:41:38 +04:00
|
|
|
PropertyList* propertyList = GetPropertyListFor(aPropertyName);
|
2004-08-24 22:50:29 +04:00
|
|
|
if (propertyList) {
|
|
|
|
if (propertyList->DeletePropertyFor(aObject)) return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_PROPTABLE_PROP_NOT_THERE;
|
|
|
|
}
|
|
|
|
|
2018-11-13 15:48:21 +03:00
|
|
|
nsPropertyTable::PropertyList* nsPropertyTable::GetPropertyListFor(
|
|
|
|
const nsAtom* aPropertyName) const {
|
2004-08-24 22:50:29 +04:00
|
|
|
PropertyList* result;
|
|
|
|
|
|
|
|
for (result = mPropertyList; result; result = result->mNext) {
|
2010-04-23 06:41:38 +04:00
|
|
|
if (result->Equals(aPropertyName)) {
|
2004-08-24 22:50:29 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2015-01-14 03:40:00 +03:00
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
nsPropertyTable::PropertyList::PropertyList(nsAtom* aName,
|
2005-01-21 20:00:07 +03:00
|
|
|
NSPropertyDtorFunc aDtorFunc,
|
2011-09-29 10:19:26 +04:00
|
|
|
void* aDtorData, bool aTransfer)
|
2005-10-27 18:09:23 +04:00
|
|
|
: mName(aName),
|
2015-09-15 00:23:47 +03:00
|
|
|
mObjectValueMap(PLDHashTable::StubOps(), sizeof(PropertyListMapEntry)),
|
2005-10-27 18:09:23 +04:00
|
|
|
mDtorFunc(aDtorFunc),
|
2006-09-16 21:15:38 +04:00
|
|
|
mDtorData(aDtorData),
|
|
|
|
mTransfer(aTransfer),
|
2012-07-30 18:20:58 +04:00
|
|
|
mNext(nullptr) {}
|
2004-08-24 22:50:29 +04:00
|
|
|
|
|
|
|
nsPropertyTable::PropertyList::~PropertyList() {}
|
|
|
|
|
|
|
|
void nsPropertyTable::PropertyList::Destroy() {
|
2015-06-09 02:01:23 +03:00
|
|
|
// Enumerate any remaining object/value pairs and destroy the value object.
|
|
|
|
if (mDtorFunc) {
|
2015-06-12 07:19:53 +03:00
|
|
|
for (auto iter = mObjectValueMap.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
auto entry = static_cast<PropertyListMapEntry*>(iter.Get());
|
2015-06-09 02:01:23 +03:00
|
|
|
mDtorFunc(const_cast<void*>(entry->key), mName, entry->value, mDtorData);
|
|
|
|
}
|
|
|
|
}
|
2004-08-24 22:50:29 +04:00
|
|
|
}
|
|
|
|
|
2006-01-27 02:40:49 +03:00
|
|
|
bool nsPropertyTable::PropertyList::DeletePropertyFor(nsPropertyOwner aObject) {
|
2015-05-21 10:34:25 +03:00
|
|
|
auto entry =
|
|
|
|
static_cast<PropertyListMapEntry*>(mObjectValueMap.Search(aObject));
|
2011-10-17 18:59:28 +04:00
|
|
|
if (!entry) return false;
|
2004-08-24 22:50:29 +04:00
|
|
|
|
2006-09-25 01:13:36 +04:00
|
|
|
void* value = entry->value;
|
2015-09-08 05:20:16 +03:00
|
|
|
mObjectValueMap.RemoveEntry(entry);
|
2004-08-24 22:50:29 +04:00
|
|
|
|
2006-09-25 01:13:36 +04:00
|
|
|
if (mDtorFunc)
|
2007-07-08 11:08:04 +04:00
|
|
|
mDtorFunc(const_cast<void*>(aObject.get()), mName, value, mDtorData);
|
2006-09-25 01:13:36 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2004-08-24 22:50:29 +04:00
|
|
|
}
|
2006-02-01 07:43:55 +03:00
|
|
|
|
2013-06-23 16:03:39 +04:00
|
|
|
size_t nsPropertyTable::PropertyList::SizeOfIncludingThis(
|
|
|
|
mozilla::MallocSizeOf aMallocSizeOf) {
|
2012-06-15 08:10:08 +04:00
|
|
|
size_t n = aMallocSizeOf(this);
|
2015-07-30 08:28:20 +03:00
|
|
|
n += mObjectValueMap.ShallowSizeOfExcludingThis(aMallocSizeOf);
|
2012-06-15 08:10:08 +04:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2013-06-23 16:03:39 +04:00
|
|
|
size_t nsPropertyTable::SizeOfExcludingThis(
|
|
|
|
mozilla::MallocSizeOf aMallocSizeOf) const {
|
2012-06-15 08:10:08 +04:00
|
|
|
size_t n = 0;
|
|
|
|
|
|
|
|
for (PropertyList* prop = mPropertyList; prop; prop = prop->mNext) {
|
|
|
|
n += prop->SizeOfIncludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2014-05-01 21:37:54 +04:00
|
|
|
size_t nsPropertyTable::SizeOfIncludingThis(
|
|
|
|
mozilla::MallocSizeOf aMallocSizeOf) const {
|
|
|
|
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2006-02-01 07:43:55 +03:00
|
|
|
/* static */
|
2017-10-03 01:05:19 +03:00
|
|
|
void nsPropertyTable::SupportsDtorFunc(void* aObject, nsAtom* aPropertyName,
|
2006-02-01 07:43:55 +03:00
|
|
|
void* aPropertyValue, void* aData) {
|
2007-07-08 11:08:04 +04:00
|
|
|
nsISupports* propertyValue = static_cast<nsISupports*>(aPropertyValue);
|
2006-02-01 07:43:55 +03:00
|
|
|
NS_IF_RELEASE(propertyValue);
|
|
|
|
}
|