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