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