]> git.saurik.com Git - apple/xnu.git/blob - libkern/c++/OSObject.cpp
64982f8ad24f360b6d7ab49f983f8b3bc7d5603e
[apple/xnu.git] / libkern / c++ / OSObject.cpp
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 /* OSObject.cpp created by gvdl on Fri 1998-11-17 */
24
25 #include <libkern/c++/OSObject.h>
26 #include <libkern/c++/OSSerialize.h>
27 #include <libkern/c++/OSLib.h>
28 #include <libkern/c++/OSCPPDebug.h>
29 #include <libkern/OSAtomic.h>
30
31 #include <libkern/c++/OSCollection.h>
32
33 __BEGIN_DECLS
34 int debug_ivars_size;
35 __END_DECLS
36
37 #if OSALLOCDEBUG
38 #define ACCUMSIZE(s) do { debug_ivars_size += (s); } while(0)
39 #else
40 #define ACCUMSIZE(s)
41 #endif
42
43 // OSDefineMetaClassAndAbstractStructors(OSObject, 0);
44 /* Class global data */
45 OSObject::MetaClass OSObject::gMetaClass;
46 const OSMetaClass * const OSObject::metaClass = &OSObject::gMetaClass;
47 const OSMetaClass * const OSObject::superClass = 0;
48
49 /* Class member functions - Can't use defaults */
50 OSObject::OSObject() { retainCount = 1; }
51 OSObject::OSObject(const OSMetaClass *) { retainCount = 1; }
52 OSObject::~OSObject() { }
53 const OSMetaClass * OSObject::getMetaClass() const
54 { return &gMetaClass; }
55 OSObject *OSObject::MetaClass::alloc() const { return 0; }
56
57 /* The OSObject::MetaClass constructor */
58 OSObject::MetaClass::MetaClass()
59 : OSMetaClass("OSObject", OSObject::superClass, sizeof(OSObject))
60 { }
61
62 // Virtual Padding
63 OSMetaClassDefineReservedUnused(OSObject, 0);
64 OSMetaClassDefineReservedUnused(OSObject, 1);
65 OSMetaClassDefineReservedUnused(OSObject, 2);
66 OSMetaClassDefineReservedUnused(OSObject, 3);
67 OSMetaClassDefineReservedUnused(OSObject, 4);
68 OSMetaClassDefineReservedUnused(OSObject, 5);
69 OSMetaClassDefineReservedUnused(OSObject, 6);
70 OSMetaClassDefineReservedUnused(OSObject, 7);
71 OSMetaClassDefineReservedUnused(OSObject, 8);
72 OSMetaClassDefineReservedUnused(OSObject, 9);
73 OSMetaClassDefineReservedUnused(OSObject, 10);
74 OSMetaClassDefineReservedUnused(OSObject, 11);
75 OSMetaClassDefineReservedUnused(OSObject, 12);
76 OSMetaClassDefineReservedUnused(OSObject, 13);
77 OSMetaClassDefineReservedUnused(OSObject, 14);
78 OSMetaClassDefineReservedUnused(OSObject, 15);
79 OSMetaClassDefineReservedUnused(OSObject, 16);
80 OSMetaClassDefineReservedUnused(OSObject, 17);
81 OSMetaClassDefineReservedUnused(OSObject, 18);
82 OSMetaClassDefineReservedUnused(OSObject, 19);
83 OSMetaClassDefineReservedUnused(OSObject, 20);
84 OSMetaClassDefineReservedUnused(OSObject, 21);
85 OSMetaClassDefineReservedUnused(OSObject, 22);
86 OSMetaClassDefineReservedUnused(OSObject, 23);
87 OSMetaClassDefineReservedUnused(OSObject, 24);
88 OSMetaClassDefineReservedUnused(OSObject, 25);
89 OSMetaClassDefineReservedUnused(OSObject, 26);
90 OSMetaClassDefineReservedUnused(OSObject, 27);
91 OSMetaClassDefineReservedUnused(OSObject, 28);
92 OSMetaClassDefineReservedUnused(OSObject, 29);
93 OSMetaClassDefineReservedUnused(OSObject, 30);
94 OSMetaClassDefineReservedUnused(OSObject, 31);
95
96 static const char *getClassName(const OSObject *obj)
97 {
98 const OSMetaClass *meta = obj->getMetaClass();
99 return (meta) ? meta->getClassName() : "unknown class?";
100 }
101
102 bool OSObject::init()
103 { return true; }
104
105 #if (!__ppc__) || (__GNUC__ < 3)
106
107 // Implemented in assembler in post gcc 3.x systems as we have a problem
108 // where the destructor in gcc2.95 gets 2 arguments. The second argument
109 // appears to be a flag argument. I have copied the assembler from Puma xnu
110 // to OSRuntimeSupport.c So for 2.95 builds use the C
111 void OSObject::free()
112 {
113 const OSMetaClass *meta = getMetaClass();
114
115 if (meta)
116 meta->instanceDestructed();
117 delete this;
118 }
119 #endif /* (!__ppc__) || (__GNUC__ < 3) */
120
121 int OSObject::getRetainCount() const
122 {
123 return (int) ((UInt16) retainCount);
124 }
125
126 void OSObject::taggedRetain(const void *tag) const
127 {
128 volatile UInt32 *countP = (volatile UInt32 *) &retainCount;
129 UInt32 inc = 1;
130 UInt32 origCount;
131 UInt32 newCount;
132
133 // Increment the collection bucket.
134 if ((const void *) OSTypeID(OSCollection) == tag)
135 inc |= (1UL<<16);
136
137 do {
138 origCount = *countP;
139 if ( ((UInt16) origCount | 0x1) == 0xffff ) {
140 const char *msg;
141 if (origCount & 0x1) {
142 // If count == 0xffff that means we are freeing now so we can
143 // just return obviously somebody is cleaning up dangling
144 // references.
145 msg = "Attempting to retain a freed object";
146 }
147 else {
148 // If count == 0xfffe then we have wrapped our reference count.
149 // We should stop counting now as this reference must be
150 // leaked rather than accidently wrapping around the clock and
151 // freeing a very active object later.
152
153 #if !DEBUG
154 break; // Break out of update loop which pegs the reference
155 #else DEBUG
156 // @@@ gvdl: eventually need to make this panic optional
157 // based on a boot argument i.e. debug= boot flag
158 msg = "About to wrap the reference count, reference leak?";
159 #endif /* !DEBUG */
160 }
161 panic("OSObject::refcount: %s", msg);
162 }
163
164 newCount = origCount + inc;
165 } while (!OSCompareAndSwap(origCount, newCount, (UInt32 *) countP));
166 }
167
168 void OSObject::taggedRelease(const void *tag) const
169 {
170 taggedRelease(tag, 1);
171 }
172
173 void OSObject::taggedRelease(const void *tag, const int when) const
174 {
175 volatile UInt32 *countP = (volatile UInt32 *) &retainCount;
176 UInt32 dec = 1;
177 UInt32 origCount;
178 UInt32 newCount;
179 UInt32 actualCount;
180
181 // Increment the collection bucket.
182 if ((const void *) OSTypeID(OSCollection) == tag)
183 dec |= (1UL<<16);
184
185 do {
186 origCount = *countP;
187
188 if ( ((UInt16) origCount | 0x1) == 0xffff ) {
189 if (origCount & 0x1) {
190 // If count == 0xffff that means we are freeing now so we can
191 // just return obviously somebody is cleaning up some dangling
192 // references. So we blow out immediately.
193 return;
194 }
195 else {
196 // If count == 0xfffe then we have wrapped our reference
197 // count. We should stop counting now as this reference must be
198 // leaked rather than accidently freeing an active object later.
199
200 #if !DEBUG
201 return; // return out of function which pegs the reference
202 #else DEBUG
203 // @@@ gvdl: eventually need to make this panic optional
204 // based on a boot argument i.e. debug= boot flag
205 panic("OSObject::refcount: %s",
206 "About to unreference a pegged object, reference leak?");
207 #endif /* !DEBUG */
208 }
209 }
210 actualCount = origCount - dec;
211 if ((UInt16) actualCount < when)
212 newCount = 0xffff;
213 else
214 newCount = actualCount;
215
216 } while (!OSCompareAndSwap(origCount, newCount, (UInt32 *) countP));
217
218 //
219 // This panic means that we have just attempted to release an object
220 // who's retain count has gone to less than the number of collections
221 // it is a member off. Take a panic immediately.
222 // In Fact the panic MAY not be a registry corruption but it is
223 // ALWAYS the wrong thing to do. I call it a registry corruption 'cause
224 // the registry is the biggest single use of a network of collections.
225 //
226 if ((UInt16) actualCount < (actualCount >> 16))
227 panic("A driver releasing a(n) %s has corrupted the registry\n",
228 getClassName(this));
229
230 // Check for a 'free' condition and that if we are first through
231 if (newCount == 0xffff)
232 ((OSObject *) this)->free();
233 }
234
235 void OSObject::release() const
236 {
237 taggedRelease(0);
238 }
239
240 void OSObject::retain() const
241 {
242 taggedRetain(0);
243 }
244
245 void OSObject::release(int when) const
246 {
247 taggedRelease(0, when);
248 }
249
250 bool OSObject::serialize(OSSerialize *s) const
251 {
252 if (s->previouslySerialized(this)) return true;
253
254 if (!s->addXMLStartTag(this, "string")) return false;
255
256 if (!s->addString(getClassName(this))) return false;
257 if (!s->addString(" is not serializable")) return false;
258
259 return s->addXMLEndTag("string");
260 }
261
262 void *OSObject::operator new(size_t size)
263 {
264 void *mem = (void *) kalloc(size);
265 assert(mem);
266 bzero(mem, size);
267
268 ACCUMSIZE(size);
269
270 return mem;
271 }
272
273 void OSObject::operator delete(void *mem, size_t size)
274 {
275 kfree((vm_offset_t) mem, size);
276
277 ACCUMSIZE(-size);
278 }