]> git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/c++/OSObject.h
a7e0505a9b8f49097a23339a8b23ab473aa3bbb3
[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 Primary implementation of the release mechanism.
67 @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 release() { release(2); } thus breaking the cirularity.
68 @param when When retainCount == when then call free(). */
69 virtual void release(int when) const;
70
71 /*! @function init
72 @abstract Mac OS X kernel's primary mechanism for constructing objects.
73 @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.
74 @result OSObject::init Always returns true, but subclasses will return false on init failure.
75 */
76 virtual bool init();
77
78 /*! @function free
79 @abstract The last reference is gone so clean up your resources.
80 @discussion Release all resources held by the object, then call your parent's free().
81
82 <br><br>Caution:
83 <br>1> You can not assume that you have completed initialization before your free is called, so be very careful in your implementation.
84 <br>2> The implementation is OSObject::free() { delete this; } so do not call super::free() until just before you return.
85 <br>3> Free is not allowed to fail all resource must be released on completion. */
86 virtual void free();
87
88 /*! @function operator delete
89 @abstract Release the 'operator new'ed memory.
90 @discussion Never attempt to delete an object that inherits from OSObject directly use $link release().
91 @param mem pointer to block of memory
92 @param size size of block of memory
93 */
94 static void operator delete(void *mem, size_t size);
95
96 public:
97
98 /*! @function operator new
99 @abstract Allocator for all objects that inherit from OSObject
100 @param size number of bytes to allocate
101 @result returns pointer to block of memory if available, 0 otherwise.
102 */
103 static void *operator new(size_t size);
104
105 /*! @function getRetainCount
106 @abstract How many times has this object been retained?
107 @result Current retain count
108 */
109 virtual int getRetainCount() const;
110
111 /*! @function retain
112 @abstract Retain a reference in this object.
113 */
114 virtual void retain() const;
115
116 /*! @function release
117 @abstract Release a reference to this object
118 */
119 virtual void release() const;
120
121 /*! @function serialize
122 @abstract
123 @discussion
124 @param s
125 @result
126 */
127 virtual bool serialize(OSSerialize *s) const;
128
129 // Unused Padding
130 OSMetaClassDeclareReservedUnused(OSObject, 0);
131 OSMetaClassDeclareReservedUnused(OSObject, 1);
132 OSMetaClassDeclareReservedUnused(OSObject, 2);
133 OSMetaClassDeclareReservedUnused(OSObject, 3);
134 OSMetaClassDeclareReservedUnused(OSObject, 4);
135 OSMetaClassDeclareReservedUnused(OSObject, 5);
136 OSMetaClassDeclareReservedUnused(OSObject, 6);
137 OSMetaClassDeclareReservedUnused(OSObject, 7);
138 OSMetaClassDeclareReservedUnused(OSObject, 8);
139 OSMetaClassDeclareReservedUnused(OSObject, 9);
140 OSMetaClassDeclareReservedUnused(OSObject, 10);
141 OSMetaClassDeclareReservedUnused(OSObject, 11);
142 OSMetaClassDeclareReservedUnused(OSObject, 12);
143 OSMetaClassDeclareReservedUnused(OSObject, 13);
144 OSMetaClassDeclareReservedUnused(OSObject, 14);
145 OSMetaClassDeclareReservedUnused(OSObject, 15);
146 OSMetaClassDeclareReservedUnused(OSObject, 16);
147 OSMetaClassDeclareReservedUnused(OSObject, 17);
148 OSMetaClassDeclareReservedUnused(OSObject, 18);
149 OSMetaClassDeclareReservedUnused(OSObject, 19);
150 OSMetaClassDeclareReservedUnused(OSObject, 20);
151 OSMetaClassDeclareReservedUnused(OSObject, 21);
152 OSMetaClassDeclareReservedUnused(OSObject, 22);
153 OSMetaClassDeclareReservedUnused(OSObject, 23);
154 OSMetaClassDeclareReservedUnused(OSObject, 24);
155 OSMetaClassDeclareReservedUnused(OSObject, 25);
156 OSMetaClassDeclareReservedUnused(OSObject, 26);
157 OSMetaClassDeclareReservedUnused(OSObject, 27);
158 OSMetaClassDeclareReservedUnused(OSObject, 28);
159 OSMetaClassDeclareReservedUnused(OSObject, 29);
160 OSMetaClassDeclareReservedUnused(OSObject, 30);
161 OSMetaClassDeclareReservedUnused(OSObject, 31);
162 };
163
164 #endif /* !_LIBKERN_OSOBJECT_H */