]>
git.saurik.com Git - apple/xnu.git/blob - libkern/c++/OSObject.cpp
de9cc00fa5c2fe94f89b35a8768b3ac43eece2bb
2 * Copyright (c) 2000 Apple 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++/OSString.h>
32 #include <libkern/c++/OSArray.h>
33 #include <libkern/c++/OSSerialize.h>
34 #include <libkern/c++/OSLib.h>
35 #include <libkern/OSDebug.h>
36 #include <libkern/c++/OSCPPDebug.h>
37 #include <IOKit/IOKitDebug.h>
38 #include <libkern/OSAtomic.h>
40 #include <libkern/c++/OSCollection.h>
42 #include <kern/queue.h>
49 // OSDefineMetaClassAndAbstractStructors(OSObject, 0);
50 /* Class global data */
51 OSObject::MetaClass
OSObject::gMetaClass
;
52 const OSMetaClass
* const OSObject::metaClass
= &OSObject::gMetaClass
;
53 const OSMetaClass
* const OSObject::superClass
= 0;
55 /* Class member functions - Can't use defaults */
60 OSObject::getMetaClass() const
65 OSObject::MetaClass::alloc() const
70 /* The OSObject::MetaClass constructor */
71 OSObject::MetaClass::MetaClass()
72 : OSMetaClass("OSObject", OSObject::superClass
, sizeof(OSObject
))
77 OSMetaClassDefineReservedUnused(OSObject
, 0);
78 OSMetaClassDefineReservedUnused(OSObject
, 1);
79 OSMetaClassDefineReservedUnused(OSObject
, 2);
80 OSMetaClassDefineReservedUnused(OSObject
, 3);
81 OSMetaClassDefineReservedUnused(OSObject
, 4);
82 OSMetaClassDefineReservedUnused(OSObject
, 5);
83 OSMetaClassDefineReservedUnused(OSObject
, 6);
84 OSMetaClassDefineReservedUnused(OSObject
, 7);
85 OSMetaClassDefineReservedUnused(OSObject
, 8);
86 OSMetaClassDefineReservedUnused(OSObject
, 9);
87 OSMetaClassDefineReservedUnused(OSObject
, 10);
88 OSMetaClassDefineReservedUnused(OSObject
, 11);
89 OSMetaClassDefineReservedUnused(OSObject
, 12);
90 OSMetaClassDefineReservedUnused(OSObject
, 13);
91 OSMetaClassDefineReservedUnused(OSObject
, 14);
92 OSMetaClassDefineReservedUnused(OSObject
, 15);
95 getClassName(const OSObject
*obj
)
97 const OSMetaClass
*meta
= obj
->getMetaClass();
98 return (meta
) ? meta
->getClassName() : "unknown class?";
102 OSObject::getRetainCount() const
104 return (int) ((UInt16
) retainCount
);
108 OSObject::taggedTryRetain(const void *tag
) const
110 volatile UInt32
*countP
= (volatile UInt32
*) &retainCount
;
115 // Increment the collection bucket.
116 if ((const void *) OSTypeID(OSCollection
) == tag
) {
122 if (((UInt16
) origCount
| 0x1) == 0xffff) {
123 if (origCount
& 0x1) {
124 // If count == 0xffff that means we are freeing now so we can
125 // just return obviously somebody is cleaning up dangling
129 // If count == 0xfffe then we have wrapped our reference count.
130 // We should stop counting now as this reference must be
131 // leaked rather than accidently wrapping around the clock and
132 // freeing a very active object later.
135 break; // Break out of update loop which pegs the reference
137 // @@@ gvdl: eventually need to make this panic optional
138 // based on a boot argument i.e. debug= boot flag
139 panic("OSObject::refcount: "
140 "About to wrap the reference count, reference leak?");
145 newCount
= origCount
+ inc
;
146 } while (!OSCompareAndSwap(origCount
, newCount
, const_cast<UInt32
*>(countP
)));
152 OSObject::taggedRetain(const void *tag
) const
154 if (!taggedTryRetain(tag
)) {
155 panic("OSObject::refcount: Attempting to retain a freed object");
160 OSObject::taggedRelease(const void *tag
) const
162 taggedRelease(tag
, 1);
166 OSObject::taggedRelease(const void *tag
, const int when
) const
168 volatile UInt32
*countP
= (volatile UInt32
*) &retainCount
;
174 // Increment the collection bucket.
175 if ((const void *) OSTypeID(OSCollection
) == tag
) {
182 if (((UInt16
) origCount
| 0x1) == 0xffff) {
183 if (origCount
& 0x1) {
184 // If count == 0xffff that means we are freeing now so we can
185 // just return obviously somebody is cleaning up some dangling
186 // references. So we blow out immediately.
189 // If count == 0xfffe then we have wrapped our reference
190 // count. We should stop counting now as this reference must be
191 // leaked rather than accidently freeing an active object later.
194 return; // return out of function which pegs the reference
196 // @@@ gvdl: eventually need to make this panic optional
197 // based on a boot argument i.e. debug= boot flag
198 panic("OSObject::refcount: %s",
199 "About to unreference a pegged object, reference leak?");
203 actualCount
= origCount
- dec
;
204 if ((UInt16
) actualCount
< when
) {
207 newCount
= actualCount
;
209 } while (!OSCompareAndSwap(origCount
, newCount
, const_cast<UInt32
*>(countP
)));
212 // This panic means that we have just attempted to release an object
213 // whose retain count has gone to less than the number of collections
214 // it is a member off. Take a panic immediately.
215 // In fact the panic MAY not be a registry corruption but it is
216 // ALWAYS the wrong thing to do. I call it a registry corruption 'cause
217 // the registry is the biggest single use of a network of collections.
219 // xxx - this error message is overly-specific;
220 // xxx - any code in the kernel could trip this,
221 // xxx - and it applies as noted to all collections, not just the registry
222 if ((UInt16
) actualCount
< (actualCount
>> 16)) {
223 panic("A kext releasing a(n) %s has corrupted the registry.",
227 // Check for a 'free' condition and that if we are first through
228 if (newCount
== 0xffff) {
229 (const_cast<OSObject
*>(this))->free();
234 OSObject::release() const
240 OSObject::retain() const
246 osobject_retain(void * object
)
248 ((OSObject
*)object
)->retain();
252 osobject_release(void * object
)
254 ((OSObject
*)object
)->release();
258 OSObject::release(int when
) const
260 taggedRelease(0, when
);
264 OSObject::serialize(OSSerialize
*s
) const
269 snprintf(cstr
, sizeof(cstr
), "%s is not serializable", getClassName(this));
272 str
= OSString::withCStringNoCopy(cstr
);
277 ok
= str
->serialize(s
);
284 OSObject::operator new(size_t size
)
287 if (kIOTracking
& gIOKitDebug
) {
288 return OSMetaClass::trackedNew(size
);
292 void * mem
= kalloc_tag_bt(size
, VM_KERN_MEMORY_LIBKERN
);
295 OSIVAR_ACCUMSIZE(size
);
301 OSObject::operator delete(void * mem
, size_t size
)
308 if (kIOTracking
& gIOKitDebug
) {
309 return OSMetaClass::trackedDelete(mem
, size
);
314 OSIVAR_ACCUMSIZE(-size
);
321 if (kIOTracking
& gIOKitDebug
) {
322 getMetaClass()->trackedInstance(this);
331 const OSMetaClass
*meta
= getMetaClass();
334 meta
->instanceDestructed();
336 if (kIOTracking
& gIOKitDebug
) {
337 getMetaClass()->trackedFree(this);
346 OSObject::trackingAccumSize(size_t size
)
348 if (kIOTracking
& gIOKitDebug
) {
349 getMetaClass()->trackedAccumSize(this, size
);
354 /* Class member functions - Can't use defaults */
355 /* During constructor vtable is always OSObject's - can't call any subclass */
360 // if (kIOTracking & gIOKitDebug) getMetaClass()->trackedInstance(this);
363 OSObject::OSObject(const OSMetaClass
*)
366 // if (kIOTracking & gIOKitDebug) getMetaClass()->trackedInstance(this);