]> git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/c++/OSObject.h
d98e9eb6344fb3ce7bc260f495edc19f08be80c8
[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 * 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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*
24 Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
25 HISTORY
26 1998-10-30 Godfrey van der Linden(gvdl)
27 Created
28 */
29 #ifndef _LIBKERN_OSOBJECT_H
30 #define _LIBKERN_OSOBJECT_H
31
32 #include <libkern/c++/OSMetaClass.h>
33
34 class OSSymbol;
35 class OSString;
36 /*!
37 @class OSObject : OSMetaClassBase
38 @abstract The root base class for Mac OS X kernel and just generally all-round useful class to have around.
39 @discussion
40 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.
41
42 <br><br> Construction <br><br>
43
44 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.
45
46 <br><br>Reference Counting<br><br>
47
48 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.
49 <br>
50 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);
51
52 <br><br>Runtime Type Information System<br><br>
53
54 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.
55 */
56 class OSObject : public OSMetaClassBase
57 {
58 OSDeclareAbstractStructors(OSObject)
59
60 private:
61 /*! @var retainCount Number of references held on this instance. */
62 mutable int retainCount;
63
64 protected:
65
66 /*! @function release
67 @abstract untagged release(when) mechansim.
68 @param when Pass through to taggedRelease. */
69 virtual void release(int when) const;
70
71 /*! @function taggedRelease
72 @abstract Primary implementation of the tagged release mechanism.
73 @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.
74 @param when If retainCount == when then call free(). */
75 virtual void taggedRelease(const void *tag, const int when) const;
76
77 /*! @function init
78 @abstract Mac OS X kernel's primary mechanism for constructing objects.
79 @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.
80 @result OSObject::init Always returns true, but subclasses will return false on init failure.
81 */
82 virtual bool init();
83
84 /*! @function free
85 @abstract The last reference is gone so clean up your resources.
86 @discussion Release all resources held by the object, then call your parent's free().
87
88 <br><br>Caution:
89 <br>1> You can not assume that you have completed initialization before your free is called, so be very careful in your implementation.
90 <br>2> The implementation is OSObject::free() { delete this; } so do not call super::free() until just before you return.
91 <br>3> Free is not allowed to fail all resource must be released on completion. */
92 virtual void free();
93
94 /*! @function operator delete
95 @abstract Release the 'operator new'ed memory.
96 @discussion Never attempt to delete an object that inherits from OSObject directly use $link release().
97 @param mem pointer to block of memory
98 @param size size of block of memory
99 */
100 static void operator delete(void *mem, size_t size);
101
102 public:
103
104 /*! @function operator new
105 @abstract Allocator for all objects that inherit from OSObject
106 @param size number of bytes to allocate
107 @result returns pointer to block of memory if available, 0 otherwise.
108 */
109 static void *operator new(size_t size);
110
111 /*! @function getRetainCount
112 @abstract How many times has this object been retained?
113 @result Current retain count
114 */
115 virtual int getRetainCount() const;
116
117 /*! @function retain
118 @abstract Retain a reference in this object.
119 @discussion Takes a reference that is NULL tagged. See taggedRetain().
120 */
121 virtual void retain() const;
122
123 /*! @function release
124 @abstract Release a reference to this object
125 @discussion Removes a reference that is NULL tagged. See taggedRelease().
126 */
127 virtual void release() const;
128
129 /*! @function taggedRetain
130 @abstract Retain a tagged reference in this object.
131 @param tag Retain a reference on this object with this tag, see taggedRelease.
132 */
133 virtual void taggedRetain(const void *tag = 0) const;
134
135 /*! @function taggedRelease
136 @abstract Release a tagged reference to this object
137 @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.
138 */
139 virtual void taggedRelease(const void *tag = 0) const;
140
141 /*! @function serialize
142 @abstract
143 @discussion
144 @param s
145 @result
146 */
147 virtual bool serialize(OSSerialize *s) const;
148
149 // Unused Padding
150 OSMetaClassDeclareReservedUnused(OSObject, 0);
151 OSMetaClassDeclareReservedUnused(OSObject, 1);
152 OSMetaClassDeclareReservedUnused(OSObject, 2);
153 OSMetaClassDeclareReservedUnused(OSObject, 3);
154 OSMetaClassDeclareReservedUnused(OSObject, 4);
155 OSMetaClassDeclareReservedUnused(OSObject, 5);
156 OSMetaClassDeclareReservedUnused(OSObject, 6);
157 OSMetaClassDeclareReservedUnused(OSObject, 7);
158 OSMetaClassDeclareReservedUnused(OSObject, 8);
159 OSMetaClassDeclareReservedUnused(OSObject, 9);
160 OSMetaClassDeclareReservedUnused(OSObject, 10);
161 OSMetaClassDeclareReservedUnused(OSObject, 11);
162 OSMetaClassDeclareReservedUnused(OSObject, 12);
163 OSMetaClassDeclareReservedUnused(OSObject, 13);
164 OSMetaClassDeclareReservedUnused(OSObject, 14);
165 OSMetaClassDeclareReservedUnused(OSObject, 15);
166 OSMetaClassDeclareReservedUnused(OSObject, 16);
167 OSMetaClassDeclareReservedUnused(OSObject, 17);
168 OSMetaClassDeclareReservedUnused(OSObject, 18);
169 OSMetaClassDeclareReservedUnused(OSObject, 19);
170 OSMetaClassDeclareReservedUnused(OSObject, 20);
171 OSMetaClassDeclareReservedUnused(OSObject, 21);
172 OSMetaClassDeclareReservedUnused(OSObject, 22);
173 OSMetaClassDeclareReservedUnused(OSObject, 23);
174 OSMetaClassDeclareReservedUnused(OSObject, 24);
175 OSMetaClassDeclareReservedUnused(OSObject, 25);
176 OSMetaClassDeclareReservedUnused(OSObject, 26);
177 OSMetaClassDeclareReservedUnused(OSObject, 27);
178 OSMetaClassDeclareReservedUnused(OSObject, 28);
179 OSMetaClassDeclareReservedUnused(OSObject, 29);
180 OSMetaClassDeclareReservedUnused(OSObject, 30);
181 OSMetaClassDeclareReservedUnused(OSObject, 31);
182 };
183
184 #endif /* !_LIBKERN_OSOBJECT_H */