Loading...
libkern/c++/OSOrderedSet.cpp xnu-12377.101.15 xnu-7195.121.3
--- xnu/xnu-12377.101.15/libkern/c++/OSOrderedSet.cpp
+++ xnu/xnu-7195.121.3/libkern/c++/OSOrderedSet.cpp
@@ -59,6 +59,8 @@
 initWithCapacity(unsigned int inCapacity,
     OSOrderFunction inOrdering, void *inOrderingRef)
 {
+	unsigned int size;
+
 	if (!super::init()) {
 		return false;
 	}
@@ -67,7 +69,8 @@
 		return false;
 	}
 
-	array = kallocp_type_container(_Element, &inCapacity, Z_WAITOK_ZERO);
+	size = sizeof(_Element) * inCapacity;
+	array = (_Element *) kalloc_container(size);
 	if (!array) {
 		return false;
 	}
@@ -78,7 +81,8 @@
 	ordering = inOrdering;
 	orderingRef = inOrderingRef;
 
-	OSCONTAINER_ACCUMSIZE(sizeof(_Element) * inCapacity);
+	bzero(array, size);
+	OSCONTAINER_ACCUMSIZE(size);
 
 	return true;
 }
@@ -97,29 +101,6 @@
 	return me;
 }
 
-static SInt32
-OSOrderedSetBlockToFunc(const OSMetaClassBase * obj1,
-    const OSMetaClassBase * obj2,
-    void * context)
-{
-	OSOrderedSet::OSOrderBlock ordering = (typeof(ordering))context;
-
-	return ordering(obj1, obj2);
-}
-
-
-OSSharedPtr<OSOrderedSet>
-OSOrderedSet::withCapacity(unsigned int capacity, OSOrderBlock ordering)
-{
-	auto me = OSMakeShared<OSOrderedSet>();
-
-	if (me && !me->initWithCapacity(capacity, &OSOrderedSetBlockToFunc, ordering)) {
-		return nullptr;
-	}
-
-	return me;
-}
-
 void
 OSOrderedSet::free()
 {
@@ -127,7 +108,7 @@
 	flushCollection();
 
 	if (array) {
-		kfree_type(_Element, capacity, array);
+		kfree(array, sizeof(_Element) * capacity);
 		OSCONTAINER_ACCUMSIZE( -(sizeof(_Element) * capacity));
 	}
 
@@ -160,7 +141,8 @@
 OSOrderedSet::ensureCapacity(unsigned int newCapacity)
 {
 	_Element *newArray;
-	unsigned int finalCapacity;
+	vm_size_t finalCapacity;
+	vm_size_t oldSize, newSize;
 
 	if (newCapacity <= capacity) {
 		return capacity;
@@ -172,13 +154,27 @@
 	if (finalCapacity < newCapacity) {
 		return capacity;
 	}
-
-	newArray = kreallocp_type_container(_Element, array,
-	    capacity, &finalCapacity, Z_WAITOK_ZERO);
+	newSize = sizeof(_Element) * finalCapacity;
+
+	newArray = (_Element *) kallocp_container(&newSize);
 	if (newArray) {
-		OSCONTAINER_ACCUMSIZE(sizeof(_Element) * (finalCapacity - capacity));
+		// use all of the actual allocation size
+		finalCapacity = (newSize / sizeof(_Element));
+		if (finalCapacity > UINT_MAX) {
+			// failure, too large
+			kfree(newArray, newSize);
+			return capacity;
+		}
+
+		oldSize = sizeof(_Element) * capacity;
+
+		OSCONTAINER_ACCUMSIZE(((size_t)newSize) - ((size_t)oldSize));
+
+		bcopy(array, newArray, oldSize);
+		bzero(&newArray[capacity], newSize - oldSize);
+		kfree(array, oldSize);
 		array = newArray;
-		capacity = finalCapacity;
+		capacity = (unsigned int) finalCapacity;
 	}
 
 	return capacity;