]>
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_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* OSObject.cpp created by gvdl on Fri 1998-11-17 */
24 #include <libkern/c++/OSObject.h>
25 #include <libkern/c++/OSSerialize.h>
26 #include <libkern/c++/OSLib.h>
27 #include <libkern/c++/OSCPPDebug.h>
28 #include <libkern/OSAtomic.h>
30 #include <libkern/c++/OSCollection.h>
37 #define ACCUMSIZE(s) do { debug_ivars_size += (s); } while(0)
42 // OSDefineMetaClassAndAbstractStructors(OSObject, 0);
43 /* Class global data */
44 OSObject::MetaClass
OSObject::gMetaClass
;
45 const OSMetaClass
* const OSObject::metaClass
= &OSObject::gMetaClass
;
46 const OSMetaClass
* const OSObject::superClass
= 0;
48 /* Class member functions - Can't use defaults */
49 OSObject::OSObject() { retainCount
= 1; }
50 OSObject::OSObject(const OSMetaClass
*) { retainCount
= 1; }
51 OSObject::~OSObject() { }
52 const OSMetaClass
* OSObject::getMetaClass() const
53 { return &gMetaClass
; }
54 OSObject
*OSObject::MetaClass::alloc() const { return 0; }
56 /* The OSObject::MetaClass constructor */
57 OSObject::MetaClass::MetaClass()
58 : OSMetaClass("OSObject", OSObject::superClass
, sizeof(OSObject
))
62 OSMetaClassDefineReservedUnused(OSObject
, 0);
63 OSMetaClassDefineReservedUnused(OSObject
, 1);
64 OSMetaClassDefineReservedUnused(OSObject
, 2);
65 OSMetaClassDefineReservedUnused(OSObject
, 3);
66 OSMetaClassDefineReservedUnused(OSObject
, 4);
67 OSMetaClassDefineReservedUnused(OSObject
, 5);
68 OSMetaClassDefineReservedUnused(OSObject
, 6);
69 OSMetaClassDefineReservedUnused(OSObject
, 7);
70 OSMetaClassDefineReservedUnused(OSObject
, 8);
71 OSMetaClassDefineReservedUnused(OSObject
, 9);
72 OSMetaClassDefineReservedUnused(OSObject
, 10);
73 OSMetaClassDefineReservedUnused(OSObject
, 11);
74 OSMetaClassDefineReservedUnused(OSObject
, 12);
75 OSMetaClassDefineReservedUnused(OSObject
, 13);
76 OSMetaClassDefineReservedUnused(OSObject
, 14);
77 OSMetaClassDefineReservedUnused(OSObject
, 15);
80 OSMetaClassDefineReservedUnused(OSObject
, 16);
81 OSMetaClassDefineReservedUnused(OSObject
, 17);
82 OSMetaClassDefineReservedUnused(OSObject
, 18);
83 OSMetaClassDefineReservedUnused(OSObject
, 19);
84 OSMetaClassDefineReservedUnused(OSObject
, 20);
85 OSMetaClassDefineReservedUnused(OSObject
, 21);
86 OSMetaClassDefineReservedUnused(OSObject
, 22);
87 OSMetaClassDefineReservedUnused(OSObject
, 23);
88 OSMetaClassDefineReservedUnused(OSObject
, 24);
89 OSMetaClassDefineReservedUnused(OSObject
, 25);
90 OSMetaClassDefineReservedUnused(OSObject
, 26);
91 OSMetaClassDefineReservedUnused(OSObject
, 27);
92 OSMetaClassDefineReservedUnused(OSObject
, 28);
93 OSMetaClassDefineReservedUnused(OSObject
, 29);
94 OSMetaClassDefineReservedUnused(OSObject
, 30);
95 OSMetaClassDefineReservedUnused(OSObject
, 31);
98 static const char *getClassName(const OSObject
*obj
)
100 const OSMetaClass
*meta
= obj
->getMetaClass();
101 return (meta
) ? meta
->getClassName() : "unknown class?";
104 bool OSObject::init()
107 #if (!__ppc__) || (__GNUC__ < 3)
109 // Implemented in assembler in post gcc 3.x systems as we have a problem
110 // where the destructor in gcc2.95 gets 2 arguments. The second argument
111 // appears to be a flag argument. I have copied the assembler from Puma xnu
112 // to OSRuntimeSupport.c So for 2.95 builds use the C
113 void OSObject::free()
115 const OSMetaClass
*meta
= getMetaClass();
118 meta
->instanceDestructed();
121 #endif /* (!__ppc__) || (__GNUC__ < 3) */
123 int OSObject::getRetainCount() const
125 return (int) ((UInt16
) retainCount
);
128 void OSObject::taggedRetain(const void *tag
) const
130 volatile UInt32
*countP
= (volatile UInt32
*) &retainCount
;
135 // Increment the collection bucket.
136 if ((const void *) OSTypeID(OSCollection
) == tag
)
141 if ( ((UInt16
) origCount
| 0x1) == 0xffff ) {
143 if (origCount
& 0x1) {
144 // If count == 0xffff that means we are freeing now so we can
145 // just return obviously somebody is cleaning up dangling
147 msg
= "Attempting to retain a freed object";
150 // If count == 0xfffe then we have wrapped our reference count.
151 // We should stop counting now as this reference must be
152 // leaked rather than accidently wrapping around the clock and
153 // freeing a very active object later.
156 break; // Break out of update loop which pegs the reference
158 // @@@ gvdl: eventually need to make this panic optional
159 // based on a boot argument i.e. debug= boot flag
160 msg
= "About to wrap the reference count, reference leak?";
163 panic("OSObject::refcount: %s", msg
);
166 newCount
= origCount
+ inc
;
167 } while (!OSCompareAndSwap(origCount
, newCount
, (UInt32
*) countP
));
170 void OSObject::taggedRelease(const void *tag
) const
172 taggedRelease(tag
, 1);
175 void OSObject::taggedRelease(const void *tag
, const int when
) const
177 volatile UInt32
*countP
= (volatile UInt32
*) &retainCount
;
183 // Increment the collection bucket.
184 if ((const void *) OSTypeID(OSCollection
) == tag
)
190 if ( ((UInt16
) origCount
| 0x1) == 0xffff ) {
191 if (origCount
& 0x1) {
192 // If count == 0xffff that means we are freeing now so we can
193 // just return obviously somebody is cleaning up some dangling
194 // references. So we blow out immediately.
198 // If count == 0xfffe then we have wrapped our reference
199 // count. We should stop counting now as this reference must be
200 // leaked rather than accidently freeing an active object later.
203 return; // return out of function which pegs the reference
205 // @@@ gvdl: eventually need to make this panic optional
206 // based on a boot argument i.e. debug= boot flag
207 panic("OSObject::refcount: %s",
208 "About to unreference a pegged object, reference leak?");
212 actualCount
= origCount
- dec
;
213 if ((UInt16
) actualCount
< when
)
216 newCount
= actualCount
;
218 } while (!OSCompareAndSwap(origCount
, newCount
, (UInt32
*) countP
));
221 // This panic means that we have just attempted to release an object
222 // who's retain count has gone to less than the number of collections
223 // it is a member off. Take a panic immediately.
224 // In Fact the panic MAY not be a registry corruption but it is
225 // ALWAYS the wrong thing to do. I call it a registry corruption 'cause
226 // the registry is the biggest single use of a network of collections.
228 if ((UInt16
) actualCount
< (actualCount
>> 16))
229 panic("A driver releasing a(n) %s has corrupted the registry\n",
232 // Check for a 'free' condition and that if we are first through
233 if (newCount
== 0xffff)
234 ((OSObject
*) this)->free();
237 void OSObject::release() const
242 void OSObject::retain() const
247 void OSObject::release(int when
) const
249 taggedRelease(0, when
);
252 bool OSObject::serialize(OSSerialize
*s
) const
254 if (s
->previouslySerialized(this)) return true;
256 if (!s
->addXMLStartTag(this, "string")) return false;
258 if (!s
->addString(getClassName(this))) return false;
259 if (!s
->addString(" is not serializable")) return false;
261 return s
->addXMLEndTag("string");
264 void *OSObject::operator new(size_t size
)
266 void *mem
= (void *) kalloc(size
);
275 void OSObject::operator delete(void *mem
, size_t size
)