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 size
= sizeof(_Element
) * inCapacity
;
72 array
= (_Element
*) kalloc(size
);
77 capacity
= inCapacity
;
78 capacityIncrement
= (inCapacity
)? inCapacity
: 16;
79 ordering
= inOrdering
;
80 orderingRef
= inOrderingRef
;
88 OSOrderedSet
* OSOrderedSet::
89 withCapacity(unsigned int capacity
,
90 OSOrderFunction ordering
, void * orderingRef
)
92 OSOrderedSet
*me
= new OSOrderedSet
;
94 if (me
&& !me
->initWithCapacity(capacity
, ordering
, orderingRef
)) {
102 void OSOrderedSet::free()
104 (void) super::setOptions(0, kImmutable
);
108 kfree((vm_offset_t
)array
, sizeof(_Element
) * capacity
);
109 ACCUMSIZE( -(sizeof(_Element
) * capacity
) );
115 unsigned int OSOrderedSet::getCount() const { return count
; }
116 unsigned int OSOrderedSet::getCapacity() const { return capacity
; }
117 unsigned int OSOrderedSet::getCapacityIncrement() const
118 { return capacityIncrement
; }
119 unsigned int OSOrderedSet::setCapacityIncrement(unsigned int increment
)
121 capacityIncrement
= (increment
)? increment
: 16;
122 return capacityIncrement
;
125 unsigned int OSOrderedSet::ensureCapacity(unsigned int newCapacity
)
128 int oldSize
, newSize
;
130 if (newCapacity
<= capacity
)
134 newCapacity
= (((newCapacity
- 1) / capacityIncrement
) + 1)
136 newSize
= sizeof(_Element
) * newCapacity
;
138 newArray
= (_Element
*) kalloc(newSize
);
140 oldSize
= sizeof(_Element
) * capacity
;
142 ACCUMSIZE(newSize
- oldSize
);
144 bcopy(array
, newArray
, oldSize
);
145 bzero(&newArray
[capacity
], newSize
- oldSize
);
146 kfree((vm_offset_t
)array
, oldSize
);
148 capacity
= newCapacity
;
154 void OSOrderedSet::flushCollection()
160 for (i
= 0; i
< count
; i
++)
161 array
[i
].obj
->taggedRelease(OSTypeID(OSCollection
));
167 bool OSOrderedSet::setObject(unsigned int index
, const OSMetaClassBase
*anObject
)
170 unsigned int newCount
= count
+ 1;
172 if ((index
> count
) || !anObject
)
175 if (containsObject(anObject
))
178 // do we need more space?
179 if (newCount
> capacity
&& newCount
> ensureCapacity(newCount
))
183 if (index
!= count
) {
184 for (i
= count
; i
> index
; i
--)
185 array
[i
] = array
[i
-1];
187 array
[index
].obj
= anObject
;
188 // array[index].pri = pri;
189 anObject
->taggedRetain(OSTypeID(OSCollection
));
196 bool OSOrderedSet::setFirstObject(const OSMetaClassBase
*anObject
)
198 return( setObject(0, anObject
));
201 bool OSOrderedSet::setLastObject(const OSMetaClassBase
*anObject
)
203 return( setObject( count
, anObject
));
207 #define ORDER(obj1,obj2) \
208 (ordering ? ((*ordering)( (OSObject *) obj1, (OSObject *) obj2, orderingRef)) : 0)
210 bool OSOrderedSet::setObject(const OSMetaClassBase
*anObject
)
214 // queue it behind those with same priority
216 (i
< count
) && (ORDER(array
[i
].obj
, anObject
) >= 0);
219 return( setObject(i
, anObject
));
222 void OSOrderedSet::removeObject(const OSMetaClassBase
*anObject
)
224 bool deleted
= false;
227 for (i
= 0; i
< count
; i
++) {
230 array
[i
-1] = array
[i
];
231 else if( (array
[i
].obj
== anObject
)) {
233 haveUpdated(); // Pity we can't flush the log
234 array
[i
].obj
->taggedRelease(OSTypeID(OSCollection
));
242 bool OSOrderedSet::containsObject(const OSMetaClassBase
*anObject
) const
244 return anObject
&& member(anObject
);
247 bool OSOrderedSet::member(const OSMetaClassBase
*anObject
) const
252 (i
< count
) && (array
[i
].obj
!= anObject
);
259 OSObject
*OSOrderedSet::getObject( unsigned int index
) const
265 // *pri = array[index].pri;
267 return( (OSObject
*) array
[index
].obj
);
270 OSObject
*OSOrderedSet::getFirstObject() const
273 return( (OSObject
*) array
[0].obj
);
278 OSObject
*OSOrderedSet::getLastObject() const
281 return( (OSObject
*) array
[count
-1].obj
);
286 SInt32
OSOrderedSet::orderObject( const OSMetaClassBase
* anObject
)
288 return( ORDER( anObject
, 0 ));
291 void *OSOrderedSet::getOrderingRef()
296 bool OSOrderedSet::isEqualTo(const OSOrderedSet
*anOrderedSet
) const
300 if ( this == anOrderedSet
)
303 if ( count
!= anOrderedSet
->getCount() )
306 for ( i
= 0; i
< count
; i
++ ) {
307 if ( !array
[i
].obj
->isEqualTo(anOrderedSet
->getObject(i
)) )
314 bool OSOrderedSet::isEqualTo(const OSMetaClassBase
*anObject
) const
318 oSet
= OSDynamicCast(OSOrderedSet
, anObject
);
320 return isEqualTo(oSet
);
325 unsigned int OSOrderedSet::iteratorSize() const
327 return( sizeof(unsigned int));
330 bool OSOrderedSet::initIterator(void *inIterator
) const
332 unsigned int *iteratorP
= (unsigned int *) inIterator
;
339 getNextObjectForIterator(void *inIterator
, OSObject
**ret
) const
341 unsigned int *iteratorP
= (unsigned int *) inIterator
;
342 unsigned int index
= (*iteratorP
)++;
345 *ret
= (OSObject
*) array
[index
].obj
;
353 unsigned OSOrderedSet::setOptions(unsigned options
, unsigned mask
, void *)
355 unsigned old
= super::setOptions(options
, mask
);
356 if ((old
^ options
) & mask
) {
358 // Value changed need to recurse over all of the child collections
359 for ( unsigned i
= 0; i
< count
; i
++ ) {
360 OSCollection
*coll
= OSDynamicCast(OSCollection
, array
[i
].obj
);
362 coll
->setOptions(options
, mask
);
369 OSCollection
* OSOrderedSet::copyCollection(OSDictionary
*cycleDict
)
371 bool allocDict
= !cycleDict
;
372 OSCollection
*ret
= 0;
373 OSOrderedSet
*newSet
= 0;
376 cycleDict
= OSDictionary::withCapacity(16);
383 ret
= super::copyCollection(cycleDict
);
387 // Duplicate the set with no contents
388 newSet
= OSOrderedSet::withCapacity(capacity
, ordering
, orderingRef
);
392 // Insert object into cycle Dictionary
393 cycleDict
->setObject((const OSSymbol
*) this, newSet
);
395 newSet
->capacityIncrement
= capacityIncrement
;
397 // Now copy over the contents to the new duplicate
398 for (unsigned int i
= 0; i
< count
; i
++) {
399 OSObject
*obj
= EXT_CAST(array
[i
].obj
);
400 OSCollection
*coll
= OSDynamicCast(OSCollection
, obj
);
402 OSCollection
*newColl
= coll
->copyCollection(cycleDict
);
404 obj
= newColl
; // Rely on cycleDict ref for a bit
410 newSet
->setLastObject(obj
);
423 cycleDict
->release();