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