Loading...
libkern/c++/OSDictionary.cpp xnu-12377.101.15 xnu-6153.41.3
--- xnu/xnu-12377.101.15/libkern/c++/OSDictionary.cpp
+++ xnu/xnu-6153.41.3/libkern/c++/OSDictionary.cpp
@@ -29,21 +29,17 @@
 /* OSDictionary.cpp converted to C++ by gvdl on Fri 1998-10-30 */
 /* OSDictionary.cpp rewritten by gvdl on Fri 1998-10-30 */
 
-#define IOKIT_ENABLE_SHARED_PTR
-
+
+#include <libkern/c++/OSDictionary.h>
 #include <libkern/c++/OSArray.h>
+#include <libkern/c++/OSSymbol.h>
+#include <libkern/c++/OSSerialize.h>
+#include <libkern/c++/OSLib.h>
 #include <libkern/c++/OSCollectionIterator.h>
-#include <libkern/c++/OSDictionary.h>
-#include <libkern/c++/OSLib.h>
-#include <libkern/c++/OSSerialize.h>
-#include <libkern/c++/OSSharedPtr.h>
-#include <libkern/c++/OSSymbol.h>
-#include <os/cpp_util.h>
 
 #define super OSCollection
 
-OSDefineMetaClassAndStructorsWithZone(OSDictionary, OSCollection,
-    (zone_create_flags_t) (ZC_CACHING | ZC_ZFREE_CLEARMEM))
+OSDefineMetaClassAndStructors(OSDictionary, OSCollection)
 OSMetaClassDefineReservedUnused(OSDictionary, 0);
 OSMetaClassDefineReservedUnused(OSDictionary, 1);
 OSMetaClassDefineReservedUnused(OSDictionary, 2);
@@ -66,11 +62,11 @@
 	const OSDictionary::dictEntry *e1 = (const OSDictionary::dictEntry *)_e1;
 	const OSDictionary::dictEntry *e2 = (const OSDictionary::dictEntry *)_e2;
 
-	if ((uintptr_t)e1->key.get() == (uintptr_t)e2->key.get()) {
+	if ((uintptr_t)e1->key == (uintptr_t)e2->key) {
 		return 0;
 	}
 
-	return (uintptr_t)e1->key.get() > (uintptr_t)e2->key.get() ? 1 : -1;
+	return (uintptr_t)e1->key > (uintptr_t)e2->key ? 1 : -1;
 }
 
 void
@@ -91,14 +87,16 @@
 		return false;
 	}
 
+	unsigned int size = inCapacity * sizeof(dictEntry);
 //fOptions |= kSort;
 
-	dictionary = kallocp_type_container(dictEntry, &inCapacity, Z_WAITOK_ZERO);
+	dictionary = (dictEntry *) kalloc_container(size);
 	if (!dictionary) {
 		return false;
 	}
 
-	OSCONTAINER_ACCUMSIZE(inCapacity * sizeof(dictEntry));
+	bzero(dictionary, size);
+	OSCONTAINER_ACCUMSIZE(size);
 
 	count = 0;
 	capacity = inCapacity;
@@ -167,16 +165,19 @@
 	}
 
 	for (unsigned int i = 0; i < theCount; i++) {
-		OSSharedPtr<const OSSymbol> key = OSSymbol::withString(*keys++);
+		const OSSymbol *key = OSSymbol::withString(*keys++);
 		const OSMetaClassBase *newObject = *objects++;
 
 		if (!key) {
 			return false;
 		}
 
-		if (!newObject || !setObject(key.get(), newObject)) {
-			return false;
-		}
+		if (!newObject || !setObject(key, newObject)) {
+			key->release();
+			return false;
+		}
+
+		key->release();
 	}
 
 	return true;
@@ -207,9 +208,10 @@
 	}
 
 	count = dict->count;
+	bcopy(dict->dictionary, dictionary, count * sizeof(dictEntry));
 	for (unsigned int i = 0; i < count; i++) {
-		dictionary[i].key = dict->dictionary[i].key;
-		dictionary[i].value = dict->dictionary[i].value;
+		dictionary[i].key->taggedRetain(OSTypeID(OSCollection));
+		dictionary[i].value->taggedRetain(OSTypeID(OSCollection));
 	}
 
 	if ((kSort & fOptions) && !(kSort & dict->fOptions)) {
@@ -219,56 +221,60 @@
 	return true;
 }
 
-OSSharedPtr<OSDictionary>
+OSDictionary *
 OSDictionary::withCapacity(unsigned int capacity)
 {
-	OSSharedPtr<OSDictionary> me = OSMakeShared<OSDictionary>();
+	OSDictionary *me = new OSDictionary;
 
 	if (me && !me->initWithCapacity(capacity)) {
-		return nullptr;
+		me->release();
+		return NULL;
 	}
 
 	return me;
 }
 
