]>
git.saurik.com Git - apple/xnu.git/blob - libkern/c++/OSObject.cpp
8a138c4600dc557bce51647bb17365bcec6c4ed7
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);
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);
103 static const char *getClassName(const OSObject
*obj
)
105 const OSMetaClass
*meta
= obj
->getMetaClass();
106 return (meta
) ? meta
->getClassName() : "unknown class?";
109 bool OSObject::init()
112 #if (!__ppc__) || (__GNUC__ < 3)
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()
120 const OSMetaClass
*meta
= getMetaClass();
123 meta
->instanceDestructed();
126 #endif /* (!__ppc__) || (__GNUC__ < 3) */
128 int OSObject::getRetainCount() const
130 return (int) ((UInt16
) retainCount
);
133 void OSObject::taggedRetain(const void *tag
) const
135 volatile UInt32
*countP
= (volatile UInt32
*) &retainCount
;
140 // Increment the collection bucket.
141 if ((const void *) OSTypeID(OSCollection
) == tag
)
146 if ( ((UInt16
) origCount
| 0x1) == 0xffff ) {
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
152 msg
= "Attempting to retain a freed object";
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.
161 break; // Break out of update loop which pegs the reference
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?";
168 panic("OSObject::refcount: %s", msg
);
171 newCount
= origCount
+ inc
;
172 } while (!OSCompareAndSwap(origCount
, newCount
, (UInt32
*) countP
));
175 void OSObject::taggedRelease(const void *tag
) const
177 taggedRelease(tag
, 1);
180 void OSObject::taggedRelease(const void *tag
, const int when
) const
182 volatile UInt32
*countP
= (volatile UInt32
*) &retainCount
;
188 // Increment the collection bucket.
189 if ((const void *) OSTypeID(OSCollection
) == tag
)
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.
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.
208 return; // return out of function which pegs the reference
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?");
217 actualCount
= origCount
- dec
;
218 if ((UInt16
) actualCount
< when
)
221 newCount
= actualCount
;
223 } while (!OSCompareAndSwap(origCount
, newCount
, (UInt32
*) countP
));
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.
233 if ((UInt16
) actualCount
< (actualCount
>> 16))
234 panic("A driver releasing a(n) %s has corrupted the registry\n",
237 // Check for a 'free' condition and that if we are first through
238 if (newCount
== 0xffff)
239 ((OSObject
*) this)->free();
242 void OSObject::release() const
247 void OSObject::retain() const
252 void OSObject::release(int when
) const
254 taggedRelease(0, when
);
257 bool OSObject::serialize(OSSerialize
*s
) const
259 if (s
->previouslySerialized(this)) return true;
261 if (!s
->addXMLStartTag(this, "string")) return false;
263 if (!s
->addString(getClassName(this))) return false;
264 if (!s
->addString(" is not serializable")) return false;
266 return s
->addXMLEndTag("string");
269 void *OSObject::operator new(size_t size
)
271 void *mem
= (void *) kalloc(size
);
280 void OSObject::operator delete(void *mem
, size_t size
)
282 kfree((vm_offset_t
) mem
, size
);