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 #if __cplusplus >= 201103L
93 #define APPLE_KEXT_OVERRIDE override
95 #define APPLE_KEXT_COMPATIBILITY_OVERRIDE
97 #define APPLE_KEXT_COMPATIBILITY_OVERRIDE APPLE_KEXT_OVERRIDE
100 #define APPLE_KEXT_OVERRIDE
101 #define APPLE_KEXT_COMPATIBILITY_OVERRIDE
106 * @class OSMetaClassBase
109 * OSMetaClassBase is the abstract bootstrap class
110 * for the Libkern and I/O Kit run-time type information system.
113 * OSMetaClassBase is the abstract C++ root class
114 * underlying the entire Libkern and I/O Kit class hierarchy.
115 * It defines the run-time type information system,
116 * including dynamic class allocation and safe type-casting,
117 * as well as the abstract interface for reference counting
118 * and a few other utility functions.
119 * OSMetaClassBase is the immediate superclass of
120 * @link //apple_ref/doc/class/OSObject OSObject@/link and
121 * @link //apple_ref/doc/class/OSMetaClass OSMetaClass@/link;
122 * no other class should derive from OSMetaClassBase.
124 * For more information, see
125 * <i>@link //apple_ref/doc/uid/TP40002799
126 * I/O Kit Device Driver Design Guidelines@/link</i>.
128 * <b>Use by Kernel Extensions</b>
130 * Kernel Extensions should never interact directly with OSMetaClassBase,
131 * but they will find useful several macros that tie in
132 * to the run-time type information system, specifically:
134 * <li><code>@link OSTypeAlloc OSTypeAlloc@/link</code> - allocation of new instances</li>
135 * <li><code>@link OSDynamicCast OSDynamicCast@/link</code> - safe type casting</li>
136 * <li><code>@link OSCheckTypeInst OSCheckTypeInst@/link</code> -
137 * checking for inheritance/derivation</li>
138 * <li><code>@link OSMemberFunctionCast OSMemberFunctionCast@/link</code> -
139 * casting C++ member functions to C function pointers
140 * for registration as callbacks</li>
143 * See @link //apple_ref/doc/class/OSMetaClass OSMetaClass@/link
144 * for more run-time type information interfaces.
146 * <b>Use Restrictions</b>
148 * OSMetaClassBase should not be subclassed by kernel extensions,
149 * nor should kernel extensions call its run-time type functions directly.
151 * The run-time type functions and macros are <b>not safe</b>
152 * to call in a primary interrupt context.
154 * <b>Concurrency Protection</b>
156 * The run-time type macros and functions of OSMetaClassBase are thread-safe.
158 class OSMetaClassBase
164 * @define OSTypeAlloc
168 * Allocates an instance of the named object class.
170 * @param type The name of the desired class to be created,
171 * as a raw token, <i>not</i> a string or macro.
174 * A pointer to the new, uninitialized object on success;
175 * <code>NULL</code> on failure.
180 * //apple_ref/cpp/clm/OSMetaClass/allocClassWithName/staticOSObject*\/(constchar*)
181 * OSMetaClass::allocClassWithName(const char *)@/link</code>
184 * //apple_ref/cpp/instm/OSMetaClass/alloc/virtualOSObject*\/()
185 * OSMetaClass::alloc@/link</code>.
187 * The OSTypeAlloc macro is used to avoid binary compatibility difficulties
188 * presented by the C++ <code>new</code> operator.
190 #define OSTypeAlloc(type) ((type *) ((type::metaClass)->alloc()))
198 * Returns the type ID (metaclass) of a class based on its name.
200 * @param type The name of the desired class, as a raw token,
201 * <i>not</i> a string or macro.
204 * The unique type ID (metaclass) for the class.
207 * It is typically more useful to determine whether a class is derived
209 * <code>@link //apple_ref/cpp/macro/OSDynamicCast OSDynamicCast@/link</code>
211 * <code>@link //apple_ref/cpp/macro/OSCheckTypeInst OSCheckTypeInst@/link</code>.
213 #define OSTypeID(type) (type::metaClass)
217 * @define OSTypeIDInst
221 * Returns the type ID (metaclass) for the class of an object instance.
223 * @param typeinst An instance of an OSObject subclass.
226 * The type ID of that object's class; that is, its metaclass.
229 * It is typically more useful to determine whether an object is derived
230 * from a particular class; see
231 * <code>@link //apple_ref/cpp/macro/OSDynamicCast OSDynamicCast@/link</code>
233 * <code>@link //apple_ref/cpp/macro/OSCheckTypeInst OSCheckTypeInst@/link</code>.
235 #define OSTypeIDInst(typeinst) ((typeinst)->getMetaClass())
239 * @define OSDynamicCast
243 * Safe type-casting for Libkern C++ objects.
245 * @param type The name of the desired class type, as a raw token,
246 * <i>not</i> a string or macro.
247 * It is assumed you intend to cast to a pointer
248 * to an object of this type.
249 * Type qualifiers, such as <code>const</code>,
250 * are not recognized and will cause
251 * a (usually obscure) compile error.
252 * @param inst A pointer to the object instance to be cast.
253 * May be <code>NULL</code>.
256 * <code>inst</code> if it is non-<code>NULL</code>
257 * and derived from <code>type</code>;
258 * otherwise <code>NULL</code>.
261 * <code>OSDynamicCast</code> is a rough equivalent
262 * to the standard C++ RTTI <code>dynamic_cast<T></code> operator.
263 * Your code should use this instead of raw C type-casting,
264 * and check the resulting value.
265 * If the result is non-<code>NULL</code>,
266 * the object is safe to use as the type-cast class;
267 * if the result is <code>NULL</code>,
268 * the object does not derive from the type-cast class
269 * and your code should take appropriate steps to handle the error.
271 #define OSDynamicCast(type, inst) \
272 ((type *) OSMetaClassBase::safeMetaCast((inst), OSTypeID(type)))
276 * @define OSCheckTypeInst
280 * Checks whether two objects are type-compatible.
282 * @param typeinst The reference object.
283 * @param inst The object to check for type compatibility.
286 * <code>true</code> if both <code>inst</code> and
287 * <code>typeinst</code> are non-<code>NULL</code>
288 * and <code>inst</code> is derived from the class of <code>typeinst</code>;
289 * otherwise <code>false</code>.
291 #define OSCheckTypeInst(typeinst, inst) \
292 OSMetaClassBase::checkTypeInst(inst, typeinst)
294 /*! @function OSSafeRelease
295 * @abstract Release an object if not <code>NULL</code>.
296 * @param inst Instance of an OSObject, may be <code>NULL</code>.
298 #define OSSafeRelease(inst) do { if (inst) (inst)->release(); } while (0)
300 /*! @function OSSafeReleaseNULL
301 * @abstract Release an object if not <code>NULL</code>, then set it to <code>NULL</code>.
302 * @param inst Instance of an OSObject, may be <code>NULL</code>.
304 #define OSSafeReleaseNULL(inst) do { if (inst) (inst)->release(); (inst) = NULL; } while (0)
306 typedef void (*_ptf_t
)(void);
308 #if APPLE_KEXT_LEGACY_ABI
310 // Arcane evil code interprets a C++ pointer to function as specified in the
311 // -fapple-kext ABI, i.e. the gcc-2.95 generated code. IT DOES NOT ALLOW
312 // the conversion of functions that are from MULTIPLY inherited classes.
315 _ptmf2ptf(const OSMetaClassBase
*self
, void (OSMetaClassBase::*func
)(void))
318 void (OSMetaClassBase::*fIn
)(void);
319 struct { // Pointer to member function 2.95
320 unsigned short fToff
;
330 if (map
.fptmf2
.fToff
) {
331 panic("Multiple inheritance is not supported");
333 } else if (map
.fptmf2
.fVInd
< 0) {
334 // Not virtual, i.e. plain member func
335 return map
.fptmf2
.u
.fPFN
;
338 const OSMetaClassBase
*fObj
;
343 // Virtual member function so dereference vtable
344 return (*u
.vtablep
)[map
.fptmf2
.fVInd
- 1];
348 #else /* !APPLE_KEXT_LEGACY_ABI */
349 #if defined(__i386__) || defined(__x86_64__)
351 // Slightly less arcane and slightly less evil code to do
352 // the same for kexts compiled with the standard Itanium C++
356 _ptmf2ptf(const OSMetaClassBase
*self
, void (OSMetaClassBase::*func
)(void))
359 void (OSMetaClassBase::*fIn
)(void);
366 if (map
.fVTOffset
& 1) {
369 const OSMetaClassBase
*fObj
;
374 // Virtual member function so dereference vtable
375 return *(_ptf_t
*)(((uintptr_t)*u
.vtablep
) + map
.fVTOffset
- 1);
377 // Not virtual, i.e. plain member func
383 #error Unknown architecture.
386 #endif /* !APPLE_KEXT_LEGACY_ABI */
389 * @define OSMemberFunctionCast
393 * Converts a C++ member function pointer, relative to an instance,
394 * to a C-style pointer to function.
396 * @param cptrtype The function type declaration to cast to
397 * (typically provided as a <code>typedef</code> by I/O KitKit classes).
398 * @param self The <code>this</code> pointer of the object whose function
400 * @param func The pointer to the member function itself,
401 * something like <code>&Class::function</code>.
404 * A pointer to a function of the given type referencing <code>self</code>.
407 * This function is used to generate pointers to C++ functions for instances,
408 * such that they can be registered as callbacks with I/O Kit objects.
410 * No warnings are generated.
412 * This function will panic if an attempt is made to call it
413 * with a multiply-inheriting class.
415 #define OSMemberFunctionCast(cptrtype, self, func) \
416 (cptrtype) OSMetaClassBase:: \
417 _ptmf2ptf(self, (void (OSMetaClassBase::*)(void)) func)
421 virtual ~OSMetaClassBase();
424 // Disable copy constructors of OSMetaClassBase based objects
425 /* Not to be included in headerdoc.
427 * @function operator =
430 * Disable implicit copy constructor by making private
432 * @param src Reference to source object that isn't allowed to be copied.
434 void operator =(OSMetaClassBase
&src
);
436 /* Not to be included in headerdoc.
438 * @function OSMetaClassBase
441 * Disable implicit copy constructor by making private
443 * @param src Reference to source object that isn't allowed to be copied.
445 OSMetaClassBase(OSMetaClassBase
&src
);
449 // xx-review: the original comment for this makes it sound to me like we don't
450 // xx-review: catch over-releasing an object...?
456 * Abstract declaration of
458 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/(int)
459 * release(int freeWhen)@/link</code>.
464 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/(int)
465 * release(int freeWhen)@/link</code>.
467 virtual void release(int freeWhen
) const = 0;
471 * @function getRetainCount
474 * Abstract declaration of
476 * //apple_ref/cpp/instm/OSObject/getRetainCount/virtualint/()
477 * getRetainCount()@/link</code>.
482 * //apple_ref/cpp/instm/OSObject/getRetainCount/virtualint/()
483 * OSObject::getRetainCount()@/link</code>.
485 virtual int getRetainCount() const = 0;
492 * Abstract declaration of
494 * //apple_ref/cpp/instm/OSObject/retain/virtualvoid/()
495 * retain()@/link</code>.
500 * //apple_ref/cpp/instm/OSObject/retain/virtualvoid/()
501 * OSObject::retain()@/link</code>.
503 virtual void retain() const = 0;
510 * Abstract declaration of
512 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/()
513 * release@/link</code>.
518 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/()
519 * OSObject::release@/link</code>.
521 virtual void release() const = 0;
525 * @function serialize
528 * Abstract declaration of
530 * //apple_ref/cpp/instm/OSObject/serialize/virtualbool/(OSSerialize*)
531 * serialize@/link</code>.
536 * //apple_ref/cpp/instm/OSObject/serialize/virtualbool/(OSSerialize*)
537 * OSObject::serialize@/link</code>.
539 virtual bool serialize(OSSerialize
* serializer
) const = 0;
543 * @function getMetaClass
546 * Returns the OSMetaClass representing
547 * an OSMetaClassBase subclass.
550 * OSObject overrides this abstract member function
551 * to return the OSMetaClass object that represents
552 * each class for run-time typing.
554 virtual const OSMetaClass
* getMetaClass() const = 0;
558 * @function isEqualTo
561 * Checks whether another object is equal to the receiver.
563 * @param anObject The object to copmare to the receiver.
566 * <code>true</code> if the objects are equal, <code>false</code> otherwise.
569 * OSMetaClassBase implements this as a direct pointer comparison,
570 * since it has no other information to judge equality by.
571 * Subclasses generally override this function
572 * to do a more meaningful comparison.
573 * For example, OSString implements it to return
574 * <code>true</code> if <code>anObject</code>
575 * is derived from OSString and represents the same C string.
577 virtual bool isEqualTo(const OSMetaClassBase
* anObject
) const;
584 * Casts this object is to the class managed by the given OSMetaClass.
586 * @param toMeta A pointer to a constant OSMetaClass
587 * for the desired target type.
590 * <code>this</code> if the object is derived
591 * from the class managed by <code>toMeta</code>,
592 * otherwise <code>NULL</code>.
595 * It is far more convenient to use
596 * <code>@link OSDynamicCast OSDynamicCast@/link</code>.
598 OSMetaClassBase
* metaCast(const OSMetaClass
* toMeta
) const;
605 * Casts this object is to the class managed by the named OSMetaClass.
607 * @param toMeta An OSSymbol naming the desired target type.
610 * <code>this</code> if the object is derived
611 * from the class named by <code>toMeta</code>,
612 * otherwise <code>NULL</code>.
615 * It is far more convenient to use
616 * <code>@link OSDynamicCast OSDynamicCast@/link</code>.
618 OSMetaClassBase
* metaCast(const OSSymbol
* toMeta
) const;
625 * Casts this object is to the class managed by the named OSMetaClass.
627 * @param toMeta An OSString naming the desired target type.
629 * <code>this</code> if the object is derived
630 * from the class named by <code>toMeta</code>,
631 * otherwise <code>NULL</code>.
634 * It is far more convenient to use
635 * <code>@link OSDynamicCast OSDynamicCast@/link</code>.
637 OSMetaClassBase
* metaCast(const OSString
* toMeta
) const;
644 * Casts this object is to the class managed by the named OSMetaClass.
646 * @param toMeta A C string naming the desired target type.
648 * <code>this</code> if the object is derived
649 * from the class named by <code>toMeta</code>,
650 * otherwise <code>NULL</code>.
653 * It is far more convenient to use
654 * <code>@link OSDynamicCast OSDynamicCast@/link</code>.
656 OSMetaClassBase
* metaCast(const char * toMeta
) const;
658 // Helper inlines for run-time type preprocessor macros
660 * @function safeMetaCast
663 * Casts an object is to the class managed by the given OSMetaClass.
665 * @param anObject A pointer to the object to be cast.
666 * @param toMeta A pointer to a constant OSMetaClass
667 * for the desired target type.
670 * <code>anObject</code> if the object is derived
671 * from the class managed by <code>toMeta</code>,
672 * otherwise <code>NULL</code>.
675 * It is far more convenient to use
676 * <code>@link OSDynamicCast OSDynamicCast@/link</code>.
678 static OSMetaClassBase
* safeMetaCast(
679 const OSMetaClassBase
* anObject
,
680 const OSMetaClass
* toMeta
);
683 * @function checkTypeInst
686 * Checks whether an object instance is of the same class
687 * as another object instance (or a subclass of that class).
689 * @param inst A pointer to the object to check.
690 * @param typeinst A pointer to an object of the class being checked.
693 * <code>true</code> if the object is derived
694 * from the class of <code>typeinst</code>
695 * or a subclass of that class,
696 * otherwise <code>false</code>.
699 * It is far more convenient to use
700 * <code>@link OSCheckTypeInst OSCheckTypeInst@/link</code>.
702 static bool checkTypeInst(
703 const OSMetaClassBase
* inst
,
704 const OSMetaClassBase
* typeinst
);
706 static void initialize(void);
711 * @function taggedRetain
714 * Abstract declaration of
716 * //apple_ref/cpp/instm/OSObject/taggedRetain/virtualvoid/(constvoid*)
717 * taggedRetain(const void *)@/link</code>.
722 * //apple_ref/cpp/instm/OSObject/taggedRetain/virtualvoid/(constvoid*)
723 * OSObject::taggedRetain(const void *)@/link</code>.
725 // WAS: virtual void _RESERVEDOSMetaClassBase0();
726 virtual void taggedRetain(const void * tag
= 0) const = 0;
730 * @function taggedRelease
733 * Abstract declaration of
735 * //apple_ref/cpp/instm/OSObject/taggedRelease/virtualvoid/(constvoid*)
736 * taggedRelease(const void *)@/link</code>.
741 * //apple_ref/cpp/instm/OSObject/taggedRelease/virtualvoid/(constvoid*)
742 * OSObject::taggedRelease(const void *)@/link</code>.
744 // WAS: virtual void _RESERVEDOSMetaClassBase1();
745 virtual void taggedRelease(const void * tag
= 0) const = 0;
749 * @function taggedRelease
752 * Abstract declaration of
754 * //apple_ref/cpp/instm/OSObject/taggedRelease/virtualvoid/(constvoid*,constint)
755 * taggedRelease(const void *, const int freeWhen)@/link</code>.
760 * //apple_ref/cpp/instm/OSObject/taggedRelease/virtualvoid/(constvoid*,constint)
761 * OSObject::taggedRelease(const void *, const int freeWhen)@/link</code>.
763 // WAS: virtual void _RESERVEDOSMetaClassBase2();
764 virtual void taggedRelease(
766 const int freeWhen
) const = 0;
769 #if APPLE_KEXT_VTABLE_PADDING
771 virtual void _RESERVEDOSMetaClassBase3();
772 virtual void _RESERVEDOSMetaClassBase4();
773 virtual void _RESERVEDOSMetaClassBase5();
774 virtual void _RESERVEDOSMetaClassBase6();
775 virtual void _RESERVEDOSMetaClassBase7();
777 } APPLE_KEXT_COMPATIBILITY
;
780 #ifdef XNU_KERNEL_PRIVATE
781 typedef bool (*OSMetaClassInstanceApplierFunction
)(const OSObject
* instance
,
783 #endif /* XNU_KERNEL_PRIVATE */
789 * OSMetaClass manages run-time type information
790 * for Libkern and I/O Kit C++ classes.
794 * OSMetaClass manages run-time type information
795 * for Libkern and I/O Kit C++ classes.
796 * An instance of OSMetaClass exists for (nearly) every such C++ class,
797 * keeping track of inheritance relationships, class lookup by name,
798 * instance counts, and more.
799 * OSMetaClass operates almost entirely behind the scenes,
800 * and kernel extensions should rarely, if ever,
801 * have to interact directly with OSMetaClass.
803 * <b>Use by Kernel Extensions</b>
805 * While kernel extensions rarey interact directly with OSMetaClass at run time,
806 * they must register their classes with the metaclass system
807 * using the macros declared here.
808 * The class declaration should use one of these two macros
809 * before its first member function declaration:
811 * <li><code>@link OSDeclareDefaultStructors OSDeclareDefaultStructors@/link</code> -
812 * for classes with no abstract member function declarations</li>
813 * <li><code>@link OSDeclareAbstractStructors OSDeclareAbstractStructors@/link</code> -
814 * for classes with at least one abstract member function declaration</li>
815 * <li><code>@link OSDeclareFinalStructors OSDeclareFinalStructors@/link</code> -
816 * for classes that should not be subclassable by another kext</li>
819 * The class implementation should then use one of these macros:
821 * <li><code>@link OSDefineMetaClassAndStructors
822 * OSDefineMetaClassAndStructors@/link</code> -
823 * for classes with no abstract member function declarations</li>
824 * <li><code>@link OSDefineMetaClassAndAbstractStructors
825 * OSDefineMetaClassAndAbstractStructors@/link</code> -
826 * for classes with at least one abstract member function declaration</li>
827 * <li><code>@link OSDefineMetaClassAndFinalStructors
828 * OSDefineMetaClassAndFinalStructors@/link</code> -
829 * for classes that should not be subclassable by another kext</li>
832 * Classes in kernel extensions that are intended for use as libraries
833 * may need to reserve vtable slots to preserve binary compatibility
834 * as new functions are added. They may do so with these macros:
836 * <li><code>@link OSMetaClassDeclareReservedUnused
837 * OSMetaClassDeclareReservedUnused@/link</code> -
838 * reserves a vtable slot</li>
839 * <li><code>@link OSMetaClassDefineReservedUnused
840 * OSMetaClassDefineReservedUnused@/link</code> -
841 * defines the reserved vtable slot as an unimplemented function</li>
842 * <li><code>@link OSMetaClassDeclareReservedUsed
843 * OSMetaClassDeclareReservedUsed@/link</code> -
844 * documents that a formerly reserved slot is now used</li>
845 * <li><code>@link OSMetaClassDefineReservedUsed
846 * OSMetaClassDefineReservedUsed@/link</code> -
847 * documents that a formerly reserved slot is now used</li>
850 * <b>Use Restrictions</b>
852 * OSMetaClass should not be explicitly subclassed by kernel extensions
853 * (the declare/define macros do that),
854 * nor should kernel extensions call its run-time type functions directly.
856 * OSMetaClass functions should be considered
857 * <b>unsafe</b> to call in a primary interrupt context.
859 * <b>Concurrency Protection</b>
861 * Kernel extensions should in general not interact
862 * with OSMetaClass objects directly,
863 * instead using the run-time type macros.
864 * Much of OSMetaClass's interface is intended for use
865 * by the run-time type information system,
866 * which handles concurrency and locking internally.
868 class OSMetaClass
: private OSMetaClassBase
872 friend class IOStatistics
;
876 // Can never be allocated must be created at compile time
877 static void * operator new(size_t size
);
879 /* Reserved for future use. (Internal use only) */
880 struct ExpansionData
*reserved
;
882 /* superClass Handle to the superclass's meta class. */
883 const OSMetaClass
*superClassLink
;
885 /* className OSSymbol of the class' name. */
886 const OSSymbol
*className
;
888 /* classSize How big is a single instance of this class. */
889 unsigned int classSize
;
891 /* instanceCount Roughly number of instances of the object,
892 * +1 for each direct subclass with a nonzero refcount.
893 * Used primarily as a code-in-use flag.
895 mutable unsigned int instanceCount
;
897 /* Not to be included in headerdoc.
899 * @function OSMetaClass
902 * The default private constructor.
906 // Called by postModLoad
907 /* Not to be included in headerdoc.
912 * Logs an error string for an <code>OSReturn</code> value
913 * using <code>printf</code>.
915 * @param result The <code>OSReturn</code> value for which to log a message.
918 * This function is used to log errors loading kernel extensions.
919 * Kernel extensions themselves should not call it.
921 static void logError(OSReturn result
);
926 * @function getMetaClassWithName
929 * Look up a metaclass in the run-time type information system.
931 * @param name The name of the desired class's metaclass.
934 * A pointer to the metaclass object if found, <code>NULL</code> otherwise.
936 static const OSMetaClass
* getMetaClassWithName(const OSSymbol
* name
);
943 * Implements the abstract <code>retain</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 retain() const;
957 * Implements the abstract <code>release</code> function to do nothing.
960 * Since an OSMetaClass instance must remain in existence
961 * for as long as its kernel extension is loaded,
962 * OSMetaClass does not use reference-counting.
964 virtual void release() const;
971 * Implements the abstract <code>release(int freeWhen)</code>
972 * function to do nothing.
974 * @param freeWhen Unused.
977 * Since an OSMetaClass instance must remain in existence
978 * for as long as its kernel extension is loaded,
979 * OSMetaClass does not use reference-counting.
981 virtual void release(int freeWhen
) const;
985 * @function taggedRetain
988 * Implements the abstract <code>taggedRetain(const void *)</code>
989 * function to do nothing.
994 * Since an OSMetaClass instance must remain in existence
995 * for as long as its kernel extension is loaded,
996 * OSMetaClass does not use reference-counting.
998 virtual void taggedRetain(const void * tag
= 0) const;
1002 * @function taggedRelease
1005 * Implements the abstract <code>taggedRelease(const void *)</code>
1006 * function to do nothing.
1008 * @param tag Unused.
1011 * Since an OSMetaClass instance must remain in existence
1012 * for as long as its kernel extension is loaded,
1013 * OSMetaClass does not use reference-counting.
1015 virtual void taggedRelease(const void * tag
= 0) const;
1019 * @function taggedRelease
1022 * Implements the abstract <code>taggedRelease(const void *, cont int)</code>
1023 * function to do nothing.
1025 * @param tag Unused.
1026 * @param freeWhen Unused.
1029 * Since an OSMetaClass instance must remain in existence
1030 * for as long as its kernel extension is loaded,
1031 * OSMetaClass does not use reference-counting.
1033 virtual void taggedRelease(
1035 const int freeWhen
) const;
1039 * @function getRetainCount
1042 * Implements the abstract <code>getRetainCount</code>
1043 * function to return 0.
1049 * Since an OSMetaClass instance must remain in existence
1050 * for as long as its kernel extension is loaded,
1051 * OSMetaClass does not use reference-counting.
1053 virtual int getRetainCount() const;
1056 /* Not to be included in headerdoc.
1058 * @function getMetaClass
1061 * Returns the meta-metaclass.
1064 * The metaclass of the OSMetaClass object.
1066 virtual const OSMetaClass
* getMetaClass() const;
1070 * @function OSMetaClass
1073 * Constructor for OSMetaClass objects.
1075 * @param className A C string naming the C++ class
1076 * that this OSMetaClass represents.
1077 * @param superclass The OSMetaClass object representing the superclass
1078 * of this metaclass's class.
1079 * @param classSize The allocation size of the represented C++ class.
1082 * This constructor is protected and cannot be used
1083 * to instantiate OSMetaClass directly, as OSMetaClass is an abstract class.
1084 * This function is called during kext loading
1085 * to queue C++ classes for registration.
1086 * See <code>@link preModLoad preModLoad@/link</code> and
1087 * <code>@link postModLoad postModLoad@/link</code>.
1089 OSMetaClass(const char * className
,
1090 const OSMetaClass
* superclass
,
1091 unsigned int classSize
);
1095 * @function ~OSMetaClass
1098 * Destructor for OSMetaClass objects.
1101 * This function is called when the kernel extension that implements
1102 * the metaclass's class is unloaded.
1103 * The destructor removes all references to the class
1104 * from the run-time type information system.
1106 virtual ~OSMetaClass();
1108 // Needs to be overriden as NULL as all OSMetaClass objects are allocated
1109 // statically at compile time, don't accidently try to free them.
1110 void operator delete(void *, size_t) { };
1113 static const OSMetaClass
* const metaClass
;
1116 * @function preModLoad
1119 * Prepares the run-time type system
1120 * for the creation of new metaclasses
1121 * during loading of a kernel extension (module).
1123 * @param kextID The bundle ID of the kext being loaded.
1126 * An opaque handle to the load context
1127 * for the kernel extension on success;
1128 * <code>NULL</code> on failure.
1131 * <i>Not for use by kernel extensions.</i>
1133 * Prepares the run-time type information system to record and register
1134 * metaclasses created by static constructors until a subsequent call to
1135 * <code>@link postModLoad postModLoad@/link</code>.
1136 * <code>preModLoad</code> takes a lock to ensure processing of a single
1137 * load operation at a time; the lock is released by
1138 * <code>@link postModLoad postModLoad@/link</code>.
1139 * Any OSMetaClass constructed between these two function calls
1140 * will be associated with <code>kextID</code>.
1142 static void * preModLoad(const char * kextID
);
1146 * @function checkModLoad
1149 * Checks whether the current kext load operation can proceed.
1151 * @param loadHandle The opaque handle returned
1152 * by <code>@link preModLoad preModLoad@/link</code>.
1154 * <code>true</code> if no errors are outstanding
1155 * and the system is ready to process more metaclasses.
1158 * <i>Not for use by kernel extensions.</i>
1160 static bool checkModLoad(void * loadHandle
);
1164 * @function postModLoad
1167 * Registers the metaclasses created during loading of a kernel extension.
1169 * @param loadHandle The opaque handle returned
1170 * by <code>@link preModLoad preModLoad@/link</code>.
1172 * The error code of the first error encountered,
1175 * //apple_ref/cpp/macro/kOSReturnSuccess
1176 * kOSReturnSuccess@/link</code>
1177 * if no error occurred.
1180 * <i>Not for use by kernel extensions.</i>
1182 * Called after all static constructors in a kernel extension
1183 * have created metaclasses,
1184 * this function checks for duplicate class names,
1185 * then registers the new metaclasses under the kext ID
1186 * that @link preModLoad preModLoad@/link was called with,
1187 * so that they can be dynamically allocated
1188 * and have their instance counts tracked.
1189 * <code>postModLoad</code> releases the lock taken by
1190 * <code>@link preModLoad preModLoad@/link</code>.
1192 static OSReturn
postModLoad(void * loadHandle
);
1195 * @function modHasInstance
1198 * Returns whether any classes defined by the named
1199 * kernel extension (or their subclasses) have existing instances.
1201 * @param kextID The bundle ID of the kernel extension to check.
1204 * <code>true</code> if the kext is found and
1205 * if any class defined by that kext
1206 * has a nonzero instance count,
1207 * <code>false</code> otherwise.
1210 * This function is called before a kernel extension's static destructors
1211 * are invoked, prior to unloading the extension.
1212 * If any classes stil have instances or subclasses with instances,
1213 * those classes are logged
1214 * (using <code>@link reportModInstances reportModInstances@/link</code>) and
1215 * the kernel extension is not be unloaded.
1217 static bool modHasInstance(const char * kextID
);
1221 * @function reportModInstances
1224 * Logs the instance counts for classes
1225 * defined by a kernel extension.
1227 * @param kextID The bundle ID of the kernel extension to report on.
1230 * This function prints the names and instance counts
1231 * of any class defined by <code>kextID</code>
1232 * that has a nonzero instance count.
1233 * It's called by <code>@link modHasInstance modHasInstance@/link</code>
1234 * to help diagnose problems unloading kernel extensions.
1236 static void reportModInstances(const char * kextID
);
1240 * @function considerUnloads
1243 * Schedule automatic unloading of unused kernel extensions.
1246 * This function schedules a check for kernel extensions
1247 * that can be automatically unloaded,
1248 * canceling any currently scheduled check.
1249 * At that time, any such kexts with no Libkern C++ instances
1250 * and no external references are unloaded.
1252 * The I/O Kit calls this function when matching goes idle.
1254 * Kernel extensions that define subclasses of
1255 * @link //apple_ref/doc/class/IOService IOService@/link
1256 * are eligible for automatic unloading.
1258 * (On releases of Mac OS X prior to Snow Leopard (10.6),
1259 * any kernel extension defining any Libkern C++ class
1260 * was eligible for automatic unloading,
1261 * but that unload did not call the module stop routine.
1262 * Non-I/O Kit kernel extensions that define Libkern C++ subclasses
1263 * should be sure to have OSBundleLibraries declarations that ensure
1264 * they will not load on releases prior to Snow Leopard.)
1266 static void considerUnloads();
1270 * @function allocClassWithName
1273 * Allocates an instance of a named OSObject-derived class.
1275 * @param name The name of the desired class.
1278 * A pointer to the newly-allocated, uninitialized object on success;
1279 * <code>NULL</code> on failure.
1282 * Kernel extensions should not need to use this function
1283 * directly, instead using static instance-creation functions
1284 * defined by classes.
1286 * This function consults the run-time type information system
1287 * to find the metaclass for the named class.
1288 * If it exists, it calls the metaclass's <code>@link alloc alloc@/link</code>
1289 * function and returns the result.
1291 static OSObject
* allocClassWithName(const OSSymbol
* name
);
1295 * function allocClassWithName
1298 * Allocates an instance of a named OSObject-derived class.
1300 * @param name The name of the desired class.
1303 * A pointer to the newly-allocated, uninitialized object on success;
1304 * <code>NULL</code> on failure.
1307 * Kernel extensions should not need to use this function
1308 * directly, instead using static instance-creation functions
1309 * defined by classes.
1311 * This function consults the run-time type information system
1312 * to find the metaclass for the named class.
1313 * If it exists, it calls the metaclass's <code>@link alloc alloc@/link</code>
1314 * function and returns the result.
1316 static OSObject
* allocClassWithName(const OSString
* name
);
1320 * function allocClassWithName
1323 * Allocates an instance of a named OSObject-derived class.
1325 * @param name The name of the desired class.
1328 * A pointer to the newly-allocated, uninitialized object on success;
1329 * <code>NULL</code> on failure.
1332 * Kernel extensions should not need to use this function
1333 * directly, instead using static instance-creation functions
1334 * defined by classes.
1336 * This function consults the run-time type information system
1337 * to find the metaclass for the named class.
1338 * If it exists, it calls the metaclass's <code>@link alloc alloc@/link</code>
1339 * function and returns the result.
1341 static OSObject
* allocClassWithName(const char * name
);
1345 * @function checkMetaCastWithName
1348 * Search the metaclass inheritance hierarchy by name for an object instance.
1350 * @param className The name of the desired class or superclass.
1351 * @param object The object whose metaclass begins the search.
1354 * <code>object</code> if it's derived from <code>className</code>;
1355 * <code>NULL</code> otherwise.
1358 * This function is the basis of the Libkern run-time type-checking system.
1359 * Kernel extensions should not use it directly,
1360 * instead using <code>@link OSDynamicCast OSDynamicCast@/link</code> or
1361 * <code>@link OSCheckTypeInst OSCheckTypeInst@/link</code>.
1363 static OSMetaClassBase
* checkMetaCastWithName(
1364 const OSSymbol
* className
,
1365 const OSMetaClassBase
* object
);
1368 * @function checkMetaCastWithName
1371 * Search the metaclass inheritance hierarchy by name for an object instance.
1373 * @param className The name of the desired class or superclass.
1374 * @param object The object whose metaclass begins the search.
1377 * <code>object</code> if it's derived from <code>className</code>;
1378 * <code>NULL</code> otherwise.
1381 * Kernel extensions should not use this function directly,
1382 * instead using <code>@link OSDynamicCast OSDynamicCast@/link</code> or
1383 * <code>@link OSCheckTypeInst OSCheckTypeInst@/link</code>.
1385 static OSMetaClassBase
* checkMetaCastWithName(
1386 const OSString
* className
,
1387 const OSMetaClassBase
* object
);
1390 * @function checkMetaCastWithName
1393 * Search the metaclass inheritance hierarchy by name for an object instance.
1395 * @param className The name of the desired class or superclass.
1396 * @param object The object whose metaclass begins the search.
1399 * <code>object</code> if it's derived from <code>className</code>;
1400 * <code>NULL</code> otherwise.
1403 * Kernel extensions should not use this function directly,
1404 * instead using <code>@link OSDynamicCast OSDynamicCast@/link</code> or
1405 * <code>@link OSCheckTypeInst OSCheckTypeInst@/link</code>.
1407 static OSMetaClassBase
* checkMetaCastWithName(
1408 const char * className
,
1409 const OSMetaClassBase
* object
);
1413 * @function instanceConstructed
1416 * Counts the instances of the class managed by this metaclass.
1419 * <i>Not for use by kernel extensions.</i>
1421 * Every non-abstract class that inherits from OSObject
1422 * has a default constructor that calls it's own metaclass's
1423 * <code>instanceConstructed</code> function.
1424 * This constructor is defined by the
1426 * OSDefineMetaClassAndStructors
1427 * OSDefineMetaClassAndStructors@/link</code>
1428 * macro that all OSObject subclasses must use.
1430 * If a class's instance count goes from 0 to 1--that is,
1431 * upon the creation of the first instance of that class--the
1432 * superclass's instance count is also incremented.
1433 * This propagates reference counts up the inheritance chain so that
1434 * superclasses are counted as "in use" when subclasses have instances.
1436 void instanceConstructed() const;
1440 * @function instanceDestructed
1443 * Counts the instances of the class managed by this metaclass.
1446 * Every non-abstract class that inherits from OSObject
1447 * has a default destructor that calls it's own metaclass's
1448 * <code>instanceDestructed</code> function.
1449 * This constructor is defined by the
1450 * @link OSDefineMetaClassAndStructors OSDefineMetaClassAndStructors@/link
1451 * macro that all OSObject subclasses must use.
1453 * If a class's instance count goes from 1 to 0--that is,
1454 * upon the destruction of the last instance of that class--the
1455 * superclass's instance count is also decremented.
1456 * This reduces "in use" counts from superclasses when their subclasses
1457 * no longer have instances.
1459 void instanceDestructed() const;
1463 * @function checkMetaCast
1466 * Check whether a given object is an instance of the receiving
1467 * metaclass's class or one derived from it.
1469 * @param object The object to check for inheritance.
1472 * <code>object</code> if it is derived from the receiver's class,
1473 * <code>NULL</code> if not.
1475 OSMetaClassBase
* checkMetaCast(const OSMetaClassBase
* object
) const;
1479 * @function getInstanceCount
1482 * Returns the number of existing instances of the metaclass's class.
1485 * The number of existing instances of the metaclass's class,
1486 * plus 1 for each subclass with any instance.
1488 unsigned int getInstanceCount() const;
1492 * @function getSuperClass
1495 * Returns the super-metaclass of the receiver.
1498 * Returns a pointer to the super-metaclass of the receiving
1499 * OSMetaClass, or <code>NULL</code> for OSObject's metaclass.
1501 const OSMetaClass
* getSuperClass() const;
1504 * @function getKmodName
1507 * Returns the bundle identifier of the kernel extension
1508 * that defines this metaclass.
1511 * The bundle identifier of the kernel extension that defines this metaclass.
1514 * "Kmod" is an older term for kernel extension.
1516 const OSSymbol
* getKmodName() const;
1520 * @function getClassName
1523 * Returns the name of the C++ class managed by this metaclass.
1526 * Returns the name of the C++ class managed by this metaclass.
1528 const char * getClassName() const;
1529 const OSSymbol
* getClassNameSymbol() const;
1533 * @function getClassSize
1536 * Returns the allocation size of the C++ class managed by this metaclass.
1539 * The allocation size of the C++ class managed by this metaclass.
1541 unsigned int getClassSize() const;
1548 * Allocates an instance of the C++ class managed by this metaclass.
1551 * A pointer to the newly allocated, uninitialized instance,
1552 * with a retain count of 1; <code>NULL</code> on allocation failure.
1555 * This function is automatically created by the metaclass-registration macros
1556 * to enable dynamic instance allocation.
1558 virtual OSObject
* alloc() const = 0;
1560 #ifdef XNU_KERNEL_PRIVATE
1561 void addInstance(const OSObject
* instance
, bool super
= false) const;
1562 void removeInstance(const OSObject
* instance
, bool super
= false) const;
1563 void applyToInstances(OSMetaClassInstanceApplierFunction applier
,
1564 void * context
) const;
1565 static void applyToInstancesOfClassName(
1566 const OSSymbol
* name
,
1567 OSMetaClassInstanceApplierFunction applier
,
1570 static void applyToInstances(OSOrderedSet
* set
,
1571 OSMetaClassInstanceApplierFunction applier
,
1576 /* Not to be included in headerdoc.
1578 * @define OSDeclareCommonStructors
1582 * Helper macro for for the standard metaclass-registration macros.
1585 * @param className The name of the C++ class, as a raw token,
1586 * <i>not</i> a string or macro.
1588 #define OSDeclareCommonStructors(className) \
1590 static const OSMetaClass * const superClass; \
1592 static const OSMetaClass * const metaClass; \
1593 static class MetaClass : public OSMetaClass { \
1596 virtual OSObject *alloc() const; \
1598 friend class className ::MetaClass; \
1599 virtual const OSMetaClass * getMetaClass() const APPLE_KEXT_OVERRIDE; \
1601 className (const OSMetaClass *); \
1602 virtual ~ className ()
1606 * @define OSDeclareDefaultStructors
1610 * Declares run-time type information and functions
1611 * for a concrete Libkern C++ class.
1613 * @param className The name of the C++ class, as a raw token,
1614 * <i>not</i> a string or macro.
1617 * Concrete Libkern C++ classes should "call" this macro
1618 * immediately after the opening brace in a class declaration.
1619 * It leaves the current privacy state as <code>protected:</code>.
1621 #define OSDeclareDefaultStructors(className) \
1622 OSDeclareCommonStructors(className); \
1629 * @define OSDeclareAbstractStructors
1633 * Declares run-time type information and functions
1634 * for an abstract Libkern C++ class.
1636 * @param className The name of the C++ class, as a raw token,
1637 * <i>not</i> a string or macro.
1640 * Abstract Libkern C++ classes--those with at least one
1641 * pure virtual method--should "call" this macro
1642 * immediately after the opening brace in a class declaration.
1643 * It leaves the current privacy state as <code>protected:</code>.
1645 #define OSDeclareAbstractStructors(className) \
1646 OSDeclareCommonStructors(className); \
1648 className (); /* Make primary constructor private in abstract */ \
1652 * @define OSDeclareFinalStructors
1656 * Declares run-time type information and functions
1657 * for a final (non-subclassable) Libkern C++ class.
1659 * @param className The name of the C++ class, as a raw token,
1660 * <i>not</i> a string or macro.
1663 * Final Libkern C++ classes--those that do not allow subclassing--should
1664 * "call" this macro immediately after the opening brace in a class declaration.
1665 * (Final classes in the kernel may actually have subclasses in the kernel,
1666 * but kexts cannot define any subclasses of a final class.)
1667 * It leaves the current privacy state as <code>protected:</code>.
1669 * <b>Note:</b> If the class is exported by a pseudokext (symbol set),
1670 * the final symbol generated by this macro must be exported
1671 * for the final-class attribute to be enforced.
1673 * <b>Warning:</b> Changing a class from "Default" to "Final" will break
1674 * binary compatibility.
1676 #define OSDeclareFinalStructors(className) \
1677 OSDeclareDefaultStructors(className) \
1679 void __OSFinalClass(void); \
1683 /* Not to be included in headerdoc.
1685 * @define OSDefineMetaClassWithInit
1689 * Helper macro for for the standard metaclass-registration macros.
1692 * @param className The name of the C++ class, as a raw token,
1693 * <i>not</i> a string or macro.
1694 * @param superclassName The name of the superclass of the C++ class,
1696 * <i>not</i> a string or macro.
1697 * @param init A function to call in the constructor
1698 * of the class's OSMetaClass.
1700 #define OSDefineMetaClassWithInit(className, superclassName, init) \
1701 /* Class global data */ \
1702 className ::MetaClass className ::gMetaClass; \
1703 const OSMetaClass * const className ::metaClass = \
1704 & className ::gMetaClass; \
1705 const OSMetaClass * const className ::superClass = \
1706 & superclassName ::gMetaClass; \
1707 /* Class member functions */ \
1708 className :: className(const OSMetaClass *meta) \
1709 : superclassName (meta) { } \
1710 className ::~ className() { } \
1711 const OSMetaClass * className ::getMetaClass() const \
1712 { return &gMetaClass; } \
1713 /* The ::MetaClass constructor */ \
1714 className ::MetaClass::MetaClass() \
1715 : OSMetaClass(#className, className::superClass, sizeof(className)) \
1719 /* Not to be included in headerdoc.
1721 * @define OSDefineAbstractStructors
1725 * Helper macro for for the standard metaclass-registration macros.
1728 * @param className The name of the C++ class, as a raw token,
1729 * <i>not</i> a string or macro.
1730 * @param superclassName The name of the superclass of the C++ class,
1732 * <i>not</i> a string or macro.
1734 #define OSDefineAbstractStructors(className, superclassName) \
1735 OSObject * className ::MetaClass::alloc() const { return 0; }
1738 /* Not to be included in headerdoc.
1740 * @define OSDefineDefaultStructors
1744 * Helper macro for for the standard metaclass-registration macros.
1747 * @param className The name of the C++ class, as a raw token,
1748 * <i>not</i> a string or macro.
1749 * @param superclassName The name of the superclass of the C++ class,
1751 * <i>not</i> a string or macro.
1753 #define OSDefineDefaultStructors(className, superclassName) \
1754 OSObject * className ::MetaClass::alloc() const \
1755 { return new className; } \
1756 className :: className () : superclassName (&gMetaClass) \
1757 { gMetaClass.instanceConstructed(); }
1759 /* Not to be included in headerdoc.
1761 * @define OSDefineDefaultStructors
1765 * Helper macro for for the standard metaclass-registration macros.
1768 * @param className The name of the C++ class, as a raw token,
1769 * <i>not</i> a string or macro.
1770 * @param superclassName The name of the superclass of the C++ class,
1772 * <i>not</i> a string or macro.
1774 #define OSDefineFinalStructors(className, superclassName) \
1775 OSDefineDefaultStructors(className, superclassName) \
1776 void className ::__OSFinalClass(void) { }
1779 /* Not to be included in headerdoc.
1781 * @define OSDefineMetaClassAndStructorsWithInit
1785 * Helper macro for for the standard metaclass-registration macros.
1788 * @param className The name of the C++ class, as a raw token,
1789 * <i>not</i> a string or macro.
1790 * @param superclassName The name of the superclass of the C++ class,
1792 * <i>not</i> a string or macro.
1793 * @param init A function to call in the constructor
1794 * of the class's OSMetaClass.
1796 #define OSDefineMetaClassAndStructorsWithInit(className, superclassName, init) \
1797 OSDefineMetaClassWithInit(className, superclassName, init) \
1798 OSDefineDefaultStructors(className, superclassName)
1801 /* Not to be included in headerdoc.
1803 * @define OSDefineMetaClassAndAbstractStructorsWithInit
1807 * Helper macro for for the standard metaclass-registration macros.
1810 * @param className The name of the C++ class, as a raw token,
1811 * <i>not</i> a string or macro.
1812 * @param superclassName The name of the superclass of the C++ class,
1814 * <i>not</i> a string or macro.
1815 * @param init A function to call in the constructor
1816 * of the class's OSMetaClass.
1818 #define OSDefineMetaClassAndAbstractStructorsWithInit(className, superclassName, init) \
1819 OSDefineMetaClassWithInit(className, superclassName, init) \
1820 OSDefineAbstractStructors(className, superclassName)
1823 /* Not to be included in headerdoc.
1825 * @define OSDefineMetaClassAndFinalStructorsWithInit
1829 * Helper macro for for the standard metaclass-registration macros.
1832 * @param className The name of the C++ class, as a raw token,
1833 * <i>not</i> a string or macro.
1834 * @param superclassName The name of the superclass of the C++ class,
1836 * <i>not</i> a string or macro.
1837 * @param init A function to call in the constructor
1838 * of the class's OSMetaClass.
1840 #define OSDefineMetaClassAndFinalStructorsWithInit(className, superclassName, init) \
1841 OSDefineMetaClassWithInit(className, superclassName, init) \
1842 OSDefineFinalStructors(className, superclassName)
1847 /* Not to be included in headerdoc.
1849 * @define OSDefineMetaClass
1853 * Helper macro for for the standard metaclass-registration macros.
1856 * @param className The name of the C++ class, as a raw token,
1857 * <i>not</i> a string or macro.
1858 * @param superclassName The name of the superclass of the C++ class,
1860 * <i>not</i> a string or macro.
1861 * @param init A function to call in the constructor
1862 * of the class's OSMetaClass.
1864 #define OSDefineMetaClass(className, superclassName) \
1865 OSDefineMetaClassWithInit(className, superclassName, )
1869 * @define OSDefineMetaClassAndStructors
1873 * Defines an OSMetaClass and associated routines
1874 * for a concrete Libkern C++ class.
1876 * @param className The name of the C++ class, as a raw token,
1877 * <i>not</i> a string or macro.
1878 * @param superclassName The name of the superclass of the C++ class,
1880 * <i>not</i> a string or macro.
1883 * Concrete Libkern C++ classes should "call" this macro
1884 * at the beginning of their implementation files,
1885 * before any function implementations for the class.
1887 #define OSDefineMetaClassAndStructors(className, superclassName) \
1888 OSDefineMetaClassAndStructorsWithInit(className, superclassName, )
1892 * @define OSDefineMetaClassAndAbstractStructors
1896 * Defines an OSMetaClass and associated routines
1897 * for an abstract Libkern C++ class.
1899 * @param className The name of the C++ class, as a raw token,
1900 * <i>not</i> a string or macro.
1901 * @param superclassName The name of the superclass of the C++ class,
1903 * <i>not</i> a string or macro.
1906 * Abstract Libkern C++ classes--those with at least one
1907 * pure virtual method--should "call" this macro
1908 * at the beginning of their implementation files,
1909 * before any function implementations for the class.
1911 #define OSDefineMetaClassAndAbstractStructors(className, superclassName) \
1912 OSDefineMetaClassAndAbstractStructorsWithInit (className, superclassName, )
1916 * @define OSDefineMetaClassAndFinalStructors
1920 * Defines an OSMetaClass and associated routines
1921 * for a final (non-subclassable) Libkern C++ class.
1923 * @param className The name of the C++ class, as a raw token,
1924 * <i>not</i> a string or macro.
1925 * @param superclassName The name of the superclass of the C++ class,
1927 * <i>not</i> a string or macro.
1930 * Final Libkern C++ classes--those that do not allow
1931 * subclassing--should "call" this macro at the beginning
1932 * of their implementation files,
1933 * before any function implementations for the class.
1934 * (Final classes in the kernel may actually have subclasses in the kernel,
1935 * but kexts cannot define any subclasses of a final class.)
1937 * <b>Note:</b> If the class is exported by a pseudokext (symbol set),
1938 * the final symbol generated by this macro must be exported
1939 * for the final-class attribute to be enforced.
1941 * <b>Warning:</b> Changing a class from "Default" to "Final" will break
1942 * binary compatibility.
1944 #define OSDefineMetaClassAndFinalStructors(className, superclassName) \
1945 OSDefineMetaClassAndFinalStructorsWithInit(className, superclassName, )
1948 // Dynamic vtable patchup support routines and types
1949 void reservedCalled(int ind
) const;
1953 * @define OSMetaClassDeclareReservedUnused
1957 * Reserves vtable space for new virtual functions
1958 * in a Libkern C++ class.
1960 * @param className The name of the C++ class, as a raw token,
1961 * <i>not</i> a string or macro.
1962 * @param index The numeric index of the vtable slot,
1963 * as a raw constant, beginning from 0.
1966 * Libkern C++ classes in kernel extensions that can be used as libraries
1967 * can provide for backward compatibility by declaring a number
1968 * of reserved vtable slots
1969 * that can be replaced with new functions as they are added.
1970 * Each reserved declaration must be accompanied in the implementation
1971 * by a corresponding reference to
1972 * <code>@link OSMetaClassDefineReservedUnused
1973 * OSMetaClassDefineReservedUnused@/link</code>.
1975 * When replacing a reserved slot, change the macro from "Unused"
1976 * to "Used" to document the fact that the slot used to be reserved,
1977 * and declare the new function immediately after the "Used" macro
1978 * to preserve vtable ordering.
1980 * <code>@link OSMetaClassDeclareReservedUsed
1981 * OSMetaClassDeclareReservedUsed@/link</code>.
1983 #if APPLE_KEXT_VTABLE_PADDING
1984 #define OSMetaClassDeclareReservedUnused(className, index) \
1986 virtual void _RESERVED ## className ## index ()
1988 #define OSMetaClassDeclareReservedUnused(className, index)
1993 * @define OSMetaClassDeclareReservedUsed
1997 * Documents use of reserved vtable space for new virtual functions
1998 * in a Libkern C++ class.
2000 * @param className The name of the C++ class, as a raw token,
2001 * <i>not</i> a string or macro.
2002 * @param index The numeric index of the vtable slot,
2003 * as a raw constant, beginning from 0.
2006 * This macro evaluates to nothing, and is used to document reserved
2007 * vtable slots as they are filled.
2009 * <code>@link OSMetaClassDeclareReservedUnused
2010 * OSMetaClassDeclareReservedUnused@/link</code>.
2012 #define OSMetaClassDeclareReservedUsed(className, index)
2016 * @define OSMetaClassDefineReservedUnused
2020 * Defines a reserved vtable slot for a Libkern C++ class.
2022 * @param className The name of the C++ class, as a raw token,
2023 * <i>not</i> a string or macro.
2024 * @param index The numeric index of the vtable slot,
2025 * as a raw constant, beginning from 0.
2028 * Libkern C++ classes in kernel extensions that can be used as libraries
2029 * can provide for backward compatibility by declaring a number
2030 * of reserved vtable slots
2031 * that can be replaced with new functions as they are added.
2032 * Each reserved defintion accompanies
2033 * a corresponding declaration created with
2034 * <code>@link OSMetaClassDeclareReservedUnused
2035 * OSMetaClassDeclareReservedUnused@/link</code>.
2037 * This macro is used in the implementation file
2038 * to provide a placeholder definition for the reserved vtable slot,
2039 * as a function that calls <code>panic</code> with an error message.
2041 * When replacing a reserved slot, change the macro from "Unused"
2042 * to "Used" to document the fact that the slot used to be reserved,
2043 * and declare the new function immediately after the "Used" macro
2044 * to preserve vtable ordering.
2046 * <code>@link OSMetaClassDefineReservedUsed
2047 * OSMetaClassDefineReservedUsed@/link</code>.
2049 #if APPLE_KEXT_VTABLE_PADDING
2050 #define OSMetaClassDefineReservedUnused(className, index) \
2051 void className ::_RESERVED ## className ## index () \
2052 { gMetaClass.reservedCalled(index); }
2054 #define OSMetaClassDefineReservedUnused(className, index)
2059 * @define OSMetaClassDefineReservedUsed
2063 * Reserves vtable space for new virtual functions in a Libkern C++ class.
2065 * @param className The name of the C++ class, as a raw token,
2066 * <i>not</i> a string or macro.
2067 * @param index The numeric index of the vtable slot,
2068 * as a raw constant, beginning from 0.
2071 * This macro evaluates to nothing, and is used to document reserved
2072 * vtable slots as they are filled.
2074 * <code>@link OSMetaClassDefineReservedUnused
2075 * OSMetaClassDefineReservedUnused@/link</code>.
2077 #define OSMetaClassDefineReservedUsed(className, index)
2079 // I/O Kit debug internal routines.
2080 static void printInstanceCounts();
2081 static void serializeClassDictionary(OSDictionary
* dict
);
2082 #ifdef XNU_KERNEL_PRIVATE
2085 static void * trackedNew(size_t size
);
2086 static void trackedDelete(void * mem
, size_t size
);
2087 void trackedInstance(OSObject
* instance
) const;
2088 void trackedFree(OSObject
* instance
) const;
2089 void trackedAccumSize(OSObject
* instance
, size_t size
) const;
2090 struct IOTrackingQueue
* getTracking() const;
2096 static OSDictionary
* getClassDictionary();
2097 virtual bool serialize(OSSerialize
* serializer
) const;
2099 // Virtual Padding functions for MetaClass's
2100 OSMetaClassDeclareReservedUnused(OSMetaClass
, 0);
2101 OSMetaClassDeclareReservedUnused(OSMetaClass
, 1);
2102 OSMetaClassDeclareReservedUnused(OSMetaClass
, 2);
2103 OSMetaClassDeclareReservedUnused(OSMetaClass
, 3);
2104 OSMetaClassDeclareReservedUnused(OSMetaClass
, 4);
2105 OSMetaClassDeclareReservedUnused(OSMetaClass
, 5);
2106 OSMetaClassDeclareReservedUnused(OSMetaClass
, 6);
2107 OSMetaClassDeclareReservedUnused(OSMetaClass
, 7);
2110 #endif /* !_LIBKERN_OSMETACLASS_H */