2 * Copyright (c) 2000-2016 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 */
73 #define APPLE_KEXT_ALIGN_CONTAINERS (0 == APPLE_KEXT_VTABLE_PADDING)
77 #define APPLE_KEXT_LEGACY_ABI 0
79 #define APPLE_KEXT_LEGACY_ABI 1
84 #define APPLE_KEXT_COMPATIBILITY_VIRTUAL
86 // private method made virtual only for binary compatibility
87 #define APPLE_KEXT_COMPATIBILITY_VIRTUAL virtual
91 #define APPLE_KEXT_DEPRECATED __attribute__((deprecated))
94 #if __cplusplus >= 201103L
95 #define APPLE_KEXT_OVERRIDE override
97 #define APPLE_KEXT_COMPATIBILITY_OVERRIDE
99 #define APPLE_KEXT_COMPATIBILITY_OVERRIDE APPLE_KEXT_OVERRIDE
102 #define APPLE_KEXT_OVERRIDE
103 #define APPLE_KEXT_COMPATIBILITY_OVERRIDE
108 * @class OSMetaClassBase
111 * OSMetaClassBase is the abstract bootstrap class
112 * for the Libkern and I/O Kit run-time type information system.
115 * OSMetaClassBase is the abstract C++ root class
116 * underlying the entire Libkern and I/O Kit class hierarchy.
117 * It defines the run-time type information system,
118 * including dynamic class allocation and safe type-casting,
119 * as well as the abstract interface for reference counting
120 * and a few other utility functions.
121 * OSMetaClassBase is the immediate superclass of
122 * @link //apple_ref/doc/class/OSObject OSObject@/link and
123 * @link //apple_ref/doc/class/OSMetaClass OSMetaClass@/link;
124 * no other class should derive from OSMetaClassBase.
126 * For more information, see
127 * <i>@link //apple_ref/doc/uid/TP40002799
128 * I/O Kit Device Driver Design Guidelines@/link</i>.
130 * <b>Use by Kernel Extensions</b>
132 * Kernel Extensions should never interact directly with OSMetaClassBase,
133 * but they will find useful several macros that tie in
134 * to the run-time type information system, specifically:
136 * <li><code>@link OSTypeAlloc OSTypeAlloc@/link</code> - allocation of new instances</li>
137 * <li><code>@link OSDynamicCast OSDynamicCast@/link</code> - safe type casting</li>
138 * <li><code>@link OSCheckTypeInst OSCheckTypeInst@/link</code> -
139 * checking for inheritance/derivation</li>
140 * <li><code>@link OSMemberFunctionCast OSMemberFunctionCast@/link</code> -
141 * casting C++ member functions to C function pointers
142 * for registration as callbacks</li>
145 * See @link //apple_ref/doc/class/OSMetaClass OSMetaClass@/link
146 * for more run-time type information interfaces.
148 * <b>Use Restrictions</b>
150 * OSMetaClassBase should not be subclassed by kernel extensions,
151 * nor should kernel extensions call its run-time type functions directly.
153 * The run-time type functions and macros are <b>not safe</b>
154 * to call in a primary interrupt context.
156 * <b>Concurrency Protection</b>
158 * The run-time type macros and functions of OSMetaClassBase are thread-safe.
160 class OSMetaClassBase
166 * @define OSTypeAlloc
170 * Allocates an instance of the named object class.
172 * @param type The name of the desired class to be created,
173 * as a raw token, <i>not</i> a string or macro.
176 * A pointer to the new, uninitialized object on success;
177 * <code>NULL</code> on failure.
182 * //apple_ref/cpp/clm/OSMetaClass/allocClassWithName/staticOSObject*\/(constchar*)
183 * OSMetaClass::allocClassWithName(const char *)@/link</code>
186 * //apple_ref/cpp/instm/OSMetaClass/alloc/virtualOSObject*\/()
187 * OSMetaClass::alloc@/link</code>.
189 * The OSTypeAlloc macro is used to avoid binary compatibility difficulties
190 * presented by the C++ <code>new</code> operator.
192 #define OSTypeAlloc(type) ((type *) ((type::metaClass)->alloc()))
200 * Returns the type ID (metaclass) of a class based on its name.
202 * @param type The name of the desired class, as a raw token,
203 * <i>not</i> a string or macro.
206 * The unique type ID (metaclass) for the class.
209 * It is typically more useful to determine whether a class is derived
211 * <code>@link //apple_ref/cpp/macro/OSDynamicCast OSDynamicCast@/link</code>
213 * <code>@link //apple_ref/cpp/macro/OSCheckTypeInst OSCheckTypeInst@/link</code>.
215 #define OSTypeID(type) (type::metaClass)
219 * @define OSTypeIDInst
223 * Returns the type ID (metaclass) for the class of an object instance.
225 * @param typeinst An instance of an OSObject subclass.
228 * The type ID of that object's class; that is, its metaclass.
231 * It is typically more useful to determine whether an object is derived
232 * from a particular class; see
233 * <code>@link //apple_ref/cpp/macro/OSDynamicCast OSDynamicCast@/link</code>
235 * <code>@link //apple_ref/cpp/macro/OSCheckTypeInst OSCheckTypeInst@/link</code>.
237 #define OSTypeIDInst(typeinst) ((typeinst)->getMetaClass())
241 * @define OSDynamicCast
245 * Safe type-casting for Libkern C++ objects.
247 * @param type The name of the desired class type, as a raw token,
248 * <i>not</i> a string or macro.
249 * It is assumed you intend to cast to a pointer
250 * to an object of this type.
251 * Type qualifiers, such as <code>const</code>,
252 * are not recognized and will cause
253 * a (usually obscure) compile error.
254 * @param inst A pointer to the object instance to be cast.
255 * May be <code>NULL</code>.
258 * <code>inst</code> if it is non-<code>NULL</code>
259 * and derived from <code>type</code>;
260 * otherwise <code>NULL</code>.
263 * <code>OSDynamicCast</code> is a rough equivalent
264 * to the standard C++ RTTI <code>dynamic_cast<T></code> operator.
265 * Your code should use this instead of raw C type-casting,
266 * and check the resulting value.
267 * If the result is non-<code>NULL</code>,
268 * the object is safe to use as the type-cast class;
269 * if the result is <code>NULL</code>,
270 * the object does not derive from the type-cast class
271 * and your code should take appropriate steps to handle the error.
273 #define OSDynamicCast(type, inst) \
274 ((type *) OSMetaClassBase::safeMetaCast((inst), OSTypeID(type)))
278 * @define OSCheckTypeInst
282 * Checks whether two objects are type-compatible.
284 * @param typeinst The reference object.
285 * @param inst The object to check for type compatibility.
288 * <code>true</code> if both <code>inst</code> and
289 * <code>typeinst</code> are non-<code>NULL</code>
290 * and <code>inst</code> is derived from the class of <code>typeinst</code>;
291 * otherwise <code>false</code>.
293 #define OSCheckTypeInst(typeinst, inst) \
294 OSMetaClassBase::checkTypeInst(inst, typeinst)
296 #define OSSafeRelease(inst) \
297 do { int OSSafeRelease __attribute__ ((deprecated("Use OSSafeReleaseNULL"))); (OSSafeRelease); \
298 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.
792 * @discussion OSMetaClass manages run-time type information
793 * for Libkern and I/O Kit C++ classes.
794 * An instance of OSMetaClass exists for (nearly) every such C++ class,
795 * keeping track of inheritance relationships, class lookup by name,
796 * instance counts, and more.
797 * OSMetaClass operates almost entirely behind the scenes,
798 * and kernel extensions should rarely, if ever,
799 * have to interact directly with OSMetaClass.
801 * <b>Use by Kernel Extensions</b>
803 * While kernel extensions rarey interact directly with OSMetaClass at run time,
804 * they must register their classes with the metaclass system
805 * using the macros declared here.
806 * The class declaration should use one of these two macros
807 * before its first member function declaration:
809 * <li><code>@link OSDeclareDefaultStructors OSDeclareDefaultStructors@/link</code> -
810 * for classes with no abstract member function declarations</li>
811 * <li><code>@link OSDeclareAbstractStructors OSDeclareAbstractStructors@/link</code> -
812 * for classes with at least one abstract member function declaration</li>
813 * <li><code>@link OSDeclareFinalStructors OSDeclareFinalStructors@/link</code> -
814 * for classes that should not be subclassable by another kext</li>
817 * The class implementation should then use one of these macros:
819 * <li><code>@link OSDefineMetaClassAndStructors
820 * OSDefineMetaClassAndStructors@/link</code> -
821 * for classes with no abstract member function declarations</li>
822 * <li><code>@link OSDefineMetaClassAndAbstractStructors
823 * OSDefineMetaClassAndAbstractStructors@/link</code> -
824 * for classes with at least one abstract member function declaration</li>
825 * <li><code>@link OSDefineMetaClassAndFinalStructors
826 * OSDefineMetaClassAndFinalStructors@/link</code> -
827 * for classes that should not be subclassable by another kext</li>
830 * Classes in kernel extensions that are intended for use as libraries
831 * may need to reserve vtable slots to preserve binary compatibility
832 * as new functions are added. They may do so with these macros:
834 * <li><code>@link OSMetaClassDeclareReservedUnused
835 * OSMetaClassDeclareReservedUnused@/link</code> -
836 * reserves a vtable slot</li>
837 * <li><code>@link OSMetaClassDefineReservedUnused
838 * OSMetaClassDefineReservedUnused@/link</code> -
839 * defines the reserved vtable slot as an unimplemented function</li>
840 * <li><code>@link OSMetaClassDeclareReservedUsed
841 * OSMetaClassDeclareReservedUsed@/link</code> -
842 * documents that a formerly reserved slot is now used</li>
843 * <li><code>@link OSMetaClassDefineReservedUsed
844 * OSMetaClassDefineReservedUsed@/link</code> -
845 * documents that a formerly reserved slot is now used</li>
848 * <b>Use Restrictions</b>
850 * OSMetaClass should not be explicitly subclassed by kernel extensions
851 * (the declare/define macros do that),
852 * nor should kernel extensions call its run-time type functions directly.
854 * OSMetaClass functions should be considered
855 * <b>unsafe</b> to call in a primary interrupt context.
857 * <b>Concurrency Protection</b>
859 * Kernel extensions should in general not interact
860 * with OSMetaClass objects directly,
861 * instead using the run-time type macros.
862 * Much of OSMetaClass's interface is intended for use
863 * by the run-time type information system,
864 * which handles concurrency and locking internally.
866 class OSMetaClass
: private OSMetaClassBase
870 friend class IOStatistics
;
874 // Can never be allocated must be created at compile time
875 static void * operator new(size_t size
);
877 /* Reserved for future use. (Internal use only) */
878 struct ExpansionData
*reserved
;
880 /* superClass Handle to the superclass's meta class. */
881 const OSMetaClass
*superClassLink
;
883 /* className OSSymbol of the class' name. */
884 const OSSymbol
*className
;
886 /* classSize How big is a single instance of this class. */
887 unsigned int classSize
;
889 /* instanceCount Roughly number of instances of the object,
890 * +1 for each direct subclass with a nonzero refcount.
891 * Used primarily as a code-in-use flag.
893 mutable unsigned int instanceCount
;
895 /* Not to be included in headerdoc.
897 * @function OSMetaClass
900 * The default private constructor.
904 // Called by postModLoad
905 /* Not to be included in headerdoc.
910 * Logs an error string for an <code>OSReturn</code> value
911 * using <code>printf</code>.
913 * @param result The <code>OSReturn</code> value for which to log a message.
916 * This function is used to log errors loading kernel extensions.
917 * Kernel extensions themselves should not call it.
919 static void logError(OSReturn result
);
924 * @function getMetaClassWithName
927 * Look up a metaclass in the run-time type information system.
929 * @param name The name of the desired class's metaclass.
932 * A pointer to the metaclass object if found, <code>NULL</code> otherwise.
934 static const OSMetaClass
* getMetaClassWithName(const OSSymbol
* name
);
941 * Implements the abstract <code>retain</code> function to do nothing.
944 * Since an OSMetaClass instance must remain in existence
945 * for as long as its kernel extension is loaded,
946 * OSMetaClass does not use reference-counting.
948 virtual void retain() const;
955 * Implements the abstract <code>release</code> function to do nothing.
958 * Since an OSMetaClass instance must remain in existence
959 * for as long as its kernel extension is loaded,
960 * OSMetaClass does not use reference-counting.
962 virtual void release() const;
969 * Implements the abstract <code>release(int freeWhen)</code>
970 * function to do nothing.
972 * @param freeWhen Unused.
975 * Since an OSMetaClass instance must remain in existence
976 * for as long as its kernel extension is loaded,
977 * OSMetaClass does not use reference-counting.
979 virtual void release(int freeWhen
) const;
983 * @function taggedRetain
986 * Implements the abstract <code>taggedRetain(const void *)</code>
987 * function to do nothing.
992 * Since an OSMetaClass instance must remain in existence
993 * for as long as its kernel extension is loaded,
994 * OSMetaClass does not use reference-counting.
996 virtual void taggedRetain(const void * tag
= 0) const;
1000 * @function taggedRelease
1003 * Implements the abstract <code>taggedRelease(const void *)</code>
1004 * function to do nothing.
1006 * @param tag Unused.
1009 * Since an OSMetaClass instance must remain in existence
1010 * for as long as its kernel extension is loaded,
1011 * OSMetaClass does not use reference-counting.
1013 virtual void taggedRelease(const void * tag
= 0) const;
1017 * @function taggedRelease
1020 * Implements the abstract <code>taggedRelease(const void *, cont int)</code>
1021 * function to do nothing.
1023 * @param tag Unused.
1024 * @param freeWhen Unused.
1027 * Since an OSMetaClass instance must remain in existence
1028 * for as long as its kernel extension is loaded,
1029 * OSMetaClass does not use reference-counting.
1031 virtual void taggedRelease(
1033 const int freeWhen
) const;
1037 * @function getRetainCount
1040 * Implements the abstract <code>getRetainCount</code>
1041 * function to return 0.
1047 * Since an OSMetaClass instance must remain in existence
1048 * for as long as its kernel extension is loaded,
1049 * OSMetaClass does not use reference-counting.
1051 virtual int getRetainCount() const;
1054 /* Not to be included in headerdoc.
1056 * @function getMetaClass
1059 * Returns the meta-metaclass.
1062 * The metaclass of the OSMetaClass object.
1064 virtual const OSMetaClass
* getMetaClass() const;
1068 * @function OSMetaClass
1071 * Constructor for OSMetaClass objects.
1073 * @param className A C string naming the C++ class
1074 * that this OSMetaClass represents.
1075 * @param superclass The OSMetaClass object representing the superclass
1076 * of this metaclass's class.
1077 * @param classSize The allocation size of the represented C++ class.
1080 * This constructor is protected and cannot be used
1081 * to instantiate OSMetaClass directly, as OSMetaClass is an abstract class.
1082 * This function is called during kext loading
1083 * to queue C++ classes for registration.
1084 * See <code>@link preModLoad preModLoad@/link</code> and
1085 * <code>@link postModLoad postModLoad@/link</code>.
1087 OSMetaClass(const char * className
,
1088 const OSMetaClass
* superclass
,
1089 unsigned int classSize
);
1093 * @function ~OSMetaClass
1096 * Destructor for OSMetaClass objects.
1099 * This function is called when the kernel extension that implements
1100 * the metaclass's class is unloaded.
1101 * The destructor removes all references to the class
1102 * from the run-time type information system.
1104 virtual ~OSMetaClass();
1106 // Needs to be overriden as NULL as all OSMetaClass objects are allocated
1107 // statically at compile time, don't accidently try to free them.
1108 void operator delete(void *, size_t) { }
1111 static const OSMetaClass
* const metaClass
;
1114 * @function preModLoad
1117 * Prepares the run-time type system
1118 * for the creation of new metaclasses
1119 * during loading of a kernel extension (module).
1121 * @param kextID The bundle ID of the kext being loaded.
1124 * An opaque handle to the load context
1125 * for the kernel extension on success;
1126 * <code>NULL</code> on failure.
1129 * <i>Not for use by kernel extensions.</i>
1131 * Prepares the run-time type information system to record and register
1132 * metaclasses created by static constructors until a subsequent call to
1133 * <code>@link postModLoad postModLoad@/link</code>.
1134 * <code>preModLoad</code> takes a lock to ensure processing of a single
1135 * load operation at a time; the lock is released by
1136 * <code>@link postModLoad postModLoad@/link</code>.
1137 * Any OSMetaClass constructed between these two function calls
1138 * will be associated with <code>kextID</code>.
1140 static void * preModLoad(const char * kextID
);
1144 * @function checkModLoad
1147 * Checks whether the current kext load operation can proceed.
1149 * @param loadHandle The opaque handle returned
1150 * by <code>@link preModLoad preModLoad@/link</code>.
1152 * <code>true</code> if no errors are outstanding
1153 * and the system is ready to process more metaclasses.
1156 * <i>Not for use by kernel extensions.</i>
1158 static bool checkModLoad(void * loadHandle
);
1162 * @function postModLoad
1165 * Registers the metaclasses created during loading of a kernel extension.
1167 * @param loadHandle The opaque handle returned
1168 * by <code>@link preModLoad preModLoad@/link</code>.
1170 * The error code of the first error encountered,
1173 * //apple_ref/cpp/macro/kOSReturnSuccess
1174 * kOSReturnSuccess@/link</code>
1175 * if no error occurred.
1178 * <i>Not for use by kernel extensions.</i>
1180 * Called after all static constructors in a kernel extension
1181 * have created metaclasses,
1182 * this function checks for duplicate class names,
1183 * then registers the new metaclasses under the kext ID
1184 * that @link preModLoad preModLoad@/link was called with,
1185 * so that they can be dynamically allocated
1186 * and have their instance counts tracked.
1187 * <code>postModLoad</code> releases the lock taken by
1188 * <code>@link preModLoad preModLoad@/link</code>.
1190 static OSReturn
postModLoad(void * loadHandle
);
1193 * @function modHasInstance
1196 * Returns whether any classes defined by the named
1197 * kernel extension (or their subclasses) have existing instances.
1199 * @param kextID The bundle ID of the kernel extension to check.
1202 * <code>true</code> if the kext is found and
1203 * if any class defined by that kext
1204 * has a nonzero instance count,
1205 * <code>false</code> otherwise.
1208 * This function is called before a kernel extension's static destructors
1209 * are invoked, prior to unloading the extension.
1210 * If any classes stil have instances or subclasses with instances,
1211 * those classes are logged
1212 * (using <code>@link reportModInstances reportModInstances@/link</code>) and
1213 * the kernel extension is not be unloaded.
1215 static bool modHasInstance(const char * kextID
);
1219 * @function reportModInstances
1222 * Logs the instance counts for classes
1223 * defined by a kernel extension.
1225 * @param kextID The bundle ID of the kernel extension to report on.
1228 * This function prints the names and instance counts
1229 * of any class defined by <code>kextID</code>
1230 * that has a nonzero instance count.
1231 * It's called by <code>@link modHasInstance modHasInstance@/link</code>
1232 * to help diagnose problems unloading kernel extensions.
1234 static void reportModInstances(const char * kextID
);
1238 * @function considerUnloads
1241 * Schedule automatic unloading of unused kernel extensions.
1244 * This function schedules a check for kernel extensions
1245 * that can be automatically unloaded,
1246 * canceling any currently scheduled check.
1247 * At that time, any such kexts with no Libkern C++ instances
1248 * and no external references are unloaded.
1250 * The I/O Kit calls this function when matching goes idle.
1252 * Kernel extensions that define subclasses of
1253 * @link //apple_ref/doc/class/IOService IOService@/link
1254 * are eligible for automatic unloading.
1256 * (On releases of Mac OS X prior to Snow Leopard (10.6),
1257 * any kernel extension defining any Libkern C++ class
1258 * was eligible for automatic unloading,
1259 * but that unload did not call the module stop routine.
1260 * Non-I/O Kit kernel extensions that define Libkern C++ subclasses
1261 * should be sure to have OSBundleLibraries declarations that ensure
1262 * they will not load on releases prior to Snow Leopard.)
1264 static void considerUnloads();
1268 * @function allocClassWithName
1271 * Allocates an instance of a named OSObject-derived class.
1273 * @param name The name of the desired class.
1276 * A pointer to the newly-allocated, uninitialized object on success;
1277 * <code>NULL</code> on failure.
1280 * Kernel extensions should not need to use this function
1281 * directly, instead using static instance-creation functions
1282 * defined by classes.
1284 * This function consults the run-time type information system
1285 * to find the metaclass for the named class.
1286 * If it exists, it calls the metaclass's <code>@link alloc alloc@/link</code>
1287 * function and returns the result.
1289 static OSObject
* allocClassWithName(const OSSymbol
* name
);
1293 * function allocClassWithName
1296 * Allocates an instance of a named OSObject-derived class.
1298 * @param name The name of the desired class.
1301 * A pointer to the newly-allocated, uninitialized object on success;
1302 * <code>NULL</code> on failure.
1305 * Kernel extensions should not need to use this function
1306 * directly, instead using static instance-creation functions
1307 * defined by classes.
1309 * This function consults the run-time type information system
1310 * to find the metaclass for the named class.
1311 * If it exists, it calls the metaclass's <code>@link alloc alloc@/link</code>
1312 * function and returns the result.
1314 static OSObject
* allocClassWithName(const OSString
* name
);
1318 * function allocClassWithName
1321 * Allocates an instance of a named OSObject-derived class.
1323 * @param name The name of the desired class.
1326 * A pointer to the newly-allocated, uninitialized object on success;
1327 * <code>NULL</code> on failure.
1330 * Kernel extensions should not need to use this function
1331 * directly, instead using static instance-creation functions
1332 * defined by classes.
1334 * This function consults the run-time type information system
1335 * to find the metaclass for the named class.
1336 * If it exists, it calls the metaclass's <code>@link alloc alloc@/link</code>
1337 * function and returns the result.
1339 static OSObject
* allocClassWithName(const char * name
);
1343 * @function checkMetaCastWithName
1346 * Search the metaclass inheritance hierarchy by name for an object instance.
1348 * @param className The name of the desired class or superclass.
1349 * @param object The object whose metaclass begins the search.
1352 * <code>object</code> if it's derived from <code>className</code>;
1353 * <code>NULL</code> otherwise.
1356 * This function is the basis of the Libkern run-time type-checking system.
1357 * Kernel extensions should not use it directly,
1358 * instead using <code>@link OSDynamicCast OSDynamicCast@/link</code> or
1359 * <code>@link OSCheckTypeInst OSCheckTypeInst@/link</code>.
1361 static OSMetaClassBase
* checkMetaCastWithName(
1362 const OSSymbol
* className
,
1363 const OSMetaClassBase
* object
);
1366 * @function checkMetaCastWithName
1369 * Search the metaclass inheritance hierarchy by name for an object instance.
1371 * @param className The name of the desired class or superclass.
1372 * @param object The object whose metaclass begins the search.
1375 * <code>object</code> if it's derived from <code>className</code>;
1376 * <code>NULL</code> otherwise.
1379 * Kernel extensions should not use this function directly,
1380 * instead using <code>@link OSDynamicCast OSDynamicCast@/link</code> or
1381 * <code>@link OSCheckTypeInst OSCheckTypeInst@/link</code>.
1383 static OSMetaClassBase
* checkMetaCastWithName(
1384 const OSString
* className
,
1385 const OSMetaClassBase
* object
);
1388 * @function checkMetaCastWithName
1391 * Search the metaclass inheritance hierarchy by name for an object instance.
1393 * @param className The name of the desired class or superclass.
1394 * @param object The object whose metaclass begins the search.
1397 * <code>object</code> if it's derived from <code>className</code>;
1398 * <code>NULL</code> otherwise.
1401 * Kernel extensions should not use this function directly,
1402 * instead using <code>@link OSDynamicCast OSDynamicCast@/link</code> or
1403 * <code>@link OSCheckTypeInst OSCheckTypeInst@/link</code>.
1405 static OSMetaClassBase
* checkMetaCastWithName(
1406 const char * className
,
1407 const OSMetaClassBase
* object
);
1411 * @function instanceConstructed
1414 * Counts the instances of the class managed by this metaclass.
1417 * <i>Not for use by kernel extensions.</i>
1419 * Every non-abstract class that inherits from OSObject
1420 * has a default constructor that calls it's own metaclass's
1421 * <code>instanceConstructed</code> function.
1422 * This constructor is defined by the
1424 * OSDefineMetaClassAndStructors
1425 * OSDefineMetaClassAndStructors@/link</code>
1426 * macro that all OSObject subclasses must use.
1428 * If a class's instance count goes from 0 to 1--that is,
1429 * upon the creation of the first instance of that class--the
1430 * superclass's instance count is also incremented.
1431 * This propagates reference counts up the inheritance chain so that
1432 * superclasses are counted as "in use" when subclasses have instances.
1434 void instanceConstructed() const;
1438 * @function instanceDestructed
1441 * Counts the instances of the class managed by this metaclass.
1444 * Every non-abstract class that inherits from OSObject
1445 * has a default destructor that calls it's own metaclass's
1446 * <code>instanceDestructed</code> function.
1447 * This constructor is defined by the
1448 * @link OSDefineMetaClassAndStructors OSDefineMetaClassAndStructors@/link
1449 * macro that all OSObject subclasses must use.
1451 * If a class's instance count goes from 1 to 0--that is,
1452 * upon the destruction of the last instance of that class--the
1453 * superclass's instance count is also decremented.
1454 * This reduces "in use" counts from superclasses when their subclasses
1455 * no longer have instances.
1457 void instanceDestructed() const;
1461 * @function checkMetaCast
1464 * Check whether a given object is an instance of the receiving
1465 * metaclass's class or one derived from it.
1467 * @param object The object to check for inheritance.
1470 * <code>object</code> if it is derived from the receiver's class,
1471 * <code>NULL</code> if not.
1473 OSMetaClassBase
* checkMetaCast(const OSMetaClassBase
* object
) const;
1477 * @function getInstanceCount
1480 * Returns the number of existing instances of the metaclass's class.
1483 * The number of existing instances of the metaclass's class,
1484 * plus 1 for each subclass with any instance.
1486 unsigned int getInstanceCount() const;
1490 * @function getSuperClass
1493 * Returns the super-metaclass of the receiver.
1496 * Returns a pointer to the super-metaclass of the receiving
1497 * OSMetaClass, or <code>NULL</code> for OSObject's metaclass.
1499 const OSMetaClass
* getSuperClass() const;
1502 * @function getKmodName
1505 * Returns the bundle identifier of the kernel extension
1506 * that defines this metaclass.
1509 * The bundle identifier of the kernel extension that defines this metaclass.
1512 * "Kmod" is an older term for kernel extension.
1514 const OSSymbol
* getKmodName() const;
1518 * @function getClassName
1521 * Returns the name of the C++ class managed by this metaclass.
1524 * Returns the name of the C++ class managed by this metaclass.
1526 const char * getClassName() const;
1527 const OSSymbol
* getClassNameSymbol() const;
1531 * @function getClassSize
1534 * Returns the allocation size of the C++ class managed by this metaclass.
1537 * The allocation size of the C++ class managed by this metaclass.
1539 unsigned int getClassSize() const;
1546 * Allocates an instance of the C++ class managed by this metaclass.
1549 * A pointer to the newly allocated, uninitialized instance,
1550 * with a retain count of 1; <code>NULL</code> on allocation failure.
1553 * This function is automatically created by the metaclass-registration macros
1554 * to enable dynamic instance allocation.
1556 virtual OSObject
* alloc() const = 0;
1558 #ifdef XNU_KERNEL_PRIVATE
1559 void addInstance(const OSObject
* instance
, bool super
= false) const;
1560 void removeInstance(const OSObject
* instance
, bool super
= false) const;
1561 void applyToInstances(OSMetaClassInstanceApplierFunction applier
,
1562 void * context
) const;
1563 static void applyToInstancesOfClassName(
1564 const OSSymbol
* name
,
1565 OSMetaClassInstanceApplierFunction applier
,
1568 static void applyToInstances(OSOrderedSet
* set
,
1569 OSMetaClassInstanceApplierFunction applier
,
1574 /* Not to be included in headerdoc.
1576 * @define OSDeclareCommonStructors
1580 * Helper macro for for the standard metaclass-registration macros.
1583 * @param className The name of the C++ class, as a raw token,
1584 * <i>not</i> a string or macro.
1586 #define OSDeclareCommonStructors(className) \
1588 static const OSMetaClass * const superClass; \
1590 static const OSMetaClass * const metaClass; \
1591 static class MetaClass : public OSMetaClass { \
1594 virtual OSObject *alloc() const; \
1596 friend class className ::MetaClass; \
1597 virtual const OSMetaClass * getMetaClass() const APPLE_KEXT_OVERRIDE; \
1599 className (const OSMetaClass *); \
1600 virtual ~ className ()
1604 * @define OSDeclareDefaultStructors
1608 * Declares run-time type information and functions
1609 * for a concrete Libkern C++ class.
1611 * @param className The name of the C++ class, as a raw token,
1612 * <i>not</i> a string or macro.
1615 * Concrete Libkern C++ classes should "call" this macro
1616 * immediately after the opening brace in a class declaration.
1617 * It leaves the current privacy state as <code>protected:</code>.
1619 #define OSDeclareDefaultStructors(className) \
1620 OSDeclareCommonStructors(className); \
1627 * @define OSDeclareAbstractStructors
1631 * Declares run-time type information and functions
1632 * for an abstract Libkern C++ class.
1634 * @param className The name of the C++ class, as a raw token,
1635 * <i>not</i> a string or macro.
1638 * Abstract Libkern C++ classes--those with at least one
1639 * pure virtual method--should "call" this macro
1640 * immediately after the opening brace in a class declaration.
1641 * It leaves the current privacy state as <code>protected:</code>.
1643 #define OSDeclareAbstractStructors(className) \
1644 OSDeclareCommonStructors(className); \
1646 className (); /* Make primary constructor private in abstract */ \
1650 * @define OSDeclareFinalStructors
1654 * Declares run-time type information and functions
1655 * for a final (non-subclassable) Libkern C++ class.
1657 * @param className The name of the C++ class, as a raw token,
1658 * <i>not</i> a string or macro.
1661 * Final Libkern C++ classes--those that do not allow subclassing--should
1662 * "call" this macro immediately after the opening brace in a class declaration.
1663 * (Final classes in the kernel may actually have subclasses in the kernel,
1664 * but kexts cannot define any subclasses of a final class.)
1665 * It leaves the current privacy state as <code>protected:</code>.
1667 * <b>Note:</b> If the class is exported by a pseudokext (symbol set),
1668 * the final symbol generated by this macro must be exported
1669 * for the final-class attribute to be enforced.
1671 * <b>Warning:</b> Changing a class from "Default" to "Final" will break
1672 * binary compatibility.
1674 #define OSDeclareFinalStructors(className) \
1675 OSDeclareDefaultStructors(className) \
1677 void __OSFinalClass(void); \
1681 /* Not to be included in headerdoc.
1683 * @define OSDefineMetaClassWithInit
1687 * Helper macro for for the standard metaclass-registration macros.
1690 * @param className The name of the C++ class, as a raw token,
1691 * <i>not</i> a string or macro.
1692 * @param superclassName The name of the superclass of the C++ class,
1694 * <i>not</i> a string or macro.
1695 * @param init A function to call in the constructor
1696 * of the class's OSMetaClass.
1698 #define OSDefineMetaClassWithInit(className, superclassName, init) \
1699 /* Class global data */ \
1700 className ::MetaClass className ::gMetaClass; \
1701 const OSMetaClass * const className ::metaClass = \
1702 & className ::gMetaClass; \
1703 const OSMetaClass * const className ::superClass = \
1704 & superclassName ::gMetaClass; \
1705 /* Class member functions */ \
1706 className :: className(const OSMetaClass *meta) \
1707 : superclassName (meta) { } \
1708 className ::~ className() { } \
1709 const OSMetaClass * className ::getMetaClass() const \
1710 { return &gMetaClass; } \
1711 /* The ::MetaClass constructor */ \
1712 className ::MetaClass::MetaClass() \
1713 : OSMetaClass(#className, className::superClass, sizeof(className)) \
1717 /* Not to be included in headerdoc.
1719 * @define OSDefineAbstractStructors
1723 * Helper macro for for the standard metaclass-registration macros.
1726 * @param className The name of the C++ class, as a raw token,
1727 * <i>not</i> a string or macro.
1728 * @param superclassName The name of the superclass of the C++ class,
1730 * <i>not</i> a string or macro.
1732 #define OSDefineAbstractStructors(className, superclassName) \
1733 OSObject * className ::MetaClass::alloc() const { return 0; }
1736 /* Not to be included in headerdoc.
1738 * @define OSDefineDefaultStructors
1742 * Helper macro for for the standard metaclass-registration macros.
1745 * @param className The name of the C++ class, as a raw token,
1746 * <i>not</i> a string or macro.
1747 * @param superclassName The name of the superclass of the C++ class,
1749 * <i>not</i> a string or macro.
1751 #define OSDefineDefaultStructors(className, superclassName) \
1752 OSObject * className ::MetaClass::alloc() const \
1753 { return new className; } \
1754 className :: className () : superclassName (&gMetaClass) \
1755 { gMetaClass.instanceConstructed(); }
1757 /* Not to be included in headerdoc.
1759 * @define OSDefineDefaultStructors
1763 * Helper macro for for the standard metaclass-registration macros.
1766 * @param className The name of the C++ class, as a raw token,
1767 * <i>not</i> a string or macro.
1768 * @param superclassName The name of the superclass of the C++ class,
1770 * <i>not</i> a string or macro.
1772 #define OSDefineFinalStructors(className, superclassName) \
1773 OSDefineDefaultStructors(className, superclassName) \
1774 void className ::__OSFinalClass(void) { }
1777 /* Not to be included in headerdoc.
1779 * @define OSDefineMetaClassAndStructorsWithInit
1783 * Helper macro for for the standard metaclass-registration macros.
1786 * @param className The name of the C++ class, as a raw token,
1787 * <i>not</i> a string or macro.
1788 * @param superclassName The name of the superclass of the C++ class,
1790 * <i>not</i> a string or macro.
1791 * @param init A function to call in the constructor
1792 * of the class's OSMetaClass.
1794 #define OSDefineMetaClassAndStructorsWithInit(className, superclassName, init) \
1795 OSDefineMetaClassWithInit(className, superclassName, init) \
1796 OSDefineDefaultStructors(className, superclassName)
1799 /* Not to be included in headerdoc.
1801 * @define OSDefineMetaClassAndAbstractStructorsWithInit
1805 * Helper macro for for the standard metaclass-registration macros.
1808 * @param className The name of the C++ class, as a raw token,
1809 * <i>not</i> a string or macro.
1810 * @param superclassName The name of the superclass of the C++ class,
1812 * <i>not</i> a string or macro.
1813 * @param init A function to call in the constructor
1814 * of the class's OSMetaClass.
1816 #define OSDefineMetaClassAndAbstractStructorsWithInit(className, superclassName, init) \
1817 OSDefineMetaClassWithInit(className, superclassName, init) \
1818 OSDefineAbstractStructors(className, superclassName)
1821 /* Not to be included in headerdoc.
1823 * @define OSDefineMetaClassAndFinalStructorsWithInit
1827 * Helper macro for for the standard metaclass-registration macros.
1830 * @param className The name of the C++ class, as a raw token,
1831 * <i>not</i> a string or macro.
1832 * @param superclassName The name of the superclass of the C++ class,
1834 * <i>not</i> a string or macro.
1835 * @param init A function to call in the constructor
1836 * of the class's OSMetaClass.
1838 #define OSDefineMetaClassAndFinalStructorsWithInit(className, superclassName, init) \
1839 OSDefineMetaClassWithInit(className, superclassName, init) \
1840 OSDefineFinalStructors(className, superclassName)
1845 /* Not to be included in headerdoc.
1847 * @define OSDefineMetaClass
1851 * Helper macro for for the standard metaclass-registration macros.
1854 * @param className The name of the C++ class, as a raw token,
1855 * <i>not</i> a string or macro.
1856 * @param superclassName The name of the superclass of the C++ class,
1858 * <i>not</i> a string or macro.
1859 * @param init A function to call in the constructor
1860 * of the class's OSMetaClass.
1862 #define OSDefineMetaClass(className, superclassName) \
1863 OSDefineMetaClassWithInit(className, superclassName, )
1867 * @define OSDefineMetaClassAndStructors
1871 * Defines an OSMetaClass and associated routines
1872 * for a concrete Libkern C++ class.
1874 * @param className The name of the C++ class, as a raw token,
1875 * <i>not</i> a string or macro.
1876 * @param superclassName The name of the superclass of the C++ class,
1878 * <i>not</i> a string or macro.
1881 * Concrete Libkern C++ classes should "call" this macro
1882 * at the beginning of their implementation files,
1883 * before any function implementations for the class.
1885 #define OSDefineMetaClassAndStructors(className, superclassName) \
1886 OSDefineMetaClassAndStructorsWithInit(className, superclassName, )
1890 * @define OSDefineMetaClassAndAbstractStructors
1894 * Defines an OSMetaClass and associated routines
1895 * for an abstract Libkern C++ class.
1897 * @param className The name of the C++ class, as a raw token,
1898 * <i>not</i> a string or macro.
1899 * @param superclassName The name of the superclass of the C++ class,
1901 * <i>not</i> a string or macro.
1904 * Abstract Libkern C++ classes--those with at least one
1905 * pure virtual method--should "call" this macro
1906 * at the beginning of their implementation files,
1907 * before any function implementations for the class.
1909 #define OSDefineMetaClassAndAbstractStructors(className, superclassName) \
1910 OSDefineMetaClassAndAbstractStructorsWithInit (className, superclassName, )
1914 * @define OSDefineMetaClassAndFinalStructors
1918 * Defines an OSMetaClass and associated routines
1919 * for a final (non-subclassable) Libkern C++ class.
1921 * @param className The name of the C++ class, as a raw token,
1922 * <i>not</i> a string or macro.
1923 * @param superclassName The name of the superclass of the C++ class,
1925 * <i>not</i> a string or macro.
1928 * Final Libkern C++ classes--those that do not allow
1929 * subclassing--should "call" this macro at the beginning
1930 * of their implementation files,
1931 * before any function implementations for the class.
1932 * (Final classes in the kernel may actually have subclasses in the kernel,
1933 * but kexts cannot define any subclasses of a final class.)
1935 * <b>Note:</b> If the class is exported by a pseudokext (symbol set),
1936 * the final symbol generated by this macro must be exported
1937 * for the final-class attribute to be enforced.
1939 * <b>Warning:</b> Changing a class from "Default" to "Final" will break
1940 * binary compatibility.
1942 #define OSDefineMetaClassAndFinalStructors(className, superclassName) \
1943 OSDefineMetaClassAndFinalStructorsWithInit(className, superclassName, )
1946 // Dynamic vtable patchup support routines and types
1947 void reservedCalled(int ind
) const;
1951 * @define OSMetaClassDeclareReservedUnused
1955 * Reserves vtable space for new virtual functions
1956 * in a Libkern C++ class.
1958 * @param className The name of the C++ class, as a raw token,
1959 * <i>not</i> a string or macro.
1960 * @param index The numeric index of the vtable slot,
1961 * as a raw constant, beginning from 0.
1964 * Libkern C++ classes in kernel extensions that can be used as libraries
1965 * can provide for backward compatibility by declaring a number
1966 * of reserved vtable slots
1967 * that can be replaced with new functions as they are added.
1968 * Each reserved declaration must be accompanied in the implementation
1969 * by a corresponding reference to
1970 * <code>@link OSMetaClassDefineReservedUnused
1971 * OSMetaClassDefineReservedUnused@/link</code>.
1973 * When replacing a reserved slot, change the macro from "Unused"
1974 * to "Used" to document the fact that the slot used to be reserved,
1975 * and declare the new function immediately after the "Used" macro
1976 * to preserve vtable ordering.
1978 * <code>@link OSMetaClassDeclareReservedUsed
1979 * OSMetaClassDeclareReservedUsed@/link</code>.
1981 #if APPLE_KEXT_VTABLE_PADDING
1982 #define OSMetaClassDeclareReservedUnused(className, index) \
1984 virtual void _RESERVED ## className ## index ()
1986 #define OSMetaClassDeclareReservedUnused(className, index)
1991 * @define OSMetaClassDeclareReservedUsed
1995 * Documents use of reserved vtable space for new virtual functions
1996 * in a Libkern C++ class.
1998 * @param className The name of the C++ class, as a raw token,
1999 * <i>not</i> a string or macro.
2000 * @param index The numeric index of the vtable slot,
2001 * as a raw constant, beginning from 0.
2004 * This macro evaluates to nothing, and is used to document reserved
2005 * vtable slots as they are filled.
2007 * <code>@link OSMetaClassDeclareReservedUnused
2008 * OSMetaClassDeclareReservedUnused@/link</code>.
2010 #define OSMetaClassDeclareReservedUsed(className, index)
2014 * @define OSMetaClassDefineReservedUnused
2018 * Defines a reserved vtable slot for a Libkern C++ class.
2020 * @param className The name of the C++ class, as a raw token,
2021 * <i>not</i> a string or macro.
2022 * @param index The numeric index of the vtable slot,
2023 * as a raw constant, beginning from 0.
2026 * Libkern C++ classes in kernel extensions that can be used as libraries
2027 * can provide for backward compatibility by declaring a number
2028 * of reserved vtable slots
2029 * that can be replaced with new functions as they are added.
2030 * Each reserved defintion accompanies
2031 * a corresponding declaration created with
2032 * <code>@link OSMetaClassDeclareReservedUnused
2033 * OSMetaClassDeclareReservedUnused@/link</code>.
2035 * This macro is used in the implementation file
2036 * to provide a placeholder definition for the reserved vtable slot,
2037 * as a function that calls <code>panic</code> with an error message.
2039 * When replacing a reserved slot, change the macro from "Unused"
2040 * to "Used" to document the fact that the slot used to be reserved,
2041 * and declare the new function immediately after the "Used" macro
2042 * to preserve vtable ordering.
2044 * <code>@link OSMetaClassDefineReservedUsed
2045 * OSMetaClassDefineReservedUsed@/link</code>.
2047 #if APPLE_KEXT_VTABLE_PADDING
2048 #define OSMetaClassDefineReservedUnused(className, index) \
2049 void className ::_RESERVED ## className ## index () \
2050 { gMetaClass.reservedCalled(index); }
2052 #define OSMetaClassDefineReservedUnused(className, index)
2057 * @define OSMetaClassDefineReservedUsed
2061 * Reserves vtable space for new virtual functions in a Libkern C++ class.
2063 * @param className The name of the C++ class, as a raw token,
2064 * <i>not</i> a string or macro.
2065 * @param index The numeric index of the vtable slot,
2066 * as a raw constant, beginning from 0.
2069 * This macro evaluates to nothing, and is used to document reserved
2070 * vtable slots as they are filled.
2072 * <code>@link OSMetaClassDefineReservedUnused
2073 * OSMetaClassDefineReservedUnused@/link</code>.
2075 #define OSMetaClassDefineReservedUsed(className, index)
2077 // I/O Kit debug internal routines.
2078 static void printInstanceCounts();
2079 static void serializeClassDictionary(OSDictionary
* dict
);
2080 #ifdef XNU_KERNEL_PRIVATE
2083 static void * trackedNew(size_t size
);
2084 static void trackedDelete(void * mem
, size_t size
);
2085 void trackedInstance(OSObject
* instance
) const;
2086 void trackedFree(OSObject
* instance
) const;
2087 void trackedAccumSize(OSObject
* instance
, size_t size
) const;
2088 struct IOTrackingQueue
* getTracking() const;
2094 static OSDictionary
* getClassDictionary();
2095 virtual bool serialize(OSSerialize
* serializer
) const;
2097 // Virtual Padding functions for MetaClass's
2098 OSMetaClassDeclareReservedUnused(OSMetaClass
, 0);
2099 OSMetaClassDeclareReservedUnused(OSMetaClass
, 1);
2100 OSMetaClassDeclareReservedUnused(OSMetaClass
, 2);
2101 OSMetaClassDeclareReservedUnused(OSMetaClass
, 3);
2102 OSMetaClassDeclareReservedUnused(OSMetaClass
, 4);
2103 OSMetaClassDeclareReservedUnused(OSMetaClass
, 5);
2104 OSMetaClassDeclareReservedUnused(OSMetaClass
, 6);
2105 OSMetaClassDeclareReservedUnused(OSMetaClass
, 7);
2108 #endif /* !_LIBKERN_OSMETACLASS_H */