2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #include <libkern/c++/OSDictionary.h>
25 #include <libkern/c++/OSOrderedSet.h>
26 #include <libkern/c++/OSLib.h>
28 #define super OSCollection
30 OSDefineMetaClassAndStructors(OSOrderedSet
, OSCollection
)
31 OSMetaClassDefineReservedUnused(OSOrderedSet
, 0);
32 OSMetaClassDefineReservedUnused(OSOrderedSet
, 1);
33 OSMetaClassDefineReservedUnused(OSOrderedSet
, 2);
34 OSMetaClassDefineReservedUnused(OSOrderedSet
, 3);
35 OSMetaClassDefineReservedUnused(OSOrderedSet
, 4);
36 OSMetaClassDefineReservedUnused(OSOrderedSet
, 5);
37 OSMetaClassDefineReservedUnused(OSOrderedSet
, 6);
38 OSMetaClassDefineReservedUnused(OSOrderedSet
, 7);
42 extern int debug_container_malloc_size
;
44 #define ACCUMSIZE(s) do { debug_container_malloc_size += (s); } while(0)
50 const OSMetaClassBase
* obj
;
54 #define EXT_CAST(obj) \
55 reinterpret_cast<OSObject *>(const_cast<OSMetaClassBase *>(obj))
58 initWithCapacity(unsigned int inCapacity
,
59 OSOrderFunction inOrdering
, void *inOrderingRef
)
66 size
= sizeof(_Element
) * inCapacity
;
67 array
= (_Element
*) kalloc(size
);
72 capacity
= inCapacity
;
73 capacityIncrement
= (inCapacity
)? inCapacity
: 16;
74 ordering
= inOrdering
;
75 orderingRef
= inOrderingRef
;
83 OSOrderedSet
* OSOrderedSet::
84 withCapacity(unsigned int capacity
,
85 OSOrderFunction ordering
, void * orderingRef
)
87 OSOrderedSet
*me
= new OSOrderedSet
;
89 if (me
&& !me
->initWithCapacity(capacity
, ordering
, orderingRef
)) {
97 void OSOrderedSet::free()
99 (void) super::setOptions(0, kImmutable
);
103 kfree((vm_offset_t
)array
, sizeof(_Element
) * capacity
);
104 ACCUMSIZE( -(sizeof(_Element
) * capacity
) );
110 unsigned int OSOrderedSet::getCount() const { return count
; }
111 unsigned int OSOrderedSet::getCapacity() const { return capacity
; }
112 unsigned int OSOrderedSet::getCapacityIncrement() const
113 { return capacityIncrement
; }
114 unsigned int OSOrderedSet::setCapacityIncrement(unsigned int increment
)
116 capacityIncrement
= (increment
)? increment
: 16;
117 return capacityIncrement
;
120 unsigned int OSOrderedSet::ensureCapacity(unsigned int newCapacity
)
123 int oldSize
, newSize
;
125 if (newCapacity
<= capacity
)
129 newCapacity
= (((newCapacity
- 1) / capacityIncrement
) + 1)
131 newSize
= sizeof(_Element
) * newCapacity
;
133 newArray
= (_Element
*) kalloc(newSize
);
135 oldSize
= sizeof(_Element
) * capacity
;
137 ACCUMSIZE(newSize
- oldSize
);
139 bcopy(array
, newArray
, oldSize
);
140 bzero(&newArray
[capacity
], newSize
- oldSize
);
141 kfree((vm_offset_t
)array
, oldSize
);
143 capacity
= newCapacity
;
149 void OSOrderedSet::flushCollection()
155 for (i
= 0; i
< count
; i
++)
156 array
[i
].obj
->taggedRelease(OSTypeID(OSCollection
));
162 bool OSOrderedSet::setObject(unsigned int index
, const OSMetaClassBase
*anObject
)
165 unsigned int newCount
= count
+ 1;
167 if ((index
> count
) || !anObject
)
170 if (containsObject(anObject
))
173 // do we need more space?
174 if (newCount
> capacity
&& newCount
> ensureCapacity(newCount
))
178 if (index
!= count
) {
179 for (i
= count
; i
> index
; i
--)
180 array
[i
] = array
[i
-1];
182 array
[index
].obj
= anObject
;
183 // array[index].pri = pri;
184 anObject
->taggedRetain(OSTypeID(OSCollection
));
191 bool OSOrderedSet::setFirstObject(const OSMetaClassBase
*anObject
)
193 return( setObject(0, anObject
));
196 bool OSOrderedSet::setLastObject(const OSMetaClassBase
*anObject
)
198 return( setObject( count
, anObject
));
202 #define ORDER(obj1,obj2) \
203 (ordering ? ((*ordering)( (OSObject *) obj1, (OSObject *) obj2, orderingRef)) : 0)
205 bool OSOrderedSet::setObject(const OSMetaClassBase
*anObject
)
209 // queue it behind those with same priority
211 (i
< count
) && (ORDER(array
[i
].obj
, anObject
) >= 0);
214 return( setObject(i
, anObject
));
217 void OSOrderedSet::removeObject(const OSMetaClassBase
*anObject
)
219 bool deleted
= false;
222 for (i
= 0; i
< count
; i
++) {
225 array
[i
-1] = array
[i
];
226 else if( (array
[i
].obj
== anObject
)) {
228 haveUpdated(); // Pity we can't flush the log
229 array
[i
].obj
->taggedRelease(OSTypeID(OSCollection
));
237 bool OSOrderedSet::containsObject(const OSMetaClassBase
*anObject
) const
239 return anObject
&& member(anObject
);
242 bool OSOrderedSet::member(const OSMetaClassBase
*anObject
) const
247 (i
< count
) && (array
[i
].obj
!= anObject
);
254 OSObject
*OSOrderedSet::getObject( unsigned int index
) const
260 // *pri = array[index].pri;
262 return( (OSObject
*) array
[index
].obj
);
265 OSObject
*OSOrderedSet::getFirstObject() const
268 return( (OSObject
*) array
[0].obj
);
273 OSObject
*OSOrderedSet::getLastObject() const
276 return( (OSObject
*) array
[count
-1].obj
);
281 SInt32
OSOrderedSet::orderObject( const OSMetaClassBase
* anObject
)
283 return( ORDER( anObject
, 0 ));
286 void *OSOrderedSet::getOrderingRef()
291 bool OSOrderedSet::isEqualTo(const OSOrderedSet
*anOrderedSet
) const
295 if ( this == anOrderedSet
)
298 if ( count
!= anOrderedSet
->getCount() )
301 for ( i
= 0; i
< count
; i
++ ) {
302 if ( !array
[i
].obj
->isEqualTo(anOrderedSet
->getObject(i
)) )
309 bool OSOrderedSet::isEqualTo(const OSMetaClassBase
*anObject
) const
313 oSet
= OSDynamicCast(OSOrderedSet
, anObject
);
315 return isEqualTo(oSet
);
320 unsigned int OSOrderedSet::iteratorSize() const
322 return( sizeof(unsigned int));
325 bool OSOrderedSet::initIterator(void *inIterator
) const
327 unsigned int *iteratorP
= (unsigned int *) inIterator
;
334 getNextObjectForIterator(void *inIterator
, OSObject
**ret
) const
336 unsigned int *iteratorP
= (unsigned int *) inIterator
;
337 unsigned int index
= (*iteratorP
)++;
340 *ret
= (OSObject
*) array
[index
].obj
;
348 unsigned OSOrderedSet::setOptions(unsigned options
, unsigned mask
, void *)
350 unsigned old
= super::setOptions(options
, mask
);
351 if ((old
^ options
) & mask
) {
353 // Value changed need to recurse over all of the child collections
354 for ( unsigned i
= 0; i
< count
; i
++ ) {
355 OSCollection
*coll
= OSDynamicCast(OSCollection
, array
[i
].obj
);
357 coll
->setOptions(options
, mask
);
364 OSCollection
* OSOrderedSet::copyCollection(OSDictionary
*cycleDict
)
366 bool allocDict
= !cycleDict
;
367 OSCollection
*ret
= 0;
368 OSOrderedSet
*newSet
= 0;
371 cycleDict
= OSDictionary::withCapacity(16);
378 ret
= super::copyCollection(cycleDict
);
382 // Duplicate the set with no contents
383 newSet
= OSOrderedSet::withCapacity(capacity
, ordering
, orderingRef
);
387 // Insert object into cycle Dictionary
388 cycleDict
->setObject((const OSSymbol
*) this, newSet
);
390 newSet
->capacityIncrement
= capacityIncrement
;
392 // Now copy over the contents to the new duplicate
393 for (unsigned int i
= 0; i
< count
; i
++) {
394 OSObject
*obj
= EXT_CAST(array
[i
].obj
);
395 OSCollection
*coll
= OSDynamicCast(OSCollection
, obj
);
397 OSCollection
*newColl
= coll
->copyCollection(cycleDict
);
399 obj
= newColl
; // Rely on cycleDict ref for a bit
405 newSet
->setLastObject(obj
);
418 cycleDict
->release();