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/pwr_mgt/RootDomain.h>
50 #include <IOKit/IOMessage.h>
51 #include <IOKit/IOLib.h>
55 #include <sys/systm.h>
56 #include <mach/mach_types.h>
57 #include <kern/locks.h>
58 #include <kern/clock.h>
59 #include <kern/thread_call.h>
60 #include <kern/host.h>
61 #include <mach/mach_interface.h>
65 #endif /* PRAGMA_MARK */
66 /*********************************************************************
68 *********************************************************************/
70 extern int debug_container_malloc_size
;
71 #define ACCUMSIZE(s) do { debug_container_malloc_size += (s); } while (0)
74 #endif /* OSALLOCDEBUG */
79 #pragma mark Internal constants & data structs
80 #endif /* PRAGMA_MARK */
81 /*********************************************************************
82 * Internal constants & data structs
83 *********************************************************************/
84 OSKextLogSpec kOSMetaClassLogSpec
=
85 kOSKextLogErrorLevel
|
87 kOSKextLogKextBookkeepingFlag
;
90 kCompletedBootstrap
= 0,
92 kMakingDictionaries
= 2
93 } sBootstrapState
= kNoDictionaries
;
95 static const int kClassCapacityIncrement
= 40;
96 static const int kKModCapacityIncrement
= 10;
97 static OSDictionary
* sAllClassesDict
;
98 static unsigned int sDeepestClass
;
99 IOLock
* sAllClassesLock
= NULL
;
100 IOLock
* sInstancesLock
= NULL
;
103 * While loading a kext and running all its constructors to register
104 * all OSMetaClass classes, the classes are queued up here. Only one
105 * kext can be in flight at a time, guarded by sStalledClassesLock
107 static struct StalledData
{
108 const char * kextIdentifier
;
110 unsigned int capacity
;
112 OSMetaClass
** classes
;
114 IOLock
* sStalledClassesLock
= NULL
;
117 struct ExpansionData
{
118 OSOrderedSet
* instances
;
124 #pragma mark OSMetaClassBase
125 #endif /* PRAGMA_MARK */
126 /*********************************************************************
128 *********************************************************************/
130 #if APPLE_KEXT_VTABLE_PADDING
131 /*********************************************************************
132 * Reserved vtable functions.
133 *********************************************************************/
135 void OSMetaClassBase::_RESERVEDOSMetaClassBase0()
136 { panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 0); }
137 void OSMetaClassBase::_RESERVEDOSMetaClassBase1()
138 { panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 1); }
139 void OSMetaClassBase::_RESERVEDOSMetaClassBase2()
140 { panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 2); }
141 #endif /* SLOT_USED */
143 // As these slots are used move them up inside the #if above
144 void OSMetaClassBase::_RESERVEDOSMetaClassBase3()
145 { panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 3); }
146 void OSMetaClassBase::_RESERVEDOSMetaClassBase4()
147 { panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 4); }
148 void OSMetaClassBase::_RESERVEDOSMetaClassBase5()
149 { panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 5); }
150 void OSMetaClassBase::_RESERVEDOSMetaClassBase6()
151 { panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 6); }
154 /*********************************************************************
155 * These used to be inline in the header but gcc didn't believe us
156 * Now we MUST pull the inline out at least until the compiler is
159 * Helper inlines for runtime type preprocessor macros
160 *********************************************************************/
162 /*********************************************************************
163 *********************************************************************/
165 OSMetaClassBase::safeMetaCast(
166 const OSMetaClassBase
* me
,
167 const OSMetaClass
* toType
)
169 return (me
)? me
->metaCast(toType
) : 0;
172 /*********************************************************************
173 *********************************************************************/
175 OSMetaClassBase::checkTypeInst(
176 const OSMetaClassBase
* inst
,
177 const OSMetaClassBase
* typeinst
)
179 const OSMetaClass
* toType
= OSTypeIDInst(typeinst
);
180 return typeinst
&& inst
&& (0 != inst
->metaCast(toType
));
183 /*********************************************************************
184 *********************************************************************/
185 void OSMetaClassBase::
188 sAllClassesLock
= IOLockAlloc();
189 sStalledClassesLock
= IOLockAlloc();
190 sInstancesLock
= IOLockAlloc();
193 #if APPLE_KEXT_VTABLE_PADDING
194 /*********************************************************************
195 * If you need this slot you had better setup an IOCTL style interface.
196 * 'Cause the whole kernel world depends on OSMetaClassBase and YOU
197 * CANT change the VTABLE size ever.
198 *********************************************************************/
200 OSMetaClassBase::_RESERVEDOSMetaClassBase7()
201 { panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 7); }
204 /*********************************************************************
205 *********************************************************************/
206 OSMetaClassBase::OSMetaClassBase()
210 /*********************************************************************
211 *********************************************************************/
212 OSMetaClassBase::~OSMetaClassBase()
216 thisVTable
= (void **) this;
217 *thisVTable
= (void *) -1UL;
220 /*********************************************************************
221 *********************************************************************/
223 OSMetaClassBase::isEqualTo(const OSMetaClassBase
* anObj
) const
225 return this == anObj
;
228 /*********************************************************************
229 *********************************************************************/
231 OSMetaClassBase::metaCast(const OSMetaClass
* toMeta
) const
233 return toMeta
->checkMetaCast(this);
236 /*********************************************************************
237 *********************************************************************/
239 OSMetaClassBase::metaCast(const OSSymbol
* toMetaSymb
) const
241 return OSMetaClass::checkMetaCastWithName(toMetaSymb
, this);
244 /*********************************************************************
245 *********************************************************************/
247 OSMetaClassBase::metaCast(const OSString
* toMetaStr
) const
249 const OSSymbol
* tempSymb
= OSSymbol::withString(toMetaStr
);
250 OSMetaClassBase
* ret
= 0;
252 ret
= metaCast(tempSymb
);
258 /*********************************************************************
259 *********************************************************************/
261 OSMetaClassBase::metaCast(const char * toMetaCStr
) const
263 const OSSymbol
* tempSymb
= OSSymbol::withCString(toMetaCStr
);
264 OSMetaClassBase
* ret
= 0;
266 ret
= metaCast(tempSymb
);
273 #pragma mark OSMetaClassMeta
274 #endif /* PRAGMA_MARK */
275 /*********************************************************************
276 * OSMetaClassMeta - the bootstrap metaclass of OSMetaClass
277 *********************************************************************/
278 class OSMetaClassMeta
: public OSMetaClass
282 OSObject
* alloc() const;
284 OSMetaClassMeta::OSMetaClassMeta()
285 : OSMetaClass("OSMetaClass", 0, sizeof(OSMetaClass
))
287 OSObject
* OSMetaClassMeta::alloc() const { return 0; }
289 static OSMetaClassMeta sOSMetaClassMeta
;
291 const OSMetaClass
* const OSMetaClass::metaClass
= &sOSMetaClassMeta
;
292 const OSMetaClass
* OSMetaClass::getMetaClass() const
293 { return &sOSMetaClassMeta
; }
296 #pragma mark OSMetaClass
297 #endif /* PRAGMA_MARK */
298 /*********************************************************************
300 *********************************************************************/
302 #if APPLE_KEXT_VTABLE_PADDING
303 /*********************************************************************
304 * Reserved functions.
305 *********************************************************************/
306 void OSMetaClass::_RESERVEDOSMetaClass0()
307 { panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 0); }
308 void OSMetaClass::_RESERVEDOSMetaClass1()
309 { panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 1); }
310 void OSMetaClass::_RESERVEDOSMetaClass2()
311 { panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 2); }
312 void OSMetaClass::_RESERVEDOSMetaClass3()
313 { panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 3); }
314 void OSMetaClass::_RESERVEDOSMetaClass4()
315 { panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 4); }
316 void OSMetaClass::_RESERVEDOSMetaClass5()
317 { panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 5); }
318 void OSMetaClass::_RESERVEDOSMetaClass6()
319 { panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 6); }
320 void OSMetaClass::_RESERVEDOSMetaClass7()
321 { panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 7); }
324 /*********************************************************************
325 *********************************************************************/
327 OSMetaClassLogErrorForKext(
331 const char * message
= NULL
;
334 case kOSReturnSuccess
:
336 case kOSMetaClassNoInit
: // xxx - never returned; logged at fail site
337 message
= "OSMetaClass: preModLoad() wasn't called (runtime internal error).";
339 case kOSMetaClassNoDicts
:
340 message
= "OSMetaClass: Allocation failure for OSMetaClass internal dictionaries.";
342 case kOSMetaClassNoKModSet
:
343 message
= "OSMetaClass: Allocation failure for internal kext recording set/set missing.";
345 case kOSMetaClassNoInsKModSet
:
346 message
= "OSMetaClass: Failed to record class in kext.";
348 case kOSMetaClassDuplicateClass
:
349 message
= "OSMetaClass: Duplicate class encountered.";
351 case kOSMetaClassNoSuper
: // xxx - never returned
352 message
= "OSMetaClass: Can't associate a class with its superclass.";
354 case kOSMetaClassInstNoSuper
: // xxx - never returned
355 message
= "OSMetaClass: Instance construction error; unknown superclass.";
357 case kOSMetaClassNoKext
:
358 message
= "OSMetaClass: Kext not found for metaclass.";
360 case kOSMetaClassInternal
:
362 message
= "OSMetaClass: Runtime internal error.";
367 OSKextLog(aKext
, kOSMetaClassLogSpec
, "%s", message
);
373 OSMetaClass::logError(OSReturn error
)
375 OSMetaClassLogErrorForKext(error
, NULL
);
378 /*********************************************************************
379 * The core constructor for a MetaClass (defined with this name always
380 * but within the scope of its represented class).
382 * MetaClass constructors are invoked in OSRuntimeInitializeCPP(),
383 * in between calls to OSMetaClass::preModLoad(), which sets up for
384 * registration, and OSMetaClass::postModLoad(), which actually
385 * records all the class/kext relationships of the new MetaClasses.
386 *********************************************************************/
387 OSMetaClass::OSMetaClass(
388 const char * inClassName
,
389 const OSMetaClass
* inSuperClass
,
390 unsigned int inClassSize
)
393 classSize
= inClassSize
;
394 superClassLink
= inSuperClass
;
396 reserved
= IONew(ExpansionData
, 1);
397 bzero(reserved
, sizeof(ExpansionData
));
399 /* Hack alert: We are just casting inClassName and storing it in
400 * an OSString * instance variable. This may be because you can't
401 * create C++ objects in static constructors, but I really don't know!
403 className
= (const OSSymbol
*)inClassName
;
405 // sStalledClassesLock taken in preModLoad
407 /* There's no way we can look up the kext here, unfortunately.
409 OSKextLog(/* kext */ NULL
, kOSMetaClassLogSpec
,
410 "OSMetaClass: preModLoad() wasn't called for class %s "
411 "(runtime internal error).",
413 } else if (!sStalled
->result
) {
414 // Grow stalled array if neccessary
415 if (sStalled
->count
>= sStalled
->capacity
) {
416 OSMetaClass
**oldStalled
= sStalled
->classes
;
417 int oldSize
= sStalled
->capacity
* sizeof(OSMetaClass
*);
418 int newSize
= oldSize
419 + kKModCapacityIncrement
* sizeof(OSMetaClass
*);
421 sStalled
->classes
= (OSMetaClass
**)kalloc(newSize
);
422 if (!sStalled
->classes
) {
423 sStalled
->classes
= oldStalled
;
424 sStalled
->result
= kOSMetaClassNoTempData
;
428 sStalled
->capacity
+= kKModCapacityIncrement
;
429 memmove(sStalled
->classes
, oldStalled
, oldSize
);
430 kfree(oldStalled
, oldSize
);
431 ACCUMSIZE(newSize
- oldSize
);
434 sStalled
->classes
[sStalled
->count
++] = this;
438 /*********************************************************************
439 *********************************************************************/
440 OSMetaClass::~OSMetaClass()
442 OSKext
* myKext
= reserved
? reserved
->kext
: 0; // do not release
444 /* Hack alert: 'className' is a C string during early C++ init, and
445 * is converted to a real OSSymbol only when we record the OSKext in
446 * OSMetaClass::postModLoad(). So only do this bit if we have an OSKext.
447 * We can't safely cast or check 'className'.
449 * Also, release className *after* calling into the kext,
450 * as removeClass() may access className.
452 IOLockLock(sAllClassesLock
);
453 if (sAllClassesDict
) {
455 sAllClassesDict
->removeObject(className
);
457 sAllClassesDict
->removeObject((char *)className
);
460 IOLockUnlock(sAllClassesLock
);
463 if (myKext
->removeClass(this) != kOSReturnSuccess
) {
464 // xxx - what can we do?
466 className
->release();
469 // sStalledClassesLock taken in preModLoad
473 /* First pass find class in stalled list. If we find it that means
474 * we started C++ init with constructors but now we're tearing down
475 * because of some failure.
477 for (i
= 0; i
< sStalled
->count
; i
++) {
478 if (this == sStalled
->classes
[i
]) {
483 /* Remove this metaclass from the stalled list so postModLoad() doesn't
484 * try to register it.
486 if (i
< sStalled
->count
) {
488 if (i
< sStalled
->count
) {
489 memmove(&sStalled
->classes
[i
], &sStalled
->classes
[i
+1],
490 (sStalled
->count
- i
) * sizeof(OSMetaClass
*));
496 /*********************************************************************
498 *********************************************************************/
499 void * OSMetaClass::operator new(__unused
size_t size
) { return 0; }
500 void OSMetaClass::retain() const { }
501 void OSMetaClass::release() const { }
502 void OSMetaClass::release(__unused
int when
) const { }
503 void OSMetaClass::taggedRetain(__unused
const void * tag
) const { }
504 void OSMetaClass::taggedRelease(__unused
const void * tag
) const { }
505 void OSMetaClass::taggedRelease(__unused
const void * tag
, __unused
const int when
) const { }
506 int OSMetaClass::getRetainCount() const { return 0; }
508 /*********************************************************************
509 *********************************************************************/
511 OSMetaClass::getClassName() const
513 if (!className
) return NULL
;
514 return className
->getCStringNoCopy();
516 /*********************************************************************
517 *********************************************************************/
519 OSMetaClass::getClassNameSymbol() const
523 /*********************************************************************
524 *********************************************************************/
526 OSMetaClass::getClassSize() const
531 /*********************************************************************
532 *********************************************************************/
534 OSMetaClass::preModLoad(const char * kextIdentifier
)
536 IOLockLock(sStalledClassesLock
);
538 assert (sStalled
== NULL
);
539 sStalled
= (StalledData
*)kalloc(sizeof(* sStalled
));
541 sStalled
->classes
= (OSMetaClass
**)
542 kalloc(kKModCapacityIncrement
* sizeof(OSMetaClass
*));
543 if (!sStalled
->classes
) {
544 kfree(sStalled
, sizeof(*sStalled
));
547 ACCUMSIZE((kKModCapacityIncrement
* sizeof(OSMetaClass
*)) +
550 sStalled
->result
= kOSReturnSuccess
;
551 sStalled
->capacity
= kKModCapacityIncrement
;
553 sStalled
->kextIdentifier
= kextIdentifier
;
554 bzero(sStalled
->classes
, kKModCapacityIncrement
* sizeof(OSMetaClass
*));
557 // keep sStalledClassesLock locked until postModLoad
562 /*********************************************************************
563 *********************************************************************/
565 OSMetaClass::checkModLoad(void * loadHandle
)
567 return sStalled
&& loadHandle
== sStalled
&&
568 sStalled
->result
== kOSReturnSuccess
;
571 /*********************************************************************
572 *********************************************************************/
574 OSMetaClass::postModLoad(void * loadHandle
)
576 OSReturn result
= kOSReturnSuccess
;
577 OSSymbol
* myKextName
= 0; // must release
578 OSKext
* myKext
= 0; // must release
580 if (!sStalled
|| loadHandle
!= sStalled
) {
581 result
= kOSMetaClassInternal
;
585 if (sStalled
->result
) {
586 result
= sStalled
->result
;
587 } else switch (sBootstrapState
) {
589 case kNoDictionaries
:
590 sBootstrapState
= kMakingDictionaries
;
591 // No break; fall through
593 case kMakingDictionaries
:
594 sAllClassesDict
= OSDictionary::withCapacity(kClassCapacityIncrement
);
595 if (!sAllClassesDict
) {
596 result
= kOSMetaClassNoDicts
;
599 sAllClassesDict
->setOptions(OSCollection::kSort
, OSCollection::kSort
);
601 // No break; fall through
603 case kCompletedBootstrap
:
606 myKextName
= const_cast<OSSymbol
*>(OSSymbol::withCStringNoCopy(
607 sStalled
->kextIdentifier
));
609 if (!sStalled
->count
) {
610 break; // Nothing to do so just get out
613 myKext
= OSKext::lookupKextWithIdentifier(myKextName
);
615 result
= kOSMetaClassNoKext
;
617 /* Log this error here so we can include the kext name.
619 OSKextLog(/* kext */ NULL
, kOSMetaClassLogSpec
,
620 "OSMetaClass: Can't record classes for kext %s - kext not found.",
621 sStalled
->kextIdentifier
);
625 /* First pass checking classes aren't already loaded. If any already
626 * exist, we don't register any, and so we don't technically have
627 * to do any C++ teardown.
629 * Hack alert: me->className has been a C string until now.
630 * We only release the OSSymbol if we store the kext.
632 IOLockLock(sAllClassesLock
);
633 for (i
= 0; i
< sStalled
->count
; i
++) {
634 const OSMetaClass
* me
= sStalled
->classes
[i
];
635 OSMetaClass
* orig
= OSDynamicCast(OSMetaClass
,
636 sAllClassesDict
->getObject((const char *)me
->className
));
640 /* Log this error here so we can include the class name.
641 * xxx - we should look up the other kext that defines the class
643 OSKextLog(myKext
, kOSMetaClassLogSpec
,
644 "OSMetaClass: Kext %s class %s is a duplicate;"
645 "kext %s already has a class by that name.",
646 sStalled
->kextIdentifier
, (const char *)me
->className
,
647 ((OSKext
*)orig
->reserved
->kext
)->getIdentifierCString());
648 result
= kOSMetaClassDuplicateClass
;
651 unsigned int depth
= 1;
652 while ((me
= me
->superClassLink
)) depth
++;
653 if (depth
> sDeepestClass
) sDeepestClass
= depth
;
655 IOLockUnlock(sAllClassesLock
);
657 /* Bail if we didn't go through the entire list of new classes
658 * (if we hit a duplicate).
660 if (i
!= sStalled
->count
) {
664 // Second pass symbolling strings and inserting classes in dictionary
665 IOLockLock(sAllClassesLock
);
666 for (i
= 0; i
< sStalled
->count
; i
++) {
667 OSMetaClass
* me
= sStalled
->classes
[i
];
669 /* Hack alert: me->className has been a C string until now.
670 * We only release the OSSymbol in ~OSMetaClass()
671 * if we set the reference to the kext.
674 OSSymbol::withCStringNoCopy((const char *)me
->className
);
676 // xxx - I suppose if these fail we're going to panic soon....
677 sAllClassesDict
->setObject(me
->className
, me
);
679 /* Do not retain the kext object here.
681 me
->reserved
->kext
= myKext
;
683 result
= myKext
->addClass(me
, sStalled
->count
);
684 if (result
!= kOSReturnSuccess
) {
685 /* OSKext::addClass() logs with kOSMetaClassNoInsKModSet. */
690 IOLockUnlock(sAllClassesLock
);
691 sBootstrapState
= kCompletedBootstrap
;
696 result
= kOSMetaClassInternal
;
701 /* Don't call logError() for success or the conditions logged above
702 * or by called function.
704 if (result
!= kOSReturnSuccess
&&
705 result
!= kOSMetaClassNoInsKModSet
&&
706 result
!= kOSMetaClassDuplicateClass
&&
707 result
!= kOSMetaClassNoKext
) {
709 OSMetaClassLogErrorForKext(result
, myKext
);
712 OSSafeRelease(myKextName
);
713 OSSafeRelease(myKext
);
716 ACCUMSIZE(-(sStalled
->capacity
* sizeof(OSMetaClass
*) +
718 kfree(sStalled
->classes
, sStalled
->capacity
* sizeof(OSMetaClass
*));
719 kfree(sStalled
, sizeof(*sStalled
));
723 IOLockUnlock(sStalledClassesLock
);
729 /*********************************************************************
730 *********************************************************************/
732 OSMetaClass::instanceConstructed() const
734 // if ((0 == OSIncrementAtomic(&(((OSMetaClass *) this)->instanceCount))) && superClassLink)
735 if ((0 == OSIncrementAtomic(&instanceCount
)) && superClassLink
) {
736 superClassLink
->instanceConstructed();
740 /*********************************************************************
741 *********************************************************************/
743 OSMetaClass::instanceDestructed() const
745 if ((1 == OSDecrementAtomic(&instanceCount
)) && superClassLink
) {
746 superClassLink
->instanceDestructed();
749 if (((int)instanceCount
) < 0) {
750 OSKext
* myKext
= reserved
->kext
;
752 OSKextLog(myKext
, kOSMetaClassLogSpec
,
753 // xxx - this phrasing is rather cryptic
754 "OSMetaClass: Class %s - bad retain (%d)",
755 getClassName(), instanceCount
);
759 /*********************************************************************
760 *********************************************************************/
762 OSMetaClass::modHasInstance(const char * kextIdentifier
)
765 OSKext
* theKext
= NULL
; // must release
767 theKext
= OSKext::lookupKextWithIdentifier(kextIdentifier
);
772 result
= theKext
->hasOSMetaClassInstances();
775 OSSafeRelease(theKext
);
779 /*********************************************************************
780 *********************************************************************/
782 OSMetaClass::reportModInstances(const char * kextIdentifier
)
784 OSKext::reportOSMetaClassInstances(kextIdentifier
,
785 kOSKextLogExplicitLevel
);
788 /*********************************************************************
789 *********************************************************************/
792 OSMetaClass::addInstance(const OSObject
* instance
, bool super
) const
794 if (!super
) IOLockLock(sInstancesLock
);
796 if (!reserved
->instances
) {
797 reserved
->instances
= OSOrderedSet::withCapacity(16);
798 if (superClassLink
) {
799 superClassLink
->addInstance(reserved
->instances
, true);
802 reserved
->instances
->setLastObject(instance
);
804 if (!super
) IOLockUnlock(sInstancesLock
);
808 OSMetaClass::removeInstance(const OSObject
* instance
, bool super
) const
810 if (!super
) IOLockLock(sInstancesLock
);
812 if (reserved
->instances
) {
813 reserved
->instances
->removeObject(instance
);
814 if (0 == reserved
->instances
->getCount()) {
815 if (superClassLink
) {
816 superClassLink
->removeInstance(reserved
->instances
, true);
818 reserved
->instances
->release();
819 reserved
->instances
= 0;
823 if (!super
) IOLockUnlock(sInstancesLock
);
827 OSMetaClass::applyToInstances(OSOrderedSet
* set
,
828 OSMetaClassInstanceApplierFunction applier
,
831 enum { kLocalDepth
= 24 };
832 unsigned int _nextIndex
[kLocalDepth
];
833 OSOrderedSet
* _sets
[kLocalDepth
];
834 unsigned int * nextIndex
= &_nextIndex
[0];
835 OSOrderedSet
** sets
= &_sets
[0];
837 OSOrderedSet
* childSet
;
838 unsigned int maxDepth
;
843 maxDepth
= sDeepestClass
;
844 if (maxDepth
> kLocalDepth
)
846 nextIndex
= IONew(typeof(nextIndex
[0]), maxDepth
);
847 sets
= IONew(typeof(sets
[0]), maxDepth
);
854 while (!done
&& (obj
= set
->getObject(idx
++)))
856 if ((childSet
= OSDynamicCast(OSOrderedSet
, obj
)))
858 if (level
>= maxDepth
) panic(">maxDepth");
860 nextIndex
[level
] = idx
;
866 done
= (*applier
)(obj
, context
);
874 idx
= nextIndex
[level
];
879 if (maxDepth
> kLocalDepth
)
881 IODelete(nextIndex
, typeof(nextIndex
[0]), maxDepth
);
882 IODelete(sets
, typeof(sets
[0]), maxDepth
);
887 OSMetaClass::applyToInstances(OSMetaClassInstanceApplierFunction applier
,
888 void * context
) const
890 IOLockLock(sInstancesLock
);
891 if (reserved
->instances
) applyToInstances(reserved
->instances
, applier
, context
);
892 IOLockUnlock(sInstancesLock
);
896 OSMetaClass::applyToInstancesOfClassName(
897 const OSSymbol
* name
,
898 OSMetaClassInstanceApplierFunction applier
,
902 OSOrderedSet
* set
= 0;
904 IOLockLock(sAllClassesLock
);
906 && (meta
= (OSMetaClass
*) sAllClassesDict
->getObject(name
))
907 && (set
= meta
->reserved
->instances
))
911 IOLockUnlock(sAllClassesLock
);
915 IOLockLock(sInstancesLock
);
916 applyToInstances(set
, applier
, context
);
917 IOLockUnlock(sInstancesLock
);
921 /*********************************************************************
922 *********************************************************************/
924 OSMetaClass::considerUnloads()
926 OSKext::considerUnloads();
929 /*********************************************************************
930 *********************************************************************/
932 OSMetaClass::getMetaClassWithName(const OSSymbol
* name
)
934 OSMetaClass
* retMeta
= 0;
940 IOLockLock(sAllClassesLock
);
941 if (sAllClassesDict
) {
942 retMeta
= (OSMetaClass
*) sAllClassesDict
->getObject(name
);
944 IOLockUnlock(sAllClassesLock
);
949 /*********************************************************************
950 *********************************************************************/
952 OSMetaClass::allocClassWithName(const OSSymbol
* name
)
954 OSObject
* result
= 0;
956 const OSMetaClass
* const meta
= getMetaClassWithName(name
);
959 result
= meta
->alloc();
965 /*********************************************************************
966 *********************************************************************/
968 OSMetaClass::allocClassWithName(const OSString
* name
)
970 const OSSymbol
* tmpKey
= OSSymbol::withString(name
);
971 OSObject
* result
= allocClassWithName(tmpKey
);
976 /*********************************************************************
977 *********************************************************************/
979 OSMetaClass::allocClassWithName(const char * name
)
981 const OSSymbol
* tmpKey
= OSSymbol::withCStringNoCopy(name
);
982 OSObject
* result
= allocClassWithName(tmpKey
);
988 /*********************************************************************
989 *********************************************************************/
991 OSMetaClass::checkMetaCastWithName(
992 const OSSymbol
* name
,
993 const OSMetaClassBase
* in
)
995 OSMetaClassBase
* result
= 0;
997 const OSMetaClass
* const meta
= getMetaClassWithName(name
);
1000 result
= meta
->checkMetaCast(in
);
1006 /*********************************************************************
1007 *********************************************************************/
1008 OSMetaClassBase
* OSMetaClass::
1009 checkMetaCastWithName(
1010 const OSString
* name
,
1011 const OSMetaClassBase
* in
)
1013 const OSSymbol
* tmpKey
= OSSymbol::withString(name
);
1014 OSMetaClassBase
* result
= checkMetaCastWithName(tmpKey
, in
);
1020 /*********************************************************************
1021 *********************************************************************/
1023 OSMetaClass::checkMetaCastWithName(
1025 const OSMetaClassBase
* in
)
1027 const OSSymbol
* tmpKey
= OSSymbol::withCStringNoCopy(name
);
1028 OSMetaClassBase
* result
= checkMetaCastWithName(tmpKey
, in
);
1034 /*********************************************************************
1035 * OSMetaClass::checkMetaCast()
1036 * Check to see if the 'check' object has this object in its metaclass chain.
1037 * Returns check if it is indeed a kind of the current meta class, 0 otherwise.
1039 * Generally this method is not invoked directly but is used to implement
1040 * the OSMetaClassBase::metaCast member function.
1042 * See also OSMetaClassBase::metaCast
1043 *********************************************************************/
1044 OSMetaClassBase
* OSMetaClass::checkMetaCast(
1045 const OSMetaClassBase
* check
) const
1047 const OSMetaClass
* const toMeta
= this;
1048 const OSMetaClass
* fromMeta
;
1050 for (fromMeta
= check
->getMetaClass(); ; fromMeta
= fromMeta
->superClassLink
) {
1051 if (toMeta
== fromMeta
) {
1052 return const_cast<OSMetaClassBase
*>(check
); // Discard const
1054 if (!fromMeta
->superClassLink
) {
1062 /*********************************************************************
1063 *********************************************************************/
1065 OSMetaClass::reservedCalled(int ind
) const
1067 const char * cname
= className
->getCStringNoCopy();
1068 panic("%s::_RESERVED%s%d called.", cname
, cname
, ind
);
1071 /*********************************************************************
1072 *********************************************************************/
1075 OSMetaClass::getSuperClass() const
1077 return superClassLink
;
1080 /*********************************************************************
1081 * xxx - I want to rename this :-/
1082 *********************************************************************/
1084 OSMetaClass::getKmodName() const
1086 OSKext
* myKext
= reserved
? reserved
->kext
: 0;
1088 return myKext
->getIdentifier();
1090 return OSSymbol::withCStringNoCopy("unknown");
1093 /*********************************************************************
1094 *********************************************************************/
1096 OSMetaClass::getInstanceCount() const
1098 return instanceCount
;
1101 /*********************************************************************
1102 *********************************************************************/
1105 OSMetaClass::printInstanceCounts()
1107 OSCollectionIterator
* classes
;
1108 OSSymbol
* className
;
1111 IOLockLock(sAllClassesLock
);
1112 classes
= OSCollectionIterator::withCollection(sAllClassesDict
);
1115 while( (className
= (OSSymbol
*)classes
->getNextObject())) {
1116 meta
= (OSMetaClass
*)sAllClassesDict
->getObject(className
);
1119 printf("%24s count: %03d x 0x%03x = 0x%06x\n",
1120 className
->getCStringNoCopy(),
1121 meta
->getInstanceCount(),
1122 meta
->getClassSize(),
1123 meta
->getInstanceCount() * meta
->getClassSize() );
1127 IOLockUnlock(sAllClassesLock
);
1131 /*********************************************************************
1132 *********************************************************************/
1134 OSMetaClass::getClassDictionary()
1136 panic("OSMetaClass::getClassDictionary() is obsoleted.\n");
1140 /*********************************************************************
1141 *********************************************************************/
1143 OSMetaClass::serialize(__unused OSSerialize
* s
) const
1145 panic("OSMetaClass::serialize(): Obsoleted\n");
1149 /*********************************************************************
1150 *********************************************************************/
1153 OSMetaClass::serializeClassDictionary(OSDictionary
* serializeDictionary
)
1155 OSDictionary
* classDict
= NULL
;
1157 IOLockLock(sAllClassesLock
);
1159 classDict
= OSDictionary::withCapacity(sAllClassesDict
->getCount());
1165 OSCollectionIterator
* classes
;
1166 const OSSymbol
* className
;
1168 classes
= OSCollectionIterator::withCollection(sAllClassesDict
);
1173 while ((className
= (const OSSymbol
*)classes
->getNextObject())) {
1174 const OSMetaClass
* meta
;
1177 meta
= (OSMetaClass
*)sAllClassesDict
->getObject(className
);
1178 count
= OSNumber::withNumber(meta
->getInstanceCount(), 32);
1180 classDict
->setObject(className
, count
);
1186 serializeDictionary
->setObject("Classes", classDict
);
1190 OSSafeRelease(classDict
);
1192 IOLockUnlock(sAllClassesLock
);