/* IOArray.cpp converted to C++ by gvdl on Fri 1998-10-30 */
+#define IOKIT_ENABLE_SHARED_PTR
+
#include <libkern/c++/OSArray.h>
#include <libkern/c++/OSDictionary.h>
-#include <libkern/c++/OSSerialize.h>
#include <libkern/c++/OSLib.h>
+#include <libkern/c++/OSSerialize.h>
+#include <libkern/c++/OSSharedPtr.h>
#include <libkern/OSDebug.h>
+#include <os/cpp_util.h>
#define super OSCollection
-OSDefineMetaClassAndStructors(OSArray, OSCollection)
+OSDefineMetaClassAndStructorsWithZone(OSArray, OSCollection,
+ (zone_create_flags_t) (ZC_CACHING | ZC_ZFREE_CLEARMEM))
OSMetaClassDefineReservedUnused(OSArray, 0);
OSMetaClassDefineReservedUnused(OSArray, 1);
OSMetaClassDefineReservedUnused(OSArray, 2);
OSMetaClassDefineReservedUnused(OSArray, 6);
OSMetaClassDefineReservedUnused(OSArray, 7);
-
-#define EXT_CAST(obj) \
- reinterpret_cast<OSObject *>(const_cast<OSMetaClassBase *>(obj))
-
bool
OSArray::initWithCapacity(unsigned int inCapacity)
{
}
// integer overflow check
- if (inCapacity > (UINT_MAX / sizeof(const OSMetaClassBase*))) {
+ if (inCapacity > (UINT_MAX / sizeof(*array))) {
return false;
}
- size = sizeof(const OSMetaClassBase *) * inCapacity;
- array = (const OSMetaClassBase **) kalloc_container(size);
+ size = sizeof(*array) * inCapacity;
+ array = (ArraySharedPtrType *)kalloc_container(size);
if (!array) {
return false;
}
capacity = inCapacity;
capacityIncrement = (inCapacity)? inCapacity : 16;
- bzero(array, size);
+ os::uninitialized_value_construct(array, array + capacity);
OSCONTAINER_ACCUMSIZE(size);
return true;
return false;
}
- array[count++] = newObject;
- newObject->taggedRetain(OSTypeID(OSCollection));
+ array[count++].reset(newObject, OSRetain);
}
return true;
anArray->count, theCapacity);
}
-OSArray *
+OSSharedPtr<OSArray>
OSArray::withCapacity(unsigned int capacity)
{
- OSArray *me = new OSArray;
+ OSSharedPtr<OSArray> me = OSMakeShared<OSArray>();
if (me && !me->initWithCapacity(capacity)) {
- me->release();
- return 0;
+ return nullptr;
}
return me;
}
-OSArray *
+OSSharedPtr<OSArray>
OSArray::withObjects(const OSObject *objects[],
unsigned int count,
unsigned int capacity)
{
- OSArray *me = new OSArray;
+ OSSharedPtr<OSArray> me = OSMakeShared<OSArray>();
if (me && !me->initWithObjects(objects, count, capacity)) {
- me->release();
- return 0;
+ return nullptr;
}
return me;
}
-OSArray *
+OSSharedPtr<OSArray>
OSArray::withArray(const OSArray *array,
unsigned int capacity)
{
- OSArray *me = new OSArray;
+ OSSharedPtr<OSArray> me = OSMakeShared<OSArray>();
if (me && !me->initWithArray(array, capacity)) {
- me->release();
- return 0;
+ return nullptr;
}
return me;
flushCollection();
if (array) {
- kfree(array, sizeof(const OSMetaClassBase *) * capacity);
- OSCONTAINER_ACCUMSIZE( -(sizeof(const OSMetaClassBase *) * capacity));
+ os::destroy(array, array + capacity);
+ kfree(array, sizeof(*array) * capacity);
+ OSCONTAINER_ACCUMSIZE( -(sizeof(*array) * capacity));
}
super::free();
unsigned int
OSArray::ensureCapacity(unsigned int newCapacity)
{
- const OSMetaClassBase **newArray;
- unsigned int finalCapacity;
+ ArraySharedPtrType *newArray;
+ vm_size_t finalCapacity;
vm_size_t oldSize, newSize;
if (newCapacity <= capacity) {
* capacityIncrement;
// integer overflow check
- if ((finalCapacity < newCapacity) || (finalCapacity > (UINT_MAX / sizeof(const OSMetaClassBase*)))) {
+ if (finalCapacity < newCapacity) {
return capacity;
}
- newSize = sizeof(const OSMetaClassBase *) * finalCapacity;
+ newSize = sizeof(*newArray) * finalCapacity;
- newArray = (const OSMetaClassBase **) kallocp_container(&newSize);
+ newArray = (decltype(newArray))kallocp_container(&newSize);
if (newArray) {
// use all of the actual allocation size
- finalCapacity = newSize / sizeof(const OSMetaClassBase *);
+ finalCapacity = (newSize / sizeof(*newArray));
+ if (finalCapacity > UINT_MAX) {
+ // failure, too large
+ kfree(newArray, newSize);
+ return capacity;
+ }
- oldSize = sizeof(const OSMetaClassBase *) * capacity;
+ oldSize = sizeof(*array) * capacity;
OSCONTAINER_ACCUMSIZE(((size_t)newSize) - ((size_t)oldSize));
- bcopy(array, newArray, oldSize);
- bzero(&newArray[capacity], newSize - oldSize);
+ os::uninitialized_move(array, array + capacity, newArray);
+ os::uninitialized_value_construct(newArray + capacity, newArray + finalCapacity);
+ os::destroy(array, array + capacity);
kfree(array, oldSize);
array = newArray;
- capacity = finalCapacity;
+ capacity = (unsigned int) finalCapacity;
}
return capacity;
haveUpdated();
for (i = 0; i < count; i++) {
- array[i]->taggedRelease(OSTypeID(OSCollection));
+ array[i].reset();
}
count = 0;
}
return setObject(count, anObject);
}
+bool
+OSArray::setObject(OSSharedPtr<const OSMetaClassBase> const& anObject)
+{
+ return setObject(count, anObject);
+}
+
bool
OSArray::setObject(unsigned int index, const OSMetaClassBase *anObject)
{
haveUpdated();
if (index != count) {
for (i = count; i > index; i--) {
- array[i] = array[i - 1];
+ array[i] = os::move(array[i - 1]);
}
}
- array[index] = anObject;
- anObject->taggedRetain(OSTypeID(OSCollection));
+ array[index].reset(anObject, OSRetain);
count++;
return true;
}
+bool
+OSArray::setObject(unsigned int index, OSSharedPtr<const OSMetaClassBase> const& anObject)
+{
+ return setObject(index, anObject.get());
+}
+
bool
OSArray::merge(const OSArray * otherArray)
{
for (unsigned int i = 0; i < otherCount; i++) {
const OSMetaClassBase *newObject = otherArray->getObject(i);
- array[count++] = newObject;
- newObject->taggedRetain(OSTypeID(OSCollection));
+ array[count++].reset(newObject, OSRetain);
}
return true;
OSArray::
replaceObject(unsigned int index, const OSMetaClassBase *anObject)
{
- const OSMetaClassBase *oldObject;
-
if ((index >= count) || !anObject) {
return;
}
haveUpdated();
- oldObject = array[index];
- array[index] = anObject;
- anObject->taggedRetain(OSTypeID(OSCollection));
- oldObject->taggedRelease(OSTypeID(OSCollection));
+ array[index].reset(anObject, OSRetain);
+}
+
+void
+OSArray::replaceObject(unsigned int index, OSSharedPtr<const OSMetaClassBase> const& anObject)
+{
+ return replaceObject(index, anObject.get());
}
void
OSArray::removeObject(unsigned int index)
{
unsigned int i;
- const OSMetaClassBase *oldObject;
+ ArraySharedPtrType oldObject;
if (index >= count) {
return;
}
haveUpdated();
- oldObject = array[index];
+ oldObject = os::move(array[index]);
count--;
for (i = index; i < count; i++) {
- array[i] = array[i + 1];
+ array[i] = os::move(array[i + 1]);
}
-
- oldObject->taggedRelease(OSTypeID(OSCollection));
}
bool
OSArray::getObject(unsigned int index) const
{
if (index >= count) {
- return 0;
+ return NULL;
} else {
- return (OSObject *) (const_cast<OSMetaClassBase *>(array[index]));
+ return static_cast<OSObject *>(const_cast<OSMetaClassBase *>(array[index].get()));
}
}
OSArray::getLastObject() const
{
if (count == 0) {
- return 0;
+ return NULL;
} else {
- return (OSObject *) (const_cast<OSMetaClassBase *>(array[count - 1]));
+ return static_cast<OSObject *>(const_cast<OSMetaClassBase *>(array[count - 1].get()));
}
}
unsigned int index = (*iteratorP)++;
if (index < count) {
- *ret = (OSObject *)(const_cast<OSMetaClassBase *> (array[index]));
+ *ret = static_cast<OSObject *>(const_cast<OSMetaClassBase *>(array[index].get()));
return true;
} else {
- *ret = 0;
+ *ret = NULL;
return false;
}
}
if ((old ^ options) & mask) {
// Value changed need to recurse over all of the child collections
for (unsigned i = 0; i < count; i++) {
- OSCollection *coll = OSDynamicCast(OSCollection, array[i]);
+ OSCollection *coll = OSDynamicCast(OSCollection, array[i].get());
if (coll) {
coll->setOptions(options, mask);
}
return old;
}
-OSCollection *
+OSSharedPtr<OSCollection>
OSArray::copyCollection(OSDictionary *cycleDict)
{
- bool allocDict = !cycleDict;
- OSCollection *ret = 0;
- OSArray *newArray = 0;
-
- if (allocDict) {
- cycleDict = OSDictionary::withCapacity(16);
- if (!cycleDict) {
- return 0;
+ OSSharedPtr<OSDictionary> ourCycleDict;
+ OSSharedPtr<OSCollection> ret;
+ OSSharedPtr<OSArray> newArray;
+
+ if (!cycleDict) {
+ ourCycleDict = OSDictionary::withCapacity(16);
+ if (!ourCycleDict) {
+ return nullptr;
}
+ cycleDict = ourCycleDict.get();
}
do {
}
// Insert object into cycle Dictionary
- cycleDict->setObject((const OSSymbol *) this, newArray);
+ cycleDict->setObject((const OSSymbol *) this, newArray.get());
for (unsigned int i = 0; i < count; i++) {
OSCollection *coll =
- OSDynamicCast(OSCollection, EXT_CAST(newArray->array[i]));
+ OSDynamicCast(OSCollection, static_cast<OSObject *>(
+ const_cast<OSMetaClassBase *>(
+ newArray->array[i].get())));
if (coll) {
- OSCollection *newColl = coll->copyCollection(cycleDict);
+ OSSharedPtr<OSCollection> newColl = coll->copyCollection(cycleDict);
if (!newColl) {
- goto abortCopy;
+ return ret;
}
- newArray->replaceObject(i, newColl);
- newColl->release();
+ newArray->replaceObject(i, newColl.get());
}
- ;
}
- ;
- ret = newArray;
- newArray = 0;
+ ret = os::move(newArray);
} while (false);
-abortCopy:
- if (newArray) {
- newArray->release();
- }
-
- if (allocDict) {
- cycleDict->release();
- }
-
return ret;
}