]>
git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/c++/OSArray.h
dce9f84e911d2b746aa50a1c41d26ebe78ac4cfa
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 /* IOArray.h created by rsulack on Thu 11-Sep-1997 */
29 /* IOArray.h converted to C++ by gvdl on Fri 1998-10-30 */
34 #include <libkern/c++/OSCollection.h>
42 * This header declares the OSArray collection class.
50 * OSArray provides an indexed store of objects.
53 * OSArray is a container for Libkern C++ objects
55 * @link //apple_ref/doc/class/OSMetaClassBase OSMetaClassBase@/link,
57 * @link //apple_ref/doc/class/OSObject OSObject@/link).
58 * Storage and access are by array index.
60 * You must generally cast retrieved objects from
61 * @link //apple_ref/cpp/cl/OSObject OSObject@/link
62 * to the desired class using
63 * <code>@link //apple_ref/cpp/macro/OSDynamicCast OSDynamicCast@/link</code>.
64 * This macro returns the object cast to the desired class,
65 * or <code>NULL</code> if the object isn't derived from that class.
67 * As with all Libkern collection classes,
68 * OSArray retains objects added to it,
69 * and releases objects removed from it (or replaced).
70 * An OSArray also grows as necessary to accommodate new objects,
71 * <i>unlike</i> Core Foundation collections (it does not, however, shrink).
73 * <b>Use Restrictions</b>
75 * With very few exceptions in the I/O Kit, all Libkern-based C++
76 * classes, functions, and macros are <b>unsafe</b>
77 * to use in a primary interrupt context.
78 * Consult the I/O Kit documentation related to primary interrupts
79 * for more information.
81 * OSArray provides no concurrency protection;
82 * it's up to the usage context to provide any protection necessary.
83 * Some portions of the I/O Kit, such as
84 * @link //apple_ref/doc/class/IORegistryEntry IORegistryEntry@/link,
85 * handle synchronization via defined member functions for setting
88 class OSArray
: public OSCollection
92 OSDeclareDefaultStructors(OSArray
)
95 const OSMetaClassBase
** array
;
97 unsigned int capacity
;
98 unsigned int capacityIncrement
;
100 struct ExpansionData
{ };
102 /* Reserved for future use. (Internal use only) */
103 ExpansionData
* reserved
;
105 /* OSCollectionIterator interfaces. */
106 virtual unsigned int iteratorSize() const;
107 virtual bool initIterator(void * iterator
) const;
108 virtual bool getNextObjectForIterator(void * iterator
, OSObject
** ret
) const;
113 * @function withCapacity
116 * Creates and initializes an empty OSArray.
118 * @param capacity The initial storage capacity of the array object.
121 * An empty instance of OSArray with a retain count of 1;
122 * <code>NULL</code> on failure.
125 * <code>capacity</code> must be nonzero.
126 * The new array will grow as needed to accommodate more objects
127 * (<i>unlike</i> @link //apple_ref/doc/uid/20001502 CFMutableArray@/link,
128 * for which the initial capacity is a hard limit).
130 static OSArray
* withCapacity(unsigned int capacity
);
134 * @function withObjects
137 * Creates and initializes an OSArray populated with objects provided.
139 * @param objects A C array of OSObject-derived instances.
140 * @param count The number of objects to be placed into the array.
141 * @param capacity The initial storage capacity of the array object.
142 * If 0, <code>count</code> is used; otherwise this value
143 * must be greater than or equal to <code>count</code>.
146 * An instance of OSArray containing the objects provided,
147 * with a retain count of 1;
148 * <code>NULL</code> on failure.
151 * <code>objects</code> must be non-<code>NULL</code>, and <code>count</code> must be nonzero.
152 * If <code>capacity</code> is nonzero,
153 * it must be greater than or equal to <code>count</code>.
154 * The new array will grow as needed to accommodate more objects
155 * (<i>unlike</i> @link //apple_ref/doc/uid/20001502 CFMutableArray@/link,
156 * for which the initial capacity is a hard limit).
158 static OSArray
* withObjects(
159 const OSObject
* objects
[],
161 unsigned int capacity
= 0);
165 * @function withArray
168 * Creates and initializes an OSArray populated with the contents of another array.
170 * @param array An OSArray whose contents will be stored
171 * in the new instance.
172 * @param capacity The initial storage capacity of the array object.
173 * If 0, the capacity is set to the number of objects
174 * in <code>array</code>;
175 * otherwise <code>capacity</code> must be
176 * greater than or equal to the number of objects
177 * in <code>array</code>.
180 * An instance of OSArray containing the objects of <code>array</code>,
181 * with a retain count of 1;
182 * <code>NULL</code> on failure.
185 * <code>array</code> must be non-<code>NULL</code>.
186 * If <code>capacity</code> is nonzero,
187 * it must be greater than or equal to <code>count</code>.
188 * The new array will grow as needed to accommodate more objects
189 * (<i>unlike</i> @link //apple_ref/doc/uid/20001502 CFMutableArray@/link,
190 * for which the initial capacity is a hard limit).
192 * The objects in <code>array</code> are retained
193 * for storage in the new OSArray,
196 static OSArray
* withArray(
197 const OSArray
* array
,
198 unsigned int capacity
= 0);
202 * @function initWithCapacity
205 * Initializes a new instance of OSArray.
207 * @param capacity The initial storage capacity of the array object.
210 * <code>true</code> on success, <code>false</code> on failure.
213 * Not for general use. Use the static instance creation method
214 * <code>@link //apple_ref/cpp/clm/OSArray/withCapacity/staticOSArray*\/(unsignedint)
215 * withCapacity@/link</code>
218 * <code>capacity</code> must be nonzero.
219 * The new array will grow as needed to accommodate more objects
220 * (<i>unlike</i> @link //apple_ref/doc/uid/20001502 CFMutableArray@/link,
221 * for which the initial capacity is a hard limit).
223 virtual bool initWithCapacity(unsigned int capacity
);
227 * @function initWithObjects
230 * Initializes a new OSArray populated with objects provided.
232 * @param objects A C array of OSObject-derived objects.
233 * @param count The number of objects to be placed into the array.
234 * @param capacity The initial storage capacity of the array object.
235 * If 0, <code>count</code> is used; otherwise this value
236 * must be greater than or equal to <code>count</code>.
239 * <code>true</code> on success, <code>false</code> on failure.
242 * Not for general use. Use the static instance creation method
244 * //apple_ref/cpp/clm/OSArray/withObjects/staticOSArray*\/(constOSObject*,unsignedint,unsignedint)
245 * withObjects@/link</code>
248 * <code>objects</code> must be non-<code>NULL</code>,
249 * and <code>count</code> must be nonzero.
250 * If <code>capacity</code> is nonzero,
251 * it must be greater than or equal to <code>count</code>.
252 * The new array will grow as needed to accommodate more objects
253 * (<i>unlike</i> @link //apple_ref/doc/uid/20001502 CFMutableArray@/link,
254 * for which the initial capacity is a hard limit).
256 virtual bool initWithObjects(
257 const OSObject
* objects
[],
259 unsigned int capacity
= 0);
262 * @function initWithArray
265 * Initializes a new OSArray populated with the contents of another array.
267 * @param anArray The array whose contents will be placed
268 * in the new instance.
269 * @param capacity The initial storage capacity of the array object.
270 * If 0, the capacity is set to the number of objects
271 * in <code>array</code>;
272 * otherwise <code>capacity</code> must be
273 * greater than or equal to the number of objects
274 * in <code>array</code>.
277 * <code>true</code> on success, <code>false</code> on failure.
280 * Not for general use. Use the static instance creation method
281 * <code>@link //apple_ref/cpp/clm/OSArray/withArray/staticOSArray*\/(constOSArray*,unsignedint)
282 * withArray@/link</code> instead.
284 * <code>array</code> must be non-<code>NULL</code>.
285 * If <code>capacity</code> is nonzero,
286 * it must be greater than or equal to <code>count</code>.
287 * The new array will grow as needed to accommodate more objects
288 * (<i>unlike</i> @link //apple_ref/doc/uid/20001502 CFMutableArray@/link,
289 * for which the initial capacity is a hard limit).
291 * The objects in <code>array</code> are retained for storage in the new OSArray,
294 virtual bool initWithArray(
295 const OSArray
* anArray
,
296 unsigned int capacity
= 0);
303 * Deallocates or releases any resources
304 * used by the OSArray instance.
307 * This function should not be called directly;
310 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/()
311 * release@/link</code>
321 * Returns the current number of objects within the array.
324 * The current number of objects within the array.
326 virtual unsigned int getCount() const;
330 * @function getCapacity
333 * Returns the number of objects the array can store
334 * without reallocating.
337 * The number objects the array can store
338 * without reallocating.
341 * OSArray objects grow when full to accommodate additional objects.
344 * //apple_ref/cpp/instm/OSArray/getCapacity/virtualunsignedint/()
345 * getCapacityIncrement@/link</code>
348 * //apple_ref/cpp/instm/OSArray/ensureCapacity/virtualunsignedint/(unsignedint)
349 * <code>ensureCapacity</code>.@/link
351 virtual unsigned int getCapacity() const;
355 * @function getCapacityIncrement
358 * Returns the storage increment of the array.
361 * The storage increment of the array.
364 * An OSArray allocates storage for objects in multiples
365 * of the capacity increment.
367 virtual unsigned int getCapacityIncrement() const;
371 * @function setCapacityIncrement
374 * Sets the storage increment of the array.
377 * The new storage increment of the array,
378 * which may be different from the number requested.
381 * An OSArray allocates storage for objects in multiples
382 * of the capacity increment.
383 * Calling this function does not immediately reallocate storage.
385 virtual unsigned int setCapacityIncrement(unsigned increment
);
389 * @function ensureCapacity
392 * Ensures the array has enough space
393 * to store the requested number of objects.
395 * @param newCapacity The total number of objects the array
396 * should be able to store.
399 * The new capacity of the array,
400 * which may be different from the number requested
401 * (if smaller, reallocation of storage failed).
404 * This function immediately resizes the array, if necessary,
405 * to accommodate at least <code>newCapacity</code> objects.
406 * If <code>newCapacity</code> is not greater than the current capacity,
407 * or if an allocation error occurs, the original capacity is returned.
409 * There is no way to reduce the capacity of an OSArray.
411 virtual unsigned int ensureCapacity(unsigned int newCapacity
);
415 * @function flushCollection
418 * Removes and releases all objects within the array.
421 * The array's capacity (and therefore direct memory consumption)
422 * is not reduced by this function.
424 virtual void flushCollection();
428 * @function setObject
431 * Appends an object onto the end of the array,
432 * increasing storage if necessary.
434 * @param anObject The object to add to the OSArray instance.
437 * <code>true</code> if the addition of <code>anObject</code> was successful,
438 * <code>false</code> if not.
441 * The array adds storage to accomodate the new object, if necessary.
442 * If successfully added, the object is retained.
444 virtual bool setObject(const OSMetaClassBase
* anObject
);
448 * @function setObject
451 * Inserts or appends an object into the array
452 * at a particular index.
454 * @param index The index in the array at which to insert the object.
455 * Must be less than or equal to the array's count.
456 * @param anObject The object to add to the array.
459 * <code>true</code> if the addition of <code>anObject</code>
460 * was successful, <code>false</code> if not.
463 * This function moves existing objects from <code>index</code> on,
464 * in order to accommodate the new object;
465 * it does not replace an existing object at <code>index</code>. See
467 * //apple_ref/cpp/instm/OSArray/replaceObject/virtualvoid/(unsignedint,constOSMetaClassBase*)
468 * replaceObject@/link</code>.
469 * If successfully added, the object is retained.
471 * The array adds storage to accomodate the new object, if necessary.
472 * Note, however, that this function does not allow for arbirtrary growth
473 * of an array by specifying an index larger than the current count.
474 * If you need to immediately grow an array by an arbitrary amount,
477 * //apple_ref/cpp/instm/OSArray/ensureCapacity/virtualunsignedint/(unsignedint)
478 * ensureCapacity@/link</code>.
480 virtual bool setObject(
482 const OSMetaClassBase
* anObject
);
489 * Appends the contents of an array onto the receiving array.
491 * @param otherArray The array whose contents will be appended
492 * to the receiving array.
494 * <code>true</code> if merging was successful, <code>false</code> otherwise.
497 * This function merely appends one array onto another.
498 * Duplicates are not avoided and no sorting is performed.
499 * Objects successfully added to the receiver are retained.
501 virtual bool merge(const OSArray
* otherArray
);
505 * @function replaceObject
508 * Replaces an object in an array at a given index.
510 * @param index The index of the object to be replaced.
511 * Must be less than the array's count.
512 * @param anObject The object to be placed into the array.
515 * The original object is released and the new object is retained.
517 virtual void replaceObject(
519 const OSMetaClassBase
* anObject
);
523 * @function removeObject
526 * Removes an object from the array.
528 * @param index The index of the object to be removed.
531 * This function moves existing objects to fill the vacated index
532 * so that there are no gaps.
533 * The object removed is released.
535 virtual void removeObject(unsigned int index
);
539 * @function isEqualTo
542 * Tests the equality of two OSArray objects.
544 * @param anArray The array object being compared against the receiver.
547 * <code>true</code> if the two arrays are equivalent,
548 *<code>false</code> otherwise.
551 * Two OSArray objects are considered equal if they have same count
552 * and if the objects at corresponding indices compare as equal using
554 * //apple_ref/cpp/instm/OSMetaClassBase/isEqualTo/virtualbool/(constOSMetaClassBase*)
555 * isEqualTo@/link</code>.
557 virtual bool isEqualTo(const OSArray
* anArray
) const;
561 * @function isEqualTo
564 * Tests the equality of an OSArray to an arbitrary object.
566 * @param anObject The object to be compared against the receiver.
569 * <code>true</code> if the two objects are equivalent,
570 * <code>false</code> otherwise.
573 * An OSArray is considered equal to another object
574 * if that object is derived from OSArray
575 * and contains the same or equivalent objects.
577 virtual bool isEqualTo(const OSMetaClassBase
* anObject
) const;
581 * @function getObject
584 * Return the object stored at a given index.
586 * @param index The index of the object to be returned to caller.
589 * The object stored at <code>index</code>,
590 * or <code>NULL</code> if <code>index</code> lies past the end of the array.
593 * The returned object will be released if removed from the array;
594 * if you plan to store the reference, you should call
596 * //apple_ref/cpp/instm/OSObject/retain/virtualvoid/()
597 * retain@/link</code>
600 virtual OSObject
* getObject(unsigned int index
) const;
604 * @function getLastObject
607 * Returns the last object in the array.
610 * The last object in the array,
611 * or <code>NULL</code> if the array is empty.
614 * The returned object will be released if removed from the array;
615 * if you plan to store the reference, you should call
617 * //apple_ref/cpp/instm/OSObject/retain/virtualvoid/()
618 * retain@/link</code>
621 virtual OSObject
* getLastObject() const;
625 * @function getNextIndexOfObject
628 * Scans the array for the next instance of a specific object
629 * at or beyond a given index.
631 * @param anObject The object to scan for.
632 * @param index The index at which to begin the scan.
635 * The next index of <code>anObject</code> in the array or (-1)
639 * This function uses pointer equivalence, and does not use
641 * //apple_ref/cpp/instm/OSMetaClassBase/isEqualTo/virtualbool/(constOSMetaClassBase*)
642 * isEqualTo@/link</code>.
644 virtual unsigned int getNextIndexOfObject(
645 const OSMetaClassBase
* anObject
,
646 unsigned int index
) const;
649 * @function serialize
652 * Archives the receiver into the provided
653 * @link //apple_ref/doc/class/OSSerialize OSSerialize@/link object.
655 * @param serializer The OSSerialize object.
657 * <code>true</code> if serialization succeeds, <code>false</code> if not.
659 virtual bool serialize(OSSerialize
* serializer
) const;
663 * @function setOptions
666 * Recursively sets option bits in an array
667 * and all child collections.
669 * @param options A bitfield whose values turn the options on (1) or off (0).
670 * @param mask A mask indicating which bits
671 * in <code>options</code> to change.
672 * Pass 0 to get the whole current options bitfield
673 * without changing any settings.
674 * @param context Unused.
677 * The options bitfield as it was before the set operation.
680 * Kernel extensions should not call this function.
682 * Child collections' options are changed only if the receiving array's
683 * options actually change.
685 virtual unsigned setOptions(
692 * @function copyCollection
695 * Creates a deep copy of an array and its child collections.
697 * @param cycleDict A dictionary of all of the collections
698 * that have been copied so far,
699 * which is used to track circular references.
700 * To start the copy at the top level,
701 * pass <code>NULL</code>.
704 * The newly copied array, with a retain count of 1,
705 * or <code>NULL</code> if there is insufficient memory to do the copy.
708 * The receiving array, and any collections it contains,
709 * recursively, are copied.
710 * Objects that are not derived from OSCollection are retained
711 * rather than copied.
713 OSCollection
* copyCollection(OSDictionary
* cycleDict
= 0);
715 OSMetaClassDeclareReservedUnused(OSArray
, 0);
716 OSMetaClassDeclareReservedUnused(OSArray
, 1);
717 OSMetaClassDeclareReservedUnused(OSArray
, 2);
718 OSMetaClassDeclareReservedUnused(OSArray
, 3);
719 OSMetaClassDeclareReservedUnused(OSArray
, 4);
720 OSMetaClassDeclareReservedUnused(OSArray
, 5);
721 OSMetaClassDeclareReservedUnused(OSArray
, 6);
722 OSMetaClassDeclareReservedUnused(OSArray
, 7);
725 #endif /* !_OS_OSARRAY_H */