-OSSharedPtr<OSDictionary>
+OSDictionary *
 OSDictionary::withObjects(const OSObject *objects[],
     const OSSymbol *keys[],
     unsigned int count,
     unsigned int capacity)
 {
-	OSSharedPtr<OSDictionary> me = OSMakeShared<OSDictionary>();
+	OSDictionary *me = new OSDictionary;
 
 	if (me && !me->initWithObjects(objects, keys, count, capacity)) {
-		return nullptr;
+		me->release();
+		return NULL;
 	}
 
 	return me;
 }
 
-OSSharedPtr<OSDictionary>
+OSDictionary *
 OSDictionary::withObjects(const OSObject *objects[],
     const OSString *keys[],
     unsigned int count,
     unsigned int capacity)
 {
-	OSSharedPtr<OSDictionary> me = OSMakeShared<OSDictionary>();
+	OSDictionary *me = new OSDictionary;
 
 	if (me && !me->initWithObjects(objects, keys, count, capacity)) {
-		return nullptr;
+		me->release();
+		return NULL;
 	}
 
 	return me;
 }
 
-OSSharedPtr<OSDictionary>
+OSDictionary *
 OSDictionary::withDictionary(const OSDictionary *dict,
     unsigned int capacity)
 {
-	OSSharedPtr<OSDictionary> me = OSMakeShared<OSDictionary>();
+	OSDictionary *me = new OSDictionary;
 
 	if (me && !me->initWithDictionary(dict, capacity)) {
-		return nullptr;
+		me->release();
+		return NULL;
 	}
 
 	return me;
@@ -280,7 +286,7 @@
 	(void) super::setOptions(0, kImmutable);
 	flushCollection();
 	if (dictionary) {
-		kfree_type(dictEntry, capacity, dictionary);
+		kfree(dictionary, capacity * sizeof(dictEntry));
 		OSCONTAINER_ACCUMSIZE( -(capacity * sizeof(dictEntry)));
 	}
 
@@ -317,6 +323,7 @@
 {
 	dictEntry *newDict;
 	unsigned int finalCapacity;
+	vm_size_t oldSize, newSize;
 
 	if (newCapacity <= capacity) {
 		return capacity;
@@ -327,14 +334,25 @@
 	    * capacityIncrement;
 
 	// integer overflow check
-	if (finalCapacity < newCapacity) {
+	if (finalCapacity < newCapacity || (finalCapacity > (UINT_MAX / sizeof(dictEntry)))) {
 		return capacity;
 	}
 
-	newDict = kreallocp_type_container(dictEntry, dictionary,
-	    capacity, &finalCapacity, Z_WAITOK_ZERO);
+	newSize = sizeof(dictEntry) * finalCapacity;
+
+	newDict = (dictEntry *) kallocp_container(&newSize);
 	if (newDict) {
-		OSCONTAINER_ACCUMSIZE(sizeof(dictEntry) * (finalCapacity - capacity));
+		// use all of the actual allocation size
+		finalCapacity = newSize / sizeof(dictEntry);
+
+		oldSize = sizeof(dictEntry) * capacity;
+
+		bcopy(dictionary, newDict, oldSize);
+		bzero(&newDict[capacity], newSize - oldSize);
+
+		OSCONTAINER_ACCUMSIZE(((size_t)newSize) - ((size_t)oldSize));
+		kfree(dictionary, oldSize);
+
 		dictionary = newDict;
 		capacity = finalCapacity;
 	}
@@ -348,8 +366,8 @@
 	haveUpdated();
 
 	for (unsigned int i = 0; i < count; i++) {
-		dictionary[i].key.reset();
-		dictionary[i].value.reset();
+		dictionary[i].key->taggedRelease(OSTypeID(OSCollection));
+		dictionary[i].value->taggedRelease(OSTypeID(OSCollection));
 	}
 	count = 0;
 }
@@ -383,11 +401,14 @@
 			return false;
 		}
 
-		OSTaggedSharedPtr<const OSMetaClassBase, OSCollection> oldObject;
+		const OSMetaClassBase *oldObject = dictionary[i].value;
 
 		haveUpdated();
 
-		dictionary[i].value.reset(anObject, OSRetain);
+		anObject->taggedRetain(OSTypeID(OSCollection));
+		dictionary[i].value = anObject;
+
+		oldObject->taggedRelease(OSTypeID(OSCollection));
 		return true;
 	}
 
@@ -398,11 +419,12 @@
 
 	haveUpdated();
 
-	new (&dictionary[count]) dictEntry();
-	os::move_backward(&dictionary[i], &dictionary[count], &dictionary[count + 1]);
-
-	dictionary[i].key.reset(aKey, OSRetain);
-	dictionary[i].value.reset(anObject, OSRetain);
+	bcopy(&dictionary[i], &dictionary[i + 1], (count - i) * sizeof(dictionary[0]));
+
+	aKey->taggedRetain(OSTypeID(OSCollection));
+	anObject->taggedRetain(OSTypeID(OSCollection));
+	dictionary[i].key = aKey;
+	dictionary[i].value = anObject;
 	count++;
 
 	return true;
@@ -413,24 +435,6 @@
 setObject(const OSSymbol *aKey, const OSMetaClassBase *anObject)
 {
 	return setObject(aKey, anObject, false);
-}
-
-bool
-OSDictionary::setObject(OSSharedPtr<const OSSymbol> const& aKey, OSSharedPtr<const OSMetaClassBase> const& anObject)
-{
-	return setObject(aKey.get(), anObject.get());
-}
-
-bool
-OSDictionary::setObject(const OSString* aKey, OSSharedPtr<const OSMetaClassBase> const& anObject)
-{
-	return setObject(aKey, anObject.get());
-}
-
-bool
-OSDictionary::setObject(const char* aKey, OSSharedPtr<const OSMetaClassBase> const& anObject)
-{
-	return setObject(aKey, anObject.get());
 }
 
 void
@@ -476,7 +480,7 @@
 OSDictionary::merge(const OSDictionary *srcDict)
 {
 	const OSSymbol * sym;
-	OSSharedPtr<OSCollectionIterator> iter;
+	OSCollectionIterator * iter;
 
 	if (!OSDynamicCast(OSDictionary, srcDict)) {
 		return false;
@@ -492,9 +496,11 @@
 
 		obj = srcDict->getObject(sym);
 		if (!setObject(sym, obj)) {
-			return false;
-		}
-	}
+			iter->release();
+			return false;
+		}
+	}
+	iter->release();
 
 	return true;
 }
