]>
git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/c++/OSObject.h
b33ed3c47d0980687a7fcff73906156036b66c21
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@
29 Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
31 1998-10-30 Godfrey van der Linden(gvdl)
34 #ifndef _LIBKERN_OSOBJECT_H
35 #define _LIBKERN_OSOBJECT_H
37 #include <libkern/c++/OSMetaClass.h>
47 * This header declares the OSObject class,
48 * which is the concrete root of the Libkern C++ class hierarchy.
56 * OSObject is the concrete root class
57 * of the Libkern and I/O Kit C++ class hierarchy.
60 * OSObject defines the minimal functionality
61 * required of Libkern and I/O Kit C++ classes:
62 * tie-in to the run-time type information facility,
63 * the dynamic allocation/initialization paradigm,
64 * and reference counting.
65 * While kernel extensions are free to use their own C++ classes internally,
66 * any interaction they have with Libkern or the I/O Kit will require
67 * classes ultimately derived from OSObject.
69 * <b>Run-Time Type Information</b>
71 * OSObject is derived from the abstract root class
72 * @link //apple_ref/doc/class/OSMetaClassBase OSMetaClassBase@/link,
73 * which declares (and defines many of) the primitives
74 * on which the run-time type information facility is based.
75 * A parallel inheritance hierarchy of metaclass objects
76 * provides run-time introspection, including access to class names,
77 * inheritance, and safe type-casting.
78 * See @link //apple_ref/doc/class/OSMetaClass OSMetaClass@/link
79 * for more information.
81 * <b>Dynamic Allocation/Initialization</b>
83 * The kernel-resident C++ runtime does not support exceptions,
84 * so Libkern classes cannot use standard C++ object
85 * constructors and destructors,
86 * which use exceptions to report errors.
87 * To support error-handling during instance creation, then,
88 * OSObject separates object allocation from initialization.
89 * You can create a new OSObject-derived instance
90 * with the <code>new</code> operator,
91 * but this does nothing more than allocate memory
92 * and initialize the reference count to 1.
93 * Following this, you must call a designated initialization function
94 * and check its <code>bool</code> return value.
95 * If the initialization fails,
96 * you must immediately call
98 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/()
99 * release@/link</code>
100 * on the instance and handle the failure in whatever way is appropriate.
101 * Many Libkern and I/O Kit classes define static instance-creation functions
102 * (beginning with the word "with")
103 * to make construction a one-step process for clients.
105 * <b>Reference Counting</b>
107 * OSObject provides reference counting services using the
109 * //apple_ref/cpp/instm/OSObject/retain/virtualvoid/()
110 * retain@/link</code>,
112 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/()
113 * release()@/link</code>,
115 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/(int)
116 * release(int freeWhen)@/link</code>
119 * //apple_ref/cpp/instm/OSObject/free/virtualvoid/()
122 * The public interface to the reference counting is
124 * //apple_ref/cpp/instm/OSObject/retain/virtualvoid/()
125 * retain@/link</code>,
128 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/()
129 * release@/link</code>;
131 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/(int)
132 * release(int freeWhen)@/link</code>
134 * for objects that have internal retain cycles.
136 * In general, a subclass is expected to only override
138 * //apple_ref/cpp/instm/OSObject/free/virtualvoid/()
140 * It may also choose to override
142 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/(int)
143 * release(int freeWhen)@/link</code>
144 * if the object has a circular retain count, as noted above.
146 * <b>Use Restrictions</b>
148 * With very few exceptions in the I/O Kit, all Libkern-based C++
149 * classes, functions, and macros are <b>unsafe</b>
150 * to use in a primary interrupt context.
151 * Consult the I/O Kit documentation related to primary interrupts
152 * for more information.
154 * <b>Concurrency Protection</b>
156 * The basic features of OSObject are thread-safe.
157 * Most Libkern subclasses are not, and require locking or other protection
158 * if instances are shared between threads.
159 * I/O Kit driver objects are either designed for use within thread-safe contexts
160 * or designed to inherently be thread-safe.
161 * Always check the individual class documentation to see what
162 * steps are necessary for concurrent use of instances.
164 class OSObject
: public OSMetaClassBase
166 OSDeclareAbstractStructors ( OSObject
)
168 friend class IOStatistics
;
172 /* Not to be included in headerdoc.
174 * @var retainCount Number of references held on this instance.
176 mutable int retainCount
;
180 // xx-review: seems not to be used, should we deprecate?
186 * Releases a reference to an object,
187 * freeing it immediately if the reference count
188 * drops below the specified threshold.
190 * @param freeWhen If decrementing the reference count makes it
191 * >= <code>freeWhen</code>, the object is immediately freed.
194 * If the receiver has <code>freeWhen</code> or fewer references
195 * after its reference count is decremented,
196 * it is immediately freed.
198 * This version of <code>release</code>
199 * can be used to break certain retain cycles in object graphs.
200 * In general, however, it should be avoided.
202 virtual void release ( int freeWhen
) const ;
205 * @function taggedRelease
208 * Releases a tagged reference to an object,
209 * freeing it immediately if the reference count
210 * drops below the specified threshold.
212 * @param tag Used for tracking collection references.
213 * @param freeWhen If decrementing the reference count makes it
214 * >= <code>freeWhen</code>, the object is immediately freed.
217 * Kernel extensions should not use this function.
218 * It is for use by OSCollection and subclasses to track
219 * inclusion in collections.
221 * If the receiver has <code>freeWhen</code> or fewer references
222 * after its reference count is decremented,
223 * it is immediately freed.
225 * This version of <code>release</code>
226 * can be used to break certain retain cycles in object graphs.
227 * In general, however, it should be avoided.
229 virtual void taggedRelease ( const void * tag
, const int freeWhen
) const ;
236 * Initializes a newly-allocated object.
239 * <code>true</code> on success, <code>false</code> on failure.
242 * Classes derived from OSObject must override the primary init method
244 * In general most implementations call
245 * <code><i>super</i>::init()</code>
246 * before doing local initialisation.
247 * If the superclass call fails then return <code>false</code> immediately.
248 * If the subclass encounters a failure then it should return <code>false</code>.
257 * Deallocates/releases resources held by the object.
260 * Classes derived from OSObject should override this function
261 * to deallocate or release all dynamic resources held by the instance,
262 * then call the superclass's implementation.
266 * <li>You can not assume that you have completed initialization
267 * before <code>free</code> is called,
268 * so be very careful in your implementation.</li>
269 * <li>OSObject's implementation performs the C++ <code>delete</code>
270 * of the instance, so be sure that you call the superclass
271 * implementation <i>last</i> in your implementation.</li>
272 * <li><code>free</code> must not fail;
273 * all resources must be deallocated or released on completion.</li>
280 * @function operator delete
283 * Frees the memory of the object itself.
285 * @param mem A pointer to the object's memory.
286 * @param size The size of the object's block of memory.
289 * Never use <code>delete</code> on objects derived from OSObject;
292 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/()
293 * release@/link</code>
296 static void operator delete ( void * mem
, size_t size
);
301 * @function operator new
304 * Allocates memory for an instance of the class.
306 * @param size The number of bytes to allocate
309 * A pointer to block of memory if available, <code>NULL</code> otherwise.
311 static void * operator new ( size_t size
);
315 * @function getRetainCount
318 * Returns the reference count of the object.
321 * The reference count of the object.
323 virtual int getRetainCount () const ;
330 * Retains a reference to the object.
333 * This function increments the reference count of the receiver by 1.
334 * If you need to maintain a reference to an object
335 * outside the context in which you received it,
336 * you should always retain it immediately.
338 virtual void retain () const ;
345 * Releases a reference to the object,
346 * freeing it immediately if the reference count drops to zero.
349 * This function decrements the reference count of the receiver by 1.
350 * If the reference count drops to zero,
351 * the object is immediately freed using
353 * //apple_ref/cpp/instm/OSObject/free/virtualvoid/()
356 virtual void release () const ;
360 * @function taggedRetain
363 * Retains a reference to the object with an optional
364 * tag used for reference-tracking.
366 * @param tag Used for tracking collection references.
369 * Kernel extensions should not use this function.
370 * It is for use by OSCollection and subclasses to track
371 * inclusion in collections.
373 * If you need to maintain a reference to an object
374 * outside the context in which you received it,
375 * you should always retain it immediately.
377 virtual void taggedRetain ( const void * tag
= 0 ) const ;
381 * @function taggedRelease
384 * Releases a tagged reference to an object,
385 * freeing it immediately if the reference count
388 * @param tag Used for tracking collection references.
391 * Kernel extensions should not use this function.
392 * It is for use by OSCollection and subclasses to track
393 * inclusion in collections.
395 virtual void taggedRelease ( const void * tag
= 0 ) const ;
396 // xx-review: used to say, "Remove a reference on this object with this tag, if an attempt is made to remove a reference that isn't associated with this tag the kernel will panic immediately", but I don't see that in the implementation
400 * @function serialize
403 * Overridden by subclasses to archive the receiver into the provided
404 * @link //apple_ref/doc/class/OSSerialize OSSerialize@/link object.
406 * @param serializer The OSSerialize object.
409 * <code>true</code> if serialization succeeds, <code>false</code> if not.
412 * OSObject's implementation writes a string indicating that
413 * the class of the object receiving the function call
414 * is not serializable.
415 * Subclasses that can meaningfully encode themselves
416 * in I/O Kit-style property list XML can override this function to do so.
418 * @link //apple_ref/doc/class/OSSerialize OSSerialize@/link
419 * for more information.
421 virtual bool serialize ( OSSerialize
* serializer
) const ;
424 OSMetaClassDeclareReservedUnused ( OSObject
, 0 );
425 OSMetaClassDeclareReservedUnused ( OSObject
, 1 );
426 OSMetaClassDeclareReservedUnused ( OSObject
, 2 );
427 OSMetaClassDeclareReservedUnused ( OSObject
, 3 );
428 OSMetaClassDeclareReservedUnused ( OSObject
, 4 );
429 OSMetaClassDeclareReservedUnused ( OSObject
, 5 );
430 OSMetaClassDeclareReservedUnused ( OSObject
, 6 );
431 OSMetaClassDeclareReservedUnused ( OSObject
, 7 );
432 OSMetaClassDeclareReservedUnused ( OSObject
, 8 );
433 OSMetaClassDeclareReservedUnused ( OSObject
, 9 );
434 OSMetaClassDeclareReservedUnused ( OSObject
, 10 );
435 OSMetaClassDeclareReservedUnused ( OSObject
, 11 );
436 OSMetaClassDeclareReservedUnused ( OSObject
, 12 );
437 OSMetaClassDeclareReservedUnused ( OSObject
, 13 );
438 OSMetaClassDeclareReservedUnused ( OSObject
, 14 );
439 OSMetaClassDeclareReservedUnused ( OSObject
, 15 );
443 #endif /* !_LIBKERN_OSOBJECT_H */