1*27b03b36SApple OSS Distributions /*
2*27b03b36SApple OSS Distributions * Copyright (c) 2000 Apple Inc. All rights reserved.
3*27b03b36SApple OSS Distributions *
4*27b03b36SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*27b03b36SApple OSS Distributions *
6*27b03b36SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*27b03b36SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*27b03b36SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*27b03b36SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*27b03b36SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*27b03b36SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*27b03b36SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*27b03b36SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*27b03b36SApple OSS Distributions *
15*27b03b36SApple OSS Distributions * Please obtain a copy of the License at
16*27b03b36SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*27b03b36SApple OSS Distributions *
18*27b03b36SApple OSS Distributions * The Original Code and all software distributed under the License are
19*27b03b36SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*27b03b36SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*27b03b36SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*27b03b36SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*27b03b36SApple OSS Distributions * Please see the License for the specific language governing rights and
24*27b03b36SApple OSS Distributions * limitations under the License.
25*27b03b36SApple OSS Distributions *
26*27b03b36SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*27b03b36SApple OSS Distributions */
28*27b03b36SApple OSS Distributions /* OSObject.cpp created by gvdl on Fri 1998-11-17 */
29*27b03b36SApple OSS Distributions
30*27b03b36SApple OSS Distributions #include <libkern/c++/OSObject.h>
31*27b03b36SApple OSS Distributions #include <libkern/c++/OSString.h>
32*27b03b36SApple OSS Distributions #include <libkern/c++/OSArray.h>
33*27b03b36SApple OSS Distributions #include <libkern/c++/OSSerialize.h>
34*27b03b36SApple OSS Distributions #include <libkern/c++/OSLib.h>
35*27b03b36SApple OSS Distributions #include <libkern/OSDebug.h>
36*27b03b36SApple OSS Distributions #include <libkern/c++/OSCPPDebug.h>
37*27b03b36SApple OSS Distributions #include <IOKit/IOKitDebug.h>
38*27b03b36SApple OSS Distributions #include <libkern/OSAtomic.h>
39*27b03b36SApple OSS Distributions
40*27b03b36SApple OSS Distributions #include <libkern/c++/OSCollection.h>
41*27b03b36SApple OSS Distributions
42*27b03b36SApple OSS Distributions #include <kern/queue.h>
43*27b03b36SApple OSS Distributions
44*27b03b36SApple OSS Distributions __BEGIN_DECLS
45*27b03b36SApple OSS Distributions size_t debug_ivars_size;
46*27b03b36SApple OSS Distributions __END_DECLS
47*27b03b36SApple OSS Distributions
48*27b03b36SApple OSS Distributions
49*27b03b36SApple OSS Distributions // OSDefineMetaClassAndAbstractStructors(OSObject, 0);
50*27b03b36SApple OSS Distributions /* Class global data */
51*27b03b36SApple OSS Distributions OSObject::MetaClass OSObject::gMetaClass;
52*27b03b36SApple OSS Distributions const OSMetaClass * const OSObject::metaClass = &OSObject::gMetaClass;
53*27b03b36SApple OSS Distributions const OSMetaClass * const OSObject::superClass = NULL;
54*27b03b36SApple OSS Distributions
55*27b03b36SApple OSS Distributions /* Class member functions - Can't use defaults */
~OSObject()56*27b03b36SApple OSS Distributions OSObject::~OSObject()
57*27b03b36SApple OSS Distributions {
58*27b03b36SApple OSS Distributions }
59*27b03b36SApple OSS Distributions const OSMetaClass *
getMetaClass() const60*27b03b36SApple OSS Distributions OSObject::getMetaClass() const
61*27b03b36SApple OSS Distributions {
62*27b03b36SApple OSS Distributions return &gMetaClass;
63*27b03b36SApple OSS Distributions }
64*27b03b36SApple OSS Distributions OSObject *
alloc() const65*27b03b36SApple OSS Distributions OSObject::MetaClass::alloc() const
66*27b03b36SApple OSS Distributions {
67*27b03b36SApple OSS Distributions return NULL;
68*27b03b36SApple OSS Distributions }
69*27b03b36SApple OSS Distributions
70*27b03b36SApple OSS Distributions /* The OSObject::MetaClass constructor */
MetaClass()71*27b03b36SApple OSS Distributions OSObject::MetaClass::MetaClass()
72*27b03b36SApple OSS Distributions : OSMetaClass("OSObject", OSObject::superClass, sizeof(OSObject))
73*27b03b36SApple OSS Distributions {
74*27b03b36SApple OSS Distributions }
75*27b03b36SApple OSS Distributions
76*27b03b36SApple OSS Distributions // Virtual Padding
77*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSObject, 0);
78*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSObject, 1);
79*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSObject, 2);
80*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSObject, 3);
81*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSObject, 4);
82*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSObject, 5);
83*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSObject, 6);
84*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSObject, 7);
85*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSObject, 8);
86*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSObject, 9);
87*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSObject, 10);
88*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSObject, 11);
89*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSObject, 12);
90*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSObject, 13);
91*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSObject, 14);
92*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSObject, 15);
93*27b03b36SApple OSS Distributions
94*27b03b36SApple OSS Distributions static const char *
getClassName(const OSObject * obj)95*27b03b36SApple OSS Distributions getClassName(const OSObject *obj)
96*27b03b36SApple OSS Distributions {
97*27b03b36SApple OSS Distributions const OSMetaClass *meta = obj->getMetaClass();
98*27b03b36SApple OSS Distributions return (meta) ? meta->getClassName() : "unknown class?";
99*27b03b36SApple OSS Distributions }
100*27b03b36SApple OSS Distributions
101*27b03b36SApple OSS Distributions int
getRetainCount() const102*27b03b36SApple OSS Distributions OSObject::getRetainCount() const
103*27b03b36SApple OSS Distributions {
104*27b03b36SApple OSS Distributions return (int) ((UInt16) retainCount);
105*27b03b36SApple OSS Distributions }
106*27b03b36SApple OSS Distributions
107*27b03b36SApple OSS Distributions bool
taggedTryRetain(const void * tag) const108*27b03b36SApple OSS Distributions OSObject::taggedTryRetain(const void *tag) const
109*27b03b36SApple OSS Distributions {
110*27b03b36SApple OSS Distributions volatile UInt32 *countP = (volatile UInt32 *) &retainCount;
111*27b03b36SApple OSS Distributions UInt32 inc = 1;
112*27b03b36SApple OSS Distributions UInt32 origCount;
113*27b03b36SApple OSS Distributions UInt32 newCount;
114*27b03b36SApple OSS Distributions
115*27b03b36SApple OSS Distributions // Increment the collection bucket.
116*27b03b36SApple OSS Distributions if ((const void *) OSTypeID(OSCollection) == tag) {
117*27b03b36SApple OSS Distributions inc |= (1UL << 16);
118*27b03b36SApple OSS Distributions }
119*27b03b36SApple OSS Distributions
120*27b03b36SApple OSS Distributions do {
121*27b03b36SApple OSS Distributions origCount = *countP;
122*27b03b36SApple OSS Distributions if (((UInt16) origCount | 0x1) == 0xffff) {
123*27b03b36SApple OSS Distributions if (origCount & 0x1) {
124*27b03b36SApple OSS Distributions // If count == 0xffff that means we are freeing now so we can
125*27b03b36SApple OSS Distributions // just return obviously somebody is cleaning up dangling
126*27b03b36SApple OSS Distributions // references.
127*27b03b36SApple OSS Distributions return false;
128*27b03b36SApple OSS Distributions } else {
129*27b03b36SApple OSS Distributions // If count == 0xfffe then we have wrapped our reference count.
130*27b03b36SApple OSS Distributions // We should stop counting now as this reference must be
131*27b03b36SApple OSS Distributions // leaked rather than accidently wrapping around the clock and
132*27b03b36SApple OSS Distributions // freeing a very active object later.
133*27b03b36SApple OSS Distributions
134*27b03b36SApple OSS Distributions #if !DEBUG
135*27b03b36SApple OSS Distributions break; // Break out of update loop which pegs the reference
136*27b03b36SApple OSS Distributions #else /* DEBUG */
137*27b03b36SApple OSS Distributions // @@@ gvdl: eventually need to make this panic optional
138*27b03b36SApple OSS Distributions // based on a boot argument i.e. debug= boot flag
139*27b03b36SApple OSS Distributions panic("OSObject::refcount: "
140*27b03b36SApple OSS Distributions "About to wrap the reference count, reference leak?");
141*27b03b36SApple OSS Distributions #endif /* !DEBUG */
142*27b03b36SApple OSS Distributions }
143*27b03b36SApple OSS Distributions }
144*27b03b36SApple OSS Distributions
145*27b03b36SApple OSS Distributions newCount = origCount + inc;
146*27b03b36SApple OSS Distributions } while (!OSCompareAndSwap(origCount, newCount, const_cast<UInt32 *>(countP)));
147*27b03b36SApple OSS Distributions
148*27b03b36SApple OSS Distributions return true;
149*27b03b36SApple OSS Distributions }
150*27b03b36SApple OSS Distributions
151*27b03b36SApple OSS Distributions void
taggedRetain(const void * tag) const152*27b03b36SApple OSS Distributions OSObject::taggedRetain(const void *tag) const
153*27b03b36SApple OSS Distributions {
154*27b03b36SApple OSS Distributions if (!taggedTryRetain(tag)) {
155*27b03b36SApple OSS Distributions panic("OSObject::refcount: Attempting to retain a freed object");
156*27b03b36SApple OSS Distributions }
157*27b03b36SApple OSS Distributions }
158*27b03b36SApple OSS Distributions
159*27b03b36SApple OSS Distributions void
taggedRelease(const void * tag) const160*27b03b36SApple OSS Distributions OSObject::taggedRelease(const void *tag) const
161*27b03b36SApple OSS Distributions {
162*27b03b36SApple OSS Distributions taggedRelease(tag, 1);
163*27b03b36SApple OSS Distributions }
164*27b03b36SApple OSS Distributions
165*27b03b36SApple OSS Distributions void
taggedRelease(const void * tag,const int when) const166*27b03b36SApple OSS Distributions OSObject::taggedRelease(const void *tag, const int when) const
167*27b03b36SApple OSS Distributions {
168*27b03b36SApple OSS Distributions volatile UInt32 *countP = (volatile UInt32 *) &retainCount;
169*27b03b36SApple OSS Distributions UInt32 dec = 1;
170*27b03b36SApple OSS Distributions UInt32 origCount;
171*27b03b36SApple OSS Distributions UInt32 newCount;
172*27b03b36SApple OSS Distributions UInt32 actualCount;
173*27b03b36SApple OSS Distributions
174*27b03b36SApple OSS Distributions // Increment the collection bucket.
175*27b03b36SApple OSS Distributions if ((const void *) OSTypeID(OSCollection) == tag) {
176*27b03b36SApple OSS Distributions dec |= (1UL << 16);
177*27b03b36SApple OSS Distributions }
178*27b03b36SApple OSS Distributions
179*27b03b36SApple OSS Distributions do {
180*27b03b36SApple OSS Distributions origCount = *countP;
181*27b03b36SApple OSS Distributions
182*27b03b36SApple OSS Distributions if (((UInt16) origCount | 0x1) == 0xffff) {
183*27b03b36SApple OSS Distributions if (origCount & 0x1) {
184*27b03b36SApple OSS Distributions // If count == 0xffff that means we are freeing now so we can
185*27b03b36SApple OSS Distributions // just return obviously somebody is cleaning up some dangling
186*27b03b36SApple OSS Distributions // references. So we blow out immediately.
187*27b03b36SApple OSS Distributions return;
188*27b03b36SApple OSS Distributions } else {
189*27b03b36SApple OSS Distributions // If count == 0xfffe then we have wrapped our reference
190*27b03b36SApple OSS Distributions // count. We should stop counting now as this reference must be
191*27b03b36SApple OSS Distributions // leaked rather than accidently freeing an active object later.
192*27b03b36SApple OSS Distributions
193*27b03b36SApple OSS Distributions #if !DEBUG
194*27b03b36SApple OSS Distributions return; // return out of function which pegs the reference
195*27b03b36SApple OSS Distributions #else /* DEBUG */
196*27b03b36SApple OSS Distributions // @@@ gvdl: eventually need to make this panic optional
197*27b03b36SApple OSS Distributions // based on a boot argument i.e. debug= boot flag
198*27b03b36SApple OSS Distributions panic("OSObject::refcount: %s",
199*27b03b36SApple OSS Distributions "About to unreference a pegged object, reference leak?");
200*27b03b36SApple OSS Distributions #endif /* !DEBUG */
201*27b03b36SApple OSS Distributions }
202*27b03b36SApple OSS Distributions }
203*27b03b36SApple OSS Distributions actualCount = origCount - dec;
204*27b03b36SApple OSS Distributions if ((UInt16) actualCount < when) {
205*27b03b36SApple OSS Distributions newCount = 0xffff;
206*27b03b36SApple OSS Distributions } else {
207*27b03b36SApple OSS Distributions newCount = actualCount;
208*27b03b36SApple OSS Distributions }
209*27b03b36SApple OSS Distributions } while (!OSCompareAndSwap(origCount, newCount, const_cast<UInt32 *>(countP)));
210*27b03b36SApple OSS Distributions
211*27b03b36SApple OSS Distributions //
212*27b03b36SApple OSS Distributions // This panic means that we have just attempted to release an object
213*27b03b36SApple OSS Distributions // whose retain count has gone to less than the number of collections
214*27b03b36SApple OSS Distributions // it is a member off. Take a panic immediately.
215*27b03b36SApple OSS Distributions // In fact the panic MAY not be a registry corruption but it is
216*27b03b36SApple OSS Distributions // ALWAYS the wrong thing to do. I call it a registry corruption 'cause
217*27b03b36SApple OSS Distributions // the registry is the biggest single use of a network of collections.
218*27b03b36SApple OSS Distributions //
219*27b03b36SApple OSS Distributions // xxx - this error message is overly-specific;
220*27b03b36SApple OSS Distributions // xxx - any code in the kernel could trip this,
221*27b03b36SApple OSS Distributions // xxx - and it applies as noted to all collections, not just the registry
222*27b03b36SApple OSS Distributions if ((UInt16) actualCount < (actualCount >> 16)) {
223*27b03b36SApple OSS Distributions panic("A kext releasing a(n) %s has corrupted the registry.",
224*27b03b36SApple OSS Distributions getClassName(this));
225*27b03b36SApple OSS Distributions }
226*27b03b36SApple OSS Distributions
227*27b03b36SApple OSS Distributions // Check for a 'free' condition and that if we are first through
228*27b03b36SApple OSS Distributions if (newCount == 0xffff) {
229*27b03b36SApple OSS Distributions (const_cast<OSObject *>(this))->free();
230*27b03b36SApple OSS Distributions }
231*27b03b36SApple OSS Distributions }
232*27b03b36SApple OSS Distributions
233*27b03b36SApple OSS Distributions void
release() const234*27b03b36SApple OSS Distributions OSObject::release() const
235*27b03b36SApple OSS Distributions {
236*27b03b36SApple OSS Distributions taggedRelease(NULL);
237*27b03b36SApple OSS Distributions }
238*27b03b36SApple OSS Distributions
239*27b03b36SApple OSS Distributions void
retain() const240*27b03b36SApple OSS Distributions OSObject::retain() const
241*27b03b36SApple OSS Distributions {
242*27b03b36SApple OSS Distributions taggedRetain(NULL);
243*27b03b36SApple OSS Distributions }
244*27b03b36SApple OSS Distributions
245*27b03b36SApple OSS Distributions extern "C" void
osobject_retain(void * object)246*27b03b36SApple OSS Distributions osobject_retain(void * object)
247*27b03b36SApple OSS Distributions {
248*27b03b36SApple OSS Distributions ((OSObject *)object)->retain();
249*27b03b36SApple OSS Distributions }
250*27b03b36SApple OSS Distributions
251*27b03b36SApple OSS Distributions extern "C" void
osobject_release(void * object)252*27b03b36SApple OSS Distributions osobject_release(void * object)
253*27b03b36SApple OSS Distributions {
254*27b03b36SApple OSS Distributions ((OSObject *)object)->release();
255*27b03b36SApple OSS Distributions }
256*27b03b36SApple OSS Distributions
257*27b03b36SApple OSS Distributions void
release(int when) const258*27b03b36SApple OSS Distributions OSObject::release(int when) const
259*27b03b36SApple OSS Distributions {
260*27b03b36SApple OSS Distributions taggedRelease(NULL, when);
261*27b03b36SApple OSS Distributions }
262*27b03b36SApple OSS Distributions
263*27b03b36SApple OSS Distributions bool
serialize(OSSerialize * s) const264*27b03b36SApple OSS Distributions OSObject::serialize(OSSerialize *s) const
265*27b03b36SApple OSS Distributions {
266*27b03b36SApple OSS Distributions char cstr[128];
267*27b03b36SApple OSS Distributions bool ok;
268*27b03b36SApple OSS Distributions
269*27b03b36SApple OSS Distributions snprintf(cstr, sizeof(cstr), "%s is not serializable", getClassName(this));
270*27b03b36SApple OSS Distributions
271*27b03b36SApple OSS Distributions OSString * str;
272*27b03b36SApple OSS Distributions str = OSString::withCStringNoCopy(cstr);
273*27b03b36SApple OSS Distributions if (!str) {
274*27b03b36SApple OSS Distributions return false;
275*27b03b36SApple OSS Distributions }
276*27b03b36SApple OSS Distributions
277*27b03b36SApple OSS Distributions ok = str->serialize(s);
278*27b03b36SApple OSS Distributions str->release();
279*27b03b36SApple OSS Distributions
280*27b03b36SApple OSS Distributions return ok;
281*27b03b36SApple OSS Distributions }
282*27b03b36SApple OSS Distributions
283*27b03b36SApple OSS Distributions /*
284*27b03b36SApple OSS Distributions * Given that all OSObjects have been transitioned to use
285*27b03b36SApple OSS Distributions * OSObject_typed_operator_new/OSObject_typed_operator_delete, this should
286*27b03b36SApple OSS Distributions * only be called from kexts that havent recompiled to use the new
287*27b03b36SApple OSS Distributions * definitions.
288*27b03b36SApple OSS Distributions */
289*27b03b36SApple OSS Distributions void *
operator new(size_t size)290*27b03b36SApple OSS Distributions OSObject::operator new(size_t size)
291*27b03b36SApple OSS Distributions {
292*27b03b36SApple OSS Distributions #if IOTRACKING
293*27b03b36SApple OSS Distributions if (kIOTracking & gIOKitDebug) {
294*27b03b36SApple OSS Distributions return OSMetaClass::trackedNew(size);
295*27b03b36SApple OSS Distributions }
296*27b03b36SApple OSS Distributions #endif
297*27b03b36SApple OSS Distributions
298*27b03b36SApple OSS Distributions void *mem = kheap_alloc(KHEAP_DEFAULT, size,
299*27b03b36SApple OSS Distributions Z_VM_TAG_BT(Z_WAITOK_ZERO, VM_KERN_MEMORY_LIBKERN));
300*27b03b36SApple OSS Distributions assert(mem);
301*27b03b36SApple OSS Distributions OSIVAR_ACCUMSIZE(size);
302*27b03b36SApple OSS Distributions
303*27b03b36SApple OSS Distributions return (void *) mem;
304*27b03b36SApple OSS Distributions }
305*27b03b36SApple OSS Distributions
306*27b03b36SApple OSS Distributions void *
OSObject_typed_operator_new(kalloc_type_view_t ktv,vm_size_t size)307*27b03b36SApple OSS Distributions OSObject_typed_operator_new(kalloc_type_view_t ktv, vm_size_t size)
308*27b03b36SApple OSS Distributions {
309*27b03b36SApple OSS Distributions #if IOTRACKING
310*27b03b36SApple OSS Distributions if (kIOTracking & gIOKitDebug) {
311*27b03b36SApple OSS Distributions return OSMetaClass::trackedNew(size);
312*27b03b36SApple OSS Distributions }
313*27b03b36SApple OSS Distributions #endif
314*27b03b36SApple OSS Distributions
315*27b03b36SApple OSS Distributions /*
316*27b03b36SApple OSS Distributions * Some classes in kexts that subclass from iokit classes
317*27b03b36SApple OSS Distributions * don't use OSDeclare/OSDefine to declare/define structors.
318*27b03b36SApple OSS Distributions * When operator new is called on such objects they end up
319*27b03b36SApple OSS Distributions * using the parent's operator new/delete. If we detect such
320*27b03b36SApple OSS Distributions * a case we default to using kalloc rather than kalloc_type
321*27b03b36SApple OSS Distributions */
322*27b03b36SApple OSS Distributions void *mem = NULL;
323*27b03b36SApple OSS Distributions if (size <= kalloc_type_get_size(ktv->kt_size)) {
324*27b03b36SApple OSS Distributions /*
325*27b03b36SApple OSS Distributions * OSObject_typed_operator_new can be called from kexts,
326*27b03b36SApple OSS Distributions * use the external symbol for kalloc_type_impl as
327*27b03b36SApple OSS Distributions * kalloc_type_views generated at some external callsites
328*27b03b36SApple OSS Distributions * many not have been processed during boot.
329*27b03b36SApple OSS Distributions */
330*27b03b36SApple OSS Distributions mem = kalloc_type_impl_external(ktv, Z_WAITOK_ZERO);
331*27b03b36SApple OSS Distributions } else {
332*27b03b36SApple OSS Distributions mem = kheap_alloc(KHEAP_DEFAULT, size,
333*27b03b36SApple OSS Distributions Z_VM_TAG_BT(Z_WAITOK_ZERO, VM_KERN_MEMORY_LIBKERN));
334*27b03b36SApple OSS Distributions }
335*27b03b36SApple OSS Distributions assert(mem);
336*27b03b36SApple OSS Distributions OSIVAR_ACCUMSIZE(size);
337*27b03b36SApple OSS Distributions
338*27b03b36SApple OSS Distributions return (void *) mem;
339*27b03b36SApple OSS Distributions }
340*27b03b36SApple OSS Distributions
341*27b03b36SApple OSS Distributions void
operator delete(void * mem,size_t size)342*27b03b36SApple OSS Distributions OSObject::operator delete(void * mem, size_t size)
343*27b03b36SApple OSS Distributions {
344*27b03b36SApple OSS Distributions if (!mem) {
345*27b03b36SApple OSS Distributions return;
346*27b03b36SApple OSS Distributions }
347*27b03b36SApple OSS Distributions
348*27b03b36SApple OSS Distributions #if IOTRACKING
349*27b03b36SApple OSS Distributions if (kIOTracking & gIOKitDebug) {
350*27b03b36SApple OSS Distributions return OSMetaClass::trackedDelete(mem, size);
351*27b03b36SApple OSS Distributions }
352*27b03b36SApple OSS Distributions #endif
353*27b03b36SApple OSS Distributions
354*27b03b36SApple OSS Distributions kheap_free(KHEAP_DEFAULT, mem, size);
355*27b03b36SApple OSS Distributions OSIVAR_ACCUMSIZE(-size);
356*27b03b36SApple OSS Distributions }
357*27b03b36SApple OSS Distributions
358*27b03b36SApple OSS Distributions void
OSObject_typed_operator_delete(kalloc_type_view_t ktv,void * mem,vm_size_t size)359*27b03b36SApple OSS Distributions OSObject_typed_operator_delete(kalloc_type_view_t ktv, void * mem,
360*27b03b36SApple OSS Distributions vm_size_t size)
361*27b03b36SApple OSS Distributions {
362*27b03b36SApple OSS Distributions if (!mem) {
363*27b03b36SApple OSS Distributions return;
364*27b03b36SApple OSS Distributions }
365*27b03b36SApple OSS Distributions
366*27b03b36SApple OSS Distributions #if IOTRACKING
367*27b03b36SApple OSS Distributions if (kIOTracking & gIOKitDebug) {
368*27b03b36SApple OSS Distributions return OSMetaClass::trackedDelete(mem, size);
369*27b03b36SApple OSS Distributions }
370*27b03b36SApple OSS Distributions #endif
371*27b03b36SApple OSS Distributions
372*27b03b36SApple OSS Distributions if (size <= kalloc_type_get_size(ktv->kt_size)) {
373*27b03b36SApple OSS Distributions kern_os_typed_free(ktv, mem, size);
374*27b03b36SApple OSS Distributions } else {
375*27b03b36SApple OSS Distributions kheap_free(KHEAP_DEFAULT, mem, size);
376*27b03b36SApple OSS Distributions }
377*27b03b36SApple OSS Distributions OSIVAR_ACCUMSIZE(-size);
378*27b03b36SApple OSS Distributions }
379*27b03b36SApple OSS Distributions
380*27b03b36SApple OSS Distributions bool
init()381*27b03b36SApple OSS Distributions OSObject::init()
382*27b03b36SApple OSS Distributions {
383*27b03b36SApple OSS Distributions #if IOTRACKING
384*27b03b36SApple OSS Distributions if (kIOTracking & gIOKitDebug) {
385*27b03b36SApple OSS Distributions getMetaClass()->trackedInstance(this);
386*27b03b36SApple OSS Distributions }
387*27b03b36SApple OSS Distributions #endif
388*27b03b36SApple OSS Distributions return true;
389*27b03b36SApple OSS Distributions }
390*27b03b36SApple OSS Distributions
391*27b03b36SApple OSS Distributions void
free()392*27b03b36SApple OSS Distributions OSObject::free()
393*27b03b36SApple OSS Distributions {
394*27b03b36SApple OSS Distributions const OSMetaClass *meta = getMetaClass();
395*27b03b36SApple OSS Distributions
396*27b03b36SApple OSS Distributions if (meta) {
397*27b03b36SApple OSS Distributions meta->instanceDestructed();
398*27b03b36SApple OSS Distributions #if IOTRACKING
399*27b03b36SApple OSS Distributions if (kIOTracking & gIOKitDebug) {
400*27b03b36SApple OSS Distributions getMetaClass()->trackedFree(this);
401*27b03b36SApple OSS Distributions }
402*27b03b36SApple OSS Distributions #endif
403*27b03b36SApple OSS Distributions }
404*27b03b36SApple OSS Distributions delete this;
405*27b03b36SApple OSS Distributions }
406*27b03b36SApple OSS Distributions
407*27b03b36SApple OSS Distributions #if IOTRACKING
408*27b03b36SApple OSS Distributions void
trackingAccumSize(size_t size)409*27b03b36SApple OSS Distributions OSObject::trackingAccumSize(size_t size)
410*27b03b36SApple OSS Distributions {
411*27b03b36SApple OSS Distributions if (kIOTracking & gIOKitDebug) {
412*27b03b36SApple OSS Distributions getMetaClass()->trackedAccumSize(this, size);
413*27b03b36SApple OSS Distributions }
414*27b03b36SApple OSS Distributions }
415*27b03b36SApple OSS Distributions #endif
416*27b03b36SApple OSS Distributions
417*27b03b36SApple OSS Distributions /* Class member functions - Can't use defaults */
418*27b03b36SApple OSS Distributions /* During constructor vtable is always OSObject's - can't call any subclass */
419*27b03b36SApple OSS Distributions
OSObject()420*27b03b36SApple OSS Distributions OSObject::OSObject()
421*27b03b36SApple OSS Distributions {
422*27b03b36SApple OSS Distributions retainCount = 1;
423*27b03b36SApple OSS Distributions // if (kIOTracking & gIOKitDebug) getMetaClass()->trackedInstance(this);
424*27b03b36SApple OSS Distributions }
425*27b03b36SApple OSS Distributions
OSObject(const OSMetaClass *)426*27b03b36SApple OSS Distributions OSObject::OSObject(const OSMetaClass *)
427*27b03b36SApple OSS Distributions {
428*27b03b36SApple OSS Distributions retainCount = 1;
429*27b03b36SApple OSS Distributions // if (kIOTracking & gIOKitDebug) getMetaClass()->trackedInstance(this);
430*27b03b36SApple OSS Distributions }
431*27b03b36SApple OSS Distributions
432*27b03b36SApple OSS Distributions
433*27b03b36SApple OSS Distributions bool
iterateObjects(void * refcon,bool (* callback)(void * refcon,OSObject * object))434*27b03b36SApple OSS Distributions OSObject::iterateObjects(void * refcon, bool (*callback)(void * refcon, OSObject * object))
435*27b03b36SApple OSS Distributions {
436*27b03b36SApple OSS Distributions OSCollection * col;
437*27b03b36SApple OSS Distributions if ((col = OSDynamicCast(OSCollection, this))) {
438*27b03b36SApple OSS Distributions return col->iterateObjects(refcon, callback);
439*27b03b36SApple OSS Distributions }
440*27b03b36SApple OSS Distributions return callback(refcon, this);
441*27b03b36SApple OSS Distributions }
442*27b03b36SApple OSS Distributions
443*27b03b36SApple OSS Distributions bool
444*27b03b36SApple OSS Distributions OSObject::iterateObjects(bool (^block)(OSObject * object))
445*27b03b36SApple OSS Distributions {
446*27b03b36SApple OSS Distributions OSCollection * col;
447*27b03b36SApple OSS Distributions if ((col = OSDynamicCast(OSCollection, this))) {
448*27b03b36SApple OSS Distributions return col->iterateObjects(block);
449*27b03b36SApple OSS Distributions }
450*27b03b36SApple OSS Distributions return block(this);
451*27b03b36SApple OSS Distributions }
452