@@ -519,10 +525,10 @@
 		while (l < r) {
 			i = (l + r) / 2;
 			if (aKey == dictionary[i].key) {
-				return const_cast<OSObject *> ((const OSObject *)dictionary[i].value.get());
+				return const_cast<OSObject *> ((const OSObject *)dictionary[i].value);
 			}
 
-			if ((uintptr_t)aKey < (uintptr_t)dictionary[i].key.get()) {
+			if ((uintptr_t)aKey < (uintptr_t)dictionary[i].key) {
 				r = i;
 			} else {
 				l = i + 1;
@@ -531,7 +537,7 @@
 	} else {
 		for (i = l; i < r; i++) {
 			if (aKey == dictionary[i].key) {
-				return const_cast<OSObject *> ((const OSObject *)dictionary[i].value.get());
+				return const_cast<OSObject *> ((const OSObject *)dictionary[i].value);
 			}
 		}
 	}
@@ -542,27 +548,30 @@
 // Wrapper macros
 #define OBJECT_WRAP_1(cmd, k)                                           \
 {                                                                       \
-    OSSharedPtr<const OSSymbol> tmpKey = k;                                         \
+    const OSSymbol *tmpKey = k;                                         \
     OSObject *retObj = NULL;                                            \
     if (tmpKey) {                                                       \
-	retObj = cmd(tmpKey.get());                                           \
+	retObj = cmd(tmpKey);                                           \
+	tmpKey->release();                                              \
     }                                                                   \
     return retObj;                                                      \
 }
 
 #define OBJECT_WRAP_2(cmd, k, o)                                        \
 {                                                                       \
-    OSSharedPtr<const OSSymbol> tmpKey = k;                                         \
-    bool ret = cmd(tmpKey.get(), o);                                          \
+    const OSSymbol *tmpKey = k;                                         \
+    bool ret = cmd(tmpKey, o);                                          \
                                                                         \
+    tmpKey->release();                                                  \
     return ret;                                                         \
 }
 
 #define OBJECT_WRAP_3(cmd, k)                                           \
 {                                                                       \
-    OSSharedPtr<const OSSymbol> tmpKey = k;                                         \
+    const OSSymbol *tmpKey = k;                                         \
     if (tmpKey) {                                                       \
-	cmd(tmpKey.get());                                                    \
+	cmd(tmpKey);                                                    \
+	tmpKey->release();                                              \
     }                                                                   \
 }
 
@@ -589,7 +598,7 @@
 bool
 OSDictionary::isEqualTo(const OSDictionary *srcDict, const OSCollection *keys) const
 {
-	OSSharedPtr<OSCollectionIterator> iter;
+	OSCollectionIterator * iter;
 	unsigned int keysCount;
 	const OSMetaClassBase * obj1;
 	const OSMetaClassBase * obj2;
@@ -624,6 +633,7 @@
 			break;
 		}
 	}
+	iter->release();
 
 	return ret;
 }
