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>
53 #include <sys/systm.h>
54 #include <mach/mach_types.h>
55 #include <kern/locks.h>
56 #include <kern/clock.h>
57 #include <kern/thread_call.h>
58 #include <kern/host.h>
59 #include <mach/mach_interface.h>
63 #endif /* PRAGMA_MARK */
64 /*********************************************************************
66 *********************************************************************/
68 extern int debug_container_malloc_size
;
69 #define ACCUMSIZE(s) do { debug_container_malloc_size += (s); } while (0)
72 #endif /* OSALLOCDEBUG */
77 #pragma mark Internal constants & data structs
78 #endif /* PRAGMA_MARK */
79 /*********************************************************************
80 * Internal constants & data structs
81 *********************************************************************/
82 OSKextLogSpec kOSMetaClassLogSpec
=
83 kOSKextLogErrorLevel
|
85 kOSKextLogKextBookkeepingFlag
;
88 kCompletedBootstrap
= 0,
90 kMakingDictionaries
= 2
91 } sBootstrapState
= kNoDictionaries
;
93 static const int kClassCapacityIncrement
= 40;
94 static const int kKModCapacityIncrement
= 10;
95 static OSDictionary
* sAllClassesDict
;
96 static unsigned int sDeepestClass
;
97 IOLock
* sAllClassesLock
= NULL
;
98 IOLock
* sInstancesLock
= NULL
;
101 * While loading a kext and running all its constructors to register
102 * all OSMetaClass classes, the classes are queued up here. Only one
103 * kext can be in flight at a time, guarded by sStalledClassesLock
105 static struct StalledData
{
106 const char * kextIdentifier
;
108 unsigned int capacity
;
110 OSMetaClass
** classes
;
112 IOLock
* sStalledClassesLock
= NULL
;
115 struct ExpansionData
{
116 OSOrderedSet
* instances
;
122 #pragma mark OSMetaClassBase
123 #endif /* PRAGMA_MARK */
124 /*********************************************************************
126 *********************************************************************/
128 #if APPLE_KEXT_VTABLE_PADDING
129 /*********************************************************************
130 * Reserved vtable functions.
131 *********************************************************************/
133 void OSMetaClassBase::_RESERVEDOSMetaClassBase0()
134 { panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 0); }
135 void OSMetaClassBase::_RESERVEDOSMetaClassBase1()
136 { panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 1); }
137 void OSMetaClassBase::_RESERVEDOSMetaClassBase2()
138 { panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 2); }
139 #endif /* SLOT_USED */
141 // As these slots are used move them up inside the #if above
142 void OSMetaClassBase::_RESERVEDOSMetaClassBase3()
143 { panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 3); }
144 void OSMetaClassBase::_RESERVEDOSMetaClassBase4()
145 { panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 4); }
146 void OSMetaClassBase::_RESERVEDOSMetaClassBase5()
147 { panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 5); }
148 void OSMetaClassBase::_RESERVEDOSMetaClassBase6()
149 { panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 6); }
152 /*********************************************************************
153 * These used to be inline in the header but gcc didn't believe us
154 * Now we MUST pull the inline out at least until the compiler is
157 * Helper inlines for runtime type preprocessor macros
158 *********************************************************************/
160 /*********************************************************************
161 *********************************************************************/
163 OSMetaClassBase::safeMetaCast(
164 const OSMetaClassBase
* me
,
165 const OSMetaClass
* toType
)
167 return (me
)? me
->metaCast(toType
) : 0;
170 /*********************************************************************
171 *********************************************************************/
173 OSMetaClassBase::checkTypeInst(
174 const OSMetaClassBase
* inst
,
175 const OSMetaClassBase
* typeinst
)
177 const OSMetaClass
* toType
= OSTypeIDInst(typeinst
);
178 return typeinst
&& inst
&& (0 != inst
->metaCast(toType
));
181 /*********************************************************************
182 *********************************************************************/
183 void OSMetaClassBase::
186 sAllClassesLock
= IOLockAlloc();
187 sStalledClassesLock
= IOLockAlloc();
188 sInstancesLock
= IOLockAlloc();
191 #if APPLE_KEXT_VTABLE_PADDING
192 /*********************************************************************
193 * If you need this slot you had better setup an IOCTL style interface.
194 * 'Cause the whole kernel world depends on OSMetaClassBase and YOU
195 * CANT change the VTABLE size ever.
196 *********************************************************************/
198 OSMetaClassBase::_RESERVEDOSMetaClassBase7()
199 { panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 7); }
202 /*********************************************************************
203 *********************************************************************/
204 OSMetaClassBase::OSMetaClassBase()
208 /*********************************************************************
209 *********************************************************************/
210 OSMetaClassBase::~OSMetaClassBase()
214 thisVTable
= (void **) this;
215 *thisVTable
= (void *) -1UL;
218 /*********************************************************************
219 *********************************************************************/
221 OSMetaClassBase::isEqualTo(const OSMetaClassBase
* anObj
) const
223 return this == anObj
;
226 /*********************************************************************
227 *********************************************************************/
229 OSMetaClassBase::metaCast(const OSMetaClass
* toMeta
) const
231 return toMeta
->checkMetaCast(this);
234 /*********************************************************************
235 *********************************************************************/
237 OSMetaClassBase::metaCast(const OSSymbol
* toMetaSymb
) const
239 return OSMetaClass::checkMetaCastWithName(toMetaSymb
, this);
242 /*********************************************************************
243 *********************************************************************/
245 OSMetaClassBase::metaCast(const OSString
* toMetaStr
) const
247 const OSSymbol
* tempSymb
= OSSymbol::withString(toMetaStr
);
248 OSMetaClassBase
* ret
= 0;
250 ret
= metaCast(tempSymb
);
256 /*********************************************************************
257 *********************************************************************/
259 OSMetaClassBase::metaCast(const char * toMetaCStr
) const
261 const OSSymbol
* tempSymb
= OSSymbol::withCString(toMetaCStr
);
262 OSMetaClassBase
* ret
= 0;
264 ret
= metaCast(tempSymb
);
271 #pragma mark OSMetaClassMeta
272 #endif /* PRAGMA_MARK */
273 /*********************************************************************
274 * OSMetaClassMeta - the bootstrap metaclass of OSMetaClass
275 *********************************************************************/
276 class OSMetaClassMeta
: public OSMetaClass
280 OSObject
* alloc() const;
282 OSMetaClassMeta::OSMetaClassMeta()
283 : OSMetaClass("OSMetaClass", 0, sizeof(OSMetaClass
))
285 OSObject
* OSMetaClassMeta::alloc() const { return 0; }
287 static OSMetaClassMeta sOSMetaClassMeta
;
289 const OSMetaClass
* const OSMetaClass::metaClass
= &sOSMetaClassMeta
;
290 const OSMetaClass
* OSMetaClass::getMetaClass() const
291 { return &sOSMetaClassMeta
; }
294 #pragma mark OSMetaClass
295 #endif /* PRAGMA_MARK */
296 /*********************************************************************
298 *********************************************************************/
300 #if APPLE_KEXT_VTABLE_PADDING
301 /*********************************************************************
302 * Reserved functions.
303 *********************************************************************/
304 void OSMetaClass::_RESERVEDOSMetaClass0()
305 { panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 0); }
306 void OSMetaClass::_RESERVEDOSMetaClass1()
307 { panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 1); }
308 void OSMetaClass::_RESERVEDOSMetaClass2()
309 { panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 2); }
310 void OSMetaClass::_RESERVEDOSMetaClass3()
311 { panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 3); }
312 void OSMetaClass::_RESERVEDOSMetaClass4()
313 { panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 4); }
314 void OSMetaClass::_RESERVEDOSMetaClass5()
315 { panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 5); }
316 void OSMetaClass::_RESERVEDOSMetaClass6()
317 { panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 6); }
318 void OSMetaClass::_RESERVEDOSMetaClass7()
319 { panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 7); }
322 /*********************************************************************
323 *********************************************************************/
325 OSMetaClassLogErrorForKext(
329 const char * message
= NULL
;
332 case kOSReturnSuccess
:
334 case kOSMetaClassNoInit
: // xxx - never returned; logged at fail site
335 message
= "OSMetaClass: preModLoad() wasn't called (runtime internal error).";
337 case kOSMetaClassNoDicts
:
338 message
= "OSMetaClass: Allocation failure for OSMetaClass internal dictionaries.";
340 case kOSMetaClassNoKModSet
:
341 message
= "OSMetaClass: Allocation failure for internal kext recording set/set missing.";
343 case kOSMetaClassNoInsKModSet
:
344 message
= "OSMetaClass: Failed to record class in kext.";
346 case kOSMetaClassDuplicateClass
:
347 message
= "OSMetaClass: Duplicate class encountered.";
349 case kOSMetaClassNoSuper
: // xxx - never returned
350 message
= "OSMetaClass: Can't associate a class with its superclass.";
352 case kOSMetaClassInstNoSuper
: // xxx - never returned
353 message
= "OSMetaClass: Instance construction error; unknown superclass.";
355 case kOSMetaClassNoKext
:
356 message
= "OSMetaClass: Kext not found for metaclass.";
358 case kOSMetaClassInternal
:
360 message
= "OSMetaClass: Runtime internal error.";
365 OSKextLog(aKext
, kOSMetaClassLogSpec
, "%s", message
);
371 OSMetaClass::logError(OSReturn error
)
373 OSMetaClassLogErrorForKext(error
, NULL
);
376 /*********************************************************************
377 * The core constructor for a MetaClass (defined with this name always
378 * but within the scope of its represented class).
380 * MetaClass constructors are invoked in OSRuntimeInitializeCPP(),
381 * in between calls to OSMetaClass::preModLoad(), which sets up for
382 * registration, and OSMetaClass::postModLoad(), which actually
383 * records all the class/kext relationships of the new MetaClasses.
384 *********************************************************************/
385 OSMetaClass::OSMetaClass(
386 const char * inClassName
,
387 const OSMetaClass
* inSuperClass
,
388 unsigned int inClassSize
)
391 classSize
= inClassSize
;
392 superClassLink
= inSuperClass
;
394 reserved
= IONew(ExpansionData
, 1);
395 bzero(reserved
, sizeof(ExpansionData
));
397 /* Hack alert: We are just casting inClassName and storing it in
398 * an OSString * instance variable. This may be because you can't
399 * create C++ objects in static constructors, but I really don't know!
401 className
= (const OSSymbol
*)inClassName
;
403 // sStalledClassesLock taken in preModLoad
405 /* There's no way we can look up the kext here, unfortunately.
407 OSKextLog(/* kext */ NULL
, kOSMetaClassLogSpec
,
408 "OSMetaClass: preModLoad() wasn't called for class %s "
409 "(runtime internal error).",
411 } else if (!sStalled
->result
) {
412 // Grow stalled array if neccessary
413 if (sStalled
->count
>= sStalled
->capacity
) {
414 OSMetaClass
**oldStalled
= sStalled
->classes
;
415 int oldSize
= sStalled
->capacity
* sizeof(OSMetaClass
*);
416 int newSize
= oldSize
417 + kKModCapacityIncrement
* sizeof(OSMetaClass
*);
419 sStalled
->classes
= (OSMetaClass
**)kalloc(newSize
);
420 if (!sStalled
->classes
) {
421 sStalled
->classes
= oldStalled
;
422 sStalled
->result
= kOSMetaClassNoTempData
;
426 sStalled
->capacity
+= kKModCapacityIncrement
;
427 memmove(sStalled
->classes
, oldStalled
, oldSize
);
428 kfree(oldStalled
, oldSize
);
429 ACCUMSIZE(newSize
- oldSize
);
432 sStalled
->classes
[sStalled
->count
++] = this;
436 /*********************************************************************
437 *********************************************************************/
438 OSMetaClass::~OSMetaClass()
440 OSKext
* myKext
= reserved
? reserved
->kext
: 0; // do not release
442 /* Hack alert: 'className' is a C string during early C++ init, and
443 * is converted to a real OSSymbol only when we record the OSKext in
444 * OSMetaClass::postModLoad(). So only do this bit if we have an OSKext.
445 * We can't safely cast or check 'className'.
447 * Also, release className *after* calling into the kext,
448 * as removeClass() may access className.
450 IOLockLock(sAllClassesLock
);
451 if (sAllClassesDict
) {
453 sAllClassesDict
->removeObject(className
);
455 sAllClassesDict
->removeObject((char *)className
);
458 IOLockUnlock(sAllClassesLock
);
461 if (myKext
->removeClass(this) != kOSReturnSuccess
) {
462 // xxx - what can we do?
464 className
->release();
467 // sStalledClassesLock taken in preModLoad
471 /* First pass find class in stalled list. If we find it that means
472 * we started C++ init with constructors but now we're tearing down
473 * because of some failure.
475 for (i
= 0; i
< sStalled
->count
; i
++) {
476 if (this == sStalled
->classes
[i
]) {
481 /* Remove this metaclass from the stalled list so postModLoad() doesn't
482 * try to register it.
484 if (i
< sStalled
->count
) {
486 if (i
< sStalled
->count
) {
487 memmove(&sStalled
->classes
[i
], &sStalled
->classes
[i
+1],
488 (sStalled
->count
- i
) * sizeof(OSMetaClass
*));
494 /*********************************************************************
496 *********************************************************************/
497 void * OSMetaClass::operator new(__unused
size_t size
) { return 0; }
498 void OSMetaClass::retain() const { }
499 void OSMetaClass::release() const { }
500 void OSMetaClass::release(__unused
int when
) const { }
501 void OSMetaClass::taggedRetain(__unused
const void * tag
) const { }
502 void OSMetaClass::taggedRelease(__unused
const void * tag
) const { }
503 void OSMetaClass::taggedRelease(__unused
const void * tag
, __unused
const int when
) const { }
504 int OSMetaClass::getRetainCount() const { return 0; }
506 /*********************************************************************
507 *********************************************************************/
509 OSMetaClass::getClassName() const
511 if (!className
) return NULL
;
512 return className
->getCStringNoCopy();
514 /*********************************************************************
515 *********************************************************************/
517 OSMetaClass::getClassNameSymbol() const
521 /*********************************************************************
522 *********************************************************************/
524 OSMetaClass::getClassSize() const
529 /*********************************************************************
530 *********************************************************************/
532 OSMetaClass::preModLoad(const char * kextIdentifier
)
534 IOLockLock(sStalledClassesLock
);
536 assert (sStalled
== NULL
);
537 sStalled
= (StalledData
*)kalloc(sizeof(* sStalled
));
539 sStalled
->classes
= (OSMetaClass
**)
540 kalloc(kKModCapacityIncrement
* sizeof(OSMetaClass
*));
541 if (!sStalled
->classes
) {
542 kfree(sStalled
, sizeof(*sStalled
));
545 ACCUMSIZE((kKModCapacityIncrement
* sizeof(OSMetaClass
*)) +
548 sStalled
->result
= kOSReturnSuccess
;
549 sStalled
->capacity
= kKModCapacityIncrement
;
551 sStalled
->kextIdentifier
= kextIdentifier
;
552 bzero(sStalled
->classes
, kKModCapacityIncrement
* sizeof(OSMetaClass
*));
555 // keep sStalledClassesLock locked until postModLoad
560 /*********************************************************************
561 *********************************************************************/
563 OSMetaClass::checkModLoad(void * loadHandle
)
565 return sStalled
&& loadHandle
== sStalled
&&
566 sStalled
->result
== kOSReturnSuccess
;
569 /*********************************************************************
570 *********************************************************************/
572 OSMetaClass::postModLoad(void * loadHandle
)
574 OSReturn result
= kOSReturnSuccess
;
575 OSSymbol
* myKextName
= 0; // must release
576 OSKext
* myKext
= 0; // must release
578 if (!sStalled
|| loadHandle
!= sStalled
) {
579 result
= kOSMetaClassInternal
;
583 if (sStalled
->result
) {
584 result
= sStalled
->result
;
585 } else switch (sBootstrapState
) {
587 case kNoDictionaries
:
588 sBootstrapState
= kMakingDictionaries
;
589 // No break; fall through
591 case kMakingDictionaries
:
592 sAllClassesDict
= OSDictionary::withCapacity(kClassCapacityIncrement
);
593 if (!sAllClassesDict
) {
594 result
= kOSMetaClassNoDicts
;
597 sAllClassesDict
->setOptions(OSCollection::kSort
, OSCollection::kSort
);
599 // No break; fall through
601 case kCompletedBootstrap
:
604 myKextName
= const_cast<OSSymbol
*>(OSSymbol::withCStringNoCopy(
605 sStalled
->kextIdentifier
));
607 if (!sStalled
->count
) {
608 break; // Nothing to do so just get out
611 myKext
= OSKext::lookupKextWithIdentifier(myKextName
);
613 result
= kOSMetaClassNoKext
;
615 /* Log this error here so we can include the kext name.
617 OSKextLog(/* kext */ NULL
, kOSMetaClassLogSpec
,
618 "OSMetaClass: Can't record classes for kext %s - kext not found.",
619 sStalled
->kextIdentifier
);
623 /* First pass checking classes aren't already loaded. If any already
624 * exist, we don't register any, and so we don't technically have
625 * to do any C++ teardown.
627 * Hack alert: me->className has been a C string until now.
628 * We only release the OSSymbol if we store the kext.
630 IOLockLock(sAllClassesLock
);
631 for (i
= 0; i
< sStalled
->count
; i
++) {
632 const OSMetaClass
* me
= sStalled
->classes
[i
];
633 OSMetaClass
* orig
= OSDynamicCast(OSMetaClass
,
634 sAllClassesDict
->getObject((const char *)me
->className
));
638 /* Log this error here so we can include the class name.
639 * xxx - we should look up the other kext that defines the class
641 OSKextLog(myKext
, kOSMetaClassLogSpec
,
642 "OSMetaClass: Kext %s class %s is a duplicate;"
643 "kext %s already has a class by that name.",
644 sStalled
->kextIdentifier
, (const char *)me
->className
,
645 ((OSKext
*)orig
->reserved
->kext
)->getIdentifierCString());
646 result
= kOSMetaClassDuplicateClass
;
649 unsigned int depth
= 1;
650 while ((me
= me
->superClassLink
)) depth
++;
651 if (depth
> sDeepestClass
) sDeepestClass
= depth
;
653 IOLockUnlock(sAllClassesLock
);
655 /* Bail if we didn't go through the entire list of new classes
656 * (if we hit a duplicate).
658 if (i
!= sStalled
->count
) {
662 // Second pass symbolling strings and inserting classes in dictionary
663 IOLockLock(sAllClassesLock
);
664 for (i
= 0; i
< sStalled
->count
; i
++) {
665 OSMetaClass
* me
= sStalled
->classes
[i
];
667 /* Hack alert: me->className has been a C string until now.
668 * We only release the OSSymbol in ~OSMetaClass()
669 * if we set the reference to the kext.
672 OSSymbol::withCStringNoCopy((const char *)me
->className
);
674 // xxx - I suppose if these fail we're going to panic soon....
675 sAllClassesDict
->setObject(me
->className
, me
);
677 /* Do not retain the kext object here.
679 me
->reserved
->kext
= myKext
;
681 result
= myKext
->addClass(me
, sStalled
->count
);
682 if (result
!= kOSReturnSuccess
) {
683 /* OSKext::addClass() logs with kOSMetaClassNoInsKModSet. */
688 IOLockUnlock(sAllClassesLock
);
689 sBootstrapState
= kCompletedBootstrap
;
694 result
= kOSMetaClassInternal
;
699 /* Don't call logError() for success or the conditions logged above
700 * or by called function.
702 if (result
!= kOSReturnSuccess
&&
703 result
!= kOSMetaClassNoInsKModSet
&&
704 result
!= kOSMetaClassDuplicateClass
&&
705 result
!= kOSMetaClassNoKext
) {
707 OSMetaClassLogErrorForKext(result
, myKext
);
710 OSSafeRelease(myKextName
);
711 OSSafeRelease(myKext
);
714 ACCUMSIZE(-(sStalled
->capacity
* sizeof(OSMetaClass
*) +
716 kfree(sStalled
->classes
, sStalled
->capacity
* sizeof(OSMetaClass
*));
717 kfree(sStalled
, sizeof(*sStalled
));
721 IOLockUnlock(sStalledClassesLock
);
727 /*********************************************************************
728 *********************************************************************/
730 OSMetaClass::instanceConstructed() const
732 // if ((0 == OSIncrementAtomic(&(((OSMetaClass *) this)->instanceCount))) && superClassLink)
733 if ((0 == OSIncrementAtomic(&instanceCount
)) && superClassLink
) {
734 superClassLink
->instanceConstructed();
738 /*********************************************************************
739 *********************************************************************/
741 OSMetaClass::instanceDestructed() const
743 if ((1 == OSDecrementAtomic(&instanceCount
)) && superClassLink
) {
744 superClassLink
->instanceDestructed();
747 if (((int)instanceCount
) < 0) {
748 OSKext
* myKext
= reserved
->kext
;
750 OSKextLog(myKext
, kOSMetaClassLogSpec
,
751 // xxx - this phrasing is rather cryptic
752 "OSMetaClass: Class %s - bad retain (%d)",
753 getClassName(), instanceCount
);
757 /*********************************************************************
758 *********************************************************************/
760 OSMetaClass::modHasInstance(const char * kextIdentifier
)
763 OSKext
* theKext
= NULL
; // must release
765 theKext
= OSKext::lookupKextWithIdentifier(kextIdentifier
);
770 result
= theKext
->hasOSMetaClassInstances();
773 OSSafeRelease(theKext
);
777 /*********************************************************************
778 *********************************************************************/
780 OSMetaClass::reportModInstances(const char * kextIdentifier
)
782 OSKext::reportOSMetaClassInstances(kextIdentifier
,
783 kOSKextLogExplicitLevel
);
786 /*********************************************************************
787 *********************************************************************/
790 OSMetaClass::addInstance(const OSObject
* instance
, bool super
) const
792 if (!super
) IOLockLock(sInstancesLock
);
794 if (!reserved
->instances
) {
795 reserved
->instances
= OSOrderedSet::withCapacity(16);
796 if (superClassLink
) {
797 superClassLink
->addInstance(reserved
->instances
, true);
800 reserved
->instances
->setLastObject(instance
);
802 if (!super
) IOLockUnlock(sInstancesLock
);
806 OSMetaClass::removeInstance(const OSObject
* instance
, bool super
) const
808 if (!super
) IOLockLock(sInstancesLock
);
810 if (reserved
->instances
) {
811 reserved
->instances
->removeObject(instance
);
812 if (0 == reserved
->instances
->getCount()) {
813 if (superClassLink
) {
814 superClassLink
->removeInstance(reserved
->instances
, true);
816 reserved
->instances
->release();
817 reserved
->instances
= 0;
821 if (!super
) IOLockUnlock(sInstancesLock
);
825 OSMetaClass::applyToInstances(OSOrderedSet
* set
,
826 OSMetaClassInstanceApplierFunction applier
,
829 enum { kLocalDepth
= 24 };
830 unsigned int _nextIndex
[kLocalDepth
];
831 OSOrderedSet
* _sets
[kLocalDepth
];
832 unsigned int * nextIndex
= &_nextIndex
[0];
833 OSOrderedSet
** sets
= &_sets
[0];
835 OSOrderedSet
* childSet
;
836 unsigned int maxDepth
;
841 maxDepth
= sDeepestClass
;
842 if (maxDepth
> kLocalDepth
)
844 nextIndex
= IONew(typeof(nextIndex
[0]), maxDepth
);
845 sets
= IONew(typeof(sets
[0]), maxDepth
);
852 while (!done
&& (obj
= set
->getObject(idx
++)))
854 if ((childSet
= OSDynamicCast(OSOrderedSet
, obj
)))
856 if (level
>= maxDepth
) panic(">maxDepth");
858 nextIndex
[level
] = idx
;
864 done
= (*applier
)(obj
, context
);
872 idx
= nextIndex
[level
];
877 if (maxDepth
> kLocalDepth
)
879 IODelete(nextIndex
, typeof(nextIndex
[0]), maxDepth
);
880 IODelete(sets
, typeof(sets
[0]), maxDepth
);
885 OSMetaClass::applyToInstances(OSMetaClassInstanceApplierFunction applier
,
886 void * context
) const
888 IOLockLock(sInstancesLock
);
889 if (reserved
->instances
) applyToInstances(reserved
->instances
, applier
, context
);
890 IOLockUnlock(sInstancesLock
);
894 OSMetaClass::applyToInstancesOfClassName(
895 const OSSymbol
* name
,
896 OSMetaClassInstanceApplierFunction applier
,
900 OSOrderedSet
* set
= 0;
902 IOLockLock(sAllClassesLock
);
904 && (meta
= (OSMetaClass
*) sAllClassesDict
->getObject(name
))
905 && (set
= meta
->reserved
->instances
))
909 IOLockUnlock(sAllClassesLock
);
913 IOLockLock(sInstancesLock
);
914 applyToInstances(set
, applier
, context
);
915 IOLockUnlock(sInstancesLock
);
919 /*********************************************************************
920 *********************************************************************/
922 OSMetaClass::considerUnloads()
924 OSKext::considerUnloads();
927 /*********************************************************************
928 *********************************************************************/
930 OSMetaClass::getMetaClassWithName(const OSSymbol
* name
)
932 OSMetaClass
* retMeta
= 0;
938 IOLockLock(sAllClassesLock
);
939 if (sAllClassesDict
) {
940 retMeta
= (OSMetaClass
*) sAllClassesDict
->getObject(name
);
942 IOLockUnlock(sAllClassesLock
);
947 /*********************************************************************
948 *********************************************************************/
950 OSMetaClass::allocClassWithName(const OSSymbol
* name
)
952 OSObject
* result
= 0;
954 const OSMetaClass
* const meta
= getMetaClassWithName(name
);
957 result
= meta
->alloc();
963 /*********************************************************************
964 *********************************************************************/
966 OSMetaClass::allocClassWithName(const OSString
* name
)
968 const OSSymbol
* tmpKey
= OSSymbol::withString(name
);
969 OSObject
* result
= allocClassWithName(tmpKey
);
974 /*********************************************************************
975 *********************************************************************/
977 OSMetaClass::allocClassWithName(const char * name
)
979 const OSSymbol
* tmpKey
= OSSymbol::withCStringNoCopy(name
);
980 OSObject
* result
= allocClassWithName(tmpKey
);
986 /*********************************************************************
987 *********************************************************************/
989 OSMetaClass::checkMetaCastWithName(
990 const OSSymbol
* name
,
991 const OSMetaClassBase
* in
)
993 OSMetaClassBase
* result
= 0;
995 const OSMetaClass
* const meta
= getMetaClassWithName(name
);
998 result
= meta
->checkMetaCast(in
);
1004 /*********************************************************************
1005 *********************************************************************/
1006 OSMetaClassBase
* OSMetaClass::
1007 checkMetaCastWithName(
1008 const OSString
* name
,
1009 const OSMetaClassBase
* in
)
1011 const OSSymbol
* tmpKey
= OSSymbol::withString(name
);
1012 OSMetaClassBase
* result
= checkMetaCastWithName(tmpKey
, in
);
1018 /*********************************************************************
1019 *********************************************************************/
1021 OSMetaClass::checkMetaCastWithName(
1023 const OSMetaClassBase
* in
)
1025 const OSSymbol
* tmpKey
= OSSymbol::withCStringNoCopy(name
);
1026 OSMetaClassBase
* result
= checkMetaCastWithName(tmpKey
, in
);
1032 /*********************************************************************
1033 * OSMetaClass::checkMetaCast()
1034 * Check to see if the 'check' object has this object in its metaclass chain.
1035 * Returns check if it is indeed a kind of the current meta class, 0 otherwise.
1037 * Generally this method is not invoked directly but is used to implement
1038 * the OSMetaClassBase::metaCast member function.
1040 * See also OSMetaClassBase::metaCast
1041 *********************************************************************/
1042 OSMetaClassBase
* OSMetaClass::checkMetaCast(
1043 const OSMetaClassBase
* check
) const
1045 const OSMetaClass
* const toMeta
= this;
1046 const OSMetaClass
* fromMeta
;
1048 for (fromMeta
= check
->getMetaClass(); ; fromMeta
= fromMeta
->superClassLink
) {
1049 if (toMeta
== fromMeta
) {
1050 return const_cast<OSMetaClassBase
*>(check
); // Discard const
1052 if (!fromMeta
->superClassLink
) {
1060 /*********************************************************************
1061 *********************************************************************/
1063 OSMetaClass::reservedCalled(int ind
) const
1065 const char * cname
= className
->getCStringNoCopy();
1066 panic("%s::_RESERVED%s%d called.", cname
, cname
, ind
);
1069 /*********************************************************************
1070 *********************************************************************/
1073 OSMetaClass::getSuperClass() const
1075 return superClassLink
;
1078 /*********************************************************************
1079 * xxx - I want to rename this :-/
1080 *********************************************************************/
1082 OSMetaClass::getKmodName() const
1084 OSKext
* myKext
= reserved
? reserved
->kext
: 0;
1086 return myKext
->getIdentifier();
1088 return OSSymbol::withCStringNoCopy("unknown");
1091 /*********************************************************************
1092 *********************************************************************/
1094 OSMetaClass::getInstanceCount() const
1096 return instanceCount
;
1099 /*********************************************************************
1100 *********************************************************************/
1103 OSMetaClass::printInstanceCounts()
1105 OSCollectionIterator
* classes
;
1106 OSSymbol
* className
;
1109 IOLockLock(sAllClassesLock
);
1110 classes
= OSCollectionIterator::withCollection(sAllClassesDict
);
1113 while( (className
= (OSSymbol
*)classes
->getNextObject())) {
1114 meta
= (OSMetaClass
*)sAllClassesDict
->getObject(className
);
1117 printf("%24s count: %03d x 0x%03x = 0x%06x\n",
1118 className
->getCStringNoCopy(),
1119 meta
->getInstanceCount(),
1120 meta
->getClassSize(),
1121 meta
->getInstanceCount() * meta
->getClassSize() );
1125 IOLockUnlock(sAllClassesLock
);
1129 /*********************************************************************
1130 *********************************************************************/
1132 OSMetaClass::getClassDictionary()
1134 panic("OSMetaClass::getClassDictionary() is obsoleted.\n");
1138 /*********************************************************************
1139 *********************************************************************/
1141 OSMetaClass::serialize(__unused OSSerialize
* s
) const
1143 panic("OSMetaClass::serialize(): Obsoleted\n");
1147 /*********************************************************************
1148 *********************************************************************/
1151 OSMetaClass::serializeClassDictionary(OSDictionary
* serializeDictionary
)
1153 OSDictionary
* classDict
= NULL
;
1155 IOLockLock(sAllClassesLock
);
1157 classDict
= OSDictionary::withCapacity(sAllClassesDict
->getCount());
1163 OSCollectionIterator
* classes
;
1164 const OSSymbol
* className
;
1166 classes
= OSCollectionIterator::withCollection(sAllClassesDict
);
1171 while ((className
= (const OSSymbol
*)classes
->getNextObject())) {
1172 const OSMetaClass
* meta
;
1175 meta
= (OSMetaClass
*)sAllClassesDict
->getObject(className
);
1176 count
= OSNumber::withNumber(meta
->getInstanceCount(), 32);
1178 classDict
->setObject(className
, count
);
1184 serializeDictionary
->setObject("Classes", classDict
);
1188 OSSafeRelease(classDict
);
1190 IOLockUnlock(sAllClassesLock
);