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