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++/OSArray.h>
32 #include <libkern/c++/OSSerialize.h>
33 #include <libkern/c++/OSLib.h>
34 #include <libkern/OSDebug.h>
35 #include <libkern/c++/OSCPPDebug.h>
36 #include <IOKit/IOKitDebug.h>
37 #include <libkern/OSAtomic.h>
39 #include <libkern/c++/OSCollection.h>
41 #include <kern/queue.h>
48 #define ACCUMSIZE(s) do { debug_ivars_size += (s); } while(0)
53 // OSDefineMetaClassAndAbstractStructors(OSObject, 0);
54 /* Class global data */
55 OSObject::MetaClass
OSObject::gMetaClass
;
56 const OSMetaClass
* const OSObject::metaClass
= &OSObject::gMetaClass
;
57 const OSMetaClass
* const OSObject::superClass
= 0;
59 /* Class member functions - Can't use defaults */
60 OSObject::OSObject() { retainCount
= 1; }
61 OSObject::OSObject(const OSMetaClass
*) { retainCount
= 1; }
62 OSObject::~OSObject() { }
63 const OSMetaClass
* OSObject::getMetaClass() const
64 { return &gMetaClass
; }
65 OSObject
*OSObject::MetaClass::alloc() const { return 0; }
67 /* The OSObject::MetaClass constructor */
68 OSObject::MetaClass::MetaClass()
69 : OSMetaClass("OSObject", OSObject::superClass
, sizeof(OSObject
))
73 OSMetaClassDefineReservedUnused(OSObject
, 0);
74 OSMetaClassDefineReservedUnused(OSObject
, 1);
75 OSMetaClassDefineReservedUnused(OSObject
, 2);
76 OSMetaClassDefineReservedUnused(OSObject
, 3);
77 OSMetaClassDefineReservedUnused(OSObject
, 4);
78 OSMetaClassDefineReservedUnused(OSObject
, 5);
79 OSMetaClassDefineReservedUnused(OSObject
, 6);
80 OSMetaClassDefineReservedUnused(OSObject
, 7);
81 OSMetaClassDefineReservedUnused(OSObject
, 8);
82 OSMetaClassDefineReservedUnused(OSObject
, 9);
83 OSMetaClassDefineReservedUnused(OSObject
, 10);
84 OSMetaClassDefineReservedUnused(OSObject
, 11);
85 OSMetaClassDefineReservedUnused(OSObject
, 12);
86 OSMetaClassDefineReservedUnused(OSObject
, 13);
87 OSMetaClassDefineReservedUnused(OSObject
, 14);
88 OSMetaClassDefineReservedUnused(OSObject
, 15);
90 static const char *getClassName(const OSObject
*obj
)
92 const OSMetaClass
*meta
= obj
->getMetaClass();
93 return (meta
) ? meta
->getClassName() : "unknown class?";
101 const OSMetaClass
*meta
= getMetaClass();
104 meta
->instanceDestructed();
108 int OSObject::getRetainCount() const
110 return (int) ((UInt16
) retainCount
);
113 void OSObject::taggedRetain(const void *tag
) const
115 volatile UInt32
*countP
= (volatile UInt32
*) &retainCount
;
120 // Increment the collection bucket.
121 if ((const void *) OSTypeID(OSCollection
) == tag
)
126 if ( ((UInt16
) origCount
| 0x1) == 0xffff ) {
128 if (origCount
& 0x1) {
129 // If count == 0xffff that means we are freeing now so we can
130 // just return obviously somebody is cleaning up dangling
132 msg
= "Attempting to retain a freed object";
135 // If count == 0xfffe then we have wrapped our reference count.
136 // We should stop counting now as this reference must be
137 // leaked rather than accidently wrapping around the clock and
138 // freeing a very active object later.
141 break; // Break out of update loop which pegs the reference
143 // @@@ gvdl: eventually need to make this panic optional
144 // based on a boot argument i.e. debug= boot flag
145 msg
= "About to wrap the reference count, reference leak?";
148 panic("OSObject::refcount: %s", msg
);
151 newCount
= origCount
+ inc
;
152 } while (!OSCompareAndSwap(origCount
, newCount
, const_cast<UInt32
*>(countP
)));
155 void OSObject::taggedRelease(const void *tag
) const
157 taggedRelease(tag
, 1);
160 void OSObject::taggedRelease(const void *tag
, const int when
) const
162 volatile UInt32
*countP
= (volatile UInt32
*) &retainCount
;
168 // Increment the collection bucket.
169 if ((const void *) OSTypeID(OSCollection
) == tag
)
175 if ( ((UInt16
) origCount
| 0x1) == 0xffff ) {
176 if (origCount
& 0x1) {
177 // If count == 0xffff that means we are freeing now so we can
178 // just return obviously somebody is cleaning up some dangling
179 // references. So we blow out immediately.
183 // If count == 0xfffe then we have wrapped our reference
184 // count. We should stop counting now as this reference must be
185 // leaked rather than accidently freeing an active object later.
188 return; // return out of function which pegs the reference
190 // @@@ gvdl: eventually need to make this panic optional
191 // based on a boot argument i.e. debug= boot flag
192 panic("OSObject::refcount: %s",
193 "About to unreference a pegged object, reference leak?");
197 actualCount
= origCount
- dec
;
198 if ((UInt16
) actualCount
< when
)
201 newCount
= actualCount
;
203 } while (!OSCompareAndSwap(origCount
, newCount
, const_cast<UInt32
*>(countP
)));
206 // This panic means that we have just attempted to release an object
207 // whose retain count has gone to less than the number of collections
208 // it is a member off. Take a panic immediately.
209 // In fact the panic MAY not be a registry corruption but it is
210 // ALWAYS the wrong thing to do. I call it a registry corruption 'cause
211 // the registry is the biggest single use of a network of collections.
213 // xxx - this error message is overly-specific;
214 // xxx - any code in the kernel could trip this,
215 // xxx - and it applies as noted to all collections, not just the registry
216 if ((UInt16
) actualCount
< (actualCount
>> 16)) {
217 panic("A kext releasing a(n) %s has corrupted the registry.",
221 // Check for a 'free' condition and that if we are first through
222 if (newCount
== 0xffff) {
223 (const_cast<OSObject
*>(this))->free();
227 void OSObject::release() const
232 void OSObject::retain() const
237 void OSObject::release(int when
) const
239 taggedRelease(0, when
);
242 bool OSObject::serialize(OSSerialize
*s
) const
244 if (s
->previouslySerialized(this)) return true;
246 if (!s
->addXMLStartTag(this, "string")) return false;
248 if (!s
->addString(getClassName(this))) return false;
249 if (!s
->addString(" is not serializable")) return false;
251 return s
->addXMLEndTag("string");
255 thread_t gOSObjectTrackThread
;
257 queue_head_t gOSObjectTrackList
=
258 { (queue_t
) &gOSObjectTrackList
, (queue_t
) &gOSObjectTrackList
};
260 lck_spin_t gOSObjectTrackLock
;
262 OSArray
* OSFlushObjectTrackList(void)
267 array
= OSArray::withCapacity(16);
269 lck_spin_lock(&gOSObjectTrackLock
);
270 while (!queue_empty(&gOSObjectTrackList
))
272 next
= queue_first(&gOSObjectTrackList
);
274 lck_spin_unlock(&gOSObjectTrackLock
);
275 array
->setObject((OSObject
*) (next
+ 1));
276 lck_spin_lock(&gOSObjectTrackLock
);
278 lck_spin_unlock(&gOSObjectTrackLock
);
283 struct OSObjectTracking
289 void *OSObject::operator new(size_t size
)
291 size_t tracking
= (gIOKitDebug
& kOSTraceObjectAlloc
)
292 ? sizeof(OSObjectTracking
) : 0;
293 OSObjectTracking
* mem
= (OSObjectTracking
*) kalloc(size
+ tracking
);
299 if ((((thread_t
) 1) == gOSObjectTrackThread
) || (current_thread() == gOSObjectTrackThread
))
301 (void) OSBacktrace(&mem
->bt
[0], sizeof(mem
->bt
) / sizeof(mem
->bt
[0]));
302 lck_spin_lock(&gOSObjectTrackLock
);
303 enqueue_tail(&gOSObjectTrackList
, &mem
->link
);
304 lck_spin_unlock(&gOSObjectTrackLock
);
318 void OSObject::operator delete(void *_mem
, size_t size
)
320 size_t tracking
= (gIOKitDebug
& kOSTraceObjectAlloc
)
321 ? sizeof(OSObjectTracking
) : 0;
322 OSObjectTracking
* mem
= (OSObjectTracking
*) _mem
;
332 lck_spin_lock(&gOSObjectTrackLock
);
334 lck_spin_unlock(&gOSObjectTrackLock
);
338 kfree(mem
, size
+ tracking
);