]>
git.saurik.com Git - apple/xnu.git/blob - libkern/c++/OSObject.cpp
86c31e279bbcea41cf95dc3a2f0161187c7d2086
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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 License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* OSObject.cpp created by gvdl on Fri 1998-11-17 */
30 #include <libkern/c++/OSObject.h>
31 #include <libkern/c++/OSSerialize.h>
32 #include <libkern/c++/OSLib.h>
33 #include <libkern/c++/OSCPPDebug.h>
34 #include <libkern/OSAtomic.h>
36 #include <libkern/c++/OSCollection.h>
43 #define ACCUMSIZE(s) do { debug_ivars_size += (s); } while(0)
48 // OSDefineMetaClassAndAbstractStructors(OSObject, 0);
49 /* Class global data */
50 OSObject::MetaClass
OSObject::gMetaClass
;
51 const OSMetaClass
* const OSObject::metaClass
= &OSObject::gMetaClass
;
52 const OSMetaClass
* const OSObject::superClass
= 0;
54 /* Class member functions - Can't use defaults */
55 OSObject::OSObject() { retainCount
= 1; }
56 OSObject::OSObject(const OSMetaClass
*) { retainCount
= 1; }
57 OSObject::~OSObject() { }
58 const OSMetaClass
* OSObject::getMetaClass() const
59 { return &gMetaClass
; }
60 OSObject
*OSObject::MetaClass::alloc() const { return 0; }
62 /* The OSObject::MetaClass constructor */
63 OSObject::MetaClass::MetaClass()
64 : OSMetaClass("OSObject", OSObject::superClass
, sizeof(OSObject
))
68 OSMetaClassDefineReservedUnused(OSObject
, 0);
69 OSMetaClassDefineReservedUnused(OSObject
, 1);
70 OSMetaClassDefineReservedUnused(OSObject
, 2);
71 OSMetaClassDefineReservedUnused(OSObject
, 3);
72 OSMetaClassDefineReservedUnused(OSObject
, 4);
73 OSMetaClassDefineReservedUnused(OSObject
, 5);
74 OSMetaClassDefineReservedUnused(OSObject
, 6);
75 OSMetaClassDefineReservedUnused(OSObject
, 7);
76 OSMetaClassDefineReservedUnused(OSObject
, 8);
77 OSMetaClassDefineReservedUnused(OSObject
, 9);
78 OSMetaClassDefineReservedUnused(OSObject
, 10);
79 OSMetaClassDefineReservedUnused(OSObject
, 11);
80 OSMetaClassDefineReservedUnused(OSObject
, 12);
81 OSMetaClassDefineReservedUnused(OSObject
, 13);
82 OSMetaClassDefineReservedUnused(OSObject
, 14);
83 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);
104 static const char *getClassName(const OSObject
*obj
)
106 const OSMetaClass
*meta
= obj
->getMetaClass();
107 return (meta
) ? meta
->getClassName() : "unknown class?";
110 bool OSObject::init()
113 #if (!__ppc__) || (__GNUC__ < 3)
115 // Implemented in assembler in post gcc 3.x systems as we have a problem
116 // where the destructor in gcc2.95 gets 2 arguments. The second argument
117 // appears to be a flag argument. I have copied the assembler from Puma xnu
118 // to OSRuntimeSupport.c So for 2.95 builds use the C
119 void OSObject::free()
121 const OSMetaClass
*meta
= getMetaClass();
124 meta
->instanceDestructed();
127 #endif /* (!__ppc__) || (__GNUC__ < 3) */
129 int OSObject::getRetainCount() const
131 return (int) ((UInt16
) retainCount
);
134 void OSObject::taggedRetain(const void *tag
) const
136 volatile UInt32
*countP
= (volatile UInt32
*) &retainCount
;
141 // Increment the collection bucket.
142 if ((const void *) OSTypeID(OSCollection
) == tag
)
147 if ( ((UInt16
) origCount
| 0x1) == 0xffff ) {
149 if (origCount
& 0x1) {
150 // If count == 0xffff that means we are freeing now so we can
151 // just return obviously somebody is cleaning up dangling
153 msg
= "Attempting to retain a freed object";
156 // If count == 0xfffe then we have wrapped our reference count.
157 // We should stop counting now as this reference must be
158 // leaked rather than accidently wrapping around the clock and
159 // freeing a very active object later.
162 break; // Break out of update loop which pegs the reference
164 // @@@ gvdl: eventually need to make this panic optional
165 // based on a boot argument i.e. debug= boot flag
166 msg
= "About to wrap the reference count, reference leak?";
169 panic("OSObject::refcount: %s", msg
);
172 newCount
= origCount
+ inc
;
173 } while (!OSCompareAndSwap(origCount
, newCount
, (UInt32
*) countP
));
176 void OSObject::taggedRelease(const void *tag
) const
178 taggedRelease(tag
, 1);
181 void OSObject::taggedRelease(const void *tag
, const int when
) const
183 volatile UInt32
*countP
= (volatile UInt32
*) &retainCount
;
189 // Increment the collection bucket.
190 if ((const void *) OSTypeID(OSCollection
) == tag
)
196 if ( ((UInt16
) origCount
| 0x1) == 0xffff ) {
197 if (origCount
& 0x1) {
198 // If count == 0xffff that means we are freeing now so we can
199 // just return obviously somebody is cleaning up some dangling
200 // references. So we blow out immediately.
204 // If count == 0xfffe then we have wrapped our reference
205 // count. We should stop counting now as this reference must be
206 // leaked rather than accidently freeing an active object later.
209 return; // return out of function which pegs the reference
211 // @@@ gvdl: eventually need to make this panic optional
212 // based on a boot argument i.e. debug= boot flag
213 panic("OSObject::refcount: %s",
214 "About to unreference a pegged object, reference leak?");
218 actualCount
= origCount
- dec
;
219 if ((UInt16
) actualCount
< when
)
222 newCount
= actualCount
;
224 } while (!OSCompareAndSwap(origCount
, newCount
, (UInt32
*) countP
));
227 // This panic means that we have just attempted to release an object
228 // who's retain count has gone to less than the number of collections
229 // it is a member off. Take a panic immediately.
230 // In Fact the panic MAY not be a registry corruption but it is
231 // ALWAYS the wrong thing to do. I call it a registry corruption 'cause
232 // the registry is the biggest single use of a network of collections.
234 if ((UInt16
) actualCount
< (actualCount
>> 16))
235 panic("A driver releasing a(n) %s has corrupted the registry\n",
238 // Check for a 'free' condition and that if we are first through
239 if (newCount
== 0xffff)
240 ((OSObject
*) this)->free();
243 void OSObject::release() const
248 void OSObject::retain() const
253 void OSObject::release(int when
) const
255 taggedRelease(0, when
);
258 bool OSObject::serialize(OSSerialize
*s
) const
260 if (s
->previouslySerialized(this)) return true;
262 if (!s
->addXMLStartTag(this, "string")) return false;
264 if (!s
->addString(getClassName(this))) return false;
265 if (!s
->addString(" is not serializable")) return false;
267 return s
->addXMLEndTag("string");
270 void *OSObject::operator new(size_t size
)
272 void *mem
= (void *) kalloc(size
);
281 void OSObject::operator delete(void *mem
, size_t size
)