2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <libkern/c++/OSDictionary.h>
30 #include <libkern/c++/OSOrderedSet.h>
31 #include <libkern/c++/OSLib.h>
33 #define super OSCollection
35 OSDefineMetaClassAndStructors(OSOrderedSet
, OSCollection
)
36 OSMetaClassDefineReservedUnused(OSOrderedSet
, 0);
37 OSMetaClassDefineReservedUnused(OSOrderedSet
, 1);
38 OSMetaClassDefineReservedUnused(OSOrderedSet
, 2);
39 OSMetaClassDefineReservedUnused(OSOrderedSet
, 3);
40 OSMetaClassDefineReservedUnused(OSOrderedSet
, 4);
41 OSMetaClassDefineReservedUnused(OSOrderedSet
, 5);
42 OSMetaClassDefineReservedUnused(OSOrderedSet
, 6);
43 OSMetaClassDefineReservedUnused(OSOrderedSet
, 7);
47 extern int debug_container_malloc_size
;
49 #define ACCUMSIZE(s) do { debug_container_malloc_size += (s); } while(0)
55 const OSMetaClassBase
* obj
;
59 #define EXT_CAST(obj) \
60 reinterpret_cast<OSObject *>(const_cast<OSMetaClassBase *>(obj))
63 initWithCapacity(unsigned int inCapacity
,
64 OSOrderFunction inOrdering
, void *inOrderingRef
)
71 if (inCapacity
> (UINT_MAX
/ sizeof(_Element
)))
74 size
= sizeof(_Element
) * inCapacity
;
75 array
= (_Element
*) kalloc(size
);
80 capacity
= inCapacity
;
81 capacityIncrement
= (inCapacity
)? inCapacity
: 16;
82 ordering
= inOrdering
;
83 orderingRef
= inOrderingRef
;
91 OSOrderedSet
* OSOrderedSet::
92 withCapacity(unsigned int capacity
,
93 OSOrderFunction ordering
, void * orderingRef
)
95 OSOrderedSet
*me
= new OSOrderedSet
;
97 if (me
&& !me
->initWithCapacity(capacity
, ordering
, orderingRef
)) {
105 void OSOrderedSet::free()
107 (void) super::setOptions(0, kImmutable
);
111 kfree(array
, sizeof(_Element
) * capacity
);
112 ACCUMSIZE( -(sizeof(_Element
) * capacity
) );
118 unsigned int OSOrderedSet::getCount() const { return count
; }
119 unsigned int OSOrderedSet::getCapacity() const { return capacity
; }
120 unsigned int OSOrderedSet::getCapacityIncrement() const
121 { return capacityIncrement
; }
122 unsigned int OSOrderedSet::setCapacityIncrement(unsigned int increment
)
124 capacityIncrement
= (increment
)? increment
: 16;
125 return capacityIncrement
;
128 unsigned int OSOrderedSet::ensureCapacity(unsigned int newCapacity
)
131 unsigned int finalCapacity
, oldSize
, newSize
;
133 if (newCapacity
<= capacity
)
137 finalCapacity
= (((newCapacity
- 1) / capacityIncrement
) + 1)
139 if ((finalCapacity
< newCapacity
) ||
140 (finalCapacity
> (UINT_MAX
/ sizeof(_Element
)))) {
143 newSize
= sizeof(_Element
) * finalCapacity
;
145 newArray
= (_Element
*) kalloc(newSize
);
147 oldSize
= sizeof(_Element
) * capacity
;
149 ACCUMSIZE(newSize
- oldSize
);
151 bcopy(array
, newArray
, oldSize
);
152 bzero(&newArray
[capacity
], newSize
- oldSize
);
153 kfree(array
, oldSize
);
155 capacity
= finalCapacity
;
161 void OSOrderedSet::flushCollection()
167 for (i
= 0; i
< count
; i
++)
168 array
[i
].obj
->taggedRelease(OSTypeID(OSCollection
));
174 bool OSOrderedSet::setObject(unsigned int index
, const OSMetaClassBase
*anObject
)
177 unsigned int newCount
= count
+ 1;
179 if ((index
> count
) || !anObject
)
182 if (containsObject(anObject
))
185 // do we need more space?
186 if (newCount
> capacity
&& newCount
> ensureCapacity(newCount
))
190 if (index
!= count
) {
191 for (i
= count
; i
> index
; i
--)
192 array
[i
] = array
[i
-1];
194 array
[index
].obj
= anObject
;
195 // array[index].pri = pri;
196 anObject
->taggedRetain(OSTypeID(OSCollection
));
203 bool OSOrderedSet::setFirstObject(const OSMetaClassBase
*anObject
)
205 return( setObject(0, anObject
));
208 bool OSOrderedSet::setLastObject(const OSMetaClassBase
*anObject
)
210 return( setObject( count
, anObject
));
214 #define ORDER(obj1,obj2) \
215 (ordering ? ((*ordering)( (const OSObject *) obj1, (const OSObject *) obj2, orderingRef)) : 0)
217 bool OSOrderedSet::setObject(const OSMetaClassBase
*anObject
)
221 // queue it behind those with same priority
223 (i
< count
) && (ORDER(array
[i
].obj
, anObject
) >= 0);
226 return( setObject(i
, anObject
));
229 void OSOrderedSet::removeObject(const OSMetaClassBase
*anObject
)
231 bool deleted
= false;
234 for (i
= 0; i
< count
; i
++) {
237 array
[i
-1] = array
[i
];
238 else if (array
[i
].obj
== anObject
) {
240 haveUpdated(); // Pity we can't flush the log
241 array
[i
].obj
->taggedRelease(OSTypeID(OSCollection
));
249 bool OSOrderedSet::containsObject(const OSMetaClassBase
*anObject
) const
251 return anObject
&& member(anObject
);
254 bool OSOrderedSet::member(const OSMetaClassBase
*anObject
) const
259 (i
< count
) && (array
[i
].obj
!= anObject
);
266 OSObject
*OSOrderedSet::getObject( unsigned int index
) const
272 // *pri = array[index].pri;
274 return( const_cast<OSObject
*>((const OSObject
*) array
[index
].obj
) );
277 OSObject
*OSOrderedSet::getFirstObject() const
280 return( const_cast<OSObject
*>((const OSObject
*) array
[0].obj
) );
285 OSObject
*OSOrderedSet::getLastObject() const
288 return( const_cast<OSObject
*>((const OSObject
*) array
[count
-1].obj
) );
293 SInt32
OSOrderedSet::orderObject( const OSMetaClassBase
* anObject
)
295 return( ORDER( anObject
, 0 ));
298 void *OSOrderedSet::getOrderingRef()
303 bool OSOrderedSet::isEqualTo(const OSOrderedSet
*anOrderedSet
) const
307 if ( this == anOrderedSet
)
310 if ( count
!= anOrderedSet
->getCount() )
313 for ( i
= 0; i
< count
; i
++ ) {
314 if ( !array
[i
].obj
->isEqualTo(anOrderedSet
->getObject(i
)) )
321 bool OSOrderedSet::isEqualTo(const OSMetaClassBase
*anObject
) const
325 oSet
= OSDynamicCast(OSOrderedSet
, anObject
);
327 return isEqualTo(oSet
);
332 unsigned int OSOrderedSet::iteratorSize() const
334 return( sizeof(unsigned int));
337 bool OSOrderedSet::initIterator(void *inIterator
) const
339 unsigned int *iteratorP
= (unsigned int *) inIterator
;
346 getNextObjectForIterator(void *inIterator
, OSObject
**ret
) const
348 unsigned int *iteratorP
= (unsigned int *) inIterator
;
349 unsigned int index
= (*iteratorP
)++;
352 *ret
= const_cast<OSObject
*>((const OSObject
*) array
[index
].obj
);
360 unsigned OSOrderedSet::setOptions(unsigned options
, unsigned mask
, void *)
362 unsigned old
= super::setOptions(options
, mask
);
363 if ((old
^ options
) & mask
) {
365 // Value changed need to recurse over all of the child collections
366 for ( unsigned i
= 0; i
< count
; i
++ ) {
367 OSCollection
*coll
= OSDynamicCast(OSCollection
, array
[i
].obj
);
369 coll
->setOptions(options
, mask
);
376 OSCollection
* OSOrderedSet::copyCollection(OSDictionary
*cycleDict
)
378 bool allocDict
= !cycleDict
;
379 OSCollection
*ret
= 0;
380 OSOrderedSet
*newSet
= 0;
383 cycleDict
= OSDictionary::withCapacity(16);
390 ret
= super::copyCollection(cycleDict
);
394 // Duplicate the set with no contents
395 newSet
= OSOrderedSet::withCapacity(capacity
, ordering
, orderingRef
);
399 // Insert object into cycle Dictionary
400 cycleDict
->setObject((const OSSymbol
*) this, newSet
);
402 newSet
->capacityIncrement
= capacityIncrement
;
404 // Now copy over the contents to the new duplicate
405 for (unsigned int i
= 0; i
< count
; i
++) {
406 OSObject
*obj
= EXT_CAST(array
[i
].obj
);
407 OSCollection
*coll
= OSDynamicCast(OSCollection
, obj
);
409 OSCollection
*newColl
= coll
->copyCollection(cycleDict
);
411 obj
= newColl
; // Rely on cycleDict ref for a bit
417 newSet
->setLastObject(obj
);
430 cycleDict
->release();