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