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