2 * Copyright (c) 2000-2006 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
;
114 IOTrackingQueue
* tracking
;
120 #pragma mark OSMetaClassBase
121 #endif /* PRAGMA_MARK */
122 /*********************************************************************
124 *********************************************************************/
126 #if APPLE_KEXT_VTABLE_PADDING
127 /*********************************************************************
128 * Reserved vtable functions.
129 *********************************************************************/
131 void OSMetaClassBase::_RESERVEDOSMetaClassBase0()
132 { panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 0); }
133 void OSMetaClassBase::_RESERVEDOSMetaClassBase1()
134 { panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 1); }
135 void OSMetaClassBase::_RESERVEDOSMetaClassBase2()
136 { panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 2); }
137 #endif /* SLOT_USED */
139 // As these slots are used move them up inside the #if above
140 void OSMetaClassBase::_RESERVEDOSMetaClassBase3()
141 { panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 3); }
142 void OSMetaClassBase::_RESERVEDOSMetaClassBase4()
143 { panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 4); }
144 void OSMetaClassBase::_RESERVEDOSMetaClassBase5()
145 { panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 5); }
146 void OSMetaClassBase::_RESERVEDOSMetaClassBase6()
147 { panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 6); }
150 /*********************************************************************
151 * These used to be inline in the header but gcc didn't believe us
152 * Now we MUST pull the inline out at least until the compiler is
155 * Helper inlines for runtime type preprocessor macros
156 *********************************************************************/
158 /*********************************************************************
159 *********************************************************************/
161 OSMetaClassBase::safeMetaCast(
162 const OSMetaClassBase
* me
,
163 const OSMetaClass
* toType
)
165 return (me
)? me
->metaCast(toType
) : 0;
168 /*********************************************************************
169 *********************************************************************/
171 OSMetaClassBase::checkTypeInst(
172 const OSMetaClassBase
* inst
,
173 const OSMetaClassBase
* typeinst
)
175 const OSMetaClass
* toType
= OSTypeIDInst(typeinst
);
176 return typeinst
&& inst
&& (0 != inst
->metaCast(toType
));
179 /*********************************************************************
180 *********************************************************************/
181 void OSMetaClassBase::
184 sAllClassesLock
= IOLockAlloc();
185 sStalledClassesLock
= IOLockAlloc();
186 sInstancesLock
= IOLockAlloc();
189 #if APPLE_KEXT_VTABLE_PADDING
190 /*********************************************************************
191 * If you need this slot you had better setup an IOCTL style interface.
192 * 'Cause the whole kernel world depends on OSMetaClassBase and YOU
193 * CANT change the VTABLE size ever.
194 *********************************************************************/
196 OSMetaClassBase::_RESERVEDOSMetaClassBase7()
197 { panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 7); }
200 /*********************************************************************
201 *********************************************************************/
202 OSMetaClassBase::OSMetaClassBase()
206 /*********************************************************************
207 *********************************************************************/
208 OSMetaClassBase::~OSMetaClassBase()
212 thisVTable
= (void **) this;
213 *thisVTable
= (void *) -1UL;
216 /*********************************************************************
217 *********************************************************************/
219 OSMetaClassBase::isEqualTo(const OSMetaClassBase
* anObj
) const
221 return this == anObj
;
224 /*********************************************************************
225 *********************************************************************/
227 OSMetaClassBase::metaCast(const OSMetaClass
* toMeta
) const
229 return toMeta
->checkMetaCast(this);
232 /*********************************************************************
233 *********************************************************************/
235 OSMetaClassBase::metaCast(const OSSymbol
* toMetaSymb
) const
237 return OSMetaClass::checkMetaCastWithName(toMetaSymb
, this);
240 /*********************************************************************
241 *********************************************************************/
243 OSMetaClassBase::metaCast(const OSString
* toMetaStr
) const
245 const OSSymbol
* tempSymb
= OSSymbol::withString(toMetaStr
);
246 OSMetaClassBase
* ret
= 0;
248 ret
= metaCast(tempSymb
);
254 /*********************************************************************
255 *********************************************************************/
257 OSMetaClassBase::metaCast(const char * toMetaCStr
) const
259 const OSSymbol
* tempSymb
= OSSymbol::withCString(toMetaCStr
);
260 OSMetaClassBase
* ret
= 0;
262 ret
= metaCast(tempSymb
);
269 #pragma mark OSMetaClassMeta
270 #endif /* PRAGMA_MARK */
271 /*********************************************************************
272 * OSMetaClassMeta - the bootstrap metaclass of OSMetaClass
273 *********************************************************************/
274 class OSMetaClassMeta
: public OSMetaClass
278 OSObject
* alloc() const;
280 OSMetaClassMeta::OSMetaClassMeta()
281 : OSMetaClass("OSMetaClass", 0, sizeof(OSMetaClass
))
283 OSObject
* OSMetaClassMeta::alloc() const { return 0; }
285 static OSMetaClassMeta sOSMetaClassMeta
;
287 const OSMetaClass
* const OSMetaClass::metaClass
= &sOSMetaClassMeta
;
288 const OSMetaClass
* OSMetaClass::getMetaClass() const
289 { return &sOSMetaClassMeta
; }
292 #pragma mark OSMetaClass
293 #endif /* PRAGMA_MARK */
294 /*********************************************************************
296 *********************************************************************/
298 #if APPLE_KEXT_VTABLE_PADDING
299 /*********************************************************************
300 * Reserved functions.
301 *********************************************************************/
302 void OSMetaClass::_RESERVEDOSMetaClass0()
303 { panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 0); }
304 void OSMetaClass::_RESERVEDOSMetaClass1()
305 { panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 1); }
306 void OSMetaClass::_RESERVEDOSMetaClass2()
307 { panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 2); }
308 void OSMetaClass::_RESERVEDOSMetaClass3()
309 { panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 3); }
310 void OSMetaClass::_RESERVEDOSMetaClass4()
311 { panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 4); }
312 void OSMetaClass::_RESERVEDOSMetaClass5()
313 { panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 5); }
314 void OSMetaClass::_RESERVEDOSMetaClass6()
315 { panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 6); }
316 void OSMetaClass::_RESERVEDOSMetaClass7()
317 { panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 7); }
320 /*********************************************************************
321 *********************************************************************/
323 OSMetaClassLogErrorForKext(
327 const char * message
= NULL
;
330 case kOSReturnSuccess
:
332 case kOSMetaClassNoInit
: // xxx - never returned; logged at fail site
333 message
= "OSMetaClass: preModLoad() wasn't called (runtime internal error).";
335 case kOSMetaClassNoDicts
:
336 message
= "OSMetaClass: Allocation failure for OSMetaClass internal dictionaries.";
338 case kOSMetaClassNoKModSet
:
339 message
= "OSMetaClass: Allocation failure for internal kext recording set/set missing.";
341 case kOSMetaClassNoInsKModSet
:
342 message
= "OSMetaClass: Failed to record class in kext.";
344 case kOSMetaClassDuplicateClass
:
345 message
= "OSMetaClass: Duplicate class encountered.";
347 case kOSMetaClassNoSuper
: // xxx - never returned
348 message
= "OSMetaClass: Can't associate a class with its superclass.";
350 case kOSMetaClassInstNoSuper
: // xxx - never returned
351 message
= "OSMetaClass: Instance construction error; unknown superclass.";
353 case kOSMetaClassNoKext
:
354 message
= "OSMetaClass: Kext not found for metaclass.";
356 case kOSMetaClassInternal
:
358 message
= "OSMetaClass: Runtime internal error.";
363 OSKextLog(aKext
, kOSMetaClassLogSpec
, "%s", message
);
369 OSMetaClass::logError(OSReturn error
)
371 OSMetaClassLogErrorForKext(error
, NULL
);
374 /*********************************************************************
375 * The core constructor for a MetaClass (defined with this name always
376 * but within the scope of its represented class).
378 * MetaClass constructors are invoked in OSRuntimeInitializeCPP(),
379 * in between calls to OSMetaClass::preModLoad(), which sets up for
380 * registration, and OSMetaClass::postModLoad(), which actually
381 * records all the class/kext relationships of the new MetaClasses.
382 *********************************************************************/
383 OSMetaClass::OSMetaClass(
384 const char * inClassName
,
385 const OSMetaClass
* inSuperClass
,
386 unsigned int inClassSize
)
389 classSize
= inClassSize
;
390 superClassLink
= inSuperClass
;
392 reserved
= IONew(ExpansionData
, 1);
393 bzero(reserved
, sizeof(ExpansionData
));
395 reserved
->tracking
= IOTrackingQueueAlloc(inClassName
, inClassSize
, 0, true);
398 /* Hack alert: We are just casting inClassName and storing it in
399 * an OSString * instance variable. This may be because you can't
400 * create C++ objects in static constructors, but I really don't know!
402 className
= (const OSSymbol
*)inClassName
;
404 // sStalledClassesLock taken in preModLoad
406 /* There's no way we can look up the kext here, unfortunately.
408 OSKextLog(/* kext */ NULL
, kOSMetaClassLogSpec
,
409 "OSMetaClass: preModLoad() wasn't called for class %s "
410 "(runtime internal error).",
412 } else if (!sStalled
->result
) {
413 // Grow stalled array if neccessary
414 if (sStalled
->count
>= sStalled
->capacity
) {
415 OSMetaClass
**oldStalled
= sStalled
->classes
;
416 int oldSize
= sStalled
->capacity
* sizeof(OSMetaClass
*);
417 int newSize
= oldSize
418 + kKModCapacityIncrement
* sizeof(OSMetaClass
*);
420 sStalled
->classes
= (OSMetaClass
**)kalloc_tag(newSize
, VM_KERN_MEMORY_OSKEXT
);
421 if (!sStalled
->classes
) {
422 sStalled
->classes
= oldStalled
;
423 sStalled
->result
= kOSMetaClassNoTempData
;
427 sStalled
->capacity
+= kKModCapacityIncrement
;
428 memmove(sStalled
->classes
, oldStalled
, oldSize
);
429 kfree(oldStalled
, oldSize
);
430 OSMETA_ACCUMSIZE(((size_t)newSize
) - ((size_t)oldSize
));
433 sStalled
->classes
[sStalled
->count
++] = this;
437 /*********************************************************************
438 *********************************************************************/
439 OSMetaClass::~OSMetaClass()
441 OSKext
* myKext
= reserved
? reserved
->kext
: 0; // do not release
443 /* Hack alert: 'className' is a C string during early C++ init, and
444 * is converted to a real OSSymbol only when we record the OSKext in
445 * OSMetaClass::postModLoad(). So only do this bit if we have an OSKext.
446 * We can't safely cast or check 'className'.
448 * Also, release className *after* calling into the kext,
449 * as removeClass() may access className.
451 IOLockLock(sAllClassesLock
);
452 if (sAllClassesDict
) {
454 sAllClassesDict
->removeObject(className
);
456 sAllClassesDict
->removeObject((char *)className
);
459 IOLockUnlock(sAllClassesLock
);
462 if (myKext
->removeClass(this) != kOSReturnSuccess
) {
463 // xxx - what can we do?
465 className
->release();
468 // sStalledClassesLock taken in preModLoad
472 /* First pass find class in stalled list. If we find it that means
473 * we started C++ init with constructors but now we're tearing down
474 * because of some failure.
476 for (i
= 0; i
< sStalled
->count
; i
++) {
477 if (this == sStalled
->classes
[i
]) {
482 /* Remove this metaclass from the stalled list so postModLoad() doesn't
483 * try to register it.
485 if (i
< sStalled
->count
) {
487 if (i
< sStalled
->count
) {
488 memmove(&sStalled
->classes
[i
], &sStalled
->classes
[i
+1],
489 (sStalled
->count
- i
) * sizeof(OSMetaClass
*));
494 IOTrackingQueueFree(reserved
->tracking
);
496 IODelete(reserved
, ExpansionData
, 1);
499 /*********************************************************************
501 *********************************************************************/
502 void OSMetaClass::retain() const { }
503 void OSMetaClass::release() const { }
504 void OSMetaClass::release(__unused
int when
) const { }
505 void OSMetaClass::taggedRetain(__unused
const void * tag
) const { }
506 void OSMetaClass::taggedRelease(__unused
const void * tag
) const { }
507 void OSMetaClass::taggedRelease(__unused
const void * tag
, __unused
const int when
) const { }
508 int OSMetaClass::getRetainCount() const { return 0; }
510 /*********************************************************************
511 *********************************************************************/
513 OSMetaClass::getClassName() const
515 if (!className
) return NULL
;
516 return className
->getCStringNoCopy();
518 /*********************************************************************
519 *********************************************************************/
521 OSMetaClass::getClassNameSymbol() const
525 /*********************************************************************
526 *********************************************************************/
528 OSMetaClass::getClassSize() const
533 /*********************************************************************
534 *********************************************************************/
536 OSMetaClass::preModLoad(const char * kextIdentifier
)
538 IOLockLock(sStalledClassesLock
);
540 assert (sStalled
== NULL
);
541 sStalled
= (StalledData
*)kalloc_tag(sizeof(* sStalled
), VM_KERN_MEMORY_OSKEXT
);
543 sStalled
->classes
= (OSMetaClass
**)
544 kalloc_tag(kKModCapacityIncrement
* sizeof(OSMetaClass
*), VM_KERN_MEMORY_OSKEXT
);
545 if (!sStalled
->classes
) {
546 kfree(sStalled
, sizeof(*sStalled
));
549 OSMETA_ACCUMSIZE((kKModCapacityIncrement
* sizeof(OSMetaClass
*)) +
552 sStalled
->result
= kOSReturnSuccess
;
553 sStalled
->capacity
= kKModCapacityIncrement
;
555 sStalled
->kextIdentifier
= kextIdentifier
;
556 bzero(sStalled
->classes
, kKModCapacityIncrement
* sizeof(OSMetaClass
*));
559 // keep sStalledClassesLock locked until postModLoad
564 /*********************************************************************
565 *********************************************************************/
567 OSMetaClass::checkModLoad(void * loadHandle
)
569 return sStalled
&& loadHandle
== sStalled
&&
570 sStalled
->result
== kOSReturnSuccess
;
573 /*********************************************************************
574 *********************************************************************/
576 OSMetaClass::postModLoad(void * loadHandle
)
578 OSReturn result
= kOSReturnSuccess
;
579 OSSymbol
* myKextName
= 0; // must release
580 OSKext
* myKext
= 0; // must release
582 if (!sStalled
|| loadHandle
!= sStalled
) {
583 result
= kOSMetaClassInternal
;
587 if (sStalled
->result
) {
588 result
= sStalled
->result
;
589 } else switch (sBootstrapState
) {
591 case kNoDictionaries
:
592 sBootstrapState
= kMakingDictionaries
;
593 // No break; fall through
595 case kMakingDictionaries
:
596 sAllClassesDict
= OSDictionary::withCapacity(kClassCapacityIncrement
);
597 if (!sAllClassesDict
) {
598 result
= kOSMetaClassNoDicts
;
601 sAllClassesDict
->setOptions(OSCollection::kSort
, OSCollection::kSort
);
603 // No break; fall through
605 case kCompletedBootstrap
:
608 myKextName
= const_cast<OSSymbol
*>(OSSymbol::withCStringNoCopy(
609 sStalled
->kextIdentifier
));
611 if (!sStalled
->count
) {
612 break; // Nothing to do so just get out
615 myKext
= OSKext::lookupKextWithIdentifier(myKextName
);
617 result
= kOSMetaClassNoKext
;
619 /* Log this error here so we can include the kext name.
621 OSKextLog(/* kext */ NULL
, kOSMetaClassLogSpec
,
622 "OSMetaClass: Can't record classes for kext %s - kext not found.",
623 sStalled
->kextIdentifier
);
627 /* First pass checking classes aren't already loaded. If any already
628 * exist, we don't register any, and so we don't technically have
629 * to do any C++ teardown.
631 * Hack alert: me->className has been a C string until now.
632 * We only release the OSSymbol if we store the kext.
634 IOLockLock(sAllClassesLock
);
635 for (i
= 0; i
< sStalled
->count
; i
++) {
636 const OSMetaClass
* me
= sStalled
->classes
[i
];
637 OSMetaClass
* orig
= OSDynamicCast(OSMetaClass
,
638 sAllClassesDict
->getObject((const char *)me
->className
));
642 /* Log this error here so we can include the class name.
643 * xxx - we should look up the other kext that defines the class
645 OSKextLog(myKext
, kOSMetaClassLogSpec
,
646 "OSMetaClass: Kext %s class %s is a duplicate;"
647 "kext %s already has a class by that name.",
648 sStalled
->kextIdentifier
, (const char *)me
->className
,
649 ((OSKext
*)orig
->reserved
->kext
)->getIdentifierCString());
650 result
= kOSMetaClassDuplicateClass
;
653 unsigned int depth
= 1;
654 while ((me
= me
->superClassLink
)) depth
++;
655 if (depth
> sDeepestClass
) sDeepestClass
= depth
;
657 IOLockUnlock(sAllClassesLock
);
659 /* Bail if we didn't go through the entire list of new classes
660 * (if we hit a duplicate).
662 if (i
!= sStalled
->count
) {
666 // Second pass symbolling strings and inserting classes in dictionary
667 IOLockLock(sAllClassesLock
);
668 for (i
= 0; i
< sStalled
->count
; i
++) {
669 OSMetaClass
* me
= sStalled
->classes
[i
];
671 /* Hack alert: me->className has been a C string until now.
672 * We only release the OSSymbol in ~OSMetaClass()
673 * if we set the reference to the kext.
676 OSSymbol::withCStringNoCopy((const char *)me
->className
);
678 // xxx - I suppose if these fail we're going to panic soon....
679 sAllClassesDict
->setObject(me
->className
, me
);
681 /* Do not retain the kext object here.
683 me
->reserved
->kext
= myKext
;
685 result
= myKext
->addClass(me
, sStalled
->count
);
686 if (result
!= kOSReturnSuccess
) {
687 /* OSKext::addClass() logs with kOSMetaClassNoInsKModSet. */
692 IOLockUnlock(sAllClassesLock
);
693 sBootstrapState
= kCompletedBootstrap
;
698 result
= kOSMetaClassInternal
;
703 /* Don't call logError() for success or the conditions logged above
704 * or by called function.
706 if (result
!= kOSReturnSuccess
&&
707 result
!= kOSMetaClassNoInsKModSet
&&
708 result
!= kOSMetaClassDuplicateClass
&&
709 result
!= kOSMetaClassNoKext
) {
711 OSMetaClassLogErrorForKext(result
, myKext
);
714 OSSafeRelease(myKextName
);
715 OSSafeRelease(myKext
);
718 OSMETA_ACCUMSIZE(-(sStalled
->capacity
* sizeof(OSMetaClass
*) +
720 kfree(sStalled
->classes
, sStalled
->capacity
* sizeof(OSMetaClass
*));
721 kfree(sStalled
, sizeof(*sStalled
));
725 IOLockUnlock(sStalledClassesLock
);
731 /*********************************************************************
732 *********************************************************************/
734 OSMetaClass::instanceConstructed() const
736 // if ((0 == OSIncrementAtomic(&(((OSMetaClass *) this)->instanceCount))) && superClassLink)
737 if ((0 == OSIncrementAtomic(&instanceCount
)) && superClassLink
) {
738 superClassLink
->instanceConstructed();
742 /*********************************************************************
743 *********************************************************************/
745 OSMetaClass::instanceDestructed() const
747 if ((1 == OSDecrementAtomic(&instanceCount
)) && superClassLink
) {
748 superClassLink
->instanceDestructed();
751 if (((int)instanceCount
) < 0) {
752 OSKext
* myKext
= reserved
->kext
;
754 OSKextLog(myKext
, kOSMetaClassLogSpec
,
755 // xxx - this phrasing is rather cryptic
756 "OSMetaClass: Class %s - bad retain (%d)",
757 getClassName(), instanceCount
);
761 /*********************************************************************
762 *********************************************************************/
764 OSMetaClass::modHasInstance(const char * kextIdentifier
)
767 OSKext
* theKext
= NULL
; // must release
769 theKext
= OSKext::lookupKextWithIdentifier(kextIdentifier
);
774 result
= theKext
->hasOSMetaClassInstances();
777 OSSafeRelease(theKext
);
781 /*********************************************************************
782 *********************************************************************/
784 OSMetaClass::reportModInstances(const char * kextIdentifier
)
786 OSKext::reportOSMetaClassInstances(kextIdentifier
,
787 kOSKextLogExplicitLevel
);
790 /*********************************************************************
791 *********************************************************************/
794 OSMetaClass::addInstance(const OSObject
* instance
, bool super
) const
796 if (!super
) IOLockLock(sInstancesLock
);
798 if (!reserved
->instances
) {
799 reserved
->instances
= OSOrderedSet::withCapacity(16);
800 if (superClassLink
) {
801 superClassLink
->addInstance(reserved
->instances
, true);
804 reserved
->instances
->setLastObject(instance
);
806 if (!super
) IOLockUnlock(sInstancesLock
);
810 OSMetaClass::removeInstance(const OSObject
* instance
, bool super
) const
812 if (!super
) IOLockLock(sInstancesLock
);
814 if (reserved
->instances
) {
815 reserved
->instances
->removeObject(instance
);
816 if (0 == reserved
->instances
->getCount()) {
817 if (superClassLink
) {
818 superClassLink
->removeInstance(reserved
->instances
, true);
820 IOLockLock(sAllClassesLock
);
821 reserved
->instances
->release();
822 reserved
->instances
= 0;
823 IOLockUnlock(sAllClassesLock
);
827 if (!super
) IOLockUnlock(sInstancesLock
);
831 OSMetaClass::applyToInstances(OSOrderedSet
* set
,
832 OSMetaClassInstanceApplierFunction applier
,
835 enum { kLocalDepth
= 24 };
836 unsigned int _nextIndex
[kLocalDepth
];
837 OSOrderedSet
* _sets
[kLocalDepth
];
838 unsigned int * nextIndex
= &_nextIndex
[0];
839 OSOrderedSet
** sets
= &_sets
[0];
841 OSOrderedSet
* childSet
;
842 unsigned int maxDepth
;
847 maxDepth
= sDeepestClass
;
848 if (maxDepth
> kLocalDepth
)
850 nextIndex
= IONew(typeof(nextIndex
[0]), maxDepth
);
851 sets
= IONew(typeof(sets
[0]), maxDepth
);
858 while (!done
&& (obj
= set
->getObject(idx
++)))
860 if ((childSet
= OSDynamicCast(OSOrderedSet
, obj
)))
862 if (level
>= maxDepth
) panic(">maxDepth");
864 nextIndex
[level
] = idx
;
870 done
= (*applier
)(obj
, context
);
878 idx
= nextIndex
[level
];
883 if (maxDepth
> kLocalDepth
)
885 IODelete(nextIndex
, typeof(nextIndex
[0]), maxDepth
);
886 IODelete(sets
, typeof(sets
[0]), maxDepth
);
891 OSMetaClass::applyToInstances(OSMetaClassInstanceApplierFunction applier
,
892 void * context
) const
894 IOLockLock(sInstancesLock
);
895 if (reserved
->instances
) applyToInstances(reserved
->instances
, applier
, context
);
896 IOLockUnlock(sInstancesLock
);
900 OSMetaClass::applyToInstancesOfClassName(
901 const OSSymbol
* name
,
902 OSMetaClassInstanceApplierFunction applier
,
906 OSOrderedSet
* set
= 0;
908 IOLockLock(sAllClassesLock
);
910 && (meta
= (OSMetaClass
*) sAllClassesDict
->getObject(name
))
911 && (set
= meta
->reserved
->instances
))
915 IOLockUnlock(sAllClassesLock
);
919 IOLockLock(sInstancesLock
);
920 applyToInstances(set
, applier
, context
);
921 IOLockUnlock(sInstancesLock
);
925 /*********************************************************************
926 *********************************************************************/
928 OSMetaClass::considerUnloads()
930 OSKext::considerUnloads();
933 /*********************************************************************
934 *********************************************************************/
936 OSMetaClass::getMetaClassWithName(const OSSymbol
* name
)
938 OSMetaClass
* retMeta
= 0;
944 IOLockLock(sAllClassesLock
);
945 if (sAllClassesDict
) {
946 retMeta
= (OSMetaClass
*) sAllClassesDict
->getObject(name
);
948 IOLockUnlock(sAllClassesLock
);
953 /*********************************************************************
954 *********************************************************************/
956 OSMetaClass::allocClassWithName(const OSSymbol
* name
)
958 OSObject
* result
= 0;
960 const OSMetaClass
* const meta
= getMetaClassWithName(name
);
963 result
= meta
->alloc();
969 /*********************************************************************
970 *********************************************************************/
972 OSMetaClass::allocClassWithName(const OSString
* name
)
974 const OSSymbol
* tmpKey
= OSSymbol::withString(name
);
975 OSObject
* result
= allocClassWithName(tmpKey
);
980 /*********************************************************************
981 *********************************************************************/
983 OSMetaClass::allocClassWithName(const char * name
)
985 const OSSymbol
* tmpKey
= OSSymbol::withCStringNoCopy(name
);
986 OSObject
* result
= allocClassWithName(tmpKey
);
992 /*********************************************************************
993 *********************************************************************/
995 OSMetaClass::checkMetaCastWithName(
996 const OSSymbol
* name
,
997 const OSMetaClassBase
* in
)
999 OSMetaClassBase
* result
= 0;
1001 const OSMetaClass
* const meta
= getMetaClassWithName(name
);
1004 result
= meta
->checkMetaCast(in
);
1010 /*********************************************************************
1011 *********************************************************************/
1012 OSMetaClassBase
* OSMetaClass::
1013 checkMetaCastWithName(
1014 const OSString
* name
,
1015 const OSMetaClassBase
* in
)
1017 const OSSymbol
* tmpKey
= OSSymbol::withString(name
);
1018 OSMetaClassBase
* result
= checkMetaCastWithName(tmpKey
, in
);
1024 /*********************************************************************
1025 *********************************************************************/
1027 OSMetaClass::checkMetaCastWithName(
1029 const OSMetaClassBase
* in
)
1031 const OSSymbol
* tmpKey
= OSSymbol::withCStringNoCopy(name
);
1032 OSMetaClassBase
* result
= checkMetaCastWithName(tmpKey
, in
);
1038 /*********************************************************************
1039 * OSMetaClass::checkMetaCast()
1040 * Check to see if the 'check' object has this object in its metaclass chain.
1041 * Returns check if it is indeed a kind of the current meta class, 0 otherwise.
1043 * Generally this method is not invoked directly but is used to implement
1044 * the OSMetaClassBase::metaCast member function.
1046 * See also OSMetaClassBase::metaCast
1047 *********************************************************************/
1048 OSMetaClassBase
* OSMetaClass::checkMetaCast(
1049 const OSMetaClassBase
* check
) const
1051 const OSMetaClass
* const toMeta
= this;
1052 const OSMetaClass
* fromMeta
;
1054 for (fromMeta
= check
->getMetaClass(); ; fromMeta
= fromMeta
->superClassLink
) {
1055 if (toMeta
== fromMeta
) {
1056 return const_cast<OSMetaClassBase
*>(check
); // Discard const
1058 if (!fromMeta
->superClassLink
) {
1066 /*********************************************************************
1067 *********************************************************************/
1069 OSMetaClass::reservedCalled(int ind
) const
1071 const char * cname
= className
->getCStringNoCopy();
1072 panic("%s::_RESERVED%s%d called.", cname
, cname
, ind
);
1075 /*********************************************************************
1076 *********************************************************************/
1079 OSMetaClass::getSuperClass() const
1081 return superClassLink
;
1084 /*********************************************************************
1085 * xxx - I want to rename this :-/
1086 *********************************************************************/
1088 OSMetaClass::getKmodName() const
1090 OSKext
* myKext
= reserved
? reserved
->kext
: 0;
1092 return myKext
->getIdentifier();
1094 return OSSymbol::withCStringNoCopy("unknown");
1097 /*********************************************************************
1098 *********************************************************************/
1100 OSMetaClass::getInstanceCount() const
1102 return instanceCount
;
1105 /*********************************************************************
1106 *********************************************************************/
1109 OSMetaClass::printInstanceCounts()
1111 OSCollectionIterator
* classes
;
1112 OSSymbol
* className
;
1115 IOLockLock(sAllClassesLock
);
1116 classes
= OSCollectionIterator::withCollection(sAllClassesDict
);
1119 while( (className
= (OSSymbol
*)classes
->getNextObject())) {
1120 meta
= (OSMetaClass
*)sAllClassesDict
->getObject(className
);
1123 printf("%24s count: %03d x 0x%03x = 0x%06x\n",
1124 className
->getCStringNoCopy(),
1125 meta
->getInstanceCount(),
1126 meta
->getClassSize(),
1127 meta
->getInstanceCount() * meta
->getClassSize() );
1131 IOLockUnlock(sAllClassesLock
);
1135 /*********************************************************************
1136 *********************************************************************/
1138 OSMetaClass::getClassDictionary()
1140 panic("OSMetaClass::getClassDictionary() is obsoleted.\n");
1144 /*********************************************************************
1145 *********************************************************************/
1147 OSMetaClass::serialize(__unused OSSerialize
* s
) const
1149 panic("OSMetaClass::serialize(): Obsoleted\n");
1153 /*********************************************************************
1154 *********************************************************************/
1157 OSMetaClass::serializeClassDictionary(OSDictionary
* serializeDictionary
)
1159 OSDictionary
* classDict
= NULL
;
1161 IOLockLock(sAllClassesLock
);
1163 classDict
= OSDictionary::withCapacity(sAllClassesDict
->getCount());
1169 OSCollectionIterator
* classes
;
1170 const OSSymbol
* className
;
1172 classes
= OSCollectionIterator::withCollection(sAllClassesDict
);
1177 while ((className
= (const OSSymbol
*)classes
->getNextObject())) {
1178 const OSMetaClass
* meta
;
1181 meta
= (OSMetaClass
*)sAllClassesDict
->getObject(className
);
1182 count
= OSNumber::withNumber(meta
->getInstanceCount(), 32);
1184 classDict
->setObject(className
, count
);
1190 serializeDictionary
->setObject("Classes", classDict
);
1194 OSSafeRelease(classDict
);
1196 IOLockUnlock(sAllClassesLock
);
1202 /*********************************************************************
1203 *********************************************************************/
1207 void *OSMetaClass::trackedNew(size_t size
)
1211 mem
= (typeof(mem
)) kalloc_tag_bt(size
+ sizeof(IOTracking
), VM_KERN_MEMORY_LIBKERN
);
1213 if (!mem
) return (mem
);
1215 memset(mem
, 0, size
+ sizeof(IOTracking
));
1218 OSIVAR_ACCUMSIZE(size
);
1223 void OSMetaClass::trackedDelete(void * instance
, size_t size
)
1225 IOTracking
* mem
= (typeof(mem
)) instance
; mem
--;
1227 kfree(mem
, size
+ sizeof(IOTracking
));
1228 OSIVAR_ACCUMSIZE(-size
);
1231 void OSMetaClass::trackedInstance(OSObject
* instance
) const
1233 IOTracking
* mem
= (typeof(mem
)) instance
; mem
--;
1235 return (IOTrackingAdd(reserved
->tracking
, mem
, classSize
, false));
1238 void OSMetaClass::trackedFree(OSObject
* instance
) const
1240 IOTracking
* mem
= (typeof(mem
)) instance
; mem
--;
1242 return (IOTrackingRemove(reserved
->tracking
, mem
, classSize
));
1245 void OSMetaClass::trackedAccumSize(OSObject
* instance
, size_t size
) const
1247 IOTracking
* mem
= (typeof(mem
)) instance
; mem
--;
1249 return (IOTrackingAccumSize(reserved
->tracking
, mem
, size
));
1252 IOTrackingQueue
* OSMetaClass::getTracking() const
1254 return (reserved
->tracking
);
1257 #endif /* IOTRACKING */