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