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 IOLock
* sAllClassesLock
= 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 #pragma mark OSMetaClassBase
116 #endif /* PRAGMA_MARK */
117 /*********************************************************************
119 *********************************************************************/
121 /*********************************************************************
122 * Reserved vtable functions.
123 *********************************************************************/
125 void OSMetaClassBase::_RESERVEDOSMetaClassBase0()
126 { panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 0); }
127 void OSMetaClassBase::_RESERVEDOSMetaClassBase1()
128 { panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 1); }
129 void OSMetaClassBase::_RESERVEDOSMetaClassBase2()
130 { panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 2); }
131 #endif /* SLOT_USED */
133 // As these slots are used move them up inside the #if above
134 void OSMetaClassBase::_RESERVEDOSMetaClassBase3()
135 { panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 3); }
136 void OSMetaClassBase::_RESERVEDOSMetaClassBase4()
137 { panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 4); }
138 void OSMetaClassBase::_RESERVEDOSMetaClassBase5()
139 { panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 5); }
140 void OSMetaClassBase::_RESERVEDOSMetaClassBase6()
141 { panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 6); }
143 /*********************************************************************
144 * These used to be inline in the header but gcc didn't believe us
145 * Now we MUST pull the inline out at least until the compiler is
148 * Helper inlines for runtime type preprocessor macros
149 *********************************************************************/
151 /*********************************************************************
152 *********************************************************************/
154 OSMetaClassBase::safeMetaCast(
155 const OSMetaClassBase
* me
,
156 const OSMetaClass
* toType
)
158 return (me
)? me
->metaCast(toType
) : 0;
161 /*********************************************************************
162 *********************************************************************/
164 OSMetaClassBase::checkTypeInst(
165 const OSMetaClassBase
* inst
,
166 const OSMetaClassBase
* typeinst
)
168 const OSMetaClass
* toType
= OSTypeIDInst(typeinst
);
169 return typeinst
&& inst
&& (0 != inst
->metaCast(toType
));
172 /*********************************************************************
173 *********************************************************************/
174 void OSMetaClassBase::
177 sAllClassesLock
= IOLockAlloc();
178 sStalledClassesLock
= IOLockAlloc();
181 /*********************************************************************
182 * If you need this slot you had better setup an IOCTL style interface.
183 * 'Cause the whole kernel world depends on OSMetaClassBase and YOU
184 * CANT change the VTABLE size ever.
185 *********************************************************************/
187 OSMetaClassBase::_RESERVEDOSMetaClassBase7()
188 { panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 7); }
190 /*********************************************************************
191 *********************************************************************/
192 OSMetaClassBase::OSMetaClassBase()
196 /*********************************************************************
197 *********************************************************************/
198 OSMetaClassBase::~OSMetaClassBase()
202 thisVTable
= (void **) this;
203 *thisVTable
= (void *) -1UL;
206 /*********************************************************************
207 *********************************************************************/
209 OSMetaClassBase::isEqualTo(const OSMetaClassBase
* anObj
) const
211 return this == anObj
;
214 /*********************************************************************
215 *********************************************************************/
217 OSMetaClassBase::metaCast(const OSMetaClass
* toMeta
) const
219 return toMeta
->checkMetaCast(this);
222 /*********************************************************************
223 *********************************************************************/
225 OSMetaClassBase::metaCast(const OSSymbol
* toMetaSymb
) const
227 return OSMetaClass::checkMetaCastWithName(toMetaSymb
, this);
230 /*********************************************************************
231 *********************************************************************/
233 OSMetaClassBase::metaCast(const OSString
* toMetaStr
) const
235 const OSSymbol
* tempSymb
= OSSymbol::withString(toMetaStr
);
236 OSMetaClassBase
* ret
= 0;
238 ret
= metaCast(tempSymb
);
244 /*********************************************************************
245 *********************************************************************/
247 OSMetaClassBase::metaCast(const char * toMetaCStr
) const
249 const OSSymbol
* tempSymb
= OSSymbol::withCString(toMetaCStr
);
250 OSMetaClassBase
* ret
= 0;
252 ret
= metaCast(tempSymb
);
259 #pragma mark OSMetaClassMeta
260 #endif /* PRAGMA_MARK */
261 /*********************************************************************
262 * OSMetaClassMeta - the bootstrap metaclass of OSMetaClass
263 *********************************************************************/
264 class OSMetaClassMeta
: public OSMetaClass
268 OSObject
* alloc() const;
270 OSMetaClassMeta::OSMetaClassMeta()
271 : OSMetaClass("OSMetaClass", 0, sizeof(OSMetaClass
))
273 OSObject
* OSMetaClassMeta::alloc() const { return 0; }
275 static OSMetaClassMeta sOSMetaClassMeta
;
277 const OSMetaClass
* const OSMetaClass::metaClass
= &sOSMetaClassMeta
;
278 const OSMetaClass
* OSMetaClass::getMetaClass() const
279 { return &sOSMetaClassMeta
; }
282 #pragma mark OSMetaClass
283 #endif /* PRAGMA_MARK */
284 /*********************************************************************
286 *********************************************************************/
288 /*********************************************************************
289 * Reserved functions.
290 *********************************************************************/
291 void OSMetaClass::_RESERVEDOSMetaClass0()
292 { panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 0); }
293 void OSMetaClass::_RESERVEDOSMetaClass1()
294 { panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 1); }
295 void OSMetaClass::_RESERVEDOSMetaClass2()
296 { panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 2); }
297 void OSMetaClass::_RESERVEDOSMetaClass3()
298 { panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 3); }
299 void OSMetaClass::_RESERVEDOSMetaClass4()
300 { panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 4); }
301 void OSMetaClass::_RESERVEDOSMetaClass5()
302 { panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 5); }
303 void OSMetaClass::_RESERVEDOSMetaClass6()
304 { panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 6); }
305 void OSMetaClass::_RESERVEDOSMetaClass7()
306 { panic("OSMetaClass::_RESERVEDOSMetaClass%d called", 7); }
308 /*********************************************************************
309 *********************************************************************/
311 OSMetaClassLogErrorForKext(
315 const char * message
= NULL
;
318 case kOSReturnSuccess
:
320 case kOSMetaClassNoInit
: // xxx - never returned; logged at fail site
321 message
= "OSMetaClass: preModLoad() wasn't called (runtime internal error).";
323 case kOSMetaClassNoDicts
:
324 message
= "OSMetaClass: Allocation failure for OSMetaClass internal dictionaries.";
326 case kOSMetaClassNoKModSet
:
327 message
= "OSMetaClass: Allocation failure for internal kext recording set/set missing.";
329 case kOSMetaClassNoInsKModSet
:
330 message
= "OSMetaClass: Failed to record class in kext.";
332 case kOSMetaClassDuplicateClass
:
333 message
= "OSMetaClass: Duplicate class encountered.";
335 case kOSMetaClassNoSuper
: // xxx - never returned
336 message
= "OSMetaClass: Can't associate a class with its superclass.";
338 case kOSMetaClassInstNoSuper
: // xxx - never returned
339 message
= "OSMetaClass: Instance construction error; unknown superclass.";
341 case kOSMetaClassNoKext
:
342 message
= "OSMetaClass: Kext not found for metaclass.";
344 case kOSMetaClassInternal
:
346 message
= "OSMetaClass: Runtime internal error.";
351 OSKextLog(aKext
, kOSMetaClassLogSpec
, "%s", message
);
357 OSMetaClass::logError(OSReturn error
)
359 OSMetaClassLogErrorForKext(error
, NULL
);
362 /*********************************************************************
363 * The core constructor for a MetaClass (defined with this name always
364 * but within the scope of its represented class).
366 * MetaClass constructors are invoked in OSRuntimeInitializeCPP(),
367 * in between calls to OSMetaClass::preModLoad(), which sets up for
368 * registration, and OSMetaClass::postModLoad(), which actually
369 * records all the class/kext relationships of the new MetaClasses.
370 *********************************************************************/
371 OSMetaClass::OSMetaClass(
372 const char * inClassName
,
373 const OSMetaClass
* inSuperClass
,
374 unsigned int inClassSize
)
377 classSize
= inClassSize
;
378 superClassLink
= inSuperClass
;
380 /* Hack alert: We are just casting inClassName and storing it in
381 * an OSString * instance variable. This may be because you can't
382 * create C++ objects in static constructors, but I really don't know!
384 className
= (const OSSymbol
*)inClassName
;
386 // sStalledClassesLock taken in preModLoad
388 /* There's no way we can look up the kext here, unfortunately.
390 OSKextLog(/* kext */ NULL
, kOSMetaClassLogSpec
,
391 "OSMetaClass: preModLoad() wasn't called for class %s "
392 "(runtime internal error).",
394 } else if (!sStalled
->result
) {
395 // Grow stalled array if neccessary
396 if (sStalled
->count
>= sStalled
->capacity
) {
397 OSMetaClass
**oldStalled
= sStalled
->classes
;
398 int oldSize
= sStalled
->capacity
* sizeof(OSMetaClass
*);
399 int newSize
= oldSize
400 + kKModCapacityIncrement
* sizeof(OSMetaClass
*);
402 sStalled
->classes
= (OSMetaClass
**)kalloc(newSize
);
403 if (!sStalled
->classes
) {
404 sStalled
->classes
= oldStalled
;
405 sStalled
->result
= kOSMetaClassNoTempData
;
409 sStalled
->capacity
+= kKModCapacityIncrement
;
410 memmove(sStalled
->classes
, oldStalled
, oldSize
);
411 kfree(oldStalled
, oldSize
);
412 ACCUMSIZE(newSize
- oldSize
);
415 sStalled
->classes
[sStalled
->count
++] = this;
419 /*********************************************************************
420 *********************************************************************/
421 OSMetaClass::~OSMetaClass()
423 OSKext
* myKext
= (OSKext
*)reserved
; // do not release
425 /* Hack alert: 'className' is a C string during early C++ init, and
426 * is converted to a real OSSymbol only when we record the OSKext in
427 * OSMetaClass::postModLoad(). So only do this bit if we have an OSKext.
428 * We can't safely cast or check 'className'.
430 * Also, release className *after* calling into the kext,
431 * as removeClass() may access className.
433 IOLockLock(sAllClassesLock
);
434 if (sAllClassesDict
) {
436 sAllClassesDict
->removeObject(className
);
438 sAllClassesDict
->removeObject((char *)className
);
441 IOLockUnlock(sAllClassesLock
);
444 if (myKext
->removeClass(this) != kOSReturnSuccess
) {
445 // xxx - what can we do?
447 className
->release();
450 // sStalledClassesLock taken in preModLoad
454 /* First pass find class in stalled list. If we find it that means
455 * we started C++ init with constructors but now we're tearing down
456 * because of some failure.
458 for (i
= 0; i
< sStalled
->count
; i
++) {
459 if (this == sStalled
->classes
[i
]) {
464 /* Remove this metaclass from the stalled list so postModLoad() doesn't
465 * try to register it.
467 if (i
< sStalled
->count
) {
469 if (i
< sStalled
->count
) {
470 memmove(&sStalled
->classes
[i
], &sStalled
->classes
[i
+1],
471 (sStalled
->count
- i
) * sizeof(OSMetaClass
*));
477 /*********************************************************************
479 *********************************************************************/
480 void * OSMetaClass::operator new(__unused
size_t size
) { return 0; }
481 void OSMetaClass::retain() const { }
482 void OSMetaClass::release() const { }
483 void OSMetaClass::release(__unused
int when
) const { }
484 void OSMetaClass::taggedRetain(__unused
const void * tag
) const { }
485 void OSMetaClass::taggedRelease(__unused
const void * tag
) const { }
486 void OSMetaClass::taggedRelease(__unused
const void * tag
, __unused
const int when
) const { }
487 int OSMetaClass::getRetainCount() const { return 0; }
489 /*********************************************************************
490 *********************************************************************/
492 OSMetaClass::getClassName() const
494 if (!className
) return NULL
;
495 return className
->getCStringNoCopy();
498 /*********************************************************************
499 *********************************************************************/
501 OSMetaClass::getClassSize() const
506 /*********************************************************************
507 *********************************************************************/
509 OSMetaClass::preModLoad(const char * kextIdentifier
)
511 IOLockLock(sStalledClassesLock
);
513 assert (sStalled
== NULL
);
514 sStalled
= (StalledData
*)kalloc(sizeof(* sStalled
));
516 sStalled
->classes
= (OSMetaClass
**)
517 kalloc(kKModCapacityIncrement
* sizeof(OSMetaClass
*));
518 if (!sStalled
->classes
) {
519 kfree(sStalled
, sizeof(*sStalled
));
522 ACCUMSIZE((kKModCapacityIncrement
* sizeof(OSMetaClass
*)) +
525 sStalled
->result
= kOSReturnSuccess
;
526 sStalled
->capacity
= kKModCapacityIncrement
;
528 sStalled
->kextIdentifier
= kextIdentifier
;
529 bzero(sStalled
->classes
, kKModCapacityIncrement
* sizeof(OSMetaClass
*));
532 // keep sStalledClassesLock locked until postModLoad
537 /*********************************************************************
538 *********************************************************************/
540 OSMetaClass::checkModLoad(void * loadHandle
)
542 return sStalled
&& loadHandle
== sStalled
&&
543 sStalled
->result
== kOSReturnSuccess
;
546 /*********************************************************************
547 *********************************************************************/
549 OSMetaClass::postModLoad(void * loadHandle
)
551 OSReturn result
= kOSReturnSuccess
;
552 OSSymbol
* myKextName
= 0; // must release
553 OSKext
* myKext
= 0; // must release
555 if (!sStalled
|| loadHandle
!= sStalled
) {
556 result
= kOSMetaClassInternal
;
560 if (sStalled
->result
) {
561 result
= sStalled
->result
;
562 } else switch (sBootstrapState
) {
564 case kNoDictionaries
:
565 sBootstrapState
= kMakingDictionaries
;
566 // No break; fall through
568 case kMakingDictionaries
:
569 sAllClassesDict
= OSDictionary::withCapacity(kClassCapacityIncrement
);
570 if (!sAllClassesDict
) {
571 result
= kOSMetaClassNoDicts
;
575 // No break; fall through
577 case kCompletedBootstrap
:
580 myKextName
= const_cast<OSSymbol
*>(OSSymbol::withCStringNoCopy(
581 sStalled
->kextIdentifier
));
583 if (!sStalled
->count
) {
584 break; // Nothing to do so just get out
587 myKext
= OSKext::lookupKextWithIdentifier(myKextName
);
589 result
= kOSMetaClassNoKext
;
591 /* Log this error here so we can include the kext name.
593 OSKextLog(/* kext */ NULL
, kOSMetaClassLogSpec
,
594 "OSMetaClass: Can't record classes for kext %s - kext not found.",
595 sStalled
->kextIdentifier
);
599 /* First pass checking classes aren't already loaded. If any already
600 * exist, we don't register any, and so we don't technically have
601 * to do any C++ teardown.
603 * Hack alert: me->className has been a C string until now.
604 * We only release the OSSymbol if we store the kext.
606 IOLockLock(sAllClassesLock
);
607 for (i
= 0; i
< sStalled
->count
; i
++) {
608 OSMetaClass
* me
= sStalled
->classes
[i
];
609 OSMetaClass
* orig
= OSDynamicCast(OSMetaClass
,
610 sAllClassesDict
->getObject((const char *)me
->className
));
614 /* Log this error here so we can include the class name.
615 * xxx - we should look up the other kext that defines the class
617 OSKextLog(myKext
, kOSMetaClassLogSpec
,
618 "OSMetaClass: Kext %s class %s is a duplicate;"
619 "kext %s already has a class by that name.",
620 sStalled
->kextIdentifier
, (const char *)me
->className
,
621 ((OSKext
*)orig
->reserved
)->getIdentifierCString());
622 result
= kOSMetaClassDuplicateClass
;
626 IOLockUnlock(sAllClassesLock
);
628 /* Bail if we didn't go through the entire list of new classes
629 * (if we hit a duplicate).
631 if (i
!= sStalled
->count
) {
635 // Second pass symbolling strings and inserting classes in dictionary
636 IOLockLock(sAllClassesLock
);
637 for (i
= 0; i
< sStalled
->count
; i
++) {
638 OSMetaClass
* me
= sStalled
->classes
[i
];
640 /* Hack alert: me->className has been a C string until now.
641 * We only release the OSSymbol in ~OSMetaClass()
642 * if we set the reference to the kext.
645 OSSymbol::withCStringNoCopy((const char *)me
->className
);
647 // xxx - I suppose if these fail we're going to panic soon....
648 sAllClassesDict
->setObject(me
->className
, me
);
650 /* Do not retain the kext object here.
652 me
->reserved
= (ExpansionData
*)myKext
;
654 result
= myKext
->addClass(me
, sStalled
->count
);
655 if (result
!= kOSReturnSuccess
) {
656 /* OSKext::addClass() logs with kOSMetaClassNoInsKModSet. */
661 IOLockUnlock(sAllClassesLock
);
662 sBootstrapState
= kCompletedBootstrap
;
667 result
= kOSMetaClassInternal
;
672 /* Don't call logError() for success or the conditions logged above
673 * or by called function.
675 if (result
!= kOSReturnSuccess
&&
676 result
!= kOSMetaClassNoInsKModSet
&&
677 result
!= kOSMetaClassDuplicateClass
&&
678 result
!= kOSMetaClassNoKext
) {
680 OSMetaClassLogErrorForKext(result
, myKext
);
683 OSSafeRelease(myKextName
);
684 OSSafeRelease(myKext
);
687 ACCUMSIZE(-(sStalled
->capacity
* sizeof(OSMetaClass
*) +
689 kfree(sStalled
->classes
, sStalled
->capacity
* sizeof(OSMetaClass
*));
690 kfree(sStalled
, sizeof(*sStalled
));
694 IOLockUnlock(sStalledClassesLock
);
700 /*********************************************************************
701 *********************************************************************/
703 OSMetaClass::instanceConstructed() const
705 // if ((0 == OSIncrementAtomic(&(((OSMetaClass *) this)->instanceCount))) && superClassLink)
706 if ((0 == OSIncrementAtomic(&instanceCount
)) && superClassLink
) {
707 superClassLink
->instanceConstructed();
711 /*********************************************************************
712 *********************************************************************/
714 OSMetaClass::instanceDestructed() const
716 if ((1 == OSDecrementAtomic(&instanceCount
)) && superClassLink
) {
717 superClassLink
->instanceDestructed();
720 if (((int)instanceCount
) < 0) {
721 OSKext
* myKext
= (OSKext
*)reserved
;
723 OSKextLog(myKext
, kOSMetaClassLogSpec
,
724 // xxx - this phrasing is rather cryptic
725 "OSMetaClass: Class %s - bad retain (%d)",
726 getClassName(), instanceCount
);
730 /*********************************************************************
731 *********************************************************************/
733 OSMetaClass::modHasInstance(const char * kextIdentifier
)
736 OSKext
* theKext
= NULL
; // must release
738 theKext
= OSKext::lookupKextWithIdentifier(kextIdentifier
);
743 result
= theKext
->hasOSMetaClassInstances();
746 OSSafeRelease(theKext
);
750 /*********************************************************************
751 *********************************************************************/
753 OSMetaClass::reportModInstances(const char * kextIdentifier
)
755 OSKext::reportOSMetaClassInstances(kextIdentifier
,
756 kOSKextLogExplicitLevel
);
760 /*********************************************************************
761 *********************************************************************/
763 OSMetaClass::considerUnloads()
765 OSKext::considerUnloads();
768 /*********************************************************************
769 *********************************************************************/
771 OSMetaClass::getMetaClassWithName(const OSSymbol
* name
)
773 OSMetaClass
* retMeta
= 0;
779 IOLockLock(sAllClassesLock
);
780 if (sAllClassesDict
) {
781 retMeta
= (OSMetaClass
*) sAllClassesDict
->getObject(name
);
783 IOLockUnlock(sAllClassesLock
);
788 /*********************************************************************
789 *********************************************************************/
791 OSMetaClass::allocClassWithName(const OSSymbol
* name
)
793 OSObject
* result
= 0;
795 const OSMetaClass
* const meta
= getMetaClassWithName(name
);
798 result
= meta
->alloc();
804 /*********************************************************************
805 *********************************************************************/
807 OSMetaClass::allocClassWithName(const OSString
* name
)
809 const OSSymbol
* tmpKey
= OSSymbol::withString(name
);
810 OSObject
* result
= allocClassWithName(tmpKey
);
815 /*********************************************************************
816 *********************************************************************/
818 OSMetaClass::allocClassWithName(const char * name
)
820 const OSSymbol
* tmpKey
= OSSymbol::withCStringNoCopy(name
);
821 OSObject
* result
= allocClassWithName(tmpKey
);
827 /*********************************************************************
828 *********************************************************************/
830 OSMetaClass::checkMetaCastWithName(
831 const OSSymbol
* name
,
832 const OSMetaClassBase
* in
)
834 OSMetaClassBase
* result
= 0;
836 const OSMetaClass
* const meta
= getMetaClassWithName(name
);
839 result
= meta
->checkMetaCast(in
);
845 /*********************************************************************
846 *********************************************************************/
847 OSMetaClassBase
* OSMetaClass::
848 checkMetaCastWithName(
849 const OSString
* name
,
850 const OSMetaClassBase
* in
)
852 const OSSymbol
* tmpKey
= OSSymbol::withString(name
);
853 OSMetaClassBase
* result
= checkMetaCastWithName(tmpKey
, in
);
859 /*********************************************************************
860 *********************************************************************/
862 OSMetaClass::checkMetaCastWithName(
864 const OSMetaClassBase
* in
)
866 const OSSymbol
* tmpKey
= OSSymbol::withCStringNoCopy(name
);
867 OSMetaClassBase
* result
= checkMetaCastWithName(tmpKey
, in
);
873 /*********************************************************************
874 * OSMetaClass::checkMetaCast()
875 * Check to see if the 'check' object has this object in its metaclass chain.
876 * Returns check if it is indeed a kind of the current meta class, 0 otherwise.
878 * Generally this method is not invoked directly but is used to implement
879 * the OSMetaClassBase::metaCast member function.
881 * See also OSMetaClassBase::metaCast
882 *********************************************************************/
883 OSMetaClassBase
* OSMetaClass::checkMetaCast(
884 const OSMetaClassBase
* check
) const
886 const OSMetaClass
* const toMeta
= this;
887 const OSMetaClass
* fromMeta
;
889 for (fromMeta
= check
->getMetaClass(); ; fromMeta
= fromMeta
->superClassLink
) {
890 if (toMeta
== fromMeta
) {
891 return const_cast<OSMetaClassBase
*>(check
); // Discard const
893 if (!fromMeta
->superClassLink
) {
901 /*********************************************************************
902 *********************************************************************/
904 OSMetaClass::reservedCalled(int ind
) const
906 const char * cname
= className
->getCStringNoCopy();
907 panic("%s::_RESERVED%s%d called.", cname
, cname
, ind
);
910 /*********************************************************************
911 *********************************************************************/
914 OSMetaClass::getSuperClass() const
916 return superClassLink
;
919 /*********************************************************************
920 * xxx - I want to rename this :-/
921 *********************************************************************/
923 OSMetaClass::getKmodName() const
925 OSKext
* myKext
= (OSKext
*)reserved
;
927 return myKext
->getIdentifier();
929 return OSSymbol::withCStringNoCopy("unknown");
932 /*********************************************************************
933 *********************************************************************/
935 OSMetaClass::getInstanceCount() const
937 return instanceCount
;
940 /*********************************************************************
941 *********************************************************************/
944 OSMetaClass::printInstanceCounts()
946 OSCollectionIterator
* classes
;
947 OSSymbol
* className
;
950 IOLockLock(sAllClassesLock
);
951 classes
= OSCollectionIterator::withCollection(sAllClassesDict
);
954 while( (className
= (OSSymbol
*)classes
->getNextObject())) {
955 meta
= (OSMetaClass
*)sAllClassesDict
->getObject(className
);
958 printf("%24s count: %03d x 0x%03x = 0x%06x\n",
959 className
->getCStringNoCopy(),
960 meta
->getInstanceCount(),
961 meta
->getClassSize(),
962 meta
->getInstanceCount() * meta
->getClassSize() );
966 IOLockUnlock(sAllClassesLock
);
970 /*********************************************************************
971 *********************************************************************/
973 OSMetaClass::getClassDictionary()
975 panic("OSMetaClass::getClassDictionary() is obsoleted.\n");
979 /*********************************************************************
980 *********************************************************************/
982 OSMetaClass::serialize(__unused OSSerialize
* s
) const
984 panic("OSMetaClass::serialize(): Obsoleted\n");
988 /*********************************************************************
989 *********************************************************************/
992 OSMetaClass::serializeClassDictionary(OSDictionary
* serializeDictionary
)
994 OSDictionary
* classDict
= NULL
;
996 IOLockLock(sAllClassesLock
);
998 classDict
= OSDictionary::withCapacity(sAllClassesDict
->getCount());
1004 OSCollectionIterator
* classes
;
1005 const OSSymbol
* className
;
1007 classes
= OSCollectionIterator::withCollection(sAllClassesDict
);
1012 while ((className
= (const OSSymbol
*)classes
->getNextObject())) {
1013 const OSMetaClass
* meta
;
1016 meta
= (OSMetaClass
*)sAllClassesDict
->getObject(className
);
1017 count
= OSNumber::withNumber(meta
->getInstanceCount(), 32);
1019 classDict
->setObject(className
, count
);
1025 serializeDictionary
->setObject("Classes", classDict
);
1029 OSSafeRelease(classDict
);
1031 IOLockUnlock(sAllClassesLock
);