]> git.saurik.com Git - apple/xnu.git/blobdiff - libkern/c++/OSSet.cpp
xnu-7195.50.7.100.1.tar.gz
[apple/xnu.git] / libkern / c++ / OSSet.cpp
index ed8b2762cea037e22eed05c465de6bbeadc83152..7aaf85cc3cdaf4e83fc2be6a8f29270793c9d9eb 100644 (file)
  */
 /* IOSet.m created by rsulack on Thu 11-Jun-1998 */
 
-#include <libkern/c++/OSDictionary.h>
+#define IOKIT_ENABLE_SHARED_PTR
+
 #include <libkern/c++/OSArray.h>
+#include <libkern/c++/OSDictionary.h>
 #include <libkern/c++/OSSerialize.h>
 #include <libkern/c++/OSSet.h>
+#include <libkern/c++/OSSharedPtr.h>
+#include <os/cpp_util.h>
+#include <kern/zalloc.h>
 
 #define super OSCollection
 
-OSDefineMetaClassAndStructors(OSSet, OSCollection)
+OSDefineMetaClassAndStructorsWithZone(OSSet, OSCollection,
+    ZC_ZFREE_CLEARMEM)
 OSMetaClassDefineReservedUnused(OSSet, 0);
 OSMetaClassDefineReservedUnused(OSSet, 1);
 OSMetaClassDefineReservedUnused(OSSet, 2);
@@ -109,60 +115,56 @@ bool
 OSSet::initWithSet(const OSSet *inSet,
     unsigned int inCapacity)
 {
-       return initWithArray(inSet->members, inCapacity);
+       return initWithArray(inSet->members.get(), inCapacity);
 }
 
-OSSet *
+OSSharedPtr<OSSet>
 OSSet::withCapacity(unsigned int capacity)
 {
-       OSSet *me = new OSSet;
+       OSSharedPtr<OSSet> me = OSMakeShared<OSSet>();
 
        if (me && !me->initWithCapacity(capacity)) {
-               me->release();
-               return NULL;
+               return nullptr;
        }
 
        return me;
 }
 
-OSSet *
+OSSharedPtr<OSSet>
 OSSet::withObjects(const OSObject *objects[],
     unsigned int count,
     unsigned int capacity)
 {
-       OSSet *me = new OSSet;
+       OSSharedPtr<OSSet> me = OSMakeShared<OSSet>();
 
        if (me && !me->initWithObjects(objects, count, capacity)) {
-               me->release();
-               return NULL;
+               return nullptr;
        }
 
        return me;
 }
 
-OSSet *
+OSSharedPtr<OSSet>
 OSSet::withArray(const OSArray *array,
     unsigned int capacity)
 {
-       OSSet *me = new OSSet;
+       OSSharedPtr<OSSet> me = OSMakeShared<OSSet>();
 
        if (me && !me->initWithArray(array, capacity)) {
-               me->release();
-               return NULL;
+               return nullptr;
        }
 
        return me;
 }
 
-OSSet *
+OSSharedPtr<OSSet>
 OSSet::withSet(const OSSet *set,
     unsigned int capacity)
 {
-       OSSet *me = new OSSet;
+       OSSharedPtr<OSSet> me = OSMakeShared<OSSet>();;
 
        if (me && !me->initWithSet(set, capacity)) {
-               me->release();
-               return NULL;
+               return nullptr;
        }
 
        return me;
@@ -173,7 +175,6 @@ OSSet::free()
 {
        if (members) {
                (void) members->super::setOptions(0, kImmutable);
-               members->release();
        }
 
        super::free();
@@ -227,6 +228,12 @@ OSSet::setObject(const OSMetaClassBase *anObject)
        }
 }
 
+bool
+OSSet::setObject(OSSharedPtr<const OSMetaClassBase> const& anObject)
+{
+       return setObject(anObject.get());
+}
+
 bool
 OSSet::merge(const OSArray * array)
 {
@@ -251,7 +258,7 @@ OSSet::merge(const OSArray * array)
 bool
 OSSet::merge(const OSSet * set)
 {
-       return merge(set->members);
+       return merge(set->members.get());
 }
 
 void
@@ -268,6 +275,12 @@ OSSet::removeObject(const OSMetaClassBase *anObject)
        }
 }
 
+void
+OSSet::removeObject(OSSharedPtr<const OSMetaClassBase> const& anObject)
+{
+       removeObject(anObject.get());
+}
+
 
 bool
 OSSet::containsObject(const OSMetaClassBase *anObject) const
@@ -406,18 +419,19 @@ OSSet::setOptions(unsigned options, unsigned mask, void *)
        return old;
 }
 
-OSCollection *
+OSSharedPtr<OSCollection>
 OSSet::copyCollection(OSDictionary *cycleDict)
 {
-       bool allocDict = !cycleDict;
-       OSCollection *ret = NULL;
-       OSSet *newSet = NULL;
-
-       if (allocDict) {
-               cycleDict = OSDictionary::withCapacity(16);
-               if (!cycleDict) {
-                       return NULL;
+       OSSharedPtr<OSDictionary> ourCycleDict;
+       OSSharedPtr<OSCollection> ret;
+       OSSharedPtr<OSSet> newSet;
+
+       if (!cycleDict) {
+               ourCycleDict = OSDictionary::withCapacity(16);
+               if (!ourCycleDict) {
+                       return nullptr;
                }
+               cycleDict = ourCycleDict.get();
        }
 
        do {
@@ -431,41 +445,28 @@ OSSet::copyCollection(OSDictionary *cycleDict)
                        continue; // Couldn't create new set abort
                }
                // Insert object into cycle Dictionary
-               cycleDict->setObject((const OSSymbol *) this, newSet);
+               cycleDict->setObject((const OSSymbol *) this, newSet.get());
 
-               OSArray *newMembers = newSet->members;
+               OSArray *newMembers = newSet->members.get();
                newMembers->capacityIncrement = members->capacityIncrement;
 
                // Now copy over the contents into the new duplicate
                for (unsigned int i = 0; i < members->count; i++) {
-                       OSObject *obj = EXT_CAST(members->array[i]);
+                       OSObject *obj = EXT_CAST(members->array[i].get());
                        OSCollection *coll = OSDynamicCast(OSCollection, obj);
                        if (coll) {
-                               OSCollection *newColl = coll->copyCollection(cycleDict);
+                               OSSharedPtr<OSCollection> newColl = coll->copyCollection(cycleDict);
                                if (newColl) {
-                                       obj = newColl; // Rely on cycleDict ref for a bit
-                                       newColl->release();
+                                       obj = newColl.get(); // Rely on cycleDict ref for a bit
                                } else {
-                                       goto abortCopy;
+                                       return ret;
                                }
                        }
-                       ;
                        newMembers->setObject(obj);
                }
-               ;
 
-               ret = newSet;
-               newSet = NULL;
+               ret = os::move(newSet);
        } while (false);
 
-abortCopy:
-       if (newSet) {
-               newSet->release();
-       }
-
-       if (allocDict) {
-               cycleDict->release();
-       }
-
        return ret;
 }