2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 #include <libkern/c++/OSDictionary.h>
24 #include <libkern/c++/OSOrderedSet.h>
25 #include <libkern/c++/OSLib.h>
27 #define super OSCollection
29 OSDefineMetaClassAndStructors(OSOrderedSet
, OSCollection
)
30 OSMetaClassDefineReservedUnused(OSOrderedSet
, 0);
31 OSMetaClassDefineReservedUnused(OSOrderedSet
, 1);
32 OSMetaClassDefineReservedUnused(OSOrderedSet
, 2);
33 OSMetaClassDefineReservedUnused(OSOrderedSet
, 3);
34 OSMetaClassDefineReservedUnused(OSOrderedSet
, 4);
35 OSMetaClassDefineReservedUnused(OSOrderedSet
, 5);
36 OSMetaClassDefineReservedUnused(OSOrderedSet
, 6);
37 OSMetaClassDefineReservedUnused(OSOrderedSet
, 7);
41 extern int debug_container_malloc_size
;
43 #define ACCUMSIZE(s) do { debug_container_malloc_size += (s); } while(0)
49 const OSMetaClassBase
* obj
;
53 #define EXT_CAST(obj) \
54 reinterpret_cast<OSObject *>(const_cast<OSMetaClassBase *>(obj))
57 initWithCapacity(unsigned int inCapacity
,
58 OSOrderFunction inOrdering
, void *inOrderingRef
)
65 size
= sizeof(_Element
) * inCapacity
;
66 array
= (_Element
*) kalloc(size
);
71 capacity
= inCapacity
;
72 capacityIncrement
= (inCapacity
)? inCapacity
: 16;
73 ordering
= inOrdering
;
74 orderingRef
= inOrderingRef
;
82 OSOrderedSet
* OSOrderedSet::
83 withCapacity(unsigned int capacity
,
84 OSOrderFunction ordering
, void * orderingRef
)
86 OSOrderedSet
*me
= new OSOrderedSet
;
88 if (me
&& !me
->initWithCapacity(capacity
, ordering
, orderingRef
)) {
96 void OSOrderedSet::free()
98 (void) super::setOptions(0, kImmutable
);
102 kfree((vm_offset_t
)array
, sizeof(_Element
) * capacity
);
103 ACCUMSIZE( -(sizeof(_Element
) * capacity
) );
109 unsigned int OSOrderedSet::getCount() const { return count
; }
110 unsigned int OSOrderedSet::getCapacity() const { return capacity
; }
111 unsigned int OSOrderedSet::getCapacityIncrement() const
112 { return capacityIncrement
; }
113 unsigned int OSOrderedSet::setCapacityIncrement(unsigned int increment
)
115 capacityIncrement
= (increment
)? increment
: 16;
116 return capacityIncrement
;
119 unsigned int OSOrderedSet::ensureCapacity(unsigned int newCapacity
)
122 int oldSize
, newSize
;
124 if (newCapacity
<= capacity
)
128 newCapacity
= (((newCapacity
- 1) / capacityIncrement
) + 1)
130 newSize
= sizeof(_Element
) * newCapacity
;
132 newArray
= (_Element
*) kalloc(newSize
);
134 oldSize
= sizeof(_Element
) * capacity
;
136 ACCUMSIZE(newSize
- oldSize
);
138 bcopy(array
, newArray
, oldSize
);
139 bzero(&newArray
[capacity
], newSize
- oldSize
);
140 kfree((vm_offset_t
)array
, oldSize
);
142 capacity
= newCapacity
;
148 void OSOrderedSet::flushCollection()
154 for (i
= 0; i
< count
; i
++)
155 array
[i
].obj
->taggedRelease(OSTypeID(OSCollection
));
161 bool OSOrderedSet::setObject(unsigned int index
, const OSMetaClassBase
*anObject
)
164 unsigned int newCount
= count
+ 1;
166 if ((index
> count
) || !anObject
)
169 if (containsObject(anObject
))
172 // do we need more space?
173 if (newCount
> capacity
&& newCount
> ensureCapacity(newCount
))
177 if (index
!= count
) {
178 for (i
= count
; i
> index
; i
--)
179 array
[i
] = array
[i
-1];
181 array
[index
].obj
= anObject
;
182 // array[index].pri = pri;
183 anObject
->taggedRetain(OSTypeID(OSCollection
));
190 bool OSOrderedSet::setFirstObject(const OSMetaClassBase
*anObject
)
192 return( setObject(0, anObject
));
195 bool OSOrderedSet::setLastObject(const OSMetaClassBase
*anObject
)
197 return( setObject( count
, anObject
));
201 #define ORDER(obj1,obj2) \
202 (ordering ? ((*ordering)( (OSObject *) obj1, (OSObject *) obj2, orderingRef)) : 0)
204 bool OSOrderedSet::setObject(const OSMetaClassBase
*anObject
)
208 // queue it behind those with same priority
210 (i
< count
) && (ORDER(array
[i
].obj
, anObject
) >= 0);
213 return( setObject(i
, anObject
));
216 void OSOrderedSet::removeObject(const OSMetaClassBase
*anObject
)
218 bool deleted
= false;
221 for (i
= 0; i
< count
; i
++) {
224 array
[i
-1] = array
[i
];
225 else if( (array
[i
].obj
== anObject
)) {
227 haveUpdated(); // Pity we can't flush the log
228 array
[i
].obj
->taggedRelease(OSTypeID(OSCollection
));
236 bool OSOrderedSet::containsObject(const OSMetaClassBase
*anObject
) const
238 return anObject
&& member(anObject
);
241 bool OSOrderedSet::member(const OSMetaClassBase
*anObject
) const
246 (i
< count
) && (array
[i
].obj
!= anObject
);
253 OSObject
*OSOrderedSet::getObject( unsigned int index
) const
259 // *pri = array[index].pri;
261 return( (OSObject
*) array
[index
].obj
);
264 OSObject
*OSOrderedSet::getFirstObject() const
267 return( (OSObject
*) array
[0].obj
);
272 OSObject
*OSOrderedSet::getLastObject() const
275 return( (OSObject
*) array
[count
-1].obj
);
280 SInt32
OSOrderedSet::orderObject( const OSMetaClassBase
* anObject
)
282 return( ORDER( anObject
, 0 ));
285 void *OSOrderedSet::getOrderingRef()
290 bool OSOrderedSet::isEqualTo(const OSOrderedSet
*anOrderedSet
) const
294 if ( this == anOrderedSet
)
297 if ( count
!= anOrderedSet
->getCount() )
300 for ( i
= 0; i
< count
; i
++ ) {
301 if ( !array
[i
].obj
->isEqualTo(anOrderedSet
->getObject(i
)) )
308 bool OSOrderedSet::isEqualTo(const OSMetaClassBase
*anObject
) const
312 oSet
= OSDynamicCast(OSOrderedSet
, anObject
);
314 return isEqualTo(oSet
);
319 unsigned int OSOrderedSet::iteratorSize() const
321 return( sizeof(unsigned int));
324 bool OSOrderedSet::initIterator(void *inIterator
) const
326 unsigned int *iteratorP
= (unsigned int *) inIterator
;
333 getNextObjectForIterator(void *inIterator
, OSObject
**ret
) const
335 unsigned int *iteratorP
= (unsigned int *) inIterator
;
336 unsigned int index
= (*iteratorP
)++;
339 *ret
= (OSObject
*) array
[index
].obj
;
347 unsigned OSOrderedSet::setOptions(unsigned options
, unsigned mask
, void *)
349 unsigned old
= super::setOptions(options
, mask
);
350 if ((old
^ options
) & mask
) {
352 // Value changed need to recurse over all of the child collections
353 for ( unsigned i
= 0; i
< count
; i
++ ) {
354 OSCollection
*coll
= OSDynamicCast(OSCollection
, array
[i
].obj
);
356 coll
->setOptions(options
, mask
);
363 OSCollection
* OSOrderedSet::copyCollection(OSDictionary
*cycleDict
)
365 bool allocDict
= !cycleDict
;
366 OSCollection
*ret
= 0;
367 OSOrderedSet
*newSet
= 0;
370 cycleDict
= OSDictionary::withCapacity(16);
377 ret
= super::copyCollection(cycleDict
);
381 // Duplicate the set with no contents
382 newSet
= OSOrderedSet::withCapacity(capacity
, ordering
, orderingRef
);
386 // Insert object into cycle Dictionary
387 cycleDict
->setObject((const OSSymbol
*) this, newSet
);
389 newSet
->capacityIncrement
= capacityIncrement
;
391 // Now copy over the contents to the new duplicate
392 for (unsigned int i
= 0; i
< count
; i
++) {
393 OSObject
*obj
= EXT_CAST(array
[i
].obj
);
394 OSCollection
*coll
= OSDynamicCast(OSCollection
, obj
);
396 OSCollection
*newColl
= coll
->copyCollection(cycleDict
);
398 obj
= newColl
; // Rely on cycleDict ref for a bit
404 newSet
->setLastObject(obj
);
417 cycleDict
->release();