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