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