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