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