]> git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/c++/OSObject.h
xnu-517.3.15.tar.gz
[apple/xnu.git] / libkern / libkern / c++ / OSObject.h
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
27 HISTORY
28 1998-10-30 Godfrey van der Linden(gvdl)
29 Created
30 */
31 #ifndef _LIBKERN_OSOBJECT_H
32 #define _LIBKERN_OSOBJECT_H
33
34 #include <libkern/c++/OSMetaClass.h>
35
36 class OSSymbol;
37 class OSString;
38 /*!
39 @class OSObject : OSMetaClassBase
40 @abstract The root base class for Mac OS X kernel and just generally all-round useful class to have around.
41 @discussion
42 Defines the minimum functionality that an object can expect. Implements reference counting, type safe object casting, allocation primitives & serialisation among other functionality. This object is an abstract base class and can not be copied, nor can it be constructed by itself.
43
44 <br><br> Construction <br><br>
45
46 As Mac OS X's C++ is based upon Embedded C++ we have a problem with the typical C++ method of using constructors. Embedded C++ does not allow exceptions. This means that the standard constructors can not report a failure. Well obviously initialisation of a new object can fail so we have had to work around this language limitation. In the Mac OS X kernel we have chosen to break object construction into two phases. Phase one is the familiar C++ new operator, the only initialisation is the object has exactly one reference after creation. Once the new is called the client MUST call init and check it's return value. If the init call fails then the object MUST be immediately released. IOKit usually implements factory methods to make construction a one step process for clients.
47
48 <br><br>Reference Counting<br><br>
49
50 OSObject provides reference counting services using the $link retain(), $link release(), $link release(int when) and $link free() functions. The public interface to the reference counting is retain() & release(). release() is implemented as a simple call to release(1). The actual implementation of release(when) is a little subtle. If the current reference count is less than or equal to the 'when' parameter the object will call free on itself.
51 <br>
52 In general a subclass is expected to only override $link free(). It may also choose to override release() if the object has a circular retain count, see $link release(int when);
53
54 <br><br>Runtime Type Information System<br><br>
55
56 The Mac OS X C++ implements a basic runtime type information system using meta class information and a number of macros, $link OSDynamicCast, $link OSTypeID, $link OSTypeIDInst, $link OSCheckTypeInst and $link OSMetaClass.
57 */
58 class OSObject : public OSMetaClassBase
59 {
60 OSDeclareAbstractStructors(OSObject)
61
62 private:
63 /*! @var retainCount Number of references held on this instance. */
64 mutable int retainCount;
65
66 protected:
67
68 /*! @function release
69 @abstract untagged release(when) mechansim.
70 @param when Pass through to taggedRelease. */
71 virtual void release(int when) const;
72
73 /*! @function taggedRelease
74 @abstract Primary implementation of the tagged release mechanism.
75 @discussion If $link retainCount <= the when argument then call $link free(). This indirect implementation of $link release allows the developer to break reference circularity. An example of this sort of problem is a parent/child mutual reference, either the parent or child can implement: void taggedRelease(tag) { taggedRelease(tag, 2); } thus breaking the cirularity.
76 @param when If retainCount == when then call free(). */
77 virtual void taggedRelease(const void *tag, const int when) const;
78
79 /*! @function init
80 @abstract Mac OS X kernel's primary mechanism for constructing objects.
81 @discussion Your responsibility as a subclass author is to override the init method of your parent. In general most of our implementations call <super>::init() before doing local initialisation, if the parent fails then return false immediately. If you have a failure during you local initialisation then return false.
82 @result OSObject::init Always returns true, but subclasses will return false on init failure.
83 */
84 virtual bool init();
85
86 /*! @function free
87 @abstract The last reference is gone so clean up your resources.
88 @discussion Release all resources held by the object, then call your parent's free().
89
90 <br><br>Caution:
91 <br>1> You can not assume that you have completed initialization before your free is called, so be very careful in your implementation.
92 <br>2> The implementation is OSObject::free() { delete this; } so do not call super::free() until just before you return.
93 <br>3> Free is not allowed to fail all resource must be released on completion. */
94 virtual void free();
95
96 /*! @function operator delete
97 @abstract Release the 'operator new'ed memory.
98 @discussion Never attempt to delete an object that inherits from OSObject directly use $link release().
99 @param mem pointer to block of memory
100 @param size size of block of memory
101 */
102 static void operator delete(void *mem, size_t size);
103
104 public:
105
106 /*! @function operator new
107 @abstract Allocator for all objects that inherit from OSObject
108 @param size number of bytes to allocate
109 @result returns pointer to block of memory if available, 0 otherwise.
110 */
111 static void *operator new(size_t size);
112
113 /*! @function getRetainCount
114 @abstract How many times has this object been retained?
115 @result Current retain count
116 */
117 virtual int getRetainCount() const;
118
119 /*! @function retain
120 @abstract Retain a reference in this object.
121 @discussion Takes a reference that is NULL tagged. See taggedRetain().
122 */
123 virtual void retain() const;
124
125 /*! @function release
126 @abstract Release a reference to this object
127 @discussion Removes a reference that is NULL tagged. See taggedRelease().
128 */
129 virtual void release() const;
130
131 /*! @function taggedRetain
132 @abstract Retain a tagged reference in this object.
133 @param tag Retain a reference on this object with this tag, see taggedRelease.
134 */
135 virtual void taggedRetain(const void *tag = 0) const;
136
137 /*! @function taggedRelease
138 @abstract Release a tagged reference to this object
139 @param tag 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.
140 */
141 virtual void taggedRelease(const void *tag = 0) const;
142
143 /*! @function serialize
144 @abstract
145 @discussion
146 @param s
147 @result
148 */
149 virtual bool serialize(OSSerialize *s) const;
150
151 // Unused Padding
152 OSMetaClassDeclareReservedUnused(OSObject, 0);
153 OSMetaClassDeclareReservedUnused(OSObject, 1);
154 OSMetaClassDeclareReservedUnused(OSObject, 2);
155 OSMetaClassDeclareReservedUnused(OSObject, 3);
156 OSMetaClassDeclareReservedUnused(OSObject, 4);
157 OSMetaClassDeclareReservedUnused(OSObject, 5);
158 OSMetaClassDeclareReservedUnused(OSObject, 6);
159 OSMetaClassDeclareReservedUnused(OSObject, 7);
160 OSMetaClassDeclareReservedUnused(OSObject, 8);
161 OSMetaClassDeclareReservedUnused(OSObject, 9);
162 OSMetaClassDeclareReservedUnused(OSObject, 10);
163 OSMetaClassDeclareReservedUnused(OSObject, 11);
164 OSMetaClassDeclareReservedUnused(OSObject, 12);
165 OSMetaClassDeclareReservedUnused(OSObject, 13);
166 OSMetaClassDeclareReservedUnused(OSObject, 14);
167 OSMetaClassDeclareReservedUnused(OSObject, 15);
168 OSMetaClassDeclareReservedUnused(OSObject, 16);
169 OSMetaClassDeclareReservedUnused(OSObject, 17);
170 OSMetaClassDeclareReservedUnused(OSObject, 18);
171 OSMetaClassDeclareReservedUnused(OSObject, 19);
172 OSMetaClassDeclareReservedUnused(OSObject, 20);
173 OSMetaClassDeclareReservedUnused(OSObject, 21);
174 OSMetaClassDeclareReservedUnused(OSObject, 22);
175 OSMetaClassDeclareReservedUnused(OSObject, 23);
176 OSMetaClassDeclareReservedUnused(OSObject, 24);
177 OSMetaClassDeclareReservedUnused(OSObject, 25);
178 OSMetaClassDeclareReservedUnused(OSObject, 26);
179 OSMetaClassDeclareReservedUnused(OSObject, 27);
180 OSMetaClassDeclareReservedUnused(OSObject, 28);
181 OSMetaClassDeclareReservedUnused(OSObject, 29);
182 OSMetaClassDeclareReservedUnused(OSObject, 30);
183 OSMetaClassDeclareReservedUnused(OSObject, 31);
184 };
185
186 #endif /* !_LIBKERN_OSOBJECT_H */