]>
git.saurik.com Git - apple/xnu.git/blob - libkern/c++/OSObject.cpp
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
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
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
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.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* OSObject.cpp created by gvdl on Fri 1998-11-17 */
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>
38 #include <libkern/c++/OSCollection.h>
45 #define ACCUMSIZE(s) do { debug_ivars_size += (s); } while(0)
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;
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; }
64 /* The OSObject::MetaClass constructor */
65 OSObject::MetaClass::MetaClass()
66 : OSMetaClass("OSObject", OSObject::superClass
, sizeof(OSObject
))
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);
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);
106 static const char *getClassName(const OSObject
*obj
)
108 const OSMetaClass
*meta
= obj
->getMetaClass();
109 return (meta
) ? meta
->getClassName() : "unknown class?";
112 bool OSObject::init()
115 #if (!__ppc__) || (__GNUC__ < 3)
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()
123 const OSMetaClass
*meta
= getMetaClass();
126 meta
->instanceDestructed();
129 #endif /* (!__ppc__) || (__GNUC__ < 3) */
131 int OSObject::getRetainCount() const
133 return (int) ((UInt16
) retainCount
);
136 void OSObject::taggedRetain(const void *tag
) const
138 volatile UInt32
*countP
= (volatile UInt32
*) &retainCount
;
143 // Increment the collection bucket.
144 if ((const void *) OSTypeID(OSCollection
) == tag
)
149 if ( ((UInt16
) origCount
| 0x1) == 0xffff ) {
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
155 msg
= "Attempting to retain a freed object";
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.
164 break; // Break out of update loop which pegs the reference
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?";
171 panic("OSObject::refcount: %s", msg
);
174 newCount
= origCount
+ inc
;
175 } while (!OSCompareAndSwap(origCount
, newCount
, (UInt32
*) countP
));
178 void OSObject::taggedRelease(const void *tag
) const
180 taggedRelease(tag
, 1);
183 void OSObject::taggedRelease(const void *tag
, const int when
) const
185 volatile UInt32
*countP
= (volatile UInt32
*) &retainCount
;
191 // Increment the collection bucket.
192 if ((const void *) OSTypeID(OSCollection
) == tag
)
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.
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.
211 return; // return out of function which pegs the reference
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?");
220 actualCount
= origCount
- dec
;
221 if ((UInt16
) actualCount
< when
)
224 newCount
= actualCount
;
226 } while (!OSCompareAndSwap(origCount
, newCount
, (UInt32
*) countP
));
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.
236 if ((UInt16
) actualCount
< (actualCount
>> 16))
237 panic("A driver releasing a(n) %s has corrupted the registry\n",
240 // Check for a 'free' condition and that if we are first through
241 if (newCount
== 0xffff)
242 ((OSObject
*) this)->free();
245 void OSObject::release() const
250 void OSObject::retain() const
255 void OSObject::release(int when
) const
257 taggedRelease(0, when
);
260 bool OSObject::serialize(OSSerialize
*s
) const
262 if (s
->previouslySerialized(this)) return true;
264 if (!s
->addXMLStartTag(this, "string")) return false;
266 if (!s
->addString(getClassName(this))) return false;
267 if (!s
->addString(" is not serializable")) return false;
269 return s
->addXMLEndTag("string");
272 void *OSObject::operator new(size_t size
)
274 void *mem
= (void *) kalloc(size
);
283 void OSObject::operator delete(void *mem
, size_t size
)