@@ -643,7 +653,7 @@
 	}
 
 	for (i = 0; i < count; i++) {
-		obj = srcDict->getObject(dictionary[i].key.get());
+		obj = srcDict->getObject(dictionary[i].key);
 		if (!obj) {
 			return false;
 		}
@@ -691,7 +701,7 @@
 	unsigned int index = (*iteratorP)++;
 
 	if (index < count) {
-		*ret = const_cast<OSSymbol*>(dictionary[index].key.get());
+		*ret = (OSObject *) dictionary[index].key;
 	} else {
 		*ret = NULL;
 	}
@@ -711,7 +721,7 @@
 	}
 
 	for (unsigned i = 0; i < count; i++) {
-		const OSSymbol *key = dictionary[i].key.get();
+		const OSSymbol *key = dictionary[i].key;
 
 		// due the nature of the XML syntax, this must be a symbol
 		if (!key->metaCast("OSSymbol")) {
@@ -760,7 +770,7 @@
 	if ((old ^ options) & mask) {
 		// Value changed need to recurse over all of the child collections
 		for (unsigned i = 0; i < count; i++) {
-			OSCollection *v = OSDynamicCast(OSCollection, dictionary[i].value.get());
+			OSCollection *v = OSDynamicCast(OSCollection, dictionary[i].value);
 			if (v) {
 				v->setOptions(options, mask);
 			}
@@ -774,19 +784,18 @@
 	return old;
 }
 
-OSSharedPtr<OSCollection>
+OSCollection *
 OSDictionary::copyCollection(OSDictionary *cycleDict)
 {
-	OSSharedPtr<OSDictionary> ourCycleDict;
-	OSSharedPtr<OSCollection> ret;
-	OSSharedPtr<OSDictionary> newDict;
-
-	if (!cycleDict) {
-		ourCycleDict = OSDictionary::withCapacity(16);
-		if (!ourCycleDict) {
-			return nullptr;
-		}
-		cycleDict = ourCycleDict.get();
+	bool allocDict = !cycleDict;
+	OSCollection *ret = NULL;
+	OSDictionary *newDict = NULL;
+
+	if (allocDict) {
+		cycleDict = OSDictionary::withCapacity(16);
+		if (!cycleDict) {
+			return NULL;
+		}
 	}
 
 	do {
@@ -802,41 +811,58 @@
 		}
 
 		// Insert object into cycle Dictionary
-		cycleDict->setObject((const OSSymbol *) this, newDict.get());
+		cycleDict->setObject((const OSSymbol *) this, newDict);
 
 		for (unsigned int i = 0; i < count; i++) {
-			const OSMetaClassBase *obj = dictionary[i].value.get();
-			OSTaggedSharedPtr<OSCollection, OSCollection> coll(OSDynamicCast(OSCollection, EXT_CAST(obj)), OSNoRetain);
+			const OSMetaClassBase *obj = dictionary[i].value;
+			OSCollection *coll = OSDynamicCast(OSCollection, EXT_CAST(obj));
 
 			if (coll) {
-				OSSharedPtr<OSCollection> newColl = coll->copyCollection(cycleDict);
+				OSCollection *newColl = coll->copyCollection(cycleDict);
 				if (!newColl) {
-					return ret;
+					goto abortCopy;
 				}
-				newDict->dictionary[i].value.detach();
-				newDict->dictionary[i].value.reset(newColl.get(), OSRetain);
+
+				newDict->dictionary[i].value = newColl;
+
+				coll->taggedRelease(OSTypeID(OSCollection));
+				newColl->taggedRetain(OSTypeID(OSCollection));
+				newColl->release();
 			}
-		}
-
-		ret = os::move(newDict);
+			;
+		}
+
+		ret = newDict;
+		newDict = NULL;
 	} while (false);
 
+abortCopy:
+	if (newDict) {
+		newDict->release();
+	}
+
+	if (allocDict) {
+		cycleDict->release();
+	}
+
 	return ret;
 }
 
-OSSharedPtr<OSArray>
+OSArray *
 OSDictionary::copyKeys(void)
 {
-	OSSharedPtr<OSArray> array;
+	OSArray * array;
 
 	array = OSArray::withCapacity(count);
 	if (!array) {
-		return nullptr;
+		return NULL;
 	}
 
 	for (unsigned int i = 0; i < count; i++) {
-		if (!array->setObject(i, dictionary[i].key.get())) {
-			return nullptr;
+		if (!array->setObject(i, dictionary[i].key)) {
+			array->release();
+			array = NULL;
+			break;
 		}
 	}
 	return array;
@@ -851,7 +877,7 @@
 	initialUpdateStamp = updateStamp;
 	done = false;
 	for (unsigned int i = 0; i < count; i++) {
-		done = callback(refcon, dictionary[i].key.get(), EXT_CAST(dictionary[i].value.get()));
+		done = callback(refcon, dictionary[i].key, EXT_CAST(dictionary[i].value));
 		if (done) {
 			break;
 		}