2 * Copyright (c) 2000-2016 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* OSMetaClass.cpp created by gvdl on Fri 1998-11-17 */
32 #include <libkern/OSReturn.h>
34 #include <libkern/c++/OSMetaClass.h>
35 #include <libkern/c++/OSObject.h>
36 #include <libkern/c++/OSKext.h>
38 #include <libkern/c++/OSCollectionIterator.h>
39 #include <libkern/c++/OSDictionary.h>
40 #include <libkern/c++/OSArray.h>
41 #include <libkern/c++/OSSet.h>
42 #include <libkern/c++/OSSymbol.h>
43 #include <libkern/c++/OSNumber.h>
44 #include <libkern/c++/OSSerialize.h>
46 #include <libkern/c++/OSLib.h>
47 #include <libkern/OSAtomic.h>
49 #include <IOKit/IOLib.h>
51 #include <IOKit/IOKitDebug.h>
56 #include <sys/systm.h>
57 #include <mach/mach_types.h>
58 #include <kern/locks.h>
59 #include <kern/clock.h>
60 #include <kern/thread_call.h>
61 #include <kern/host.h>
62 #include <mach/mach_interface.h>
66 #endif /* PRAGMA_MARK */
67 /*********************************************************************
69 *********************************************************************/
73 #pragma mark Internal constants & data structs
74 #endif /* PRAGMA_MARK */
75 /*********************************************************************
76 * Internal constants & data structs
77 *********************************************************************/
78 OSKextLogSpec kOSMetaClassLogSpec
=
79 kOSKextLogErrorLevel
|
81 kOSKextLogKextBookkeepingFlag
;
84 kCompletedBootstrap
= 0,
86 kMakingDictionaries
= 2
87 } sBootstrapState
= kNoDictionaries
;
89 static const int kClassCapacityIncrement
= 40;
90 static const int kKModCapacityIncrement
= 10;
91 static OSDictionary
* sAllClassesDict
;
92 static unsigned int sDeepestClass
;
93 IOLock
* sAllClassesLock
= NULL
;
94 IOLock
* sInstancesLock
= NULL
;
97 * While loading a kext and running all its constructors to register
98 * all OSMetaClass classes, the classes are queued up here. Only one
99 * kext can be in flight at a time, guarded by sStalledClassesLock
101 static struct StalledData
{
102 const char * kextIdentifier
;
104 unsigned int capacity
;
106 OSMetaClass
** classes
;
108 IOLock
* sStalledClassesLock
= NULL
;
110 struct ExpansionData
{
111 OSOrderedSet
* instances
;
115 IOTrackingQueue
* tracking
;
121 #pragma mark OSMetaClassBase
122 #endif /* PRAGMA_MARK */
123 /*********************************************************************
125 *********************************************************************/
127 #if APPLE_KEXT_VTABLE_PADDING
128 /*********************************************************************
129 * Reserved vtable functions.
130 *********************************************************************/
132 void OSMetaClassBase::_RESERVEDOSMetaClassBase0()
133 { panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 0); }
134 void OSMetaClassBase::_RESERVEDOSMetaClassBase1()
135 { panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 1); }
136 void OSMetaClassBase::_RESERVEDOSMetaClassBase2()
137 { panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 2); }
138 #endif /* SLOT_USED */
140 // As these slots are used move them up inside the #if above
141 void OSMetaClassBase::_RESERVEDOSMetaClassBase3()
142 { panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 3); }
143 void OSMetaClassBase::_RESERVEDOSMetaClassBase4()
144 { panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 4); }
145 void OSMetaClassBase::_RESERVEDOSMetaClassBase5()
146 { panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 5); }
147 void OSMetaClassBase::_RESERVEDOSMetaClassBase6()
148 { panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 6); }
151 /*********************************************************************
152 * These used to be inline in the header but gcc didn't believe us
153 * Now we MUST pull the inline out at least until the compiler is
156 * Helper inlines for runtime type preprocessor macros
157 *********************************************************************/
159 /*********************************************************************
160 *********************************************************************/
162 OSMetaClassBase::safeMetaCast(
163 const OSMetaClassBase
* me
,
164 const OSMetaClass
* toType
)
166 return (me
)? me
->metaCast(toType
) : 0;
169 /*********************************************************************
170 *********************************************************************/
172 OSMetaClassBase::checkTypeInst(
173 const OSMetaClassBase
* inst
,
174 const OSMetaClassBase
* typeinst
)
176 const OSMetaClass
* toType
= OSTypeIDInst(typeinst
);
177 return typeinst
&& inst
&& (0 != inst
->metaCast(toType
));
180 /*********************************************************************
181 *********************************************************************/
182 void OSMetaClassBase::
185 sAllClassesLock
= IOLockAlloc();
186 sStalledClassesLock
= IOLockAlloc();
187 sInstancesLock
= IOLockAlloc();
190 #if APPLE_KEXT_VTABLE_PADDING
191 /*********************************************************************
192 * If you need this slot you had better setup an IOCTL style interface.
193 * 'Cause the whole kernel world depends on OSMetaClassBase and YOU
194 * CANT change the VTABLE size ever.
195 *********************************************************************/
197 OSMetaClassBase::_RESERVEDOSMetaClassBase7()
198 { panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 7); }
201 /*********************************************************************
202 *********************************************************************/
203 OSMetaClassBase::OSMetaClassBase()
207 /*********************************************************************
208 *********************************************************************/
209 OSMetaClassBase::~OSMetaClassBase()
213 thisVTable
= (void **) this;
214 *thisVTable
= (void *) -1UL;
217 /*********************************************************************
218 *********************************************************************/
220 OSMetaClassBase::isEqualTo(const OSMetaClassBase
* anObj
) const
222 return this == anObj
;
225 /*********************************************************************
226 *********************************************************************/
228 OSMetaClassBase::metaCast(const OSMetaClass
* toMeta
) const
230 return toMeta
->checkMetaCast(this);
233 /*********************************************************************
234 *********************************************************************/
236 OSMetaClassBase::metaCast(const OSSymbol
* toMetaSymb
) const
238 return OSMetaClass::checkMetaCastWithName(toMetaSymb
, this);
241 /*********************************************************************
242 *********************************************************************/
244 OSMetaClassBase::metaCast(const OSString
* toMetaStr
) const
246 const OSSymbol
* tempSymb
= OSSymbol::withString(toMetaStr
);
247 OSMetaClassBase
* ret
= 0;
249 ret
= metaCast(tempSymb
);
255 /*********************************************************************
256 *********************************************************************/
258 OSMetaClassBase::metaCast(const char * toMetaCStr
) const
260 const OSSymbol
* tempSymb
= OSSymbol::withCString(toMetaCStr
);
261 OSMetaClassBase
* ret
= 0;
263 ret
= metaCast(tempSymb
);
270 #pragma mark OSMetaClassMeta
271 #endif /* PRAGMA_MARK */
272 /*********************************************************************
273 * OSMetaClassMeta - the bootstrap metaclass of OSMetaClass
274 *********************************************************************/
275 class OSMetaClassMeta
: public OSMetaClass
279 OSObject
* alloc() const;
281 OSMetaClassMeta::OSMetaClassMeta()
282 : OSMetaClass("OSMetaClass", 0, sizeof(OSMetaClass
))
284 OSObject
* OSMetaClassMeta::alloc() const { return 0; }
286 static OSMetaClassMeta sOSMetaClassMeta
;
288 const OSMetaClass
* const OSMetaClass::metaClass
= &sOSMetaClassMeta
;
289 const OSMetaClass
* OSMetaClass::getMetaClass() const
290 { return &sOSMetaClassMeta
; }
293 #pragma mark OSMetaClass
294 #endif /* PRAGMA_MARK */
295 /*********************************************************************
297 *********************************************************************/
299 #if APPLE_KEXT_VTABLE_PADDING
300 /*********************************************************************
301 * Reserved functions.
302 *********************************************************************/
303 void OSMetaClass::_RESERVEDOSMetaClass0()
304 { panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 0); }
305 void OSMetaClass::_RESERVEDOSMetaClass1()
306 { panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 1); }
307 void OSMetaClass::_RESERVEDOSMetaClass2()
308 { panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 2); }
309 void OSMetaClass::_RESERVEDOSMetaClass3()
310 { panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 3); }
311 void OSMetaClass::_RESERVEDOSMetaClass4()
312 { panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 4); }
313 void OSMetaClass::_RESERVEDOSMetaClass5()
314 { panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 5); }
315 void OSMetaClass::_RESERVEDOSMetaClass6()
316 { panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 6); }
317 void OSMetaClass::_RESERVEDOSMetaClass7()
318 { panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 7); }
321 /*********************************************************************
322 *********************************************************************/
324 OSMetaClassLogErrorForKext(
328 const char * message
= NULL
;
331 case kOSReturnSuccess
:
333 case kOSMetaClassNoInit
: // xxx - never returned; logged at fail site
334 message
= "OSMetaClass: preModLoad() wasn't called (runtime internal error).";
336 case kOSMetaClassNoDicts
:
337 message
= "OSMetaClass: Allocation failure for OSMetaClass internal dictionaries.";
339 case kOSMetaClassNoKModSet
:
340 message
= "OSMetaClass: Allocation failure for internal kext recording set/set missing.";
342 case kOSMetaClassNoInsKModSet
:
343 message
= "OSMetaClass: Failed to record class in kext.";
345 case kOSMetaClassDuplicateClass
:
346 message
= "OSMetaClass: Duplicate class encountered.";
348 case kOSMetaClassNoSuper
: // xxx - never returned
349 message
= "OSMetaClass: Can't associate a class with its superclass.";
351 case kOSMetaClassInstNoSuper
: // xxx - never returned
352 message
= "OSMetaClass: Instance construction error; unknown superclass.";
354 case kOSMetaClassNoKext
:
355 message
= "OSMetaClass: Kext not found for metaclass.";
357 case kOSMetaClassInternal
:
359 message
= "OSMetaClass: Runtime internal error.";
364 OSKextLog(aKext
, kOSMetaClassLogSpec
, "%s", message
);
370 OSMetaClass::logError(OSReturn error
)
372 OSMetaClassLogErrorForKext(error
, NULL
);
375 /*********************************************************************
376 * The core constructor for a MetaClass (defined with this name always
377 * but within the scope of its represented class).
379 * MetaClass constructors are invoked in OSRuntimeInitializeCPP(),
380 * in between calls to OSMetaClass::preModLoad(), which sets up for
381 * registration, and OSMetaClass::postModLoad(), which actually
382 * records all the class/kext relationships of the new MetaClasses.
383 *********************************************************************/
384 OSMetaClass::OSMetaClass(
385 const char * inClassName
,
386 const OSMetaClass
* inSuperClass
,
387 unsigned int inClassSize
)
390 classSize
= inClassSize
;
391 superClassLink
= inSuperClass
;
393 reserved
= IONew(ExpansionData
, 1);
394 bzero(reserved
, sizeof(ExpansionData
));
396 uint32_t numSiteQs
= 0;
397 if ((this == &OSSymbol ::gMetaClass
)
398 || (this == &OSString ::gMetaClass
)
399 || (this == &OSNumber ::gMetaClass
)
400 || (this == &OSString ::gMetaClass
)
401 || (this == &OSData ::gMetaClass
)
402 || (this == &OSDictionary::gMetaClass
)
403 || (this == &OSArray ::gMetaClass
)
404 || (this == &OSSet ::gMetaClass
)) numSiteQs
= 27;
406 reserved
->tracking
= IOTrackingQueueAlloc(inClassName
, (uintptr_t) this,
407 inClassSize
, 0, kIOTrackingQueueTypeAlloc
,
411 /* Hack alert: We are just casting inClassName and storing it in
412 * an OSString * instance variable. This may be because you can't
413 * create C++ objects in static constructors, but I really don't know!
415 className
= (const OSSymbol
*)inClassName
;
417 // sStalledClassesLock taken in preModLoad
419 /* There's no way we can look up the kext here, unfortunately.
421 OSKextLog(/* kext */ NULL
, kOSMetaClassLogSpec
,
422 "OSMetaClass: preModLoad() wasn't called for class %s "
423 "(runtime internal error).",
425 } else if (!sStalled
->result
) {
426 // Grow stalled array if neccessary
427 if (sStalled
->count
>= sStalled
->capacity
) {
428 OSMetaClass
**oldStalled
= sStalled
->classes
;
429 int oldSize
= sStalled
->capacity
* sizeof(OSMetaClass
*);
430 int newSize
= oldSize
431 + kKModCapacityIncrement
* sizeof(OSMetaClass
*);
433 sStalled
->classes
= (OSMetaClass
**)kalloc_tag(newSize
, VM_KERN_MEMORY_OSKEXT
);
434 if (!sStalled
->classes
) {
435 sStalled
->classes
= oldStalled
;
436 sStalled
->result
= kOSMetaClassNoTempData
;
440 sStalled
->capacity
+= kKModCapacityIncrement
;
441 memmove(sStalled
->classes
, oldStalled
, oldSize
);
442 kfree(oldStalled
, oldSize
);
443 OSMETA_ACCUMSIZE(((size_t)newSize
) - ((size_t)oldSize
));
446 sStalled
->classes
[sStalled
->count
++] = this;
450 /*********************************************************************
451 *********************************************************************/
452 OSMetaClass::~OSMetaClass()
454 OSKext
* myKext
= reserved
? reserved
->kext
: 0; // do not release
456 /* Hack alert: 'className' is a C string during early C++ init, and
457 * is converted to a real OSSymbol only when we record the OSKext in
458 * OSMetaClass::postModLoad(). So only do this bit if we have an OSKext.
459 * We can't safely cast or check 'className'.
461 * Also, release className *after* calling into the kext,
462 * as removeClass() may access className.
464 IOLockLock(sAllClassesLock
);
465 if (sAllClassesDict
) {
467 sAllClassesDict
->removeObject(className
);
469 sAllClassesDict
->removeObject((char *)className
);
472 IOLockUnlock(sAllClassesLock
);
475 if (myKext
->removeClass(this) != kOSReturnSuccess
) {
476 // xxx - what can we do?
478 className
->release();
481 // sStalledClassesLock taken in preModLoad
485 /* First pass find class in stalled list. If we find it that means
486 * we started C++ init with constructors but now we're tearing down
487 * because of some failure.
489 for (i
= 0; i
< sStalled
->count
; i
++) {
490 if (this == sStalled
->classes
[i
]) {
495 /* Remove this metaclass from the stalled list so postModLoad() doesn't
496 * try to register it.
498 if (i
< sStalled
->count
) {
500 if (i
< sStalled
->count
) {
501 memmove(&sStalled
->classes
[i
], &sStalled
->classes
[i
+1],
502 (sStalled
->count
- i
) * sizeof(OSMetaClass
*));
507 IOTrackingQueueFree(reserved
->tracking
);
509 IODelete(reserved
, ExpansionData
, 1);
512 /*********************************************************************
514 *********************************************************************/
515 void OSMetaClass::retain() const { }
516 void OSMetaClass::release() const { }
517 void OSMetaClass::release(__unused
int when
) const { }
518 void OSMetaClass::taggedRetain(__unused
const void * tag
) const { }
519 void OSMetaClass::taggedRelease(__unused
const void * tag
) const { }
520 void OSMetaClass::taggedRelease(__unused
const void * tag
, __unused
const int when
) const { }
521 int OSMetaClass::getRetainCount() const { return 0; }
523 /*********************************************************************
524 *********************************************************************/
526 OSMetaClass::getClassName() const
528 if (!className
) return NULL
;
529 return className
->getCStringNoCopy();
531 /*********************************************************************
532 *********************************************************************/
534 OSMetaClass::getClassNameSymbol() const
538 /*********************************************************************
539 *********************************************************************/
541 OSMetaClass::getClassSize() const
546 /*********************************************************************
547 *********************************************************************/
549 OSMetaClass::preModLoad(const char * kextIdentifier
)
551 IOLockLock(sStalledClassesLock
);
553 assert (sStalled
== NULL
);
554 sStalled
= (StalledData
*)kalloc_tag(sizeof(* sStalled
), VM_KERN_MEMORY_OSKEXT
);
556 sStalled
->classes
= (OSMetaClass
**)
557 kalloc_tag(kKModCapacityIncrement
* sizeof(OSMetaClass
*), VM_KERN_MEMORY_OSKEXT
);
558 if (!sStalled
->classes
) {
559 kfree(sStalled
, sizeof(*sStalled
));
562 OSMETA_ACCUMSIZE((kKModCapacityIncrement
* sizeof(OSMetaClass
*)) +
565 sStalled
->result
= kOSReturnSuccess
;
566 sStalled
->capacity
= kKModCapacityIncrement
;
568 sStalled
->kextIdentifier
= kextIdentifier
;
569 bzero(sStalled
->classes
, kKModCapacityIncrement
* sizeof(OSMetaClass
*));
572 // keep sStalledClassesLock locked until postModLoad
577 /*********************************************************************
578 *********************************************************************/
580 OSMetaClass::checkModLoad(void * loadHandle
)
582 return sStalled
&& loadHandle
== sStalled
&&
583 sStalled
->result
== kOSReturnSuccess
;
586 /*********************************************************************
587 *********************************************************************/
589 OSMetaClass::postModLoad(void * loadHandle
)
591 OSReturn result
= kOSReturnSuccess
;
592 OSSymbol
* myKextName
= 0; // must release
593 OSKext
* myKext
= 0; // must release
595 if (!sStalled
|| loadHandle
!= sStalled
) {
596 result
= kOSMetaClassInternal
;
600 if (sStalled
->result
) {
601 result
= sStalled
->result
;
602 } else switch (sBootstrapState
) {
604 case kNoDictionaries
:
605 sBootstrapState
= kMakingDictionaries
;
606 // No break; fall through
607 [[clang::fallthrough]];
609 case kMakingDictionaries
:
610 sAllClassesDict
= OSDictionary::withCapacity(kClassCapacityIncrement
);
611 if (!sAllClassesDict
) {
612 result
= kOSMetaClassNoDicts
;
615 sAllClassesDict
->setOptions(OSCollection::kSort
, OSCollection::kSort
);
617 // No break; fall through
618 [[clang::fallthrough]];
620 case kCompletedBootstrap
:
623 myKextName
= const_cast<OSSymbol
*>(OSSymbol::withCStringNoCopy(
624 sStalled
->kextIdentifier
));
626 if (!sStalled
->count
) {
627 break; // Nothing to do so just get out
630 myKext
= OSKext::lookupKextWithIdentifier(myKextName
);
632 result
= kOSMetaClassNoKext
;
634 /* Log this error here so we can include the kext name.
636 OSKextLog(/* kext */ NULL
, kOSMetaClassLogSpec
,
637 "OSMetaClass: Can't record classes for kext %s - kext not found.",
638 sStalled
->kextIdentifier
);
642 /* First pass checking classes aren't already loaded. If any already
643 * exist, we don't register any, and so we don't technically have
644 * to do any C++ teardown.
646 * Hack alert: me->className has been a C string until now.
647 * We only release the OSSymbol if we store the kext.
649 IOLockLock(sAllClassesLock
);
650 for (i
= 0; i
< sStalled
->count
; i
++) {
651 const OSMetaClass
* me
= sStalled
->classes
[i
];
652 OSMetaClass
* orig
= OSDynamicCast(OSMetaClass
,
653 sAllClassesDict
->getObject((const char *)me
->className
));
657 /* Log this error here so we can include the class name.
658 * xxx - we should look up the other kext that defines the class
663 OSKextLog(myKext
, kOSMetaClassLogSpec
,
664 #endif /* CONFIG_EMBEDDED */
665 "OSMetaClass: Kext %s class %s is a duplicate;"
666 "kext %s already has a class by that name.",
667 sStalled
->kextIdentifier
, (const char *)me
->className
,
668 ((OSKext
*)orig
->reserved
->kext
)->getIdentifierCString());
669 result
= kOSMetaClassDuplicateClass
;
672 unsigned int depth
= 1;
673 while ((me
= me
->superClassLink
)) depth
++;
674 if (depth
> sDeepestClass
) sDeepestClass
= depth
;
676 IOLockUnlock(sAllClassesLock
);
678 /* Bail if we didn't go through the entire list of new classes
679 * (if we hit a duplicate).
681 if (i
!= sStalled
->count
) {
685 // Second pass symbolling strings and inserting classes in dictionary
686 IOLockLock(sAllClassesLock
);
687 for (i
= 0; i
< sStalled
->count
; i
++) {
688 OSMetaClass
* me
= sStalled
->classes
[i
];
690 /* Hack alert: me->className has been a C string until now.
691 * We only release the OSSymbol in ~OSMetaClass()
692 * if we set the reference to the kext.
695 OSSymbol::withCStringNoCopy((const char *)me
->className
);
697 // xxx - I suppose if these fail we're going to panic soon....
698 sAllClassesDict
->setObject(me
->className
, me
);
700 /* Do not retain the kext object here.
702 me
->reserved
->kext
= myKext
;
704 result
= myKext
->addClass(me
, sStalled
->count
);
705 if (result
!= kOSReturnSuccess
) {
706 /* OSKext::addClass() logs with kOSMetaClassNoInsKModSet. */
711 IOLockUnlock(sAllClassesLock
);
712 sBootstrapState
= kCompletedBootstrap
;
717 result
= kOSMetaClassInternal
;
722 /* Don't call logError() for success or the conditions logged above
723 * or by called function.
725 if (result
!= kOSReturnSuccess
&&
726 result
!= kOSMetaClassNoInsKModSet
&&
727 result
!= kOSMetaClassDuplicateClass
&&
728 result
!= kOSMetaClassNoKext
) {
730 OSMetaClassLogErrorForKext(result
, myKext
);
733 OSSafeReleaseNULL(myKextName
);
734 OSSafeReleaseNULL(myKext
);
737 OSMETA_ACCUMSIZE(-(sStalled
->capacity
* sizeof(OSMetaClass
*) +
739 kfree(sStalled
->classes
, sStalled
->capacity
* sizeof(OSMetaClass
*));
740 kfree(sStalled
, sizeof(*sStalled
));
744 IOLockUnlock(sStalledClassesLock
);
750 /*********************************************************************
751 *********************************************************************/
753 OSMetaClass::instanceConstructed() const
755 // if ((0 == OSIncrementAtomic(&(((OSMetaClass *) this)->instanceCount))) && superClassLink)
756 if ((0 == OSIncrementAtomic(&instanceCount
)) && superClassLink
) {
757 superClassLink
->instanceConstructed();
761 /*********************************************************************
762 *********************************************************************/
764 OSMetaClass::instanceDestructed() const
766 if ((1 == OSDecrementAtomic(&instanceCount
)) && superClassLink
) {
767 superClassLink
->instanceDestructed();
770 if (((int)instanceCount
) < 0) {
771 OSKext
* myKext
= reserved
->kext
;
773 OSKextLog(myKext
, kOSMetaClassLogSpec
,
774 // xxx - this phrasing is rather cryptic
775 "OSMetaClass: Class %s - bad retain (%d)",
776 getClassName(), instanceCount
);
780 /*********************************************************************
781 *********************************************************************/
783 OSMetaClass::modHasInstance(const char * kextIdentifier
)
786 OSKext
* theKext
= NULL
; // must release
788 theKext
= OSKext::lookupKextWithIdentifier(kextIdentifier
);
793 result
= theKext
->hasOSMetaClassInstances();
796 OSSafeReleaseNULL(theKext
);
800 /*********************************************************************
801 *********************************************************************/
803 OSMetaClass::reportModInstances(const char * kextIdentifier
)
805 OSKext::reportOSMetaClassInstances(kextIdentifier
,
806 kOSKextLogExplicitLevel
);
809 /*********************************************************************
810 *********************************************************************/
813 OSMetaClass::addInstance(const OSObject
* instance
, bool super
) const
815 if (!super
) IOLockLock(sInstancesLock
);
817 if (!reserved
->instances
) {
818 reserved
->instances
= OSOrderedSet::withCapacity(16);
819 if (superClassLink
) {
820 superClassLink
->addInstance(reserved
->instances
, true);
823 reserved
->instances
->setLastObject(instance
);
825 if (!super
) IOLockUnlock(sInstancesLock
);
829 OSMetaClass::removeInstance(const OSObject
* instance
, bool super
) const
831 if (!super
) IOLockLock(sInstancesLock
);
833 if (reserved
->instances
) {
834 reserved
->instances
->removeObject(instance
);
835 if (0 == reserved
->instances
->getCount()) {
836 if (superClassLink
) {
837 superClassLink
->removeInstance(reserved
->instances
, true);
839 IOLockLock(sAllClassesLock
);
840 reserved
->instances
->release();
841 reserved
->instances
= 0;
842 IOLockUnlock(sAllClassesLock
);
846 if (!super
) IOLockUnlock(sInstancesLock
);
850 OSMetaClass::applyToInstances(OSOrderedSet
* set
,
851 OSMetaClassInstanceApplierFunction applier
,
854 enum { kLocalDepth
= 24 };
855 unsigned int _nextIndex
[kLocalDepth
];
856 OSOrderedSet
* _sets
[kLocalDepth
];
857 unsigned int * nextIndex
= &_nextIndex
[0];
858 OSOrderedSet
** sets
= &_sets
[0];
860 OSOrderedSet
* childSet
;
861 unsigned int maxDepth
;
866 maxDepth
= sDeepestClass
;
867 if (maxDepth
> kLocalDepth
)
869 nextIndex
= IONew(typeof(nextIndex
[0]), maxDepth
);
870 sets
= IONew(typeof(sets
[0]), maxDepth
);
877 while (!done
&& (obj
= set
->getObject(idx
++)))
879 if ((childSet
= OSDynamicCast(OSOrderedSet
, obj
)))
881 if (level
>= maxDepth
) panic(">maxDepth");
883 nextIndex
[level
] = idx
;
889 done
= (*applier
)(obj
, context
);
897 idx
= nextIndex
[level
];
902 if (maxDepth
> kLocalDepth
)
904 IODelete(nextIndex
, typeof(nextIndex
[0]), maxDepth
);
905 IODelete(sets
, typeof(sets
[0]), maxDepth
);
910 OSMetaClass::applyToInstances(OSMetaClassInstanceApplierFunction applier
,
911 void * context
) const
913 IOLockLock(sInstancesLock
);
914 if (reserved
->instances
) applyToInstances(reserved
->instances
, applier
, context
);
915 IOLockUnlock(sInstancesLock
);
919 OSMetaClass::applyToInstancesOfClassName(
920 const OSSymbol
* name
,
921 OSMetaClassInstanceApplierFunction applier
,
925 OSOrderedSet
* set
= 0;
927 IOLockLock(sAllClassesLock
);
929 && (meta
= (OSMetaClass
*) sAllClassesDict
->getObject(name
))
930 && (set
= meta
->reserved
->instances
))
934 IOLockUnlock(sAllClassesLock
);
938 IOLockLock(sInstancesLock
);
939 applyToInstances(set
, applier
, context
);
940 IOLockUnlock(sInstancesLock
);
944 /*********************************************************************
945 *********************************************************************/
947 OSMetaClass::considerUnloads()
949 OSKext::considerUnloads();
952 /*********************************************************************
953 *********************************************************************/
955 OSMetaClass::removeClasses(OSCollection
* metaClasses
)
957 OSCollectionIterator
* classIterator
;
958 OSMetaClass
* checkClass
;
961 classIterator
= OSCollectionIterator::withCollection(metaClasses
);
962 if (!classIterator
) return (false);
964 IOLockLock(sAllClassesLock
);
969 while ((checkClass
= (OSMetaClass
*)classIterator
->getNextObject())
970 && !checkClass
->getInstanceCount()
971 && !checkClass
->reserved
->retain
) {}
972 if (checkClass
) break;
973 classIterator
->reset();
974 while ((checkClass
= (OSMetaClass
*)classIterator
->getNextObject()))
976 sAllClassesDict
->removeObject(checkClass
->className
);
982 IOLockUnlock(sAllClassesLock
);
983 OSSafeReleaseNULL(classIterator
);
989 /*********************************************************************
990 *********************************************************************/
992 OSMetaClass::getMetaClassWithName(const OSSymbol
* name
)
994 OSMetaClass
* retMeta
= 0;
1000 IOLockLock(sAllClassesLock
);
1001 if (sAllClassesDict
) {
1002 retMeta
= (OSMetaClass
*) sAllClassesDict
->getObject(name
);
1004 IOLockUnlock(sAllClassesLock
);
1009 /*********************************************************************
1010 *********************************************************************/
1012 OSMetaClass::copyMetaClassWithName(const OSSymbol
* name
)
1014 const OSMetaClass
* meta
;
1016 if (!name
) return (0);
1019 IOLockLock(sAllClassesLock
);
1020 if (sAllClassesDict
) {
1021 meta
= (OSMetaClass
*) sAllClassesDict
->getObject(name
);
1022 if (meta
) OSIncrementAtomic(&meta
->reserved
->retain
);
1024 IOLockUnlock(sAllClassesLock
);
1029 /*********************************************************************
1030 *********************************************************************/
1032 OSMetaClass::releaseMetaClass() const
1034 OSDecrementAtomic(&reserved
->retain
);
1037 /*********************************************************************
1038 *********************************************************************/
1040 OSMetaClass::allocClassWithName(const OSSymbol
* name
)
1042 const OSMetaClass
* meta
;
1046 meta
= copyMetaClassWithName(name
);
1049 result
= meta
->alloc();
1050 meta
->releaseMetaClass();
1056 /*********************************************************************
1057 *********************************************************************/
1059 OSMetaClass::allocClassWithName(const OSString
* name
)
1061 const OSSymbol
* tmpKey
= OSSymbol::withString(name
);
1062 OSObject
* result
= allocClassWithName(tmpKey
);
1067 /*********************************************************************
1068 *********************************************************************/
1070 OSMetaClass::allocClassWithName(const char * name
)
1072 const OSSymbol
* tmpKey
= OSSymbol::withCStringNoCopy(name
);
1073 OSObject
* result
= allocClassWithName(tmpKey
);
1079 /*********************************************************************
1080 *********************************************************************/
1082 OSMetaClass::checkMetaCastWithName(
1083 const OSSymbol
* name
,
1084 const OSMetaClassBase
* in
)
1086 OSMetaClassBase
* result
= 0;
1088 const OSMetaClass
* const meta
= getMetaClassWithName(name
);
1091 result
= meta
->checkMetaCast(in
);
1097 /*********************************************************************
1098 *********************************************************************/
1099 OSMetaClassBase
* OSMetaClass::
1100 checkMetaCastWithName(
1101 const OSString
* name
,
1102 const OSMetaClassBase
* in
)
1104 const OSSymbol
* tmpKey
= OSSymbol::withString(name
);
1105 OSMetaClassBase
* result
= checkMetaCastWithName(tmpKey
, in
);
1111 /*********************************************************************
1112 *********************************************************************/
1114 OSMetaClass::checkMetaCastWithName(
1116 const OSMetaClassBase
* in
)
1118 const OSSymbol
* tmpKey
= OSSymbol::withCStringNoCopy(name
);
1119 OSMetaClassBase
* result
= checkMetaCastWithName(tmpKey
, in
);
1125 /*********************************************************************
1126 * OSMetaClass::checkMetaCast()
1127 * Check to see if the 'check' object has this object in its metaclass chain.
1128 * Returns check if it is indeed a kind of the current meta class, 0 otherwise.
1130 * Generally this method is not invoked directly but is used to implement
1131 * the OSMetaClassBase::metaCast member function.
1133 * See also OSMetaClassBase::metaCast
1134 *********************************************************************/
1135 OSMetaClassBase
* OSMetaClass::checkMetaCast(
1136 const OSMetaClassBase
* check
) const
1138 const OSMetaClass
* const toMeta
= this;
1139 const OSMetaClass
* fromMeta
;
1141 for (fromMeta
= check
->getMetaClass(); ; fromMeta
= fromMeta
->superClassLink
) {
1142 if (toMeta
== fromMeta
) {
1143 return const_cast<OSMetaClassBase
*>(check
); // Discard const
1145 if (!fromMeta
->superClassLink
) {
1153 /*********************************************************************
1154 *********************************************************************/
1156 OSMetaClass::reservedCalled(int ind
) const
1158 const char * cname
= className
->getCStringNoCopy();
1159 panic("%s::_RESERVED%s%d called.", cname
, cname
, ind
);
1162 /*********************************************************************
1163 *********************************************************************/
1166 OSMetaClass::getSuperClass() const
1168 return superClassLink
;
1171 /*********************************************************************
1172 * xxx - I want to rename this :-/
1173 *********************************************************************/
1175 OSMetaClass::getKmodName() const
1177 OSKext
* myKext
= reserved
? reserved
->kext
: 0;
1179 return myKext
->getIdentifier();
1181 return OSSymbol::withCStringNoCopy("unknown");
1184 /*********************************************************************
1185 *********************************************************************/
1187 OSMetaClass::getInstanceCount() const
1189 return instanceCount
;
1192 /*********************************************************************
1193 *********************************************************************/
1196 OSMetaClass::printInstanceCounts()
1198 OSCollectionIterator
* classes
;
1199 OSSymbol
* className
;
1202 IOLockLock(sAllClassesLock
);
1203 classes
= OSCollectionIterator::withCollection(sAllClassesDict
);
1206 while( (className
= (OSSymbol
*)classes
->getNextObject())) {
1207 meta
= (OSMetaClass
*)sAllClassesDict
->getObject(className
);
1210 printf("%24s count: %03d x 0x%03x = 0x%06x\n",
1211 className
->getCStringNoCopy(),
1212 meta
->getInstanceCount(),
1213 meta
->getClassSize(),
1214 meta
->getInstanceCount() * meta
->getClassSize() );
1218 IOLockUnlock(sAllClassesLock
);
1222 /*********************************************************************
1223 *********************************************************************/
1225 OSMetaClass::getClassDictionary()
1227 panic("OSMetaClass::getClassDictionary() is obsoleted.\n");
1231 /*********************************************************************
1232 *********************************************************************/
1234 OSMetaClass::serialize(__unused OSSerialize
* s
) const
1236 panic("OSMetaClass::serialize(): Obsoleted\n");
1240 /*********************************************************************
1241 *********************************************************************/
1244 OSMetaClass::serializeClassDictionary(OSDictionary
* serializeDictionary
)
1246 OSDictionary
* classDict
= NULL
;
1248 IOLockLock(sAllClassesLock
);
1250 classDict
= OSDictionary::withCapacity(sAllClassesDict
->getCount());
1256 OSCollectionIterator
* classes
;
1257 const OSSymbol
* className
;
1259 classes
= OSCollectionIterator::withCollection(sAllClassesDict
);
1264 while ((className
= (const OSSymbol
*)classes
->getNextObject())) {
1265 const OSMetaClass
* meta
;
1268 meta
= (OSMetaClass
*)sAllClassesDict
->getObject(className
);
1269 count
= OSNumber::withNumber(meta
->getInstanceCount(), 32);
1271 classDict
->setObject(className
, count
);
1277 serializeDictionary
->setObject("Classes", classDict
);
1281 OSSafeReleaseNULL(classDict
);
1283 IOLockUnlock(sAllClassesLock
);
1289 /*********************************************************************
1290 *********************************************************************/
1294 void *OSMetaClass::trackedNew(size_t size
)
1298 mem
= (typeof(mem
)) kalloc_tag_bt(size
+ sizeof(IOTracking
), VM_KERN_MEMORY_LIBKERN
);
1300 if (!mem
) return (mem
);
1302 memset(mem
, 0, size
+ sizeof(IOTracking
));
1305 OSIVAR_ACCUMSIZE(size
);
1310 void OSMetaClass::trackedDelete(void * instance
, size_t size
)
1312 IOTracking
* mem
= (typeof(mem
)) instance
; mem
--;
1314 kfree(mem
, size
+ sizeof(IOTracking
));
1315 OSIVAR_ACCUMSIZE(-size
);
1318 void OSMetaClass::trackedInstance(OSObject
* instance
) const
1320 IOTracking
* mem
= (typeof(mem
)) instance
; mem
--;
1322 return (IOTrackingAdd(reserved
->tracking
, mem
, classSize
, false, VM_KERN_MEMORY_NONE
));
1325 void OSMetaClass::trackedFree(OSObject
* instance
) const
1327 IOTracking
* mem
= (typeof(mem
)) instance
; mem
--;
1329 return (IOTrackingRemove(reserved
->tracking
, mem
, classSize
));
1332 void OSMetaClass::trackedAccumSize(OSObject
* instance
, size_t size
) const
1334 IOTracking
* mem
= (typeof(mem
)) instance
; mem
--;
1336 return (IOTrackingAccumSize(reserved
->tracking
, mem
, size
));
1339 IOTrackingQueue
* OSMetaClass::getTracking() const
1341 return (reserved
->tracking
);
1344 #endif /* IOTRACKING */