]>
git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/c++/OSSet.h
bec190e9f5a5a1e66102ce9420572d5b47b00482
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@
28 /* IOSet.h created by rsulack on Thu 11-Jun-1998 */
29 /* IOSet.h converted to C++ by gvdl on Fri 1998-10-30 */
34 #include <libkern/c++/OSCollection.h>
42 * This header declares the OSSet collection class.
50 * OSSet provides an unordered set store of objects.
53 * OSSet is a container for Libkern C++ objects
55 * @link //apple_ref/doc/class/OSMetaClassBase OSMetaClassBase@/link,
56 * in particular @link //apple_ref/doc/class/OSObject OSObject@/link).
57 * Storage and access follow basic set logic: you can add or remove an object,
58 * and test whether the set contains a particular object.
59 * A given object is only stored in the set once,
60 * and there is no ordering of objects in the set.
61 * A subclass @link //apple_ref/doc/class/OSOrderedSet OSOrderedSet@/link,
62 * provides for ordered set logic.
64 * As with all Libkern collection classes,
65 * OSSet retains objects added to it,
66 * and releases objects removed from it.
67 * An OSSet also grows as necessary to accommodate new objects,
68 * <i>unlike</i> Core Foundation collections (it does not, however, shrink).
70 * <b>Use Restrictions</b>
72 * With very few exceptions in the I/O Kit, all Libkern-based C++
73 * classes, functions, and macros are <b>unsafe</b>
74 * to use in a primary interrupt context.
75 * Consult the I/O Kit documentation related to primary interrupts
76 * for more information.
78 * OSSet provides no concurrency protection;
79 * it's up to the usage context to provide any protection necessary.
80 * Some portions of the I/O Kit, such as
81 * @link //apple_ref/doc/class/IORegistryEntry IORegistryEntry@/link,
82 * handle synchronization via defined member functions for setting
85 class OSSet
: public OSCollection
87 friend class OSSerialize
;
89 OSDeclareDefaultStructors(OSSet
)
91 #if APPLE_KEXT_ALIGN_CONTAINERS
96 #else /* APPLE_KEXT_ALIGN_CONTAINERS */
102 struct ExpansionData
{ };
104 /* Reserved for future use. (Internal use only) */
105 ExpansionData
* reserved
;
107 #endif /* APPLE_KEXT_ALIGN_CONTAINERS */
110 * OSCollectionIterator interfaces.
112 virtual unsigned int iteratorSize() const APPLE_KEXT_OVERRIDE
;
113 virtual bool initIterator(void * iterator
) const APPLE_KEXT_OVERRIDE
;
114 virtual bool getNextObjectForIterator(void * iterator
, OSObject
** ret
) const APPLE_KEXT_OVERRIDE
;
120 * @function withCapacity
123 * Creates and initializes an empty OSSet.
125 * @param capacity The initial storage capacity of the new set object.
128 * An empty instance of OSSet
129 * with a retain count of 1;
130 * <code>NULL</code> on failure.
133 * <code>capacity</code> must be nonzero.
134 * The new OSSet will grow as needed to accommodate more key/object pairs
135 * (<i>unlike</i> @link //apple_ref/doc/uid/20001503 CFMutableSet@/link,
136 * for which the initial capacity is a hard limit).
138 static OSSet
* withCapacity(unsigned int capacity
);
142 * @function withObjects
145 * Creates and initializes an OSSet
146 * populated with objects provided.
148 * @param objects A C array of OSMetaClassBase-derived objects.
149 * @param count The number of objects to be placed into the set.
150 * @param capacity The initial storage capacity of the new set object.
151 * If 0, <code>count</code> is used; otherwise this value
152 * must be greater than or equal to <code>count</code>.
155 * An instance of OSSet
156 * containing the objects provided,
157 * with a retain count of 1;
158 * <code>NULL</code> on failure.
161 * <code>objects</code> must be non-<code>NULL</code>,
162 * and <code>count</code> must be nonzero.
163 * If <code>capacity</code> is nonzero,
164 * it must be greater than or equal to <code>count</code>.
165 * The new OSSet will grow as needed to accommodate more objects
166 * (<i>unlike</i> @link //apple_ref/doc/uid/20001503 CFMutableSet@/link,
167 * for which the initial capacity is a hard limit).
169 * The objects in <code>objects</code> are retained for storage in the new set,
172 static OSSet
* withObjects(
173 const OSObject
* objects
[],
175 unsigned int capacity
= 0);
179 * @function withArray
182 * Creates and initializes an OSSet
183 * populated with the contents of an OSArray.
185 * @param array An array whose objects will be stored in the new OSSet.
186 * @param capacity The initial storage capacity of the new set object.
187 * If 0, the capacity is set to the number of objects
188 * in <code>array</code>;
189 * otherwise <code>capacity</code> must be greater than or equal to
190 * the number of objects in <code>array</code>.
192 * An instance of OSSet containing
193 * the objects of <code>array</code>,
194 * with a retain count of 1;
195 * <code>NULL</code> on failure.
198 * Each distinct object in <code>array</code> is added to the new set.
200 * <code>array</code> must be non-<code>NULL</code>.
201 * If <code>capacity</code> is nonzero,
202 * it must be greater than or equal to <code>count</code>.
203 * The new OSSet will grow as needed to accommodate more key-object pairs
204 * (<i>unlike</i> @link //apple_ref/doc/uid/20001503 CFMutableSet@/link,
205 * for which the initial capacity is a hard limit).
207 * The objects in <code>array</code> are retained for storage in the new set,
210 static OSSet
* withArray(
211 const OSArray
* array
,
212 unsigned int capacity
= 0);
219 * Creates and initializes an OSSet
220 * populated with the contents of another OSSet.
222 * @param set An OSSet whose contents will be stored
223 * in the new instance.
224 * @param capacity The initial storage capacity of the set object.
225 * If 0, the capacity is set to the number of objects
226 * in <code>set</code>;
227 * otherwise <code>capacity</code> must be greater than or equal to
228 * the number of objects in <code>array</code>.
230 * An instance of OSArray
231 * containing the objects of <code>set</code>,
232 * with a retain count of 1;
233 * <code>NULL</code> on failure.
236 * <code>set</code> must be non-<code>NULL</code>.
237 * If <code>capacity</code> is nonzero,
238 * it must be greater than or equal to <code>count</code>.
239 * The array will grow as needed to accommodate more key-object pairs
240 * (<i>unlike</i> @link //apple_ref/doc/uid/20001503 CFMutableSet@/link,
241 * for which the initial capacity is a hard limit).
243 * The objects in <code>set</code> are retained for storage in the new set,
246 static OSSet
* withSet(const OSSet
* set
,
247 unsigned int capacity
= 0);
251 * @function initWithCapacity
254 * Initializes a new instance of OSSet.
256 * @param capacity The initial storage capacity of the new set object.
259 * <code>true</code> on success, <code>false</code> on failure.
262 * Not for general use. Use the static instance creation method
264 * //apple_ref/cpp/clm/OSSet/withCapacity/staticOSSet*\/(unsignedint)
265 * withCapacity@/link</code>
268 * <code>capacity</code> must be nonzero.
269 * The new set will grow as needed to accommodate more key/object pairs
270 * (<i>unlike</i> @link //apple_ref/doc/uid/20001503 CFMutableSet@/link,
271 * for which the initial capacity is a hard limit).
273 virtual bool initWithCapacity(unsigned int capacity
);
277 * @function initWithObjects
280 * Initializes a new OSSet populated with objects provided.
282 * @param objects A C array of OSObject-derived objects.
283 * @param count The number of objects to be placed into the set.
284 * @param capacity The initial storage capacity of the new set object.
285 * If 0, <code>count</code> is used; otherwise this value
286 * must be greater than or equal to <code>count</code>.
289 * <code>true</code> on success, <code>false</code> on failure.
292 * Not for general use. Use the static instance creation method
294 * //apple_ref/cpp/clm/OSSet/withObjects/staticOSSet*\/(constOSObject*,unsignedint,unsignedint)
295 * withObjects@/link</code>
298 * <code>objects</code> must be non-<code>NULL</code>,
299 * and <code>count</code> must be nonzero.
300 * If <code>capacity</code> is nonzero, it must be greater than or equal to <code>count</code>.
301 * The new array will grow as needed to accommodate more key-object pairs
302 * (<i>unlike</i> @link //apple_ref/doc/uid/20001503 CFMutableSet@/link,
303 * for which the initial capacity is a hard limit).
305 * The objects in <code>objects</code> are retained for storage in the new set,
308 virtual bool initWithObjects(
309 const OSObject
* objects
[],
311 unsigned int capacity
= 0);
315 * @function initWithArray
317 * @abstract Initializes a new OSSet
318 * populated with the contents of an OSArray.
320 * @param array An OSAray whose contents will be placed
321 * in the new instance.
322 * @param capacity The initial storage capacity of the new set object.
323 * If 0, the capacity is set
324 * to the number of objects in <code>array</code>;
325 * otherwise <code>capacity</code> must be greater than or equal to
326 * the number of objects in <code>array</code>.
329 * <code>true</code> on success, <code>false</code> on failure.
332 * Not for general use. Use the static instance creation method
334 * //apple_ref/cpp/clm/OSSet/withArray/staticOSSet*\/(constOSArray*,unsignedint)
335 * withArray@/link</code>
338 * <code>array</code> must be non-<code>NULL</code>.
339 * If <code>capacity</code> is nonzero,
340 * it must be greater than or equal to <code>count</code>.
341 * The new array will grow as needed to accommodate more key-object pairs
342 * (<i>unlike</i> @link //apple_ref/doc/uid/20001503 CFMutableSet@/link,
343 * for which the initial capacity is a hard limit).
345 * The objects in <code>array</code> are retained for storage in the new set,
348 virtual bool initWithArray(
349 const OSArray
* array
,
350 unsigned int capacity
= 0);
354 * @function initWithSet
357 * Initializes a new OSSet
358 * populated with the contents of another OSSet.
360 * @param set A set whose contents will be placed in the new instance.
361 * @param capacity The initial storage capacity of the new set object.
362 * If 0, the capacity is set
363 * to the number of objects in <code>set</code>;
364 * otherwise <code>capacity</code> must be greater than or equal to
365 * the number of objects in <code>set</code>.
368 * <code>true</code> on success, <code>false</code> on failure.
371 * Not for general use. Use the static instance creation method
372 * <code>@link withSet withSet@/link</code> instead.
374 * <code>set</code> must be non-<code>NULL</code>.
375 * If <code>capacity</code> is nonzero,
376 * it must be greater than or equal to <code>count</code>.
377 * The new set will grow as needed to accommodate more key-object pairs
378 * (<i>unlike</i> @link //apple_ref/doc/uid/20001503 CFMutableSet@/link,
379 * for which the initial capacity is a hard limit).
381 * The objects in <code>set</code> are retained for storage in the new set,
384 virtual bool initWithSet(const OSSet
*set
,
385 unsigned int capacity
= 0);
392 * Deallocates or releases any resources
393 * used by the OSSet instance.
396 * This function should not be called directly;
399 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/()
400 * release@/link</code>
403 virtual void free() APPLE_KEXT_OVERRIDE
;
410 * Returns the current number of objects within the set.
413 * The current number of objects within the set.
415 virtual unsigned int getCount() const APPLE_KEXT_OVERRIDE
;
419 * @function getCapacity
422 * Returns the number of objects the set
423 * can store without reallocating.
426 * The number objects the set
427 * can store without reallocating.
430 * OSSet objects grow when full to accommodate additional objects.
433 * //apple_ref/cpp/instm/OSSet/getCapacityIncrement/virtualunsignedint/()
434 * getCapacityIncrement@/link</code>
437 * //apple_ref/cpp/instm/OSSet/ensureCapacity/virtualunsignedint/(unsignedint)
438 * ensureCapacity@/link</code>.
440 virtual unsigned int getCapacity() const APPLE_KEXT_OVERRIDE
;
444 * @function getCapacityIncrement
447 * Returns the storage increment of the set.
450 * The storage increment of the set.
453 * An OSSet allocates storage for objects in multiples
454 * of the capacity increment.
456 virtual unsigned int getCapacityIncrement() const APPLE_KEXT_OVERRIDE
;
460 * @function setCapacityIncrement
463 * Sets the storage increment of the set.
466 * The new storage increment of the set,
467 * which may be different from the number requested.
470 * An OSSet allocates storage for objects in multiples
471 * of the capacity increment.
472 * Calling this function does not immediately reallocate storage.
474 virtual unsigned int setCapacityIncrement(unsigned increment
) APPLE_KEXT_OVERRIDE
;
478 * @function ensureCapacity
481 * Ensures the set has enough space
482 * to store the requested number of distinct objects.
484 * @param newCapacity The total number of distinct objects the set
485 * should be able to store.
487 * The new capacity of the set,
488 * which may be different from the number requested
489 * (if smaller, reallocation of storage failed).
492 * This function immediately resizes the set, if necessary,
493 * to accommodate at least <code>newCapacity</code> distinct objects.
494 * If <code>newCapacity</code> is not greater than the current capacity,
495 * or if an allocation error occurs, the original capacity is returned.
497 * There is no way to reduce the capacity of an OSSet.
499 virtual unsigned int ensureCapacity(unsigned int newCapacity
) APPLE_KEXT_OVERRIDE
;
503 * @function flushCollection
506 * Removes and releases all objects within the set.
509 * The set's capacity (and therefore direct memory consumption)
510 * is not reduced by this function.
512 virtual void flushCollection() APPLE_KEXT_OVERRIDE
;
516 * @function setObject
519 * Adds an object to the OSSet if it is not already present.
521 * @param anObject The OSMetaClassBase-derived object to be added to the set.
524 * <code>true</code> if <code>anObject</code> was successfully
525 * added to the set, <code>false</code> otherwise
526 * (including if it was already in the set).
529 * The set adds storage to accomodate the new object, if necessary.
530 * If successfully added, the object is retained.
532 * A <code>false</code> return value can mean either
533 * that <code>anObject</code> is already present in the set,
534 * or that a memory allocation failure occurred.
535 * If you need to know whether the object
536 * is already present, use
537 * <code>@link containsObject containsObject@/link</code>.
539 virtual bool setObject(const OSMetaClassBase
* anObject
);
546 * Adds the contents of an OSArray to the set.
548 * @param array The OSArray object containing the objects to be added.
551 * <code>true</code> if all objects from <code>array</code>
552 * are successfully added the receiver (or were already present),
553 * <code>false</code> otherwise.
556 * This functions adds to the receiving set
557 * all objects from <code>array</code>
558 * that are not already in the receiving set.
559 * Objects added to the receiver are retained.
561 * In releases prior to 10.7, this function would return <code>false</code>
562 * if an object from <code>array</code> was already present in the set,
563 * or if <code>array</code> was empty.
564 * This is no longer the case, so this function correctly returns <code>true</code>
565 * when the semantic of merging is met.
567 virtual bool merge(const OSArray
* array
);
574 * Adds the contents of an OSet to the set.
576 * @param set The OSSet object containing the objects to be added.
579 * <code>true</code> if any object from <code>set</code>
580 * are successfully added the receiver (or were already present),
581 * <code>false</code> otherwise.
584 * This functions adds to the receiving set
585 * all objects from <code>set</code>
586 * that are not already in the receiving set.
587 * Objects added to the receiver are retained.
589 * In releases prior to 10.7, this function would return <code>false</code>
590 * if an object from <code>set</code> was already present in the set,
591 * or if <code>set</code> was empty.
592 * This is no longer the case, so this function correctly returns <code>true</code>
593 * when the semantic of merging is met.
595 virtual bool merge(const OSSet
* set
);
599 * @function removeObject
602 * Removes an object from the set.
604 * @param anObject The OSMetaClassBase-derived object
605 * to be removed from the set.
608 * The object removed from the set is released.
610 virtual void removeObject(const OSMetaClassBase
* anObject
);
614 * @function containsObject
617 * Checks the set for the presence of an object.
619 * @param anObject The OSMetaClassBase-derived object
620 * to check for in the set.
623 * <code>true</code> if <code>anObject</code> is present within the set,
624 * <code>false</code> otherwise.
627 * Pointer equality is used.
628 * This function returns <code>false</code> if passed <code>NULL</code>.
630 virtual bool containsObject(const OSMetaClassBase
* anObject
) const;
637 * Checks the set for the presence of an object.
639 * @param anObject The OSMetaClassBase-derived object
640 * to check for in the set.
643 * <code>true</code> if <code>anObject</code> is present
644 * within the set, <code>false</code> otherwise.
647 * Pointer equality is used. This function returns <code>false</code>
648 * if passed <code>NULL</code>.
650 * <code>@link containsObject containsObject@/link</code>
651 * checks for <code>NULL</code> first,
652 * and is therefore more efficient than this function.
654 virtual bool member(const OSMetaClassBase
* anObject
) const;
658 * @function getAnyObject
661 * Returns an arbitrary (not random) object from the set.
664 * An arbitrary (not random) object
665 * if one exists within the set.
668 * The returned object will be released if removed from the set;
669 * if you plan to store the reference, you should call
671 * //apple_ref/cpp/instm/OSObject/retain/virtualvoid/()
672 * retain@/link</code>
675 virtual OSObject
* getAnyObject() const;
679 * @function isEqualTo
682 * Tests the equality of two OSSet objects.
684 * @param aSet The set object being compared against the receiver.
686 * <code>true</code> if the two sets are equivalent,
687 * <code>false</code> otherwise.
690 * Two OSSet objects are considered equal if they have same count
691 * and the same object pointer values.
693 virtual bool isEqualTo(const OSSet
* aSet
) const;
697 * @function isEqualTo
700 * Tests the equality of an OSSet against an arbitrary object.
702 * @param anObject The object being compared against the receiver.
704 * <code>true</code> if the two objects are equivalent,
705 * <code>false</code> otherwise.
708 * An OSSet object is considered equal to another object if the other object
709 * is derived from OSSet and compares equal as a set.
711 virtual bool isEqualTo(const OSMetaClassBase
* anObject
) const APPLE_KEXT_OVERRIDE
;
715 * @function serialize
718 * Archives the receiver into the provided
719 * @link //apple_ref/doc/class/OSSerialize OSSerialize@/link object.
721 * @param serializer The OSSerialize object.
724 * <code>true</code> if serialization succeeds, <code>false</code> if not.
726 virtual bool serialize(OSSerialize
* serializer
) const APPLE_KEXT_OVERRIDE
;
730 * @function setOptions
733 * Recursively sets option bits in the set
734 * and all child collections.
736 * @param options A bitfield whose values turn the options on (1) or off (0).
737 * @param mask A mask indicating which bits
738 * in <code>options</code> to change.
739 * Pass 0 to get the whole current options bitfield
740 * without changing any settings.
741 * @param context Unused.
744 * The options bitfield as it was before the set operation.
747 * Kernel extensions should not call this function.
749 * Child collections' options are changed only if the receiving set's
750 * options actually change.
752 virtual unsigned setOptions(unsigned options
, unsigned mask
, void * context
= 0) APPLE_KEXT_OVERRIDE
;
756 * @function copyCollection
759 * Creates a deep copy of this set and its child collections.
761 * @param cycleDict A dictionary of all of the collections
762 * that have been copied so far,
763 * which is used to track circular references.
764 * To start the copy at the top level,
765 * pass <code>NULL</code>.
768 * The newly copied set, with a retain count of 1,
769 * or <code>NULL</code> if there is insufficient memory to do the copy.
772 * The receiving set, and any collections it contains,
773 * recursively, are copied.
774 * Objects that are not derived from OSCollection are retained
775 * rather than copied.
777 OSCollection
*copyCollection(OSDictionary
*cycleDict
= 0) APPLE_KEXT_OVERRIDE
;
779 OSMetaClassDeclareReservedUnused(OSSet
, 0);
780 OSMetaClassDeclareReservedUnused(OSSet
, 1);
781 OSMetaClassDeclareReservedUnused(OSSet
, 2);
782 OSMetaClassDeclareReservedUnused(OSSet
, 3);
783 OSMetaClassDeclareReservedUnused(OSSet
, 4);
784 OSMetaClassDeclareReservedUnused(OSSet
, 5);
785 OSMetaClassDeclareReservedUnused(OSSet
, 6);
786 OSMetaClassDeclareReservedUnused(OSSet
, 7);
789 #endif /* !_OS_OSSET_H */