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