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::retain() const { }
498 void OSMetaClass::release() const { }
499 void OSMetaClass::release(__unused
int when
) const { }
500 void OSMetaClass::taggedRetain(__unused
const void * tag
) const { }
501 void OSMetaClass::taggedRelease(__unused
const void * tag
) const { }
502 void OSMetaClass::taggedRelease(__unused
const void * tag
, __unused
const int when
) const { }
503 int OSMetaClass::getRetainCount() const { return 0; }
505 /*********************************************************************
506 *********************************************************************/
508 OSMetaClass::getClassName() const
510 if (!className
) return NULL
;
511 return className
->getCStringNoCopy();
513 /*********************************************************************
514 *********************************************************************/
516 OSMetaClass::getClassNameSymbol() const
520 /*********************************************************************
521 *********************************************************************/
523 OSMetaClass::getClassSize() const
528 /*********************************************************************
529 *********************************************************************/
531 OSMetaClass::preModLoad(const char * kextIdentifier
)
533 IOLockLock(sStalledClassesLock
);
535 assert (sStalled
== NULL
);
536 sStalled
= (StalledData
*)kalloc(sizeof(* sStalled
));
538 sStalled
->classes
= (OSMetaClass
**)
539 kalloc(kKModCapacityIncrement
* sizeof(OSMetaClass
*));
540 if (!sStalled
->classes
) {
541 kfree(sStalled
, sizeof(*sStalled
));
544 ACCUMSIZE((kKModCapacityIncrement
* sizeof(OSMetaClass
*)) +
547 sStalled
->result
= kOSReturnSuccess
;
548 sStalled
->capacity
= kKModCapacityIncrement
;
550 sStalled
->kextIdentifier
= kextIdentifier
;
551 bzero(sStalled
->classes
, kKModCapacityIncrement
* sizeof(OSMetaClass
*));
554 // keep sStalledClassesLock locked until postModLoad
559 /*********************************************************************
560 *********************************************************************/
562 OSMetaClass::checkModLoad(void * loadHandle
)
564 return sStalled
&& loadHandle
== sStalled
&&
565 sStalled
->result
== kOSReturnSuccess
;
568 /*********************************************************************
569 *********************************************************************/
571 OSMetaClass::postModLoad(void * loadHandle
)
573 OSReturn result
= kOSReturnSuccess
;
574 OSSymbol
* myKextName
= 0; // must release
575 OSKext
* myKext
= 0; // must release
577 if (!sStalled
|| loadHandle
!= sStalled
) {
578 result
= kOSMetaClassInternal
;
582 if (sStalled
->result
) {
583 result
= sStalled
->result
;
584 } else switch (sBootstrapState
) {
586 case kNoDictionaries
:
587 sBootstrapState
= kMakingDictionaries
;
588 // No break; fall through
590 case kMakingDictionaries
:
591 sAllClassesDict
= OSDictionary::withCapacity(kClassCapacityIncrement
);
592 if (!sAllClassesDict
) {
593 result
= kOSMetaClassNoDicts
;
596 sAllClassesDict
->setOptions(OSCollection::kSort
, OSCollection::kSort
);
598 // No break; fall through
600 case kCompletedBootstrap
:
603 myKextName
= const_cast<OSSymbol
*>(OSSymbol::withCStringNoCopy(
604 sStalled
->kextIdentifier
));
606 if (!sStalled
->count
) {
607 break; // Nothing to do so just get out
610 myKext
= OSKext::lookupKextWithIdentifier(myKextName
);
612 result
= kOSMetaClassNoKext
;
614 /* Log this error here so we can include the kext name.
616 OSKextLog(/* kext */ NULL
, kOSMetaClassLogSpec
,
617 "OSMetaClass: Can't record classes for kext %s - kext not found.",
618 sStalled
->kextIdentifier
);
622 /* First pass checking classes aren't already loaded. If any already
623 * exist, we don't register any, and so we don't technically have
624 * to do any C++ teardown.
626 * Hack alert: me->className has been a C string until now.
627 * We only release the OSSymbol if we store the kext.
629 IOLockLock(sAllClassesLock
);
630 for (i
= 0; i
< sStalled
->count
; i
++) {
631 const OSMetaClass
* me
= sStalled
->classes
[i
];
632 OSMetaClass
* orig
= OSDynamicCast(OSMetaClass
,
633 sAllClassesDict
->getObject((const char *)me
->className
));
637 /* Log this error here so we can include the class name.
638 * xxx - we should look up the other kext that defines the class
640 OSKextLog(myKext
, kOSMetaClassLogSpec
,
641 "OSMetaClass: Kext %s class %s is a duplicate;"
642 "kext %s already has a class by that name.",
643 sStalled
->kextIdentifier
, (const char *)me
->className
,
644 ((OSKext
*)orig
->reserved
->kext
)->getIdentifierCString());
645 result
= kOSMetaClassDuplicateClass
;
648 unsigned int depth
= 1;
649 while ((me
= me
->superClassLink
)) depth
++;
650 if (depth
> sDeepestClass
) sDeepestClass
= depth
;
652 IOLockUnlock(sAllClassesLock
);
654 /* Bail if we didn't go through the entire list of new classes
655 * (if we hit a duplicate).
657 if (i
!= sStalled
->count
) {
661 // Second pass symbolling strings and inserting classes in dictionary
662 IOLockLock(sAllClassesLock
);
663 for (i
= 0; i
< sStalled
->count
; i
++) {
664 OSMetaClass
* me
= sStalled
->classes
[i
];
666 /* Hack alert: me->className has been a C string until now.
667 * We only release the OSSymbol in ~OSMetaClass()
668 * if we set the reference to the kext.
671 OSSymbol::withCStringNoCopy((const char *)me
->className
);
673 // xxx - I suppose if these fail we're going to panic soon....
674 sAllClassesDict
->setObject(me
->className
, me
);
676 /* Do not retain the kext object here.
678 me
->reserved
->kext
= myKext
;
680 result
= myKext
->addClass(me
, sStalled
->count
);
681 if (result
!= kOSReturnSuccess
) {
682 /* OSKext::addClass() logs with kOSMetaClassNoInsKModSet. */
687 IOLockUnlock(sAllClassesLock
);
688 sBootstrapState
= kCompletedBootstrap
;
693 result
= kOSMetaClassInternal
;
698 /* Don't call logError() for success or the conditions logged above
699 * or by called function.
701 if (result
!= kOSReturnSuccess
&&
702 result
!= kOSMetaClassNoInsKModSet
&&
703 result
!= kOSMetaClassDuplicateClass
&&
704 result
!= kOSMetaClassNoKext
) {
706 OSMetaClassLogErrorForKext(result
, myKext
);
709 OSSafeRelease(myKextName
);
710 OSSafeRelease(myKext
);
713 ACCUMSIZE(-(sStalled
->capacity
* sizeof(OSMetaClass
*) +
715 kfree(sStalled
->classes
, sStalled
->capacity
* sizeof(OSMetaClass
*));
716 kfree(sStalled
, sizeof(*sStalled
));
720 IOLockUnlock(sStalledClassesLock
);
726 /*********************************************************************
727 *********************************************************************/
729 OSMetaClass::instanceConstructed() const
731 // if ((0 == OSIncrementAtomic(&(((OSMetaClass *) this)->instanceCount))) && superClassLink)
732 if ((0 == OSIncrementAtomic(&instanceCount
)) && superClassLink
) {
733 superClassLink
->instanceConstructed();
737 /*********************************************************************
738 *********************************************************************/
740 OSMetaClass::instanceDestructed() const
742 if ((1 == OSDecrementAtomic(&instanceCount
)) && superClassLink
) {
743 superClassLink
->instanceDestructed();
746 if (((int)instanceCount
) < 0) {
747 OSKext
* myKext
= reserved
->kext
;
749 OSKextLog(myKext
, kOSMetaClassLogSpec
,
750 // xxx - this phrasing is rather cryptic
751 "OSMetaClass: Class %s - bad retain (%d)",
752 getClassName(), instanceCount
);
756 /*********************************************************************
757 *********************************************************************/
759 OSMetaClass::modHasInstance(const char * kextIdentifier
)
762 OSKext
* theKext
= NULL
; // must release
764 theKext
= OSKext::lookupKextWithIdentifier(kextIdentifier
);
769 result
= theKext
->hasOSMetaClassInstances();
772 OSSafeRelease(theKext
);
776 /*********************************************************************
777 *********************************************************************/
779 OSMetaClass::reportModInstances(const char * kextIdentifier
)
781 OSKext::reportOSMetaClassInstances(kextIdentifier
,
782 kOSKextLogExplicitLevel
);
785 /*********************************************************************
786 *********************************************************************/
789 OSMetaClass::addInstance(const OSObject
* instance
, bool super
) const
791 if (!super
) IOLockLock(sInstancesLock
);
793 if (!reserved
->instances
) {
794 reserved
->instances
= OSOrderedSet::withCapacity(16);
795 if (superClassLink
) {
796 superClassLink
->addInstance(reserved
->instances
, true);
799 reserved
->instances
->setLastObject(instance
);
801 if (!super
) IOLockUnlock(sInstancesLock
);
805 OSMetaClass::removeInstance(const OSObject
* instance
, bool super
) const
807 if (!super
) IOLockLock(sInstancesLock
);
809 if (reserved
->instances
) {
810 reserved
->instances
->removeObject(instance
);
811 if (0 == reserved
->instances
->getCount()) {
812 if (superClassLink
) {
813 superClassLink
->removeInstance(reserved
->instances
, true);
815 IOLockLock(sAllClassesLock
);
816 reserved
->instances
->release();
817 reserved
->instances
= 0;
818 IOLockUnlock(sAllClassesLock
);
822 if (!super
) IOLockUnlock(sInstancesLock
);
826 OSMetaClass::applyToInstances(OSOrderedSet
* set
,
827 OSMetaClassInstanceApplierFunction applier
,
830 enum { kLocalDepth
= 24 };
831 unsigned int _nextIndex
[kLocalDepth
];
832 OSOrderedSet
* _sets
[kLocalDepth
];
833 unsigned int * nextIndex
= &_nextIndex
[0];
834 OSOrderedSet
** sets
= &_sets
[0];
836 OSOrderedSet
* childSet
;
837 unsigned int maxDepth
;
842 maxDepth
= sDeepestClass
;
843 if (maxDepth
> kLocalDepth
)
845 nextIndex
= IONew(typeof(nextIndex
[0]), maxDepth
);
846 sets
= IONew(typeof(sets
[0]), maxDepth
);
853 while (!done
&& (obj
= set
->getObject(idx
++)))
855 if ((childSet
= OSDynamicCast(OSOrderedSet
, obj
)))
857 if (level
>= maxDepth
) panic(">maxDepth");
859 nextIndex
[level
] = idx
;
865 done
= (*applier
)(obj
, context
);
873 idx
= nextIndex
[level
];
878 if (maxDepth
> kLocalDepth
)
880 IODelete(nextIndex
, typeof(nextIndex
[0]), maxDepth
);
881 IODelete(sets
, typeof(sets
[0]), maxDepth
);
886 OSMetaClass::applyToInstances(OSMetaClassInstanceApplierFunction applier
,
887 void * context
) const
889 IOLockLock(sInstancesLock
);
890 if (reserved
->instances
) applyToInstances(reserved
->instances
, applier
, context
);
891 IOLockUnlock(sInstancesLock
);
895 OSMetaClass::applyToInstancesOfClassName(
896 const OSSymbol
* name
,
897 OSMetaClassInstanceApplierFunction applier
,
901 OSOrderedSet
* set
= 0;
903 IOLockLock(sAllClassesLock
);
905 && (meta
= (OSMetaClass
*) sAllClassesDict
->getObject(name
))
906 && (set
= meta
->reserved
->instances
))
910 IOLockUnlock(sAllClassesLock
);
914 IOLockLock(sInstancesLock
);
915 applyToInstances(set
, applier
, context
);
916 IOLockUnlock(sInstancesLock
);
920 /*********************************************************************
921 *********************************************************************/
923 OSMetaClass::considerUnloads()
925 OSKext::considerUnloads();
928 /*********************************************************************
929 *********************************************************************/
931 OSMetaClass::getMetaClassWithName(const OSSymbol
* name
)
933 OSMetaClass
* retMeta
= 0;
939 IOLockLock(sAllClassesLock
);
940 if (sAllClassesDict
) {
941 retMeta
= (OSMetaClass
*) sAllClassesDict
->getObject(name
);
943 IOLockUnlock(sAllClassesLock
);
948 /*********************************************************************
949 *********************************************************************/
951 OSMetaClass::allocClassWithName(const OSSymbol
* name
)
953 OSObject
* result
= 0;
955 const OSMetaClass
* const meta
= getMetaClassWithName(name
);
958 result
= meta
->alloc();
964 /*********************************************************************
965 *********************************************************************/
967 OSMetaClass::allocClassWithName(const OSString
* name
)
969 const OSSymbol
* tmpKey
= OSSymbol::withString(name
);
970 OSObject
* result
= allocClassWithName(tmpKey
);
975 /*********************************************************************
976 *********************************************************************/
978 OSMetaClass::allocClassWithName(const char * name
)
980 const OSSymbol
* tmpKey
= OSSymbol::withCStringNoCopy(name
);
981 OSObject
* result
= allocClassWithName(tmpKey
);
987 /*********************************************************************
988 *********************************************************************/
990 OSMetaClass::checkMetaCastWithName(
991 const OSSymbol
* name
,
992 const OSMetaClassBase
* in
)
994 OSMetaClassBase
* result
= 0;
996 const OSMetaClass
* const meta
= getMetaClassWithName(name
);
999 result
= meta
->checkMetaCast(in
);
1005 /*********************************************************************
1006 *********************************************************************/
1007 OSMetaClassBase
* OSMetaClass::
1008 checkMetaCastWithName(
1009 const OSString
* name
,
1010 const OSMetaClassBase
* in
)
1012 const OSSymbol
* tmpKey
= OSSymbol::withString(name
);
1013 OSMetaClassBase
* result
= checkMetaCastWithName(tmpKey
, in
);
1019 /*********************************************************************
1020 *********************************************************************/
1022 OSMetaClass::checkMetaCastWithName(
1024 const OSMetaClassBase
* in
)
1026 const OSSymbol
* tmpKey
= OSSymbol::withCStringNoCopy(name
);
1027 OSMetaClassBase
* result
= checkMetaCastWithName(tmpKey
, in
);
1033 /*********************************************************************
1034 * OSMetaClass::checkMetaCast()
1035 * Check to see if the 'check' object has this object in its metaclass chain.
1036 * Returns check if it is indeed a kind of the current meta class, 0 otherwise.
1038 * Generally this method is not invoked directly but is used to implement
1039 * the OSMetaClassBase::metaCast member function.
1041 * See also OSMetaClassBase::metaCast
1042 *********************************************************************/
1043 OSMetaClassBase
* OSMetaClass::checkMetaCast(
1044 const OSMetaClassBase
* check
) const
1046 const OSMetaClass
* const toMeta
= this;
1047 const OSMetaClass
* fromMeta
;
1049 for (fromMeta
= check
->getMetaClass(); ; fromMeta
= fromMeta
->superClassLink
) {
1050 if (toMeta
== fromMeta
) {
1051 return const_cast<OSMetaClassBase
*>(check
); // Discard const
1053 if (!fromMeta
->superClassLink
) {
1061 /*********************************************************************
1062 *********************************************************************/
1064 OSMetaClass::reservedCalled(int ind
) const
1066 const char * cname
= className
->getCStringNoCopy();
1067 panic("%s::_RESERVED%s%d called.", cname
, cname
, ind
);
1070 /*********************************************************************
1071 *********************************************************************/
1074 OSMetaClass::getSuperClass() const
1076 return superClassLink
;
1079 /*********************************************************************
1080 * xxx - I want to rename this :-/
1081 *********************************************************************/
1083 OSMetaClass::getKmodName() const
1085 OSKext
* myKext
= reserved
? reserved
->kext
: 0;
1087 return myKext
->getIdentifier();
1089 return OSSymbol::withCStringNoCopy("unknown");
1092 /*********************************************************************
1093 *********************************************************************/
1095 OSMetaClass::getInstanceCount() const
1097 return instanceCount
;
1100 /*********************************************************************
1101 *********************************************************************/
1104 OSMetaClass::printInstanceCounts()
1106 OSCollectionIterator
* classes
;
1107 OSSymbol
* className
;
1110 IOLockLock(sAllClassesLock
);
1111 classes
= OSCollectionIterator::withCollection(sAllClassesDict
);
1114 while( (className
= (OSSymbol
*)classes
->getNextObject())) {
1115 meta
= (OSMetaClass
*)sAllClassesDict
->getObject(className
);
1118 printf("%24s count: %03d x 0x%03x = 0x%06x\n",
1119 className
->getCStringNoCopy(),
1120 meta
->getInstanceCount(),
1121 meta
->getClassSize(),
1122 meta
->getInstanceCount() * meta
->getClassSize() );
1126 IOLockUnlock(sAllClassesLock
);
1130 /*********************************************************************
1131 *********************************************************************/
1133 OSMetaClass::getClassDictionary()
1135 panic("OSMetaClass::getClassDictionary() is obsoleted.\n");
1139 /*********************************************************************
1140 *********************************************************************/
1142 OSMetaClass::serialize(__unused OSSerialize
* s
) const
1144 panic("OSMetaClass::serialize(): Obsoleted\n");
1148 /*********************************************************************
1149 *********************************************************************/
1152 OSMetaClass::serializeClassDictionary(OSDictionary
* serializeDictionary
)
1154 OSDictionary
* classDict
= NULL
;
1156 IOLockLock(sAllClassesLock
);
1158 classDict
= OSDictionary::withCapacity(sAllClassesDict
->getCount());
1164 OSCollectionIterator
* classes
;
1165 const OSSymbol
* className
;
1167 classes
= OSCollectionIterator::withCollection(sAllClassesDict
);
1172 while ((className
= (const OSSymbol
*)classes
->getNextObject())) {
1173 const OSMetaClass
* meta
;
1176 meta
= (OSMetaClass
*)sAllClassesDict
->getObject(className
);
1177 count
= OSNumber::withNumber(meta
->getInstanceCount(), 32);
1179 classDict
->setObject(className
, count
);
1185 serializeDictionary
->setObject("Classes", classDict
);
1189 OSSafeRelease(classDict
);
1191 IOLockUnlock(sAllClassesLock
);