2 * Copyright (c) 2000 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 #ifndef _LIBKERN_OSMETACLASS_H
29 #define _LIBKERN_OSMETACLASS_H
31 #include <sys/types.h>
33 #include <libkern/OSReturn.h>
34 #include <kern/debug.h>
42 #ifdef XNU_KERNEL_PRIVATE
51 * This header declares the OSMetaClassBase and OSMetaClass classes,
52 * which together form the basis of the Libkern and I/O Kit C++ class hierarchy
53 * and run-time type information facility.
58 #define APPLE_KEXT_COMPATIBILITY
60 #ifdef XNU_KERNEL_PRIVATE
63 #define APPLE_KEXT_VTABLE_PADDING 1
65 #else /* XNU_KERNEL_PRIVATE */
66 #include <TargetConditionals.h>
69 #define APPLE_KEXT_VTABLE_PADDING 1
71 #endif /* XNU_KERNEL_PRIVATE */
75 #define APPLE_KEXT_LEGACY_ABI 0
77 #define APPLE_KEXT_LEGACY_ABI 1
82 #define APPLE_KEXT_COMPATIBILITY_VIRTUAL
84 // private method made virtual only for binary compatibility
85 #define APPLE_KEXT_COMPATIBILITY_VIRTUAL virtual
89 #define APPLE_KEXT_DEPRECATED __attribute__((deprecated))
92 * @class OSMetaClassBase
95 * OSMetaClassBase is the abstract bootstrap class
96 * for the Libkern and I/O Kit run-time type information system.
99 * OSMetaClassBase is the abstract C++ root class
100 * underlying the entire Libkern and I/O Kit class hierarchy.
101 * It defines the run-time type information system,
102 * including dynamic class allocation and safe type-casting,
103 * as well as the abstract interface for reference counting
104 * and a few other utility functions.
105 * OSMetaClassBase is the immediate superclass of
106 * @link //apple_ref/doc/class/OSObject OSObject@/link and
107 * @link //apple_ref/doc/class/OSMetaClass OSMetaClass@/link;
108 * no other class should derive from OSMetaClassBase.
110 * For more information, see
111 * <i>@link //apple_ref/doc/uid/TP40002799
112 * I/O Kit Device Driver Design Guidelines@/link</i>.
114 * <b>Use by Kernel Extensions</b>
116 * Kernel Extensions should never interact directly with OSMetaClassBase,
117 * but they will find useful several macros that tie in
118 * to the run-time type information system, specifically:
120 * <li><code>@link OSTypeAlloc OSTypeAlloc@/link</code> - allocation of new instances</li>
121 * <li><code>@link OSDynamicCast OSDynamicCast@/link</code> - safe type casting</li>
122 * <li><code>@link OSCheckTypeInst OSCheckTypeInst@/link</code> -
123 * checking for inheritance/derivation</li>
124 * <li><code>@link OSMemberFunctionCast OSMemberFunctionCast@/link</code> -
125 * casting C++ member functions to C function pointers
126 * for registration as callbacks</li>
129 * See @link //apple_ref/doc/class/OSMetaClass OSMetaClass@/link
130 * for more run-time type information interfaces.
132 * <b>Use Restrictions</b>
134 * OSMetaClassBase should not be subclassed by kernel extensions,
135 * nor should kernel extensions call its run-time type functions directly.
137 * The run-time type functions and macros are <b>not safe</b>
138 * to call in a primary interrupt context.
140 * <b>Concurrency Protection</b>
142 * The run-time type macros and functions of OSMetaClassBase are thread-safe.
144 class OSMetaClassBase
150 * @define OSTypeAlloc
154 * Allocates an instance of the named object class.
156 * @param type The name of the desired class to be created,
157 * as a raw token, <i>not</i> a string or macro.
160 * A pointer to the new, uninitialized object on success;
161 * <code>NULL</code> on failure.
166 * //apple_ref/cpp/clm/OSMetaClass/allocClassWithName/staticOSObject*\/(constchar*)
167 * OSMetaClass::allocClassWithName(const char *)@/link</code>
170 * //apple_ref/cpp/instm/OSMetaClass/alloc/virtualOSObject*\/()
171 * OSMetaClass::alloc@/link</code>.
173 * The OSTypeAlloc macro is used to avoid binary compatibility difficulties
174 * presented by the C++ <code>new</code> operator.
176 #define OSTypeAlloc(type) ((type *) ((type::metaClass)->alloc()))
184 * Returns the type ID (metaclass) of a class based on its name.
186 * @param type The name of the desired class, as a raw token,
187 * <i>not</i> a string or macro.
190 * The unique type ID (metaclass) for the class.
193 * It is typically more useful to determine whether a class is derived
195 * <code>@link //apple_ref/cpp/macro/OSDynamicCast OSDynamicCast@/link</code>
197 * <code>@link //apple_ref/cpp/macro/OSCheckTypeInst OSCheckTypeInst@/link</code>.
199 #define OSTypeID(type) (type::metaClass)
203 * @define OSTypeIDInst
207 * Returns the type ID (metaclass) for the class of an object instance.
209 * @param typeinst An instance of an OSObject subclass.
212 * The type ID of that object's class; that is, its metaclass.
215 * It is typically more useful to determine whether an object is derived
216 * from a particular class; see
217 * <code>@link //apple_ref/cpp/macro/OSDynamicCast OSDynamicCast@/link</code>
219 * <code>@link //apple_ref/cpp/macro/OSCheckTypeInst OSCheckTypeInst@/link</code>.
221 #define OSTypeIDInst(typeinst) ((typeinst)->getMetaClass())
225 * @define OSDynamicCast
229 * Safe type-casting for Libkern C++ objects.
231 * @param type The name of the desired class type, as a raw token,
232 * <i>not</i> a string or macro.
233 * It is assumed you intend to cast to a pointer
234 * to an object of this type.
235 * Type qualifiers, such as <code>const</code>,
236 * are not recognized and will cause
237 * a (usually obscure) compile error.
238 * @param inst A pointer to the object instance to be cast.
239 * May be <code>NULL</code>.
242 * <code>inst</code> if it is non-<code>NULL</code>
243 * and derived from <code>type</code>;
244 * otherwise <code>NULL</code>.
247 * <code>OSDynamicCast</code> is a rough equivalent
248 * to the standard C++ RTTI <code>dynamic_cast<T></code> operator.
249 * Your code should use this instead of raw C type-casting,
250 * and check the resulting value.
251 * If the result is non-<code>NULL</code>,
252 * the object is safe to use as the type-cast class;
253 * if the result is <code>NULL</code>,
254 * the object does not derive from the type-cast class
255 * and your code should take appropriate steps to handle the error.
257 #define OSDynamicCast(type, inst) \
258 ((type *) OSMetaClassBase::safeMetaCast((inst), OSTypeID(type)))
262 * @define OSCheckTypeInst
266 * Checks whether two objects are type-compatible.
268 * @param typeinst The reference object.
269 * @param inst The object to check for type compatibility.
272 * <code>true</code> if both <code>inst</code> and
273 * <code>typeinst</code> are non-<code>NULL</code>
274 * and <code>inst</code> is derived from the class of <code>typeinst</code>;
275 * otherwise <code>false</code>.
277 #define OSCheckTypeInst(typeinst, inst) \
278 OSMetaClassBase::checkTypeInst(inst, typeinst)
280 /*! @function OSSafeRelease
281 * @abstract Release an object if not <code>NULL</code>.
282 * @param inst Instance of an OSObject, may be <code>NULL</code>.
284 #define OSSafeRelease(inst) do { if (inst) (inst)->release(); } while (0)
286 /*! @function OSSafeReleaseNULL
287 * @abstract Release an object if not <code>NULL</code>, then set it to <code>NULL</code>.
288 * @param inst Instance of an OSObject, may be <code>NULL</code>.
290 #define OSSafeReleaseNULL(inst) do { if (inst) (inst)->release(); (inst) = NULL; } while (0)
292 typedef void (*_ptf_t
)(void);
294 #if APPLE_KEXT_LEGACY_ABI
296 // Arcane evil code interprets a C++ pointer to function as specified in the
297 // -fapple-kext ABI, i.e. the gcc-2.95 generated code. IT DOES NOT ALLOW
298 // the conversion of functions that are from MULTIPLY inherited classes.
301 _ptmf2ptf(const OSMetaClassBase
*self
, void (OSMetaClassBase::*func
)(void))
304 void (OSMetaClassBase::*fIn
)(void);
305 struct { // Pointer to member function 2.95
306 unsigned short fToff
;
316 if (map
.fptmf2
.fToff
) {
317 panic("Multiple inheritance is not supported");
319 } else if (map
.fptmf2
.fVInd
< 0) {
320 // Not virtual, i.e. plain member func
321 return map
.fptmf2
.u
.fPFN
;
324 const OSMetaClassBase
*fObj
;
329 // Virtual member function so dereference vtable
330 return (*u
.vtablep
)[map
.fptmf2
.fVInd
- 1];
334 #else /* !APPLE_KEXT_LEGACY_ABI */
335 #if defined(__i386__) || defined(__x86_64__)
337 // Slightly less arcane and slightly less evil code to do
338 // the same for kexts compiled with the standard Itanium C++
342 _ptmf2ptf(const OSMetaClassBase
*self
, void (OSMetaClassBase::*func
)(void))
345 void (OSMetaClassBase::*fIn
)(void);
352 if (map
.fVTOffset
& 1) {
355 const OSMetaClassBase
*fObj
;
360 // Virtual member function so dereference vtable
361 return *(_ptf_t
*)(((uintptr_t)*u
.vtablep
) + map
.fVTOffset
- 1);
363 // Not virtual, i.e. plain member func
369 #error Unknown architecture.
372 #endif /* !APPLE_KEXT_LEGACY_ABI */
375 * @define OSMemberFunctionCast
379 * Converts a C++ member function pointer, relative to an instance,
380 * to a C-style pointer to function.
382 * @param cptrtype The function type declaration to cast to
383 * (typically provided as a <code>typedef</code> by I/O KitKit classes).
384 * @param self The <code>this</code> pointer of the object whose function
386 * @param func The pointer to the member function itself,
387 * something like <code>&Class::function</code>.
390 * A pointer to a function of the given type referencing <code>self</code>.
393 * This function is used to generate pointers to C++ functions for instances,
394 * such that they can be registered as callbacks with I/O Kit objects.
396 * No warnings are generated.
398 * This function will panic if an attempt is made to call it
399 * with a multiply-inheriting class.
401 #define OSMemberFunctionCast(cptrtype, self, func) \
402 (cptrtype) OSMetaClassBase:: \
403 _ptmf2ptf(self, (void (OSMetaClassBase::*)(void)) func)
407 virtual ~OSMetaClassBase();
410 // Disable copy constructors of OSMetaClassBase based objects
411 /* Not to be included in headerdoc.
413 * @function operator =
416 * Disable implicit copy constructor by making private
418 * @param src Reference to source object that isn't allowed to be copied.
420 void operator =(OSMetaClassBase
&src
);
422 /* Not to be included in headerdoc.
424 * @function OSMetaClassBase
427 * Disable implicit copy constructor by making private
429 * @param src Reference to source object that isn't allowed to be copied.
431 OSMetaClassBase(OSMetaClassBase
&src
);
435 // xx-review: the original comment for this makes it sound to me like we don't
436 // xx-review: catch over-releasing an object...?
442 * Abstract declaration of
444 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/(int)
445 * release(int freeWhen)@/link</code>.
450 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/(int)
451 * release(int freeWhen)@/link</code>.
453 virtual void release(int freeWhen
) const = 0;
457 * @function getRetainCount
460 * Abstract declaration of
462 * //apple_ref/cpp/instm/OSObject/getRetainCount/virtualint/()
463 * getRetainCount()@/link</code>.
468 * //apple_ref/cpp/instm/OSObject/getRetainCount/virtualint/()
469 * OSObject::getRetainCount()@/link</code>.
471 virtual int getRetainCount() const = 0;
478 * Abstract declaration of
480 * //apple_ref/cpp/instm/OSObject/retain/virtualvoid/()
481 * retain()@/link</code>.
486 * //apple_ref/cpp/instm/OSObject/retain/virtualvoid/()
487 * OSObject::retain()@/link</code>.
489 virtual void retain() const = 0;
496 * Abstract declaration of
498 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/()
499 * release@/link</code>.
504 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/()
505 * OSObject::release@/link</code>.
507 virtual void release() const = 0;
511 * @function serialize
514 * Abstract declaration of
516 * //apple_ref/cpp/instm/OSObject/serialize/virtualbool/(OSSerialize*)
517 * serialize@/link</code>.
522 * //apple_ref/cpp/instm/OSObject/serialize/virtualbool/(OSSerialize*)
523 * OSObject::serialize@/link</code>.
525 virtual bool serialize(OSSerialize
* serializer
) const = 0;
529 * @function getMetaClass
532 * Returns the OSMetaClass representing
533 * an OSMetaClassBase subclass.
536 * OSObject overrides this abstract member function
537 * to return the OSMetaClass object that represents
538 * each class for run-time typing.
540 virtual const OSMetaClass
* getMetaClass() const = 0;
544 * @function isEqualTo
547 * Checks whether another object is equal to the receiver.
549 * @param anObject The object to copmare to the receiver.
552 * <code>true</code> if the objects are equal, <code>false</code> otherwise.
555 * OSMetaClassBase implements this as a direct pointer comparison,
556 * since it has no other information to judge equality by.
557 * Subclasses generally override this function
558 * to do a more meaningful comparison.
559 * For example, OSString implements it to return
560 * <code>true</code> if <code>anObject</code>
561 * is derived from OSString and represents the same C string.
563 virtual bool isEqualTo(const OSMetaClassBase
* anObject
) const;
570 * Casts this object is to the class managed by the given OSMetaClass.
572 * @param toMeta A pointer to a constant OSMetaClass
573 * for the desired target type.
576 * <code>this</code> if the object is derived
577 * from the class managed by <code>toMeta</code>,
578 * otherwise <code>NULL</code>.
581 * It is far more convenient to use
582 * <code>@link OSDynamicCast OSDynamicCast@/link</code>.
584 OSMetaClassBase
* metaCast(const OSMetaClass
* toMeta
) const;
591 * Casts this object is to the class managed by the named OSMetaClass.
593 * @param toMeta An OSSymbol naming the desired target type.
596 * <code>this</code> if the object is derived
597 * from the class named by <code>toMeta</code>,
598 * otherwise <code>NULL</code>.
601 * It is far more convenient to use
602 * <code>@link OSDynamicCast OSDynamicCast@/link</code>.
604 OSMetaClassBase
* metaCast(const OSSymbol
* toMeta
) const;
611 * Casts this object is to the class managed by the named OSMetaClass.
613 * @param toMeta An OSString naming the desired target type.
615 * <code>this</code> if the object is derived
616 * from the class named by <code>toMeta</code>,
617 * otherwise <code>NULL</code>.
620 * It is far more convenient to use
621 * <code>@link OSDynamicCast OSDynamicCast@/link</code>.
623 OSMetaClassBase
* metaCast(const OSString
* toMeta
) const;
630 * Casts this object is to the class managed by the named OSMetaClass.
632 * @param toMeta A C string naming the desired target type.
634 * <code>this</code> if the object is derived
635 * from the class named by <code>toMeta</code>,
636 * otherwise <code>NULL</code>.
639 * It is far more convenient to use
640 * <code>@link OSDynamicCast OSDynamicCast@/link</code>.
642 OSMetaClassBase
* metaCast(const char * toMeta
) const;
644 // Helper inlines for run-time type preprocessor macros
646 * @function safeMetaCast
649 * Casts an object is to the class managed by the given OSMetaClass.
651 * @param anObject A pointer to the object to be cast.
652 * @param toMeta A pointer to a constant OSMetaClass
653 * for the desired target type.
656 * <code>anObject</code> if the object is derived
657 * from the class managed by <code>toMeta</code>,
658 * otherwise <code>NULL</code>.
661 * It is far more convenient to use
662 * <code>@link OSDynamicCast OSDynamicCast@/link</code>.
664 static OSMetaClassBase
* safeMetaCast(
665 const OSMetaClassBase
* anObject
,
666 const OSMetaClass
* toMeta
);
669 * @function checkTypeInst
672 * Checks whether an object instance is of the same class
673 * as another object instance (or a subclass of that class).
675 * @param inst A pointer to the object to check.
676 * @param typeinst A pointer to an object of the class being checked.
679 * <code>true</code> if the object is derived
680 * from the class of <code>typeinst</code>
681 * or a subclass of that class,
682 * otherwise <code>false</code>.
685 * It is far more convenient to use
686 * <code>@link OSCheckTypeInst OSCheckTypeInst@/link</code>.
688 static bool checkTypeInst(
689 const OSMetaClassBase
* inst
,
690 const OSMetaClassBase
* typeinst
);
692 static void initialize(void);
697 * @function taggedRetain
700 * Abstract declaration of
702 * //apple_ref/cpp/instm/OSObject/taggedRetain/virtualvoid/(constvoid*)
703 * taggedRetain(const void *)@/link</code>.
708 * //apple_ref/cpp/instm/OSObject/taggedRetain/virtualvoid/(constvoid*)
709 * OSObject::taggedRetain(const void *)@/link</code>.
711 // WAS: virtual void _RESERVEDOSMetaClassBase0();
712 virtual void taggedRetain(const void * tag
= 0) const = 0;
716 * @function taggedRelease
719 * Abstract declaration of
721 * //apple_ref/cpp/instm/OSObject/taggedRelease/virtualvoid/(constvoid*)
722 * taggedRelease(const void *)@/link</code>.
727 * //apple_ref/cpp/instm/OSObject/taggedRelease/virtualvoid/(constvoid*)
728 * OSObject::taggedRelease(const void *)@/link</code>.
730 // WAS: virtual void _RESERVEDOSMetaClassBase1();
731 virtual void taggedRelease(const void * tag
= 0) const = 0;
735 * @function taggedRelease
738 * Abstract declaration of
740 * //apple_ref/cpp/instm/OSObject/taggedRelease/virtualvoid/(constvoid*,constint)
741 * taggedRelease(const void *, const int freeWhen)@/link</code>.
746 * //apple_ref/cpp/instm/OSObject/taggedRelease/virtualvoid/(constvoid*,constint)
747 * OSObject::taggedRelease(const void *, const int freeWhen)@/link</code>.
749 // WAS: virtual void _RESERVEDOSMetaClassBase2();
750 virtual void taggedRelease(
752 const int freeWhen
) const = 0;
755 #if APPLE_KEXT_VTABLE_PADDING
757 virtual void _RESERVEDOSMetaClassBase3();
758 virtual void _RESERVEDOSMetaClassBase4();
759 virtual void _RESERVEDOSMetaClassBase5();
760 virtual void _RESERVEDOSMetaClassBase6();
761 virtual void _RESERVEDOSMetaClassBase7();
763 } APPLE_KEXT_COMPATIBILITY
;
766 #ifdef XNU_KERNEL_PRIVATE
767 typedef bool (*OSMetaClassInstanceApplierFunction
)(const OSObject
* instance
,
769 #endif /* XNU_KERNEL_PRIVATE */
775 * OSMetaClass manages run-time type information
776 * for Libkern and I/O Kit C++ classes.
780 * OSMetaClass manages run-time type information
781 * for Libkern and I/O Kit C++ classes.
782 * An instance of OSMetaClass exists for (nearly) every such C++ class,
783 * keeping track of inheritance relationships, class lookup by name,
784 * instance counts, and more.
785 * OSMetaClass operates almost entirely behind the scenes,
786 * and kernel extensions should rarely, if ever,
787 * have to interact directly with OSMetaClass.
789 * <b>Use by Kernel Extensions</b>
791 * While kernel extensions rarey interact directly with OSMetaClass at run time,
792 * they must register their classes with the metaclass system
793 * using the macros declared here.
794 * The class declaration should use one of these two macros
795 * before its first member function declaration:
797 * <li><code>@link OSDeclareDefaultStructors OSDeclareDefaultStructors@/link</code> -
798 * for classes with no abstract member function declarations</li>
799 * <li><code>@link OSDeclareAbstractStructors OSDeclareAbstractStructors@/link</code> -
800 * for classes with at least one abstract member function declaration</li>
801 * <li><code>@link OSDeclareFinalStructors OSDeclareFinalStructors@/link</code> -
802 * for classes that should not be subclassable by another kext</li>
805 * The class implementation should then use one of these macros:
807 * <li><code>@link OSDefineMetaClassAndStructors
808 * OSDefineMetaClassAndStructors@/link</code> -
809 * for classes with no abstract member function declarations</li>
810 * <li><code>@link OSDefineMetaClassAndAbstractStructors
811 * OSDefineMetaClassAndAbstractStructors@/link</code> -
812 * for classes with at least one abstract member function declaration</li>
813 * <li><code>@link OSDefineMetaClassAndFinalStructors
814 * OSDefineMetaClassAndFinalStructors@/link</code> -
815 * for classes that should not be subclassable by another kext</li>
818 * Classes in kernel extensions that are intended for use as libraries
819 * may need to reserve vtable slots to preserve binary compatibility
820 * as new functions are added. They may do so with these macros:
822 * <li><code>@link OSMetaClassDeclareReservedUnused
823 * OSMetaClassDeclareReservedUnused@/link</code> -
824 * reserves a vtable slot</li>
825 * <li><code>@link OSMetaClassDefineReservedUnused
826 * OSMetaClassDefineReservedUnused@/link</code> -
827 * defines the reserved vtable slot as an unimplemented function</li>
828 * <li><code>@link OSMetaClassDeclareReservedUsed
829 * OSMetaClassDeclareReservedUsed@/link</code> -
830 * documents that a formerly reserved slot is now used</li>
831 * <li><code>@link OSMetaClassDefineReservedUsed
832 * OSMetaClassDefineReservedUsed@/link</code> -
833 * documents that a formerly reserved slot is now used</li>
836 * <b>Use Restrictions</b>
838 * OSMetaClass should not be explicitly subclassed by kernel extensions
839 * (the declare/define macros do that),
840 * nor should kernel extensions call its run-time type functions directly.
842 * OSMetaClass functions should be considered
843 * <b>unsafe</b> to call in a primary interrupt context.
845 * <b>Concurrency Protection</b>
847 * Kernel extensions should in general not interact
848 * with OSMetaClass objects directly,
849 * instead using the run-time type macros.
850 * Much of OSMetaClass's interface is intended for use
851 * by the run-time type information system,
852 * which handles concurrency and locking internally.
854 class OSMetaClass
: private OSMetaClassBase
858 friend class IOStatistics
;
862 // Can never be allocated must be created at compile time
863 static void * operator new(size_t size
);
865 /* Reserved for future use. (Internal use only) */
866 struct ExpansionData
*reserved
;
868 /* superClass Handle to the superclass's meta class. */
869 const OSMetaClass
*superClassLink
;
871 /* className OSSymbol of the class' name. */
872 const OSSymbol
*className
;
874 /* classSize How big is a single instance of this class. */
875 unsigned int classSize
;
877 /* instanceCount Roughly number of instances of the object,
878 * +1 for each direct subclass with a nonzero refcount.
879 * Used primarily as a code-in-use flag.
881 mutable unsigned int instanceCount
;
883 /* Not to be included in headerdoc.
885 * @function OSMetaClass
888 * The default private constructor.
892 // Called by postModLoad
893 /* Not to be included in headerdoc.
898 * Logs an error string for an <code>OSReturn</code> value
899 * using <code>printf</code>.
901 * @param result The <code>OSReturn</code> value for which to log a message.
904 * This function is used to log errors loading kernel extensions.
905 * Kernel extensions themselves should not call it.
907 static void logError(OSReturn result
);
912 * @function getMetaClassWithName
915 * Look up a metaclass in the run-time type information system.
917 * @param name The name of the desired class's metaclass.
920 * A pointer to the metaclass object if found, <code>NULL</code> otherwise.
922 static const OSMetaClass
* getMetaClassWithName(const OSSymbol
* name
);
929 * Implements the abstract <code>retain</code> function to do nothing.
932 * Since an OSMetaClass instance must remain in existence
933 * for as long as its kernel extension is loaded,
934 * OSMetaClass does not use reference-counting.
936 virtual void retain() const;
943 * Implements the abstract <code>release</code> function to do nothing.
946 * Since an OSMetaClass instance must remain in existence
947 * for as long as its kernel extension is loaded,
948 * OSMetaClass does not use reference-counting.
950 virtual void release() const;
957 * Implements the abstract <code>release(int freeWhen)</code>
958 * function to do nothing.
960 * @param freeWhen Unused.
963 * Since an OSMetaClass instance must remain in existence
964 * for as long as its kernel extension is loaded,
965 * OSMetaClass does not use reference-counting.
967 virtual void release(int freeWhen
) const;
971 * @function taggedRetain
974 * Implements the abstract <code>taggedRetain(const void *)</code>
975 * function to do nothing.
980 * Since an OSMetaClass instance must remain in existence
981 * for as long as its kernel extension is loaded,
982 * OSMetaClass does not use reference-counting.
984 virtual void taggedRetain(const void * tag
= 0) const;
988 * @function taggedRelease
991 * Implements the abstract <code>taggedRelease(const void *)</code>
992 * function to do nothing.
997 * Since an OSMetaClass instance must remain in existence
998 * for as long as its kernel extension is loaded,
999 * OSMetaClass does not use reference-counting.
1001 virtual void taggedRelease(const void * tag
= 0) const;
1005 * @function taggedRelease
1008 * Implements the abstract <code>taggedRelease(const void *, cont int)</code>
1009 * function to do nothing.
1011 * @param tag Unused.
1012 * @param freeWhen Unused.
1015 * Since an OSMetaClass instance must remain in existence
1016 * for as long as its kernel extension is loaded,
1017 * OSMetaClass does not use reference-counting.
1019 virtual void taggedRelease(
1021 const int freeWhen
) const;
1025 * @function getRetainCount
1028 * Implements the abstract <code>getRetainCount</code>
1029 * function to return 0.
1035 * Since an OSMetaClass instance must remain in existence
1036 * for as long as its kernel extension is loaded,
1037 * OSMetaClass does not use reference-counting.
1039 virtual int getRetainCount() const;
1042 /* Not to be included in headerdoc.
1044 * @function getMetaClass
1047 * Returns the meta-metaclass.
1050 * The metaclass of the OSMetaClass object.
1052 virtual const OSMetaClass
* getMetaClass() const;
1056 * @function OSMetaClass
1059 * Constructor for OSMetaClass objects.
1061 * @param className A C string naming the C++ class
1062 * that this OSMetaClass represents.
1063 * @param superclass The OSMetaClass object representing the superclass
1064 * of this metaclass's class.
1065 * @param classSize The allocation size of the represented C++ class.
1068 * This constructor is protected and cannot be used
1069 * to instantiate OSMetaClass directly, as OSMetaClass is an abstract class.
1070 * This function is called during kext loading
1071 * to queue C++ classes for registration.
1072 * See <code>@link preModLoad preModLoad@/link</code> and
1073 * <code>@link postModLoad postModLoad@/link</code>.
1075 OSMetaClass(const char * className
,
1076 const OSMetaClass
* superclass
,
1077 unsigned int classSize
);
1081 * @function ~OSMetaClass
1084 * Destructor for OSMetaClass objects.
1087 * This function is called when the kernel extension that implements
1088 * the metaclass's class is unloaded.
1089 * The destructor removes all references to the class
1090 * from the run-time type information system.
1092 virtual ~OSMetaClass();
1094 // Needs to be overriden as NULL as all OSMetaClass objects are allocated
1095 // statically at compile time, don't accidently try to free them.
1096 void operator delete(void *, size_t) { };
1099 static const OSMetaClass
* const metaClass
;
1102 * @function preModLoad
1105 * Prepares the run-time type system
1106 * for the creation of new metaclasses
1107 * during loading of a kernel extension (module).
1109 * @param kextID The bundle ID of the kext being loaded.
1112 * An opaque handle to the load context
1113 * for the kernel extension on success;
1114 * <code>NULL</code> on failure.
1117 * <i>Not for use by kernel extensions.</i>
1119 * Prepares the run-time type information system to record and register
1120 * metaclasses created by static constructors until a subsequent call to
1121 * <code>@link postModLoad postModLoad@/link</code>.
1122 * <code>preModLoad</code> takes a lock to ensure processing of a single
1123 * load operation at a time; the lock is released by
1124 * <code>@link postModLoad postModLoad@/link</code>.
1125 * Any OSMetaClass constructed between these two function calls
1126 * will be associated with <code>kextID</code>.
1128 static void * preModLoad(const char * kextID
);
1132 * @function checkModLoad
1135 * Checks whether the current kext load operation can proceed.
1137 * @param loadHandle The opaque handle returned
1138 * by <code>@link preModLoad preModLoad@/link</code>.
1140 * <code>true</code> if no errors are outstanding
1141 * and the system is ready to process more metaclasses.
1144 * <i>Not for use by kernel extensions.</i>
1146 static bool checkModLoad(void * loadHandle
);
1150 * @function postModLoad
1153 * Registers the metaclasses created during loading of a kernel extension.
1155 * @param loadHandle The opaque handle returned
1156 * by <code>@link preModLoad preModLoad@/link</code>.
1158 * The error code of the first error encountered,
1161 * //apple_ref/cpp/macro/kOSReturnSuccess
1162 * kOSReturnSuccess@/link</code>
1163 * if no error occurred.
1166 * <i>Not for use by kernel extensions.</i>
1168 * Called after all static constructors in a kernel extension
1169 * have created metaclasses,
1170 * this function checks for duplicate class names,
1171 * then registers the new metaclasses under the kext ID
1172 * that @link preModLoad preModLoad@/link was called with,
1173 * so that they can be dynamically allocated
1174 * and have their instance counts tracked.
1175 * <code>postModLoad</code> releases the lock taken by
1176 * <code>@link preModLoad preModLoad@/link</code>.
1178 static OSReturn
postModLoad(void * loadHandle
);
1181 * @function modHasInstance
1184 * Returns whether any classes defined by the named
1185 * kernel extension (or their subclasses) have existing instances.
1187 * @param kextID The bundle ID of the kernel extension to check.
1190 * <code>true</code> if the kext is found and
1191 * if any class defined by that kext
1192 * has a nonzero instance count,
1193 * <code>false</code> otherwise.
1196 * This function is called before a kernel extension's static destructors
1197 * are invoked, prior to unloading the extension.
1198 * If any classes stil have instances or subclasses with instances,
1199 * those classes are logged
1200 * (using <code>@link reportModInstances reportModInstances@/link</code>) and
1201 * the kernel extension is not be unloaded.
1203 static bool modHasInstance(const char * kextID
);
1207 * @function reportModInstances
1210 * Logs the instance counts for classes
1211 * defined by a kernel extension.
1213 * @param kextID The bundle ID of the kernel extension to report on.
1216 * This function prints the names and instance counts
1217 * of any class defined by <code>kextID</code>
1218 * that has a nonzero instance count.
1219 * It's called by <code>@link modHasInstance modHasInstance@/link</code>
1220 * to help diagnose problems unloading kernel extensions.
1222 static void reportModInstances(const char * kextID
);
1226 * @function considerUnloads
1229 * Schedule automatic unloading of unused kernel extensions.
1232 * This function schedules a check for kernel extensions
1233 * that can be automatically unloaded,
1234 * canceling any currently scheduled check.
1235 * At that time, any such kexts with no Libkern C++ instances
1236 * and no external references are unloaded.
1238 * The I/O Kit calls this function when matching goes idle.
1240 * Kernel extensions that define subclasses of
1241 * @link //apple_ref/doc/class/IOService IOService@/link
1242 * are eligible for automatic unloading.
1244 * (On releases of Mac OS X prior to Snow Leopard (10.6),
1245 * any kernel extension defining any Libkern C++ class
1246 * was eligible for automatic unloading,
1247 * but that unload did not call the module stop routine.
1248 * Non-I/O Kit kernel extensions that define Libkern C++ subclasses
1249 * should be sure to have OSBundleLibraries declarations that ensure
1250 * they will not load on releases prior to Snow Leopard.)
1252 static void considerUnloads();
1256 * @function allocClassWithName
1259 * Allocates an instance of a named OSObject-derived class.
1261 * @param name The name of the desired class.
1264 * A pointer to the newly-allocated, uninitialized object on success;
1265 * <code>NULL</code> on failure.
1268 * Kernel extensions should not need to use this function
1269 * directly, instead using static instance-creation functions
1270 * defined by classes.
1272 * This function consults the run-time type information system
1273 * to find the metaclass for the named class.
1274 * If it exists, it calls the metaclass's <code>@link alloc alloc@/link</code>
1275 * function and returns the result.
1277 static OSObject
* allocClassWithName(const OSSymbol
* name
);
1281 * function allocClassWithName
1284 * Allocates an instance of a named OSObject-derived class.
1286 * @param name The name of the desired class.
1289 * A pointer to the newly-allocated, uninitialized object on success;
1290 * <code>NULL</code> on failure.
1293 * Kernel extensions should not need to use this function
1294 * directly, instead using static instance-creation functions
1295 * defined by classes.
1297 * This function consults the run-time type information system
1298 * to find the metaclass for the named class.
1299 * If it exists, it calls the metaclass's <code>@link alloc alloc@/link</code>
1300 * function and returns the result.
1302 static OSObject
* allocClassWithName(const OSString
* name
);
1306 * function allocClassWithName
1309 * Allocates an instance of a named OSObject-derived class.
1311 * @param name The name of the desired class.
1314 * A pointer to the newly-allocated, uninitialized object on success;
1315 * <code>NULL</code> on failure.
1318 * Kernel extensions should not need to use this function
1319 * directly, instead using static instance-creation functions
1320 * defined by classes.
1322 * This function consults the run-time type information system
1323 * to find the metaclass for the named class.
1324 * If it exists, it calls the metaclass's <code>@link alloc alloc@/link</code>
1325 * function and returns the result.
1327 static OSObject
* allocClassWithName(const char * name
);
1331 * @function checkMetaCastWithName
1334 * Search the metaclass inheritance hierarchy by name for an object instance.
1336 * @param className The name of the desired class or superclass.
1337 * @param object The object whose metaclass begins the search.
1340 * <code>object</code> if it's derived from <code>className</code>;
1341 * <code>NULL</code> otherwise.
1344 * This function is the basis of the Libkern run-time type-checking system.
1345 * Kernel extensions should not use it directly,
1346 * instead using <code>@link OSDynamicCast OSDynamicCast@/link</code> or
1347 * <code>@link OSCheckTypeInst OSCheckTypeInst@/link</code>.
1349 static OSMetaClassBase
* checkMetaCastWithName(
1350 const OSSymbol
* className
,
1351 const OSMetaClassBase
* object
);
1354 * @function checkMetaCastWithName
1357 * Search the metaclass inheritance hierarchy by name for an object instance.
1359 * @param className The name of the desired class or superclass.
1360 * @param object The object whose metaclass begins the search.
1363 * <code>object</code> if it's derived from <code>className</code>;
1364 * <code>NULL</code> otherwise.
1367 * Kernel extensions should not use this function directly,
1368 * instead using <code>@link OSDynamicCast OSDynamicCast@/link</code> or
1369 * <code>@link OSCheckTypeInst OSCheckTypeInst@/link</code>.
1371 static OSMetaClassBase
* checkMetaCastWithName(
1372 const OSString
* className
,
1373 const OSMetaClassBase
* object
);
1376 * @function checkMetaCastWithName
1379 * Search the metaclass inheritance hierarchy by name for an object instance.
1381 * @param className The name of the desired class or superclass.
1382 * @param object The object whose metaclass begins the search.
1385 * <code>object</code> if it's derived from <code>className</code>;
1386 * <code>NULL</code> otherwise.
1389 * Kernel extensions should not use this function directly,
1390 * instead using <code>@link OSDynamicCast OSDynamicCast@/link</code> or
1391 * <code>@link OSCheckTypeInst OSCheckTypeInst@/link</code>.
1393 static OSMetaClassBase
* checkMetaCastWithName(
1394 const char * className
,
1395 const OSMetaClassBase
* object
);
1399 * @function instanceConstructed
1402 * Counts the instances of the class managed by this metaclass.
1405 * <i>Not for use by kernel extensions.</i>
1407 * Every non-abstract class that inherits from OSObject
1408 * has a default constructor that calls it's own metaclass's
1409 * <code>instanceConstructed</code> function.
1410 * This constructor is defined by the
1412 * OSDefineMetaClassAndStructors
1413 * OSDefineMetaClassAndStructors@/link</code>
1414 * macro that all OSObject subclasses must use.
1416 * If a class's instance count goes from 0 to 1--that is,
1417 * upon the creation of the first instance of that class--the
1418 * superclass's instance count is also incremented.
1419 * This propagates reference counts up the inheritance chain so that
1420 * superclasses are counted as "in use" when subclasses have instances.
1422 void instanceConstructed() const;
1426 * @function instanceDestructed
1429 * Counts the instances of the class managed by this metaclass.
1432 * Every non-abstract class that inherits from OSObject
1433 * has a default destructor that calls it's own metaclass's
1434 * <code>instanceDestructed</code> function.
1435 * This constructor is defined by the
1436 * @link OSDefineMetaClassAndStructors OSDefineMetaClassAndStructors@/link
1437 * macro that all OSObject subclasses must use.
1439 * If a class's instance count goes from 1 to 0--that is,
1440 * upon the destruction of the last instance of that class--the
1441 * superclass's instance count is also decremented.
1442 * This reduces "in use" counts from superclasses when their subclasses
1443 * no longer have instances.
1445 void instanceDestructed() const;
1449 * @function checkMetaCast
1452 * Check whether a given object is an instance of the receiving
1453 * metaclass's class or one derived from it.
1455 * @param object The object to check for inheritance.
1458 * <code>object</code> if it is derived from the receiver's class,
1459 * <code>NULL</code> if not.
1461 OSMetaClassBase
* checkMetaCast(const OSMetaClassBase
* object
) const;
1465 * @function getInstanceCount
1468 * Returns the number of existing instances of the metaclass's class.
1471 * The number of existing instances of the metaclass's class,
1472 * plus 1 for each subclass with any instance.
1474 unsigned int getInstanceCount() const;
1478 * @function getSuperClass
1481 * Returns the super-metaclass of the receiver.
1484 * Returns a pointer to the super-metaclass of the receiving
1485 * OSMetaClass, or <code>NULL</code> for OSObject's metaclass.
1487 const OSMetaClass
* getSuperClass() const;
1490 * @function getKmodName
1493 * Returns the bundle identifier of the kernel extension
1494 * that defines this metaclass.
1497 * The bundle identifier of the kernel extension that defines this metaclass.
1500 * "Kmod" is an older term for kernel extension.
1502 const OSSymbol
* getKmodName() const;
1506 * @function getClassName
1509 * Returns the name of the C++ class managed by this metaclass.
1512 * Returns the name of the C++ class managed by this metaclass.
1514 const char * getClassName() const;
1515 const OSSymbol
* getClassNameSymbol() const;
1519 * @function getClassSize
1522 * Returns the allocation size of the C++ class managed by this metaclass.
1525 * The allocation size of the C++ class managed by this metaclass.
1527 unsigned int getClassSize() const;
1534 * Allocates an instance of the C++ class managed by this metaclass.
1537 * A pointer to the newly allocated, uninitialized instance,
1538 * with a retain count of 1; <code>NULL</code> on allocation failure.
1541 * This function is automatically created by the metaclass-registration macros
1542 * to enable dynamic instance allocation.
1544 virtual OSObject
* alloc() const = 0;
1546 #ifdef XNU_KERNEL_PRIVATE
1547 void addInstance(const OSObject
* instance
, bool super
= false) const;
1548 void removeInstance(const OSObject
* instance
, bool super
= false) const;
1549 void applyToInstances(OSMetaClassInstanceApplierFunction applier
,
1550 void * context
) const;
1551 static void applyToInstancesOfClassName(
1552 const OSSymbol
* name
,
1553 OSMetaClassInstanceApplierFunction applier
,
1556 static void applyToInstances(OSOrderedSet
* set
,
1557 OSMetaClassInstanceApplierFunction applier
,
1562 /* Not to be included in headerdoc.
1564 * @define OSDeclareCommonStructors
1568 * Helper macro for for the standard metaclass-registration macros.
1571 * @param className The name of the C++ class, as a raw token,
1572 * <i>not</i> a string or macro.
1574 #define OSDeclareCommonStructors(className) \
1576 static const OSMetaClass * const superClass; \
1578 static const OSMetaClass * const metaClass; \
1579 static class MetaClass : public OSMetaClass { \
1582 virtual OSObject *alloc() const; \
1584 friend class className ::MetaClass; \
1585 virtual const OSMetaClass * getMetaClass() const; \
1587 className (const OSMetaClass *); \
1588 virtual ~ className ()
1592 * @define OSDeclareDefaultStructors
1596 * Declares run-time type information and functions
1597 * for a concrete Libkern C++ class.
1599 * @param className The name of the C++ class, as a raw token,
1600 * <i>not</i> a string or macro.
1603 * Concrete Libkern C++ classes should "call" this macro
1604 * immediately after the opening brace in a class declaration.
1605 * It leaves the current privacy state as <code>protected:</code>.
1607 #define OSDeclareDefaultStructors(className) \
1608 OSDeclareCommonStructors(className); \
1615 * @define OSDeclareAbstractStructors
1619 * Declares run-time type information and functions
1620 * for an abstract Libkern C++ class.
1622 * @param className The name of the C++ class, as a raw token,
1623 * <i>not</i> a string or macro.
1626 * Abstract Libkern C++ classes--those with at least one
1627 * pure virtual method--should "call" this macro
1628 * immediately after the opening brace in a class declaration.
1629 * It leaves the current privacy state as <code>protected:</code>.
1631 #define OSDeclareAbstractStructors(className) \
1632 OSDeclareCommonStructors(className); \
1634 className (); /* Make primary constructor private in abstract */ \
1638 * @define OSDeclareFinalStructors
1642 * Declares run-time type information and functions
1643 * for a final (non-subclassable) Libkern C++ class.
1645 * @param className The name of the C++ class, as a raw token,
1646 * <i>not</i> a string or macro.
1649 * Final Libkern C++ classes--those that do not allow subclassing--should
1650 * "call" this macro immediately after the opening brace in a class declaration.
1651 * (Final classes in the kernel may actually have subclasses in the kernel,
1652 * but kexts cannot define any subclasses of a final class.)
1653 * It leaves the current privacy state as <code>protected:</code>.
1655 * <b>Note:</b> If the class is exported by a pseudokext (symbol set),
1656 * the final symbol generated by this macro must be exported
1657 * for the final-class attribute to be enforced.
1659 * <b>Warning:</b> Changing a class from "Default" to "Final" will break
1660 * binary compatibility.
1662 #define OSDeclareFinalStructors(className) \
1663 OSDeclareDefaultStructors(className) \
1665 void __OSFinalClass(void); \
1669 /* Not to be included in headerdoc.
1671 * @define OSDefineMetaClassWithInit
1675 * Helper macro for for the standard metaclass-registration macros.
1678 * @param className The name of the C++ class, as a raw token,
1679 * <i>not</i> a string or macro.
1680 * @param superclassName The name of the superclass of the C++ class,
1682 * <i>not</i> a string or macro.
1683 * @param init A function to call in the constructor
1684 * of the class's OSMetaClass.
1686 #define OSDefineMetaClassWithInit(className, superclassName, init) \
1687 /* Class global data */ \
1688 className ::MetaClass className ::gMetaClass; \
1689 const OSMetaClass * const className ::metaClass = \
1690 & className ::gMetaClass; \
1691 const OSMetaClass * const className ::superClass = \
1692 & superclassName ::gMetaClass; \
1693 /* Class member functions */ \
1694 className :: className(const OSMetaClass *meta) \
1695 : superclassName (meta) { } \
1696 className ::~ className() { } \
1697 const OSMetaClass * className ::getMetaClass() const \
1698 { return &gMetaClass; } \
1699 /* The ::MetaClass constructor */ \
1700 className ::MetaClass::MetaClass() \
1701 : OSMetaClass(#className, className::superClass, sizeof(className)) \
1705 /* Not to be included in headerdoc.
1707 * @define OSDefineAbstractStructors
1711 * Helper macro for for the standard metaclass-registration macros.
1714 * @param className The name of the C++ class, as a raw token,
1715 * <i>not</i> a string or macro.
1716 * @param superclassName The name of the superclass of the C++ class,
1718 * <i>not</i> a string or macro.
1720 #define OSDefineAbstractStructors(className, superclassName) \
1721 OSObject * className ::MetaClass::alloc() const { return 0; }
1724 /* Not to be included in headerdoc.
1726 * @define OSDefineDefaultStructors
1730 * Helper macro for for the standard metaclass-registration macros.
1733 * @param className The name of the C++ class, as a raw token,
1734 * <i>not</i> a string or macro.
1735 * @param superclassName The name of the superclass of the C++ class,
1737 * <i>not</i> a string or macro.
1739 #define OSDefineDefaultStructors(className, superclassName) \
1740 OSObject * className ::MetaClass::alloc() const \
1741 { return new className; } \
1742 className :: className () : superclassName (&gMetaClass) \
1743 { gMetaClass.instanceConstructed(); }
1745 /* Not to be included in headerdoc.
1747 * @define OSDefineDefaultStructors
1751 * Helper macro for for the standard metaclass-registration macros.
1754 * @param className The name of the C++ class, as a raw token,
1755 * <i>not</i> a string or macro.
1756 * @param superclassName The name of the superclass of the C++ class,
1758 * <i>not</i> a string or macro.
1760 #define OSDefineFinalStructors(className, superclassName) \
1761 OSDefineDefaultStructors(className, superclassName) \
1762 void className ::__OSFinalClass(void) { }
1765 /* Not to be included in headerdoc.
1767 * @define OSDefineMetaClassAndStructorsWithInit
1771 * Helper macro for for the standard metaclass-registration macros.
1774 * @param className The name of the C++ class, as a raw token,
1775 * <i>not</i> a string or macro.
1776 * @param superclassName The name of the superclass of the C++ class,
1778 * <i>not</i> a string or macro.
1779 * @param init A function to call in the constructor
1780 * of the class's OSMetaClass.
1782 #define OSDefineMetaClassAndStructorsWithInit(className, superclassName, init) \
1783 OSDefineMetaClassWithInit(className, superclassName, init) \
1784 OSDefineDefaultStructors(className, superclassName)
1787 /* Not to be included in headerdoc.
1789 * @define OSDefineMetaClassAndAbstractStructorsWithInit
1793 * Helper macro for for the standard metaclass-registration macros.
1796 * @param className The name of the C++ class, as a raw token,
1797 * <i>not</i> a string or macro.
1798 * @param superclassName The name of the superclass of the C++ class,
1800 * <i>not</i> a string or macro.
1801 * @param init A function to call in the constructor
1802 * of the class's OSMetaClass.
1804 #define OSDefineMetaClassAndAbstractStructorsWithInit(className, superclassName, init) \
1805 OSDefineMetaClassWithInit(className, superclassName, init) \
1806 OSDefineAbstractStructors(className, superclassName)
1809 /* Not to be included in headerdoc.
1811 * @define OSDefineMetaClassAndFinalStructorsWithInit
1815 * Helper macro for for the standard metaclass-registration macros.
1818 * @param className The name of the C++ class, as a raw token,
1819 * <i>not</i> a string or macro.
1820 * @param superclassName The name of the superclass of the C++ class,
1822 * <i>not</i> a string or macro.
1823 * @param init A function to call in the constructor
1824 * of the class's OSMetaClass.
1826 #define OSDefineMetaClassAndFinalStructorsWithInit(className, superclassName, init) \
1827 OSDefineMetaClassWithInit(className, superclassName, init) \
1828 OSDefineFinalStructors(className, superclassName)
1833 /* Not to be included in headerdoc.
1835 * @define OSDefineMetaClass
1839 * Helper macro for for the standard metaclass-registration macros.
1842 * @param className The name of the C++ class, as a raw token,
1843 * <i>not</i> a string or macro.
1844 * @param superclassName The name of the superclass of the C++ class,
1846 * <i>not</i> a string or macro.
1847 * @param init A function to call in the constructor
1848 * of the class's OSMetaClass.
1850 #define OSDefineMetaClass(className, superclassName) \
1851 OSDefineMetaClassWithInit(className, superclassName, )
1855 * @define OSDefineMetaClassAndStructors
1859 * Defines an OSMetaClass and associated routines
1860 * for a concrete Libkern C++ class.
1862 * @param className The name of the C++ class, as a raw token,
1863 * <i>not</i> a string or macro.
1864 * @param superclassName The name of the superclass of the C++ class,
1866 * <i>not</i> a string or macro.
1869 * Concrete Libkern C++ classes should "call" this macro
1870 * at the beginning of their implementation files,
1871 * before any function implementations for the class.
1873 #define OSDefineMetaClassAndStructors(className, superclassName) \
1874 OSDefineMetaClassAndStructorsWithInit(className, superclassName, )
1878 * @define OSDefineMetaClassAndAbstractStructors
1882 * Defines an OSMetaClass and associated routines
1883 * for an abstract Libkern C++ class.
1885 * @param className The name of the C++ class, as a raw token,
1886 * <i>not</i> a string or macro.
1887 * @param superclassName The name of the superclass of the C++ class,
1889 * <i>not</i> a string or macro.
1892 * Abstract Libkern C++ classes--those with at least one
1893 * pure virtual method--should "call" this macro
1894 * at the beginning of their implementation files,
1895 * before any function implementations for the class.
1897 #define OSDefineMetaClassAndAbstractStructors(className, superclassName) \
1898 OSDefineMetaClassAndAbstractStructorsWithInit (className, superclassName, )
1902 * @define OSDefineMetaClassAndFinalStructors
1906 * Defines an OSMetaClass and associated routines
1907 * for a final (non-subclassable) Libkern C++ class.
1909 * @param className The name of the C++ class, as a raw token,
1910 * <i>not</i> a string or macro.
1911 * @param superclassName The name of the superclass of the C++ class,
1913 * <i>not</i> a string or macro.
1916 * Final Libkern C++ classes--those that do not allow
1917 * subclassing--should "call" this macro at the beginning
1918 * of their implementation files,
1919 * before any function implementations for the class.
1920 * (Final classes in the kernel may actually have subclasses in the kernel,
1921 * but kexts cannot define any subclasses of a final class.)
1923 * <b>Note:</b> If the class is exported by a pseudokext (symbol set),
1924 * the final symbol generated by this macro must be exported
1925 * for the final-class attribute to be enforced.
1927 * <b>Warning:</b> Changing a class from "Default" to "Final" will break
1928 * binary compatibility.
1930 #define OSDefineMetaClassAndFinalStructors(className, superclassName) \
1931 OSDefineMetaClassAndFinalStructorsWithInit(className, superclassName, )
1934 // Dynamic vtable patchup support routines and types
1935 void reservedCalled(int ind
) const;
1939 * @define OSMetaClassDeclareReservedUnused
1943 * Reserves vtable space for new virtual functions
1944 * in a Libkern C++ class.
1946 * @param className The name of the C++ class, as a raw token,
1947 * <i>not</i> a string or macro.
1948 * @param index The numeric index of the vtable slot,
1949 * as a raw constant, beginning from 0.
1952 * Libkern C++ classes in kernel extensions that can be used as libraries
1953 * can provide for backward compatibility by declaring a number
1954 * of reserved vtable slots
1955 * that can be replaced with new functions as they are added.
1956 * Each reserved declaration must be accompanied in the implementation
1957 * by a corresponding reference to
1958 * <code>@link OSMetaClassDefineReservedUnused
1959 * OSMetaClassDefineReservedUnused@/link</code>.
1961 * When replacing a reserved slot, change the macro from "Unused"
1962 * to "Used" to document the fact that the slot used to be reserved,
1963 * and declare the new function immediately after the "Used" macro
1964 * to preserve vtable ordering.
1966 * <code>@link OSMetaClassDeclareReservedUsed
1967 * OSMetaClassDeclareReservedUsed@/link</code>.
1969 #if APPLE_KEXT_VTABLE_PADDING
1970 #define OSMetaClassDeclareReservedUnused(className, index) \
1972 virtual void _RESERVED ## className ## index ()
1974 #define OSMetaClassDeclareReservedUnused(className, index)
1979 * @define OSMetaClassDeclareReservedUsed
1983 * Documents use of reserved vtable space for new virtual functions
1984 * in a Libkern C++ class.
1986 * @param className The name of the C++ class, as a raw token,
1987 * <i>not</i> a string or macro.
1988 * @param index The numeric index of the vtable slot,
1989 * as a raw constant, beginning from 0.
1992 * This macro evaluates to nothing, and is used to document reserved
1993 * vtable slots as they are filled.
1995 * <code>@link OSMetaClassDeclareReservedUnused
1996 * OSMetaClassDeclareReservedUnused@/link</code>.
1998 #define OSMetaClassDeclareReservedUsed(className, index)
2002 * @define OSMetaClassDefineReservedUnused
2006 * Defines a reserved vtable slot for a Libkern C++ class.
2008 * @param className The name of the C++ class, as a raw token,
2009 * <i>not</i> a string or macro.
2010 * @param index The numeric index of the vtable slot,
2011 * as a raw constant, beginning from 0.
2014 * Libkern C++ classes in kernel extensions that can be used as libraries
2015 * can provide for backward compatibility by declaring a number
2016 * of reserved vtable slots
2017 * that can be replaced with new functions as they are added.
2018 * Each reserved defintion accompanies
2019 * a corresponding declaration created with
2020 * <code>@link OSMetaClassDeclareReservedUnused
2021 * OSMetaClassDeclareReservedUnused@/link</code>.
2023 * This macro is used in the implementation file
2024 * to provide a placeholder definition for the reserved vtable slot,
2025 * as a function that calls <code>panic</code> with an error message.
2027 * When replacing a reserved slot, change the macro from "Unused"
2028 * to "Used" to document the fact that the slot used to be reserved,
2029 * and declare the new function immediately after the "Used" macro
2030 * to preserve vtable ordering.
2032 * <code>@link OSMetaClassDefineReservedUsed
2033 * OSMetaClassDefineReservedUsed@/link</code>.
2035 #if APPLE_KEXT_VTABLE_PADDING
2036 #define OSMetaClassDefineReservedUnused(className, index) \
2037 void className ::_RESERVED ## className ## index () \
2038 { gMetaClass.reservedCalled(index); }
2040 #define OSMetaClassDefineReservedUnused(className, index)
2045 * @define OSMetaClassDefineReservedUsed
2049 * Reserves vtable space for new virtual functions in a Libkern C++ class.
2051 * @param className The name of the C++ class, as a raw token,
2052 * <i>not</i> a string or macro.
2053 * @param index The numeric index of the vtable slot,
2054 * as a raw constant, beginning from 0.
2057 * This macro evaluates to nothing, and is used to document reserved
2058 * vtable slots as they are filled.
2060 * <code>@link OSMetaClassDefineReservedUnused
2061 * OSMetaClassDefineReservedUnused@/link</code>.
2063 #define OSMetaClassDefineReservedUsed(className, index)
2065 // I/O Kit debug internal routines.
2066 static void printInstanceCounts();
2067 static void serializeClassDictionary(OSDictionary
* dict
);
2071 static OSDictionary
* getClassDictionary();
2072 virtual bool serialize(OSSerialize
* serializer
) const;
2074 // Virtual Padding functions for MetaClass's
2075 OSMetaClassDeclareReservedUnused(OSMetaClass
, 0);
2076 OSMetaClassDeclareReservedUnused(OSMetaClass
, 1);
2077 OSMetaClassDeclareReservedUnused(OSMetaClass
, 2);
2078 OSMetaClassDeclareReservedUnused(OSMetaClass
, 3);
2079 OSMetaClassDeclareReservedUnused(OSMetaClass
, 4);
2080 OSMetaClassDeclareReservedUnused(OSMetaClass
, 5);
2081 OSMetaClassDeclareReservedUnused(OSMetaClass
, 6);
2082 OSMetaClassDeclareReservedUnused(OSMetaClass
, 7);
2085 #endif /* !_LIBKERN_OSMETACLASS_H */