xref: /xnu-11215/libkern/c++/OSObject.cpp (revision 5c2921b0)
1c1dac77fSApple OSS Distributions /*
23ca3bd55SApple OSS Distributions  * Copyright (c) 2000 Apple Inc. All rights reserved.
3c1dac77fSApple OSS Distributions  *
4e13b1fa5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5c1dac77fSApple OSS Distributions  *
6e13b1fa5SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7e13b1fa5SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8e13b1fa5SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9e13b1fa5SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10e13b1fa5SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11e13b1fa5SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12e13b1fa5SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13e13b1fa5SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14c1dac77fSApple OSS Distributions  *
15e13b1fa5SApple OSS Distributions  * Please obtain a copy of the License at
16e13b1fa5SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17e13b1fa5SApple OSS Distributions  *
18e13b1fa5SApple OSS Distributions  * The Original Code and all software distributed under the License are
19e13b1fa5SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20c1dac77fSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21c1dac77fSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22e13b1fa5SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23e13b1fa5SApple OSS Distributions  * Please see the License for the specific language governing rights and
24e13b1fa5SApple OSS Distributions  * limitations under the License.
25c1dac77fSApple OSS Distributions  *
26e13b1fa5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27c1dac77fSApple OSS Distributions  */
28c1dac77fSApple OSS Distributions /* OSObject.cpp created by gvdl on Fri 1998-11-17 */
29c1dac77fSApple OSS Distributions 
30c1dac77fSApple OSS Distributions #include <libkern/c++/OSObject.h>
31a3bb9fccSApple OSS Distributions #include <libkern/c++/OSString.h>
32e13b1fa5SApple OSS Distributions #include <libkern/c++/OSArray.h>
33c1dac77fSApple OSS Distributions #include <libkern/c++/OSSerialize.h>
34c1dac77fSApple OSS Distributions #include <libkern/c++/OSLib.h>
35e13b1fa5SApple OSS Distributions #include <libkern/OSDebug.h>
36c1dac77fSApple OSS Distributions #include <libkern/c++/OSCPPDebug.h>
37e13b1fa5SApple OSS Distributions #include <IOKit/IOKitDebug.h>
38c1dac77fSApple OSS Distributions #include <libkern/OSAtomic.h>
39c1dac77fSApple OSS Distributions 
40fad439e7SApple OSS Distributions #include <libkern/c++/OSCollection.h>
41fad439e7SApple OSS Distributions 
42e13b1fa5SApple OSS Distributions #include <kern/queue.h>
43e13b1fa5SApple OSS Distributions 
44c1dac77fSApple OSS Distributions __BEGIN_DECLS
45bb611c8fSApple OSS Distributions size_t debug_ivars_size;
46c1dac77fSApple OSS Distributions __END_DECLS
47c1dac77fSApple OSS Distributions 
48c1dac77fSApple OSS Distributions 
49c1dac77fSApple OSS Distributions // OSDefineMetaClassAndAbstractStructors(OSObject, 0);
50c1dac77fSApple OSS Distributions /* Class global data */
51c1dac77fSApple OSS Distributions OSObject::MetaClass OSObject::gMetaClass;
52c1dac77fSApple OSS Distributions const OSMetaClass * const OSObject::metaClass = &OSObject::gMetaClass;
53a5e72196SApple OSS Distributions const OSMetaClass * const OSObject::superClass = NULL;
54c1dac77fSApple OSS Distributions 
55c1dac77fSApple OSS Distributions /* Class member functions - Can't use defaults */
~OSObject()56a5e72196SApple OSS Distributions OSObject::~OSObject()
57a5e72196SApple OSS Distributions {
58a5e72196SApple OSS Distributions }
59a5e72196SApple OSS Distributions const OSMetaClass *
getMetaClass() const60a5e72196SApple OSS Distributions OSObject::getMetaClass() const
61a5e72196SApple OSS Distributions {
62a5e72196SApple OSS Distributions 	return &gMetaClass;
63a5e72196SApple OSS Distributions }
64a5e72196SApple OSS Distributions OSObject *
alloc() const65a5e72196SApple OSS Distributions OSObject::MetaClass::alloc() const
66a5e72196SApple OSS Distributions {
67a5e72196SApple OSS Distributions 	return NULL;
68a5e72196SApple OSS Distributions }
69c1dac77fSApple OSS Distributions 
70c1dac77fSApple OSS Distributions /* The OSObject::MetaClass constructor */
MetaClass()71c1dac77fSApple OSS Distributions OSObject::MetaClass::MetaClass()
72c1dac77fSApple OSS Distributions 	: OSMetaClass("OSObject", OSObject::superClass, sizeof(OSObject))
73a5e72196SApple OSS Distributions {
74a5e72196SApple OSS Distributions }
75c1dac77fSApple OSS Distributions 
76c1dac77fSApple OSS Distributions // Virtual Padding
77c1dac77fSApple OSS Distributions OSMetaClassDefineReservedUnused(OSObject, 0);
78c1dac77fSApple OSS Distributions OSMetaClassDefineReservedUnused(OSObject, 1);
79c1dac77fSApple OSS Distributions OSMetaClassDefineReservedUnused(OSObject, 2);
80c1dac77fSApple OSS Distributions OSMetaClassDefineReservedUnused(OSObject, 3);
81c1dac77fSApple OSS Distributions OSMetaClassDefineReservedUnused(OSObject, 4);
82c1dac77fSApple OSS Distributions OSMetaClassDefineReservedUnused(OSObject, 5);
83c1dac77fSApple OSS Distributions OSMetaClassDefineReservedUnused(OSObject, 6);
84c1dac77fSApple OSS Distributions OSMetaClassDefineReservedUnused(OSObject, 7);
85c1dac77fSApple OSS Distributions OSMetaClassDefineReservedUnused(OSObject, 8);
86c1dac77fSApple OSS Distributions OSMetaClassDefineReservedUnused(OSObject, 9);
87c1dac77fSApple OSS Distributions OSMetaClassDefineReservedUnused(OSObject, 10);
88c1dac77fSApple OSS Distributions OSMetaClassDefineReservedUnused(OSObject, 11);
89c1dac77fSApple OSS Distributions OSMetaClassDefineReservedUnused(OSObject, 12);
90c1dac77fSApple OSS Distributions OSMetaClassDefineReservedUnused(OSObject, 13);
91c1dac77fSApple OSS Distributions OSMetaClassDefineReservedUnused(OSObject, 14);
92c1dac77fSApple OSS Distributions OSMetaClassDefineReservedUnused(OSObject, 15);
93e13b1fa5SApple OSS Distributions 
94a5e72196SApple OSS Distributions static const char *
getClassName(const OSObject * obj)95a5e72196SApple OSS Distributions getClassName(const OSObject *obj)
96fad439e7SApple OSS Distributions {
97fad439e7SApple OSS Distributions 	const OSMetaClass *meta = obj->getMetaClass();
98fad439e7SApple OSS Distributions 	return (meta) ? meta->getClassName() : "unknown class?";
99fad439e7SApple OSS Distributions }
100c1dac77fSApple OSS Distributions 
101a5e72196SApple OSS Distributions int
getRetainCount() const102a5e72196SApple OSS Distributions OSObject::getRetainCount() const
103c1dac77fSApple OSS Distributions {
104fad439e7SApple OSS Distributions 	return (int) ((UInt16) retainCount);
105c1dac77fSApple OSS Distributions }
106c1dac77fSApple OSS Distributions 
107a5e72196SApple OSS Distributions bool
taggedTryRetain(const void * tag) const108a5e72196SApple OSS Distributions OSObject::taggedTryRetain(const void *tag) const
109c1dac77fSApple OSS Distributions {
110368ad365SApple OSS Distributions 	volatile UInt32 *countP = (volatile UInt32 *) &retainCount;
111368ad365SApple OSS Distributions 	UInt32 inc = 1;
112368ad365SApple OSS Distributions 	UInt32 origCount;
113368ad365SApple OSS Distributions 	UInt32 newCount;
114368ad365SApple OSS Distributions 
115368ad365SApple OSS Distributions 	// Increment the collection bucket.
116a5e72196SApple OSS Distributions 	if ((const void *) OSTypeID(OSCollection) == tag) {
117368ad365SApple OSS Distributions 		inc |= (1UL << 16);
118a5e72196SApple OSS Distributions 	}
119368ad365SApple OSS Distributions 
120368ad365SApple OSS Distributions 	do {
121368ad365SApple OSS Distributions 		origCount = *countP;
122368ad365SApple OSS Distributions 		if (((UInt16) origCount | 0x1) == 0xffff) {
123368ad365SApple OSS Distributions 			if (origCount & 0x1) {
124368ad365SApple OSS Distributions 				// If count == 0xffff that means we are freeing now so we can
125368ad365SApple OSS Distributions 				// just return obviously somebody is cleaning up dangling
126368ad365SApple OSS Distributions 				// references.
127a5e72196SApple OSS Distributions 				return false;
128a5e72196SApple OSS Distributions 			} else {
129368ad365SApple OSS Distributions 				// If count == 0xfffe then we have wrapped our reference count.
130368ad365SApple OSS Distributions 				// We should stop counting now as this reference must be
131368ad365SApple OSS Distributions 				// leaked rather than accidently wrapping around the clock and
132368ad365SApple OSS Distributions 				// freeing a very active object later.
133368ad365SApple OSS Distributions 
134fad439e7SApple OSS Distributions #if !DEBUG
135368ad365SApple OSS Distributions 				break; // Break out of update loop which pegs the reference
1363ca3bd55SApple OSS Distributions #else /* DEBUG */
137368ad365SApple OSS Distributions 				// @@@ gvdl: eventually need to make this panic optional
138368ad365SApple OSS Distributions 				// based on a boot argument i.e. debug= boot flag
139a5e72196SApple OSS Distributions 				panic("OSObject::refcount: "
140a5e72196SApple OSS Distributions 				    "About to wrap the reference count, reference leak?");
141368ad365SApple OSS Distributions #endif /* !DEBUG */
142368ad365SApple OSS Distributions 			}
143368ad365SApple OSS Distributions 		}
144fad439e7SApple OSS Distributions 
145fad439e7SApple OSS Distributions 		newCount = origCount + inc;
1463ca3bd55SApple OSS Distributions 	} while (!OSCompareAndSwap(origCount, newCount, const_cast<UInt32 *>(countP)));
147a5e72196SApple OSS Distributions 
148a5e72196SApple OSS Distributions 	return true;
149c1dac77fSApple OSS Distributions }
150c1dac77fSApple OSS Distributions 
151a5e72196SApple OSS Distributions void
taggedRetain(const void * tag) const152a5e72196SApple OSS Distributions OSObject::taggedRetain(const void *tag) const
153a5e72196SApple OSS Distributions {
154a5e72196SApple OSS Distributions 	if (!taggedTryRetain(tag)) {
155a5e72196SApple OSS Distributions 		panic("OSObject::refcount: Attempting to retain a freed object");
156a5e72196SApple OSS Distributions 	}
157a5e72196SApple OSS Distributions }
158a5e72196SApple OSS Distributions 
159a5e72196SApple OSS Distributions void
taggedRelease(const void * tag) const160a5e72196SApple OSS Distributions OSObject::taggedRelease(const void *tag) const
161c1dac77fSApple OSS Distributions {
162fad439e7SApple OSS Distributions 	taggedRelease(tag, 1);
163fad439e7SApple OSS Distributions }
164fad439e7SApple OSS Distributions 
165a5e72196SApple OSS Distributions void
taggedRelease(const void * tag,const int when) const166a5e72196SApple OSS Distributions OSObject::taggedRelease(const void *tag, const int when) const
167fad439e7SApple OSS Distributions {
168fad439e7SApple OSS Distributions 	volatile UInt32 *countP = (volatile UInt32 *) &retainCount;
169fad439e7SApple OSS Distributions 	UInt32 dec = 1;
170fad439e7SApple OSS Distributions 	UInt32 origCount;
171fad439e7SApple OSS Distributions 	UInt32 newCount;
172fad439e7SApple OSS Distributions 	UInt32 actualCount;
173fad439e7SApple OSS Distributions 
174368ad365SApple OSS Distributions 	// Increment the collection bucket.
175a5e72196SApple OSS Distributions 	if ((const void *) OSTypeID(OSCollection) == tag) {
176fad439e7SApple OSS Distributions 		dec |= (1UL << 16);
177a5e72196SApple OSS Distributions 	}
178fad439e7SApple OSS Distributions 
179fad439e7SApple OSS Distributions 	do {
180fad439e7SApple OSS Distributions 		origCount = *countP;
181fad439e7SApple OSS Distributions 
182368ad365SApple OSS Distributions 		if (((UInt16) origCount | 0x1) == 0xffff) {
183368ad365SApple OSS Distributions 			if (origCount & 0x1) {
184368ad365SApple OSS Distributions 				// If count == 0xffff that means we are freeing now so we can
185368ad365SApple OSS Distributions 				// just return obviously somebody is cleaning up some dangling
186368ad365SApple OSS Distributions 				// references.  So we blow out immediately.
187368ad365SApple OSS Distributions 				return;
188a5e72196SApple OSS Distributions 			} else {
189368ad365SApple OSS Distributions 				// If count == 0xfffe then we have wrapped our reference
190368ad365SApple OSS Distributions 				// count.  We should stop counting now as this reference must be
191368ad365SApple OSS Distributions 				// leaked rather than accidently freeing an active object later.
192368ad365SApple OSS Distributions 
193368ad365SApple OSS Distributions #if !DEBUG
194368ad365SApple OSS Distributions 				return; // return out of function which pegs the reference
1953ca3bd55SApple OSS Distributions #else /* DEBUG */
196368ad365SApple OSS Distributions 				// @@@ gvdl: eventually need to make this panic optional
197368ad365SApple OSS Distributions 				// based on a boot argument i.e. debug= boot flag
198368ad365SApple OSS Distributions 				panic("OSObject::refcount: %s",
199368ad365SApple OSS Distributions 				    "About to unreference a pegged object, reference leak?");
200368ad365SApple OSS Distributions #endif /* !DEBUG */
201368ad365SApple OSS Distributions 			}
202368ad365SApple OSS Distributions 		}
203fad439e7SApple OSS Distributions 		actualCount = origCount - dec;
204a5e72196SApple OSS Distributions 		if ((UInt16) actualCount < when) {
205368ad365SApple OSS Distributions 			newCount = 0xffff;
206a5e72196SApple OSS Distributions 		} else {
207fad439e7SApple OSS Distributions 			newCount = actualCount;
208a5e72196SApple OSS Distributions 		}
2093ca3bd55SApple OSS Distributions 	} while (!OSCompareAndSwap(origCount, newCount, const_cast<UInt32 *>(countP)));
210fad439e7SApple OSS Distributions 
211fad439e7SApple OSS Distributions 	//
212fad439e7SApple OSS Distributions 	// This panic means that we have just attempted to release an object
2133ca3bd55SApple OSS Distributions 	// whose retain count has gone to less than the number of collections
214fad439e7SApple OSS Distributions 	// it is a member off.  Take a panic immediately.
2153ca3bd55SApple OSS Distributions 	// In fact the panic MAY not be a registry corruption but it is
216fad439e7SApple OSS Distributions 	// ALWAYS the wrong thing to do.  I call it a registry corruption 'cause
217fad439e7SApple OSS Distributions 	// the registry is the biggest single use of a network of collections.
218fad439e7SApple OSS Distributions 	//
2193ca3bd55SApple OSS Distributions // xxx - this error message is overly-specific;
2203ca3bd55SApple OSS Distributions // xxx - any code in the kernel could trip this,
2213ca3bd55SApple OSS Distributions // xxx - and it applies as noted to all collections, not just the registry
2223ca3bd55SApple OSS Distributions 	if ((UInt16) actualCount < (actualCount >> 16)) {
2233ca3bd55SApple OSS Distributions 		panic("A kext releasing a(n) %s has corrupted the registry.",
224fad439e7SApple OSS Distributions 		    getClassName(this));
2253ca3bd55SApple OSS Distributions 	}
226fad439e7SApple OSS Distributions 
227fad439e7SApple OSS Distributions 	// Check for a 'free' condition and that if we are first through
2283ca3bd55SApple OSS Distributions 	if (newCount == 0xffff) {
2293ca3bd55SApple OSS Distributions 		(const_cast<OSObject *>(this))->free();
2303ca3bd55SApple OSS Distributions 	}
231c1dac77fSApple OSS Distributions }
232c1dac77fSApple OSS Distributions 
233a5e72196SApple OSS Distributions void
release() const234a5e72196SApple OSS Distributions OSObject::release() const
235c1dac77fSApple OSS Distributions {
236a5e72196SApple OSS Distributions 	taggedRelease(NULL);
237fad439e7SApple OSS Distributions }
238fad439e7SApple OSS Distributions 
239a5e72196SApple OSS Distributions void
retain() const240a5e72196SApple OSS Distributions OSObject::retain() const
241fad439e7SApple OSS Distributions {
242a5e72196SApple OSS Distributions 	taggedRetain(NULL);
243fad439e7SApple OSS Distributions }
244fad439e7SApple OSS Distributions 
24588cc0b97SApple OSS Distributions extern "C" void
osobject_retain(void * object)24688cc0b97SApple OSS Distributions osobject_retain(void * object)
24788cc0b97SApple OSS Distributions {
24888cc0b97SApple OSS Distributions 	((OSObject *)object)->retain();
24988cc0b97SApple OSS Distributions }
25088cc0b97SApple OSS Distributions 
25188cc0b97SApple OSS Distributions extern "C" void
osobject_release(void * object)25288cc0b97SApple OSS Distributions osobject_release(void * object)
25388cc0b97SApple OSS Distributions {
25488cc0b97SApple OSS Distributions 	((OSObject *)object)->release();
25588cc0b97SApple OSS Distributions }
25688cc0b97SApple OSS Distributions 
257a5e72196SApple OSS Distributions void
release(int when) const258a5e72196SApple OSS Distributions OSObject::release(int when) const
259fad439e7SApple OSS Distributions {
260a5e72196SApple OSS Distributions 	taggedRelease(NULL, when);
261c1dac77fSApple OSS Distributions }
262c1dac77fSApple OSS Distributions 
263a5e72196SApple OSS Distributions bool
serialize(OSSerialize * s) const264a5e72196SApple OSS Distributions OSObject::serialize(OSSerialize *s) const
265c1dac77fSApple OSS Distributions {
266a3bb9fccSApple OSS Distributions 	char cstr[128];
267a3bb9fccSApple OSS Distributions 	bool ok;
268c1dac77fSApple OSS Distributions 
269a3bb9fccSApple OSS Distributions 	snprintf(cstr, sizeof(cstr), "%s is not serializable", getClassName(this));
270c1dac77fSApple OSS Distributions 
271a3bb9fccSApple OSS Distributions 	OSString * str;
272a3bb9fccSApple OSS Distributions 	str = OSString::withCStringNoCopy(cstr);
273a5e72196SApple OSS Distributions 	if (!str) {
274a5e72196SApple OSS Distributions 		return false;
275a5e72196SApple OSS Distributions 	}
276c1dac77fSApple OSS Distributions 
277a3bb9fccSApple OSS Distributions 	ok = str->serialize(s);
278a3bb9fccSApple OSS Distributions 	str->release();
279a3bb9fccSApple OSS Distributions 
280a5e72196SApple OSS Distributions 	return ok;
281c1dac77fSApple OSS Distributions }
282c1dac77fSApple OSS Distributions 
283*5c2921b0SApple OSS Distributions /*
284*5c2921b0SApple OSS Distributions  * Ignore -Wxnu-typed-allocators for the operator new/delete implementations
285*5c2921b0SApple OSS Distributions  */
286*5c2921b0SApple OSS Distributions __typed_allocators_ignore_push
287*5c2921b0SApple OSS Distributions 
288*5c2921b0SApple OSS Distributions /*
289*5c2921b0SApple OSS Distributions  * Given that all OSObjects have been transitioned to use
290*5c2921b0SApple OSS Distributions  * OSObject_typed_operator_new/OSObject_typed_operator_delete, this should
291*5c2921b0SApple OSS Distributions  * only be called from kexts that havent recompiled to use the new
292*5c2921b0SApple OSS Distributions  * definitions.
293*5c2921b0SApple OSS Distributions  */
294a5e72196SApple OSS Distributions void *
operator new(size_t size)295a5e72196SApple OSS Distributions OSObject::operator new(size_t size)
296c1dac77fSApple OSS Distributions {
2970f3703acSApple OSS Distributions #if IOTRACKING
298a5e72196SApple OSS Distributions 	if (kIOTracking & gIOKitDebug) {
299a5e72196SApple OSS Distributions 		return OSMetaClass::trackedNew(size);
300a5e72196SApple OSS Distributions 	}
3010f3703acSApple OSS Distributions #endif
302e13b1fa5SApple OSS Distributions 
303e7776783SApple OSS Distributions 	void *mem = kheap_alloc(KHEAP_DEFAULT, size,
304e7776783SApple OSS Distributions 	    Z_VM_TAG_BT(Z_WAITOK_ZERO, VM_KERN_MEMORY_LIBKERN));
305c1dac77fSApple OSS Distributions 	assert(mem);
3060f3703acSApple OSS Distributions 	OSIVAR_ACCUMSIZE(size);
307c1dac77fSApple OSS Distributions 
308e13b1fa5SApple OSS Distributions 	return (void *) mem;
309c1dac77fSApple OSS Distributions }
310c1dac77fSApple OSS Distributions 
311e6231be0SApple OSS Distributions void *
OSObject_typed_operator_new(kalloc_type_view_t ktv,vm_size_t size)312e6231be0SApple OSS Distributions OSObject_typed_operator_new(kalloc_type_view_t ktv, vm_size_t size)
313e6231be0SApple OSS Distributions {
314e6231be0SApple OSS Distributions #if IOTRACKING
315e6231be0SApple OSS Distributions 	if (kIOTracking & gIOKitDebug) {
316e6231be0SApple OSS Distributions 		return OSMetaClass::trackedNew(size);
317e6231be0SApple OSS Distributions 	}
318e6231be0SApple OSS Distributions #endif
319e6231be0SApple OSS Distributions 
320e6231be0SApple OSS Distributions 	/*
321e6231be0SApple OSS Distributions 	 * Some classes in kexts that subclass from iokit classes
322e6231be0SApple OSS Distributions 	 * don't use OSDeclare/OSDefine to declare/define structors.
323e6231be0SApple OSS Distributions 	 * When operator new is called on such objects they end up
324e6231be0SApple OSS Distributions 	 * using the parent's operator new/delete. If we detect such
325e6231be0SApple OSS Distributions 	 * a case we default to using kalloc rather than kalloc_type
326e6231be0SApple OSS Distributions 	 */
327e6231be0SApple OSS Distributions 	void *mem = NULL;
328e6231be0SApple OSS Distributions 	if (size <= kalloc_type_get_size(ktv->kt_size)) {
329e6231be0SApple OSS Distributions 		/*
330e6231be0SApple OSS Distributions 		 * OSObject_typed_operator_new can be called from kexts,
331e6231be0SApple OSS Distributions 		 * use the external symbol for kalloc_type_impl as
332e6231be0SApple OSS Distributions 		 * kalloc_type_views generated at some external callsites
333e6231be0SApple OSS Distributions 		 * many not have been processed during boot.
334e6231be0SApple OSS Distributions 		 */
335e7776783SApple OSS Distributions 		mem = kalloc_type_impl_external(ktv, Z_WAITOK_ZERO);
336e6231be0SApple OSS Distributions 	} else {
337e7776783SApple OSS Distributions 		mem = kheap_alloc(KHEAP_DEFAULT, size,
338e7776783SApple OSS Distributions 		    Z_VM_TAG_BT(Z_WAITOK_ZERO, VM_KERN_MEMORY_LIBKERN));
339e6231be0SApple OSS Distributions 	}
340e6231be0SApple OSS Distributions 	assert(mem);
341e6231be0SApple OSS Distributions 	OSIVAR_ACCUMSIZE(size);
342e6231be0SApple OSS Distributions 
343e6231be0SApple OSS Distributions 	return (void *) mem;
344e6231be0SApple OSS Distributions }
345e6231be0SApple OSS Distributions 
346a5e72196SApple OSS Distributions void
operator delete(void * mem,size_t size)347a5e72196SApple OSS Distributions OSObject::operator delete(void * mem, size_t size)
348c1dac77fSApple OSS Distributions {
349a5e72196SApple OSS Distributions 	if (!mem) {
350a5e72196SApple OSS Distributions 		return;
351a5e72196SApple OSS Distributions 	}
352e13b1fa5SApple OSS Distributions 
3530f3703acSApple OSS Distributions #if IOTRACKING
354a5e72196SApple OSS Distributions 	if (kIOTracking & gIOKitDebug) {
355a5e72196SApple OSS Distributions 		return OSMetaClass::trackedDelete(mem, size);
356a5e72196SApple OSS Distributions 	}
3570f3703acSApple OSS Distributions #endif
358e13b1fa5SApple OSS Distributions 
359*5c2921b0SApple OSS Distributions 	kheap_free(KHEAP_DEFAULT, mem, size);
3600f3703acSApple OSS Distributions 	OSIVAR_ACCUMSIZE(-size);
361e13b1fa5SApple OSS Distributions }
362e13b1fa5SApple OSS Distributions 
363e6231be0SApple OSS Distributions void
OSObject_typed_operator_delete(kalloc_type_view_t ktv,void * mem,vm_size_t size)364e6231be0SApple OSS Distributions OSObject_typed_operator_delete(kalloc_type_view_t ktv, void * mem,
365e6231be0SApple OSS Distributions     vm_size_t size)
366e6231be0SApple OSS Distributions {
367e6231be0SApple OSS Distributions 	if (!mem) {
368e6231be0SApple OSS Distributions 		return;
369e6231be0SApple OSS Distributions 	}
370e6231be0SApple OSS Distributions 
371e6231be0SApple OSS Distributions #if IOTRACKING
372e6231be0SApple OSS Distributions 	if (kIOTracking & gIOKitDebug) {
373e6231be0SApple OSS Distributions 		return OSMetaClass::trackedDelete(mem, size);
374e6231be0SApple OSS Distributions 	}
375e6231be0SApple OSS Distributions #endif
376e6231be0SApple OSS Distributions 
377e6231be0SApple OSS Distributions 	if (size <= kalloc_type_get_size(ktv->kt_size)) {
378e6231be0SApple OSS Distributions 		kern_os_typed_free(ktv, mem, size);
379e6231be0SApple OSS Distributions 	} else {
380*5c2921b0SApple OSS Distributions 		kheap_free(KHEAP_DEFAULT, mem, size);
381e6231be0SApple OSS Distributions 	}
382e6231be0SApple OSS Distributions 	OSIVAR_ACCUMSIZE(-size);
383e6231be0SApple OSS Distributions }
384e6231be0SApple OSS Distributions 
385*5c2921b0SApple OSS Distributions __typed_allocators_ignore_pop
386e6231be0SApple OSS Distributions 
387a5e72196SApple OSS Distributions bool
init()388a5e72196SApple OSS Distributions OSObject::init()
3890f3703acSApple OSS Distributions {
3900f3703acSApple OSS Distributions #if IOTRACKING
391a5e72196SApple OSS Distributions 	if (kIOTracking & gIOKitDebug) {
392a5e72196SApple OSS Distributions 		getMetaClass()->trackedInstance(this);
393a5e72196SApple OSS Distributions 	}
3940f3703acSApple OSS Distributions #endif
3950f3703acSApple OSS Distributions 	return true;
3960f3703acSApple OSS Distributions }
397c1dac77fSApple OSS Distributions 
398a5e72196SApple OSS Distributions void
free()399a5e72196SApple OSS Distributions OSObject::free()
4000f3703acSApple OSS Distributions {
4010f3703acSApple OSS Distributions 	const OSMetaClass *meta = getMetaClass();
4020f3703acSApple OSS Distributions 
403a5e72196SApple OSS Distributions 	if (meta) {
4040f3703acSApple OSS Distributions 		meta->instanceDestructed();
4050f3703acSApple OSS Distributions #if IOTRACKING
406a5e72196SApple OSS Distributions 		if (kIOTracking & gIOKitDebug) {
407a5e72196SApple OSS Distributions 			getMetaClass()->trackedFree(this);
408a5e72196SApple OSS Distributions 		}
4090f3703acSApple OSS Distributions #endif
4100f3703acSApple OSS Distributions 	}
4110f3703acSApple OSS Distributions 	delete this;
4120f3703acSApple OSS Distributions }
4130f3703acSApple OSS Distributions 
4140f3703acSApple OSS Distributions #if IOTRACKING
415a5e72196SApple OSS Distributions void
trackingAccumSize(size_t size)416a5e72196SApple OSS Distributions OSObject::trackingAccumSize(size_t size)
4170f3703acSApple OSS Distributions {
418a5e72196SApple OSS Distributions 	if (kIOTracking & gIOKitDebug) {
419a5e72196SApple OSS Distributions 		getMetaClass()->trackedAccumSize(this, size);
420a5e72196SApple OSS Distributions 	}
4210f3703acSApple OSS Distributions }
4220f3703acSApple OSS Distributions #endif
4230f3703acSApple OSS Distributions 
4240f3703acSApple OSS Distributions /* Class member functions - Can't use defaults */
4250f3703acSApple OSS Distributions /* During constructor vtable is always OSObject's - can't call any subclass */
4260f3703acSApple OSS Distributions 
OSObject()4270f3703acSApple OSS Distributions OSObject::OSObject()
4280f3703acSApple OSS Distributions {
4290f3703acSApple OSS Distributions 	retainCount = 1;
4300f3703acSApple OSS Distributions //    if (kIOTracking & gIOKitDebug) getMetaClass()->trackedInstance(this);
4310f3703acSApple OSS Distributions }
4320f3703acSApple OSS Distributions 
OSObject(const OSMetaClass *)4330f3703acSApple OSS Distributions OSObject::OSObject(const OSMetaClass *)
4340f3703acSApple OSS Distributions {
4350f3703acSApple OSS Distributions 	retainCount = 1;
4360f3703acSApple OSS Distributions //    if (kIOTracking & gIOKitDebug) getMetaClass()->trackedInstance(this);
437c1dac77fSApple OSS Distributions }
438a5e72196SApple OSS Distributions 
439a5e72196SApple OSS Distributions 
440a5e72196SApple OSS Distributions bool
iterateObjects(void * refcon,bool (* callback)(void * refcon,OSObject * object))441a5e72196SApple OSS Distributions OSObject::iterateObjects(void * refcon, bool (*callback)(void * refcon, OSObject * object))
442a5e72196SApple OSS Distributions {
443a5e72196SApple OSS Distributions 	OSCollection * col;
444a5e72196SApple OSS Distributions 	if ((col = OSDynamicCast(OSCollection, this))) {
445a5e72196SApple OSS Distributions 		return col->iterateObjects(refcon, callback);
446a5e72196SApple OSS Distributions 	}
447a5e72196SApple OSS Distributions 	return callback(refcon, this);
448a5e72196SApple OSS Distributions }
449a5e72196SApple OSS Distributions 
450a5e72196SApple OSS Distributions bool
451a5e72196SApple OSS Distributions OSObject::iterateObjects(bool (^block)(OSObject * object))
452a5e72196SApple OSS Distributions {
453a5e72196SApple OSS Distributions 	OSCollection * col;
454a5e72196SApple OSS Distributions 	if ((col = OSDynamicCast(OSCollection, this))) {
455a5e72196SApple OSS Distributions 		return col->iterateObjects(block);
456a5e72196SApple OSS Distributions 	}
457a5e72196SApple OSS Distributions 	return block(this);
458a5e72196SApple OSS Distributions }
459