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