]>
git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/c++/OSCollection.h
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 /* IOCollection.h created by gvdl on Thu 1998-10-22 */
30 #ifndef _OS_OSCOLLECTION_H
31 #define _OS_OSCOLLECTION_H
33 #include <libkern/c++/OSObject.h>
34 #include <libkern/c++/OSPtr.h>
39 typedef OSCollection
* OSCollectionPtr
;
41 // We're not necessarily in C++11 mode, so we need to disable warnings
42 // for C++11 extensions
43 #pragma clang diagnostic push
44 #pragma clang diagnostic ignored "-Wc++11-extensions"
47 using OSCollectionTaggedPtr
= T
*;
49 #pragma clang diagnostic pop
56 * This header declares the OSDictionary collection class.
64 * The abstract superclass for Libkern collections.
67 * OSCollection is the abstract superclass
68 * for all Libkern C++ object collections.
69 * It defines the necessary interfaces for managing storage space
70 * and iterating through an arbitrary collection
72 * @link //apple_ref/cpp/class/OSIterator OSIterator@/link
74 * @link //apple_ref/cpp/class/OSCollectionIterator OSCollectionIterator@/link
76 * It is up to concrete subclasses
77 * to define their specific content management functions.
79 * <b>Use Restrictions</b>
81 * With very few exceptions in the I/O Kit, all Libkern-based C++
82 * classes, functions, and macros are <b>unsafe</b>
83 * to use in a primary interrupt context.
84 * Consult the I/O Kit documentation related to primary interrupts
85 * for more information.
87 * OSCollection provides no concurrency protection;
88 * it's up to the usage context to provide any protection necessary.
89 * Some portions of the I/O Kit, such as
90 * @link //apple_ref/doc/class/IORegistryEntry IORegistryEntry@/link,
91 * handle synchronization via defined member functions for setting
94 class OSCollection
: public OSObject
96 friend class OSCollectionIterator
;
98 OSDeclareAbstractStructors(OSCollection
);
100 struct ExpansionData
{ };
103 /* Not to be included in headerdoc.
108 * A counter for changes to the collection object.
111 * The update stamp is used primarily to track validity
112 * of iteration contexts.
113 * See @link //apple_ref/cpp/class/OSIterator OSIterator@/link and
114 * @link //apple_ref/cpp/class/OSCollectionIterator OSCollectionIterator@/link
115 * for more information.
117 unsigned int updateStamp
;
119 #ifdef XNU_KERNEL_PRIVATE
123 #endif /* XNU_KERNEL_PRIVATE */
124 /* Reserved for future use. (Internal use only) */
125 // ExpansionData * reserved;
126 unsigned int fOptions
;
129 // Member functions used by the OSCollectionIterator class.
133 * @function iteratorSize
136 * Returns the size in bytes of a subclass's iteration context.
139 * The size in bytes of the iteration context
140 * needed by the subclass of OSCollection.
143 * This pure virtual member function, which subclasses must implement,
145 * @link //apple_ref/doc/class/OSCollectionIterator OSCollectionIterator@/link
146 * object so that it can allocate the storage needed
147 * for the iteration context.
148 * An iteration context contains the data necessary
149 * to iterate through the collection.
151 virtual unsigned int iteratorSize() const = 0;
155 * @function initIterator
158 * Initializes the iteration context for a collection subclass.
160 * @param iterationContext The iteration context to initialize.
163 * <code>true</code> if initialization was successful,
164 * <code>false</code> otherwise.
167 * This pure virtual member function, which subclasses must implement,
169 * @link //apple_ref/doc/class/OSCollectionIterator OSCollectionIterator@/link
170 * object to initialize an iteration context for a collection.
171 * The collection object should interpret <code>iterationContext</code> appropriately
172 * and initialize its contents to begin an iteration.
174 * This function can be called repeatedly for a given context,
175 * whenever the iterator is reset via the
176 * @link //apple_ref/cpp/instm/OSCollectionIterator/reset/virtualvoid/()
177 * OSCollectionIterator::reset@/link
180 virtual bool initIterator(void * iterationContext
) const = 0;
184 * @function getNextObjectForIterator
187 * Returns the next member of a collection.
189 * @param iterationContext The iteration context.
190 * @param nextObject The object returned by reference to the caller.
193 * <code>true</code> if an object was found, <code>false</code> otherwise.
196 * This pure virtual member function, which subclasses must implement,
198 * @link //apple_ref/doc/class/OSCollectionIterator OSCollectionIterator@/link
199 * to get the next object for a given iteration context.
200 * The collection object should interpret
201 * <code>iterationContext</code> appropriately,
202 * advance the context from its current object
203 * to the next object (if it exists),
204 * return that object by reference in <code>nextObject</code>,
205 * and return <code>true</code> for the function call.
206 * If there is no next object, the collection object must return <code>false</code>.
208 * For associative collections, the object returned should be the key
209 * used to access its associated value, and not the value itself.
211 virtual bool getNextObjectForIterator(
212 void * iterationContext
,
213 OSObject
** nextObject
) const = 0;
220 * Initializes the OSCollection object.
223 * <code>true</code> on success, <code>false</code> otherwise.
226 * This function is used to initialize state
227 * within a newly created OSCollection object.
229 virtual bool init() APPLE_KEXT_OVERRIDE
;
234 * @typedef _OSCollectionFlags
238 * Used with <code>@link setOptions setOptions@/link</code>
239 * to indicate the collection's contents should
240 * or should not change.
242 * An @link //apple_ref/doc/class/IORegistryEntry IORegistryEntry@/link
243 * object marks collections immutable when set
244 * as properties of a registry entry that's attached to a plane.
245 * This is generally an advisory flag, used for debugging;
246 * setting it does not mean a collection will in fact
247 * disallow modifications.
250 kImmutable
= 0x00000001,
252 kMASK
= (unsigned) - 1
253 } _OSCollectionFlags
;
255 // xx-review: should be protected, not public
258 * @function haveUpdated
261 * Tracks updates to the collection.
264 * Subclasses call this function <i>before</i>
265 * making any change to their contents (not after, as the name implies).
266 * Update tracking is used for collection iterators,
267 * and to enforce certain protections in the IORegistry.
276 * Returns the number of objects in the collection.
279 * The number of objects in the collection.
282 * Subclasses must implement this pure virtual member function.
284 virtual unsigned int getCount() const = 0;
288 * @function getCapacity
291 * Returns the number of objects the collection
292 * can store without reallocating.
295 * The number objects the collection
296 * can store without reallocating.
299 * Subclasses must implement this pure virtual member function.
301 virtual unsigned int getCapacity() const = 0;
305 * @function getCapacityIncrement
308 * Returns the storage increment of the collection.
311 * The storage increment of the collection.
314 * Subclasses must implement this pure virtual member function.
315 * Most collection subclasses allocate their storage
316 * in multiples of the capacity increment.
320 * //apple_ref/cpp/instm/OSCollection/ensureCapacity/virtualunsignedint/(unsignedint)
321 * ensureCapacity@/link</code>
322 * for how the capacity increment is used.
324 virtual unsigned int getCapacityIncrement() const = 0;
328 * @function setCapacityIncrement
331 * Sets the storage increment of the collection.
334 * The new storage increment of the collection,
335 * which may be different from the number requested.
338 * Subclasses must implement this pure virtual member function.
339 * Most collection subclasses allocate their storage
340 * in multiples of the capacity increment.
342 * Collection subclasses should gracefully handle
343 * an <code>increment</code> of zero
344 * by applying (and returning) a positive minimum capacity.
346 * Setting the capacity increment does not trigger an immediate adjustment
347 * of a collection's storage.
351 * //apple_ref/cpp/instm/OSCollection/ensureCapacity/virtualunsignedint/(unsignedint)
352 * ensureCapacity@/link
353 * for how the capacity increment is used.
355 virtual unsigned int setCapacityIncrement(unsigned increment
) = 0;
359 * @function ensureCapacity
362 * Ensures the collection has enough space to store
363 * the requested number of objects.
365 * @param newCapacity The total number of objects the collection
366 * should be able to store.
369 * The new capacity of the collection,
370 * which may be different from the number requested
371 * (if smaller, reallocation of storage failed).
374 * Subclasses implement this pure virtual member function
375 * to adjust their storage so that they can hold
376 * at least <code>newCapacity</code> objects.
377 * Libkern collections generally allocate storage
378 * in multiples of their capacity increment.
380 * Subclass methods that add objects to the collection
381 * should call this function before adding any object,
382 * and should check the return value for success.
384 * Collection subclasses may reduce their storage
385 * when the number of contained objects falls below some threshold,
386 * but no Libkern collections currently do.
388 virtual unsigned int ensureCapacity(unsigned int newCapacity
) = 0;
392 * @function flushCollection
395 * Empties the collection, releasing any objects retained.
398 * Subclasses implement this pure virtual member function
399 * to remove their entire contents.
400 * This must not release the collection itself.
402 virtual void flushCollection() = 0;
406 * @function setOptions
409 * Recursively sets option bits in this collection
410 * and all child collections.
412 * @param options A bitfield whose values turn the options on (1) or off (0).
413 * @param mask A mask indicating which bits
414 * in <code>options</code> to change.
415 * Pass 0 to get the whole current options bitfield
416 * without changing any settings.
417 * @param context Unused.
420 * The options bitfield as it was before the set operation.
423 * Kernel extensions should not call this function.
425 * The only option currently in use is
426 * <code>@link //apple_ref/doc/title:econst/OSCollectionFlags/kImmutable
427 * kImmutable@/link</code>.
429 * Subclasses should override this function to recursively apply
430 * the options to their contents if the options actually change.
432 virtual unsigned setOptions(
435 void * context
= NULL
);
438 * @function copyCollection
441 * Creates a deep copy of a collection.
443 * @param cycleDict A dictionary of all of the collections
444 * that have been copied so far,
445 * to start the copy at the top level
446 * pass <code>NULL</code> for <code>cycleDict</code>.
449 * The newly copied collecton,
450 * <code>NULL</code> on failure.
453 * This function copies the collection
454 * and all of the contained collections recursively.
455 * Objects that are not derived from OSCollection are retained
456 * rather than copied.
458 * Subclasses of OSCollection must override this function
459 * to properly support deep copies.
461 virtual OSPtr
<OSCollection
> copyCollection(OSDictionary
* cycleDict
= NULL
);
464 * @function iterateObjects
467 * Invoke a callback for each member of the collection.
469 * @param refcon A reference constant for the callback.
470 * @param callback The callback function,
471 * called with the refcon and each member object
472 * of the collection in turn, on the callers thread.
473 * The callback should return true to early terminate
474 * the iteration, false otherwise.
477 * False if the collection iteration was made invalid
478 * (see OSCollectionIterator::isValid()) otherwise true.
480 bool iterateObjects(void * refcon
, bool (*callback
)(void * refcon
, OSObject
* object
));
485 * @function iterateObjects
488 * Invoke a block for each member of the collection.
490 * @param block The block,
491 * called with the refcon and each member object
492 * of the collection in turn, on the callers thread.
493 * The block should return true to early terminate
494 * the iteration, false otherwise.
497 * False if the collection iteration was made invalid
498 * (see OSCollectionIterator::isValid()) otherwise true.
500 bool iterateObjects(bool (^block
)(OSObject
* object
));
502 #endif /* __BLOCKS__ */
504 OSMetaClassDeclareReservedUsedX86(OSCollection
, 0);
505 OSMetaClassDeclareReservedUsedX86(OSCollection
, 1);
506 OSMetaClassDeclareReservedUnused(OSCollection
, 2);
507 OSMetaClassDeclareReservedUnused(OSCollection
, 3);
508 OSMetaClassDeclareReservedUnused(OSCollection
, 4);
509 OSMetaClassDeclareReservedUnused(OSCollection
, 5);
510 OSMetaClassDeclareReservedUnused(OSCollection
, 6);
511 OSMetaClassDeclareReservedUnused(OSCollection
, 7);
514 #endif /* !_OS_OSCOLLECTION_H */