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
45 #endif /* XNU_KERNEL_PRIVATE */
52 * This header declares the OSMetaClassBase and OSMetaClass classes,
53 * which together form the basis of the Libkern and I/O Kit C++ class hierarchy
54 * and run-time type information facility.
59 #define APPLE_KEXT_COMPATIBILITY
61 #ifdef XNU_KERNEL_PRIVATE
63 #ifdef CONFIG_EMBEDDED
64 #define APPLE_KEXT_VTABLE_PADDING 0
65 #else /* CONFIG_EMBEDDED */
67 #define APPLE_KEXT_VTABLE_PADDING 1
68 #endif /* CONFIG_EMBEDDED */
70 #else /* XNU_KERNEL_PRIVATE */
71 #include <TargetConditionals.h>
73 #if TARGET_OS_EMBEDDED
74 #define APPLE_KEXT_VTABLE_PADDING 0
75 #else /* TARGET_OS_EMBEDDED */
77 #define APPLE_KEXT_VTABLE_PADDING 1
78 #endif /* TARGET_OS_EMBEDDED */
80 #endif /* XNU_KERNEL_PRIVATE */
82 #define APPLE_KEXT_ALIGN_CONTAINERS (0 == APPLE_KEXT_VTABLE_PADDING)
86 #define APPLE_KEXT_LEGACY_ABI 0
87 #elif defined(__arm__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2))
88 #define APPLE_KEXT_LEGACY_ABI 0
90 #define APPLE_KEXT_LEGACY_ABI 1
95 #define APPLE_KEXT_COMPATIBILITY_VIRTUAL
97 // private method made virtual only for binary compatibility
98 #define APPLE_KEXT_COMPATIBILITY_VIRTUAL virtual
102 #define APPLE_KEXT_DEPRECATED __attribute__((deprecated))
105 #if __cplusplus >= 201103L
106 #define APPLE_KEXT_OVERRIDE override
107 #if defined(__LP64__)
108 #define APPLE_KEXT_COMPATIBILITY_OVERRIDE
110 #define APPLE_KEXT_COMPATIBILITY_OVERRIDE APPLE_KEXT_OVERRIDE
113 #define APPLE_KEXT_OVERRIDE
114 #define APPLE_KEXT_COMPATIBILITY_OVERRIDE
119 * @class OSMetaClassBase
122 * OSMetaClassBase is the abstract bootstrap class
123 * for the Libkern and I/O Kit run-time type information system.
126 * OSMetaClassBase is the abstract C++ root class
127 * underlying the entire Libkern and I/O Kit class hierarchy.
128 * It defines the run-time type information system,
129 * including dynamic class allocation and safe type-casting,
130 * as well as the abstract interface for reference counting
131 * and a few other utility functions.
132 * OSMetaClassBase is the immediate superclass of
133 * @link //apple_ref/doc/class/OSObject OSObject@/link and
134 * @link //apple_ref/doc/class/OSMetaClass OSMetaClass@/link;
135 * no other class should derive from OSMetaClassBase.
137 * For more information, see
138 * <i>@link //apple_ref/doc/uid/TP40002799
139 * I/O Kit Device Driver Design Guidelines@/link</i>.
141 * <b>Use by Kernel Extensions</b>
143 * Kernel Extensions should never interact directly with OSMetaClassBase,
144 * but they will find useful several macros that tie in
145 * to the run-time type information system, specifically:
147 * <li><code>@link OSTypeAlloc OSTypeAlloc@/link</code> - allocation of new instances</li>
148 * <li><code>@link OSDynamicCast OSDynamicCast@/link</code> - safe type casting</li>
149 * <li><code>@link OSCheckTypeInst OSCheckTypeInst@/link</code> -
150 * checking for inheritance/derivation</li>
151 * <li><code>@link OSMemberFunctionCast OSMemberFunctionCast@/link</code> -
152 * casting C++ member functions to C function pointers
153 * for registration as callbacks</li>
156 * See @link //apple_ref/doc/class/OSMetaClass OSMetaClass@/link
157 * for more run-time type information interfaces.
159 * <b>Use Restrictions</b>
161 * OSMetaClassBase should not be subclassed by kernel extensions,
162 * nor should kernel extensions call its run-time type functions directly.
164 * The run-time type functions and macros are <b>not safe</b>
165 * to call in a primary interrupt context.
167 * <b>Concurrency Protection</b>
169 * The run-time type macros and functions of OSMetaClassBase are thread-safe.
171 class OSMetaClassBase
177 * @define OSTypeAlloc
181 * Allocates an instance of the named object class.
183 * @param type The name of the desired class to be created,
184 * as a raw token, <i>not</i> a string or macro.
187 * A pointer to the new, uninitialized object on success;
188 * <code>NULL</code> on failure.
193 * //apple_ref/cpp/clm/OSMetaClass/allocClassWithName/staticOSObject*\/(constchar*)
194 * OSMetaClass::allocClassWithName(const char *)@/link</code>
197 * //apple_ref/cpp/instm/OSMetaClass/alloc/virtualOSObject*\/()
198 * OSMetaClass::alloc@/link</code>.
200 * The OSTypeAlloc macro is used to avoid binary compatibility difficulties
201 * presented by the C++ <code>new</code> operator.
203 #define OSTypeAlloc(type) ((type *) ((type::metaClass)->alloc()))
211 * Returns the type ID (metaclass) of a class based on its name.
213 * @param type The name of the desired class, as a raw token,
214 * <i>not</i> a string or macro.
217 * The unique type ID (metaclass) for the class.
220 * It is typically more useful to determine whether a class is derived
222 * <code>@link //apple_ref/cpp/macro/OSDynamicCast OSDynamicCast@/link</code>
224 * <code>@link //apple_ref/cpp/macro/OSCheckTypeInst OSCheckTypeInst@/link</code>.
226 #define OSTypeID(type) (type::metaClass)
230 * @define OSTypeIDInst
234 * Returns the type ID (metaclass) for the class of an object instance.
236 * @param typeinst An instance of an OSObject subclass.
239 * The type ID of that object's class; that is, its metaclass.
242 * It is typically more useful to determine whether an object is derived
243 * from a particular class; see
244 * <code>@link //apple_ref/cpp/macro/OSDynamicCast OSDynamicCast@/link</code>
246 * <code>@link //apple_ref/cpp/macro/OSCheckTypeInst OSCheckTypeInst@/link</code>.
248 #define OSTypeIDInst(typeinst) ((typeinst)->getMetaClass())
252 * @define OSDynamicCast
256 * Safe type-casting for Libkern C++ objects.
258 * @param type The name of the desired class type, as a raw token,
259 * <i>not</i> a string or macro.
260 * It is assumed you intend to cast to a pointer
261 * to an object of this type.
262 * Type qualifiers, such as <code>const</code>,
263 * are not recognized and will cause
264 * a (usually obscure) compile error.
265 * @param inst A pointer to the object instance to be cast.
266 * May be <code>NULL</code>.
269 * <code>inst</code> if it is non-<code>NULL</code>
270 * and derived from <code>type</code>;
271 * otherwise <code>NULL</code>.
274 * <code>OSDynamicCast</code> is a rough equivalent
275 * to the standard C++ RTTI <code>dynamic_cast<T></code> operator.
276 * Your code should use this instead of raw C type-casting,
277 * and check the resulting value.
278 * If the result is non-<code>NULL</code>,
279 * the object is safe to use as the type-cast class;
280 * if the result is <code>NULL</code>,
281 * the object does not derive from the type-cast class
282 * and your code should take appropriate steps to handle the error.
284 #define OSDynamicCast(type, inst) \
285 ((type *) OSMetaClassBase::safeMetaCast((inst), OSTypeID(type)))
289 * @define OSCheckTypeInst
293 * Checks whether two objects are type-compatible.
295 * @param typeinst The reference object.
296 * @param inst The object to check for type compatibility.
299 * <code>true</code> if both <code>inst</code> and
300 * <code>typeinst</code> are non-<code>NULL</code>
301 * and <code>inst</code> is derived from the class of <code>typeinst</code>;
302 * otherwise <code>false</code>.
304 #define OSCheckTypeInst(typeinst, inst) \
305 OSMetaClassBase::checkTypeInst(inst, typeinst)
307 #define OSSafeRelease(inst) \
308 do { int OSSafeRelease __attribute__ ((deprecated("Use OSSafeReleaseNULL"))); (OSSafeRelease); \
309 if (inst) (inst)->release(); } while (0)
311 /*! @function OSSafeReleaseNULL
312 * @abstract Release an object if not <code>NULL</code>, then set it to <code>NULL</code>.
313 * @param inst Instance of an OSObject, may be <code>NULL</code>.
315 #define OSSafeReleaseNULL(inst) do { if (inst) (inst)->release(); (inst) = NULL; } while (0)
317 typedef void (*_ptf_t
)(void);
319 #if APPLE_KEXT_LEGACY_ABI
321 // Arcane evil code interprets a C++ pointer to function as specified in the
322 // -fapple-kext ABI, i.e. the gcc-2.95 generated code. IT DOES NOT ALLOW
323 // the conversion of functions that are from MULTIPLY inherited classes.
326 _ptmf2ptf(const OSMetaClassBase
*self
, void (OSMetaClassBase::*func
)(void))
329 void (OSMetaClassBase::*fIn
)(void);
330 struct { // Pointer to member function 2.95
331 unsigned short fToff
;
341 if (map
.fptmf2
.fToff
) {
342 panic("Multiple inheritance is not supported");
344 } else if (map
.fptmf2
.fVInd
< 0) {
345 // Not virtual, i.e. plain member func
346 return map
.fptmf2
.u
.fPFN
;
349 const OSMetaClassBase
*fObj
;
354 // Virtual member function so dereference vtable
355 return (*u
.vtablep
)[map
.fptmf2
.fVInd
- 1];
359 #else /* !APPLE_KEXT_LEGACY_ABI */
360 #if defined(__arm__) || defined(__arm64__)
361 typedef long int ptrdiff_t;
363 * Ugly reverse engineered ABI. Where does it come from? Nobody knows.
364 * <rdar://problem/5641129> gcc 4.2-built ARM kernel panics with multiple inheritance (no, really)
367 _ptmf2ptf(const OSMetaClassBase
*self
, void (OSMetaClassBase::*func
)(void))
374 void (OSMetaClassBase::*fIn
)(void);
381 if (map
.pTMF
.delta
& 1) {
384 const OSMetaClassBase
*fObj
;
389 // Virtual member function so dereference table
390 return *(_ptf_t
*)(((uintptr_t)*u
.vtablep
) + (uintptr_t)map
.pTMF
.fPFN
);
392 // Not virtual, i.e. plain member func
393 return map
.pTMF
.fPFN
;
396 #elif defined(__i386__) || defined(__x86_64__)
398 // Slightly less arcane and slightly less evil code to do
399 // the same for kexts compiled with the standard Itanium C++
403 _ptmf2ptf(const OSMetaClassBase
*self
, void (OSMetaClassBase::*func
)(void))
406 void (OSMetaClassBase::*fIn
)(void);
413 if (map
.fVTOffset
& 1) {
416 const OSMetaClassBase
*fObj
;
421 // Virtual member function so dereference vtable
422 return *(_ptf_t
*)(((uintptr_t)*u
.vtablep
) + map
.fVTOffset
- 1);
424 // Not virtual, i.e. plain member func
430 #error Unknown architecture.
433 #endif /* !APPLE_KEXT_LEGACY_ABI */
436 * @define OSMemberFunctionCast
440 * Converts a C++ member function pointer, relative to an instance,
441 * to a C-style pointer to function.
443 * @param cptrtype The function type declaration to cast to
444 * (typically provided as a <code>typedef</code> by I/O KitKit classes).
445 * @param self The <code>this</code> pointer of the object whose function
447 * @param func The pointer to the member function itself,
448 * something like <code>&Class::function</code>.
451 * A pointer to a function of the given type referencing <code>self</code>.
454 * This function is used to generate pointers to C++ functions for instances,
455 * such that they can be registered as callbacks with I/O Kit objects.
457 * No warnings are generated.
459 * This function will panic if an attempt is made to call it
460 * with a multiply-inheriting class.
462 #define OSMemberFunctionCast(cptrtype, self, func) \
463 (cptrtype) OSMetaClassBase:: \
464 _ptmf2ptf(self, (void (OSMetaClassBase::*)(void)) func)
468 virtual ~OSMetaClassBase();
471 // Disable copy constructors of OSMetaClassBase based objects
472 /* Not to be included in headerdoc.
474 * @function operator =
477 * Disable implicit copy constructor by making private
479 * @param src Reference to source object that isn't allowed to be copied.
481 void operator =(OSMetaClassBase
&src
);
483 /* Not to be included in headerdoc.
485 * @function OSMetaClassBase
488 * Disable implicit copy constructor by making private
490 * @param src Reference to source object that isn't allowed to be copied.
492 OSMetaClassBase(OSMetaClassBase
&src
);
496 // xx-review: the original comment for this makes it sound to me like we don't
497 // xx-review: catch over-releasing an object...?
503 * Abstract declaration of
505 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/(int)
506 * release(int freeWhen)@/link</code>.
511 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/(int)
512 * release(int freeWhen)@/link</code>.
514 virtual void release(int freeWhen
) const = 0;
518 * @function getRetainCount
521 * Abstract declaration of
523 * //apple_ref/cpp/instm/OSObject/getRetainCount/virtualint/()
524 * getRetainCount()@/link</code>.
529 * //apple_ref/cpp/instm/OSObject/getRetainCount/virtualint/()
530 * OSObject::getRetainCount()@/link</code>.
532 virtual int getRetainCount() const = 0;
539 * Abstract declaration of
541 * //apple_ref/cpp/instm/OSObject/retain/virtualvoid/()
542 * retain()@/link</code>.
547 * //apple_ref/cpp/instm/OSObject/retain/virtualvoid/()
548 * OSObject::retain()@/link</code>.
550 virtual void retain() const = 0;
557 * Abstract declaration of
559 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/()
560 * release@/link</code>.
565 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/()
566 * OSObject::release@/link</code>.
568 virtual void release() const = 0;
572 * @function serialize
575 * Abstract declaration of
577 * //apple_ref/cpp/instm/OSObject/serialize/virtualbool/(OSSerialize*)
578 * serialize@/link</code>.
583 * //apple_ref/cpp/instm/OSObject/serialize/virtualbool/(OSSerialize*)
584 * OSObject::serialize@/link</code>.
586 virtual bool serialize(OSSerialize
* serializer
) const = 0;
590 * @function getMetaClass
593 * Returns the OSMetaClass representing
594 * an OSMetaClassBase subclass.
597 * OSObject overrides this abstract member function
598 * to return the OSMetaClass object that represents
599 * each class for run-time typing.
601 virtual const OSMetaClass
* getMetaClass() const = 0;
605 * @function isEqualTo
608 * Checks whether another object is equal to the receiver.
610 * @param anObject The object to copmare to the receiver.
613 * <code>true</code> if the objects are equal, <code>false</code> otherwise.
616 * OSMetaClassBase implements this as a direct pointer comparison,
617 * since it has no other information to judge equality by.
618 * Subclasses generally override this function
619 * to do a more meaningful comparison.
620 * For example, OSString implements it to return
621 * <code>true</code> if <code>anObject</code>
622 * is derived from OSString and represents the same C string.
624 virtual bool isEqualTo(const OSMetaClassBase
* anObject
) const;
631 * Casts this object is to the class managed by the given OSMetaClass.
633 * @param toMeta A pointer to a constant OSMetaClass
634 * for the desired target type.
637 * <code>this</code> if the object is derived
638 * from the class managed by <code>toMeta</code>,
639 * otherwise <code>NULL</code>.
642 * It is far more convenient to use
643 * <code>@link OSDynamicCast OSDynamicCast@/link</code>.
645 OSMetaClassBase
* metaCast(const OSMetaClass
* toMeta
) const;
652 * Casts this object is to the class managed by the named OSMetaClass.
654 * @param toMeta An OSSymbol naming the desired target type.
657 * <code>this</code> if the object is derived
658 * from the class named by <code>toMeta</code>,
659 * otherwise <code>NULL</code>.
662 * It is far more convenient to use
663 * <code>@link OSDynamicCast OSDynamicCast@/link</code>.
665 OSMetaClassBase
* metaCast(const OSSymbol
* toMeta
) const;
672 * Casts this object is to the class managed by the named OSMetaClass.
674 * @param toMeta An OSString naming the desired target type.
676 * <code>this</code> if the object is derived
677 * from the class named by <code>toMeta</code>,
678 * otherwise <code>NULL</code>.
681 * It is far more convenient to use
682 * <code>@link OSDynamicCast OSDynamicCast@/link</code>.
684 OSMetaClassBase
* metaCast(const OSString
* toMeta
) const;
691 * Casts this object is to the class managed by the named OSMetaClass.
693 * @param toMeta A C string naming the desired target type.
695 * <code>this</code> if the object is derived
696 * from the class named by <code>toMeta</code>,
697 * otherwise <code>NULL</code>.
700 * It is far more convenient to use
701 * <code>@link OSDynamicCast OSDynamicCast@/link</code>.
703 OSMetaClassBase
* metaCast(const char * toMeta
) const;
705 // Helper inlines for run-time type preprocessor macros
707 * @function safeMetaCast
710 * Casts an object is to the class managed by the given OSMetaClass.
712 * @param anObject A pointer to the object to be cast.
713 * @param toMeta A pointer to a constant OSMetaClass
714 * for the desired target type.
717 * <code>anObject</code> if the object is derived
718 * from the class managed by <code>toMeta</code>,
719 * otherwise <code>NULL</code>.
722 * It is far more convenient to use
723 * <code>@link OSDynamicCast OSDynamicCast@/link</code>.
725 static OSMetaClassBase
* safeMetaCast(
726 const OSMetaClassBase
* anObject
,
727 const OSMetaClass
* toMeta
);
730 * @function checkTypeInst
733 * Checks whether an object instance is of the same class
734 * as another object instance (or a subclass of that class).
736 * @param inst A pointer to the object to check.
737 * @param typeinst A pointer to an object of the class being checked.
740 * <code>true</code> if the object is derived
741 * from the class of <code>typeinst</code>
742 * or a subclass of that class,
743 * otherwise <code>false</code>.
746 * It is far more convenient to use
747 * <code>@link OSCheckTypeInst OSCheckTypeInst@/link</code>.
749 static bool checkTypeInst(
750 const OSMetaClassBase
* inst
,
751 const OSMetaClassBase
* typeinst
);
753 static void initialize(void);
758 * @function taggedRetain
761 * Abstract declaration of
763 * //apple_ref/cpp/instm/OSObject/taggedRetain/virtualvoid/(constvoid*)
764 * taggedRetain(const void *)@/link</code>.
769 * //apple_ref/cpp/instm/OSObject/taggedRetain/virtualvoid/(constvoid*)
770 * OSObject::taggedRetain(const void *)@/link</code>.
772 // WAS: virtual void _RESERVEDOSMetaClassBase0();
773 virtual void taggedRetain(const void * tag
= 0) const = 0;
777 * @function taggedRelease
780 * Abstract declaration of
782 * //apple_ref/cpp/instm/OSObject/taggedRelease/virtualvoid/(constvoid*)
783 * taggedRelease(const void *)@/link</code>.
788 * //apple_ref/cpp/instm/OSObject/taggedRelease/virtualvoid/(constvoid*)
789 * OSObject::taggedRelease(const void *)@/link</code>.
791 // WAS: virtual void _RESERVEDOSMetaClassBase1();
792 virtual void taggedRelease(const void * tag
= 0) const = 0;
796 * @function taggedRelease
799 * Abstract declaration of
801 * //apple_ref/cpp/instm/OSObject/taggedRelease/virtualvoid/(constvoid*,constint)
802 * taggedRelease(const void *, const int freeWhen)@/link</code>.
807 * //apple_ref/cpp/instm/OSObject/taggedRelease/virtualvoid/(constvoid*,constint)
808 * OSObject::taggedRelease(const void *, const int freeWhen)@/link</code>.
810 // WAS: virtual void _RESERVEDOSMetaClassBase2();
811 virtual void taggedRelease(
813 const int freeWhen
) const = 0;
816 #if APPLE_KEXT_VTABLE_PADDING
818 virtual void _RESERVEDOSMetaClassBase3();
819 virtual void _RESERVEDOSMetaClassBase4();
820 virtual void _RESERVEDOSMetaClassBase5();
821 virtual void _RESERVEDOSMetaClassBase6();
822 virtual void _RESERVEDOSMetaClassBase7();
824 } APPLE_KEXT_COMPATIBILITY
;
827 #ifdef XNU_KERNEL_PRIVATE
828 typedef bool (*OSMetaClassInstanceApplierFunction
)(const OSObject
* instance
,
830 #endif /* XNU_KERNEL_PRIVATE */
836 * OSMetaClass manages run-time type information
837 * for Libkern and I/O Kit C++ classes.
839 * @discussion OSMetaClass manages run-time type information
840 * for Libkern and I/O Kit C++ classes.
841 * An instance of OSMetaClass exists for (nearly) every such C++ class,
842 * keeping track of inheritance relationships, class lookup by name,
843 * instance counts, and more.
844 * OSMetaClass operates almost entirely behind the scenes,
845 * and kernel extensions should rarely, if ever,
846 * have to interact directly with OSMetaClass.
848 * <b>Use by Kernel Extensions</b>
850 * While kernel extensions rarey interact directly with OSMetaClass at run time,
851 * they must register their classes with the metaclass system
852 * using the macros declared here.
853 * The class declaration should use one of these two macros
854 * before its first member function declaration:
856 * <li><code>@link OSDeclareDefaultStructors OSDeclareDefaultStructors@/link</code> -
857 * for classes with no abstract member function declarations</li>
858 * <li><code>@link OSDeclareAbstractStructors OSDeclareAbstractStructors@/link</code> -
859 * for classes with at least one abstract member function declaration</li>
860 * <li><code>@link OSDeclareFinalStructors OSDeclareFinalStructors@/link</code> -
861 * for classes that should not be subclassable by another kext</li>
864 * The class implementation should then use one of these macros:
866 * <li><code>@link OSDefineMetaClassAndStructors
867 * OSDefineMetaClassAndStructors@/link</code> -
868 * for classes with no abstract member function declarations</li>
869 * <li><code>@link OSDefineMetaClassAndAbstractStructors
870 * OSDefineMetaClassAndAbstractStructors@/link</code> -
871 * for classes with at least one abstract member function declaration</li>
872 * <li><code>@link OSDefineMetaClassAndFinalStructors
873 * OSDefineMetaClassAndFinalStructors@/link</code> -
874 * for classes that should not be subclassable by another kext</li>
877 * Classes in kernel extensions that are intended for use as libraries
878 * may need to reserve vtable slots to preserve binary compatibility
879 * as new functions are added. They may do so with these macros:
881 * <li><code>@link OSMetaClassDeclareReservedUnused
882 * OSMetaClassDeclareReservedUnused@/link</code> -
883 * reserves a vtable slot</li>
884 * <li><code>@link OSMetaClassDefineReservedUnused
885 * OSMetaClassDefineReservedUnused@/link</code> -
886 * defines the reserved vtable slot as an unimplemented function</li>
887 * <li><code>@link OSMetaClassDeclareReservedUsed
888 * OSMetaClassDeclareReservedUsed@/link</code> -
889 * documents that a formerly reserved slot is now used</li>
890 * <li><code>@link OSMetaClassDefineReservedUsed
891 * OSMetaClassDefineReservedUsed@/link</code> -
892 * documents that a formerly reserved slot is now used</li>
895 * <b>Use Restrictions</b>
897 * OSMetaClass should not be explicitly subclassed by kernel extensions
898 * (the declare/define macros do that),
899 * nor should kernel extensions call its run-time type functions directly.
901 * OSMetaClass functions should be considered
902 * <b>unsafe</b> to call in a primary interrupt context.
904 * <b>Concurrency Protection</b>
906 * Kernel extensions should in general not interact
907 * with OSMetaClass objects directly,
908 * instead using the run-time type macros.
909 * Much of OSMetaClass's interface is intended for use
910 * by the run-time type information system,
911 * which handles concurrency and locking internally.
913 class OSMetaClass
: private OSMetaClassBase
917 friend class IOStatistics
;
921 // Can never be allocated must be created at compile time
922 static void * operator new(size_t size
);
924 /* Reserved for future use. (Internal use only) */
925 struct ExpansionData
*reserved
;
927 /* superClass Handle to the superclass's meta class. */
928 const OSMetaClass
*superClassLink
;
930 /* className OSSymbol of the class' name. */
931 const OSSymbol
*className
;
933 /* classSize How big is a single instance of this class. */
934 unsigned int classSize
;
936 /* instanceCount Roughly number of instances of the object,
937 * +1 for each direct subclass with a nonzero refcount.
938 * Used primarily as a code-in-use flag.
940 mutable unsigned int instanceCount
;
942 /* Not to be included in headerdoc.
944 * @function OSMetaClass
947 * The default private constructor.
951 // Called by postModLoad
952 /* Not to be included in headerdoc.
957 * Logs an error string for an <code>OSReturn</code> value
958 * using <code>printf</code>.
960 * @param result The <code>OSReturn</code> value for which to log a message.
963 * This function is used to log errors loading kernel extensions.
964 * Kernel extensions themselves should not call it.
966 static void logError(OSReturn result
);
971 * @function getMetaClassWithName
974 * Look up a metaclass in the run-time type information system.
976 * @param name The name of the desired class's metaclass.
979 * A pointer to the metaclass object if found, <code>NULL</code> otherwise.
981 static const OSMetaClass
* getMetaClassWithName(const OSSymbol
* name
);
983 #if XNU_KERNEL_PRIVATE
986 * @function copyMetaClassWithName
989 * Look up a metaclass in the run-time type information system.
991 * @param name The name of the desired class's metaclass.
994 * A pointer to the metaclass object if found, <code>NULL</code> otherwise.
995 * The metaclass will be protected from unloading until releaseMetaClass()
998 static const OSMetaClass
* copyMetaClassWithName(const OSSymbol
* name
);
1000 * @function releaseMetaClass
1003 * Releases reference obtained from copyMetaClassWithName().
1006 * The metaclass will be protected from unloading until releaseMetaClass()
1009 void releaseMetaClass() const;
1011 #endif /* XNU_KERNEL_PRIVATE */
1018 * Implements the abstract <code>retain</code> function to do nothing.
1021 * Since an OSMetaClass instance must remain in existence
1022 * for as long as its kernel extension is loaded,
1023 * OSMetaClass does not use reference-counting.
1025 virtual void retain() const;
1032 * Implements the abstract <code>release</code> function to do nothing.
1035 * Since an OSMetaClass instance must remain in existence
1036 * for as long as its kernel extension is loaded,
1037 * OSMetaClass does not use reference-counting.
1039 virtual void release() const;
1046 * Implements the abstract <code>release(int freeWhen)</code>
1047 * function to do nothing.
1049 * @param freeWhen Unused.
1052 * Since an OSMetaClass instance must remain in existence
1053 * for as long as its kernel extension is loaded,
1054 * OSMetaClass does not use reference-counting.
1056 virtual void release(int freeWhen
) const;
1060 * @function taggedRetain
1063 * Implements the abstract <code>taggedRetain(const void *)</code>
1064 * function to do nothing.
1066 * @param tag Unused.
1069 * Since an OSMetaClass instance must remain in existence
1070 * for as long as its kernel extension is loaded,
1071 * OSMetaClass does not use reference-counting.
1073 virtual void taggedRetain(const void * tag
= 0) const;
1077 * @function taggedRelease
1080 * Implements the abstract <code>taggedRelease(const void *)</code>
1081 * function to do nothing.
1083 * @param tag Unused.
1086 * Since an OSMetaClass instance must remain in existence
1087 * for as long as its kernel extension is loaded,
1088 * OSMetaClass does not use reference-counting.
1090 virtual void taggedRelease(const void * tag
= 0) const;
1094 * @function taggedRelease
1097 * Implements the abstract <code>taggedRelease(const void *, cont int)</code>
1098 * function to do nothing.
1100 * @param tag Unused.
1101 * @param freeWhen Unused.
1104 * Since an OSMetaClass instance must remain in existence
1105 * for as long as its kernel extension is loaded,
1106 * OSMetaClass does not use reference-counting.
1108 virtual void taggedRelease(
1110 const int freeWhen
) const;
1114 * @function getRetainCount
1117 * Implements the abstract <code>getRetainCount</code>
1118 * function to return 0.
1124 * Since an OSMetaClass instance must remain in existence
1125 * for as long as its kernel extension is loaded,
1126 * OSMetaClass does not use reference-counting.
1128 virtual int getRetainCount() const;
1131 /* Not to be included in headerdoc.
1133 * @function getMetaClass
1136 * Returns the meta-metaclass.
1139 * The metaclass of the OSMetaClass object.
1141 virtual const OSMetaClass
* getMetaClass() const;
1145 * @function OSMetaClass
1148 * Constructor for OSMetaClass objects.
1150 * @param className A C string naming the C++ class
1151 * that this OSMetaClass represents.
1152 * @param superclass The OSMetaClass object representing the superclass
1153 * of this metaclass's class.
1154 * @param classSize The allocation size of the represented C++ class.
1157 * This constructor is protected and cannot be used
1158 * to instantiate OSMetaClass directly, as OSMetaClass is an abstract class.
1159 * This function is called during kext loading
1160 * to queue C++ classes for registration.
1161 * See <code>@link preModLoad preModLoad@/link</code> and
1162 * <code>@link postModLoad postModLoad@/link</code>.
1164 OSMetaClass(const char * className
,
1165 const OSMetaClass
* superclass
,
1166 unsigned int classSize
);
1170 * @function ~OSMetaClass
1173 * Destructor for OSMetaClass objects.
1176 * This function is called when the kernel extension that implements
1177 * the metaclass's class is unloaded.
1178 * The destructor removes all references to the class
1179 * from the run-time type information system.
1181 virtual ~OSMetaClass();
1183 // Needs to be overriden as NULL as all OSMetaClass objects are allocated
1184 // statically at compile time, don't accidently try to free them.
1185 void operator delete(void *, size_t) { }
1188 static const OSMetaClass
* const metaClass
;
1191 * @function preModLoad
1194 * Prepares the run-time type system
1195 * for the creation of new metaclasses
1196 * during loading of a kernel extension (module).
1198 * @param kextID The bundle ID of the kext being loaded.
1201 * An opaque handle to the load context
1202 * for the kernel extension on success;
1203 * <code>NULL</code> on failure.
1206 * <i>Not for use by kernel extensions.</i>
1208 * Prepares the run-time type information system to record and register
1209 * metaclasses created by static constructors until a subsequent call to
1210 * <code>@link postModLoad postModLoad@/link</code>.
1211 * <code>preModLoad</code> takes a lock to ensure processing of a single
1212 * load operation at a time; the lock is released by
1213 * <code>@link postModLoad postModLoad@/link</code>.
1214 * Any OSMetaClass constructed between these two function calls
1215 * will be associated with <code>kextID</code>.
1217 static void * preModLoad(const char * kextID
);
1221 * @function checkModLoad
1224 * Checks whether the current kext load operation can proceed.
1226 * @param loadHandle The opaque handle returned
1227 * by <code>@link preModLoad preModLoad@/link</code>.
1229 * <code>true</code> if no errors are outstanding
1230 * and the system is ready to process more metaclasses.
1233 * <i>Not for use by kernel extensions.</i>
1235 static bool checkModLoad(void * loadHandle
);
1239 * @function postModLoad
1242 * Registers the metaclasses created during loading of a kernel extension.
1244 * @param loadHandle The opaque handle returned
1245 * by <code>@link preModLoad preModLoad@/link</code>.
1247 * The error code of the first error encountered,
1250 * //apple_ref/cpp/macro/kOSReturnSuccess
1251 * kOSReturnSuccess@/link</code>
1252 * if no error occurred.
1255 * <i>Not for use by kernel extensions.</i>
1257 * Called after all static constructors in a kernel extension
1258 * have created metaclasses,
1259 * this function checks for duplicate class names,
1260 * then registers the new metaclasses under the kext ID
1261 * that @link preModLoad preModLoad@/link was called with,
1262 * so that they can be dynamically allocated
1263 * and have their instance counts tracked.
1264 * <code>postModLoad</code> releases the lock taken by
1265 * <code>@link preModLoad preModLoad@/link</code>.
1267 static OSReturn
postModLoad(void * loadHandle
);
1270 * @function modHasInstance
1273 * Returns whether any classes defined by the named
1274 * kernel extension (or their subclasses) have existing instances.
1276 * @param kextID The bundle ID of the kernel extension to check.
1279 * <code>true</code> if the kext is found and
1280 * if any class defined by that kext
1281 * has a nonzero instance count,
1282 * <code>false</code> otherwise.
1285 * This function is called before a kernel extension's static destructors
1286 * are invoked, prior to unloading the extension.
1287 * If any classes stil have instances or subclasses with instances,
1288 * those classes are logged
1289 * (using <code>@link reportModInstances reportModInstances@/link</code>) and
1290 * the kernel extension is not be unloaded.
1292 static bool modHasInstance(const char * kextID
);
1296 * @function reportModInstances
1299 * Logs the instance counts for classes
1300 * defined by a kernel extension.
1302 * @param kextID The bundle ID of the kernel extension to report on.
1305 * This function prints the names and instance counts
1306 * of any class defined by <code>kextID</code>
1307 * that has a nonzero instance count.
1308 * It's called by <code>@link modHasInstance modHasInstance@/link</code>
1309 * to help diagnose problems unloading kernel extensions.
1311 static void reportModInstances(const char * kextID
);
1315 * @function considerUnloads
1318 * Schedule automatic unloading of unused kernel extensions.
1321 * This function schedules a check for kernel extensions
1322 * that can be automatically unloaded,
1323 * canceling any currently scheduled check.
1324 * At that time, any such kexts with no Libkern C++ instances
1325 * and no external references are unloaded.
1327 * The I/O Kit calls this function when matching goes idle.
1329 * Kernel extensions that define subclasses of
1330 * @link //apple_ref/doc/class/IOService IOService@/link
1331 * are eligible for automatic unloading.
1333 * (On releases of Mac OS X prior to Snow Leopard (10.6),
1334 * any kernel extension defining any Libkern C++ class
1335 * was eligible for automatic unloading,
1336 * but that unload did not call the module stop routine.
1337 * Non-I/O Kit kernel extensions that define Libkern C++ subclasses
1338 * should be sure to have OSBundleLibraries declarations that ensure
1339 * they will not load on releases prior to Snow Leopard.)
1341 static void considerUnloads();
1343 #if XNU_KERNEL_PRIVATE
1344 static bool removeClasses(OSCollection
* metaClasses
);
1345 #endif /* XNU_KERNEL_PRIVATE */
1348 * @function allocClassWithName
1351 * Allocates an instance of a named OSObject-derived class.
1353 * @param name The name of the desired class.
1356 * A pointer to the newly-allocated, uninitialized object on success;
1357 * <code>NULL</code> on failure.
1360 * Kernel extensions should not need to use this function
1361 * directly, instead using static instance-creation functions
1362 * defined by classes.
1364 * This function consults the run-time type information system
1365 * to find the metaclass for the named class.
1366 * If it exists, it calls the metaclass's <code>@link alloc alloc@/link</code>
1367 * function and returns the result.
1369 static OSObject
* allocClassWithName(const OSSymbol
* name
);
1373 * function allocClassWithName
1376 * Allocates an instance of a named OSObject-derived class.
1378 * @param name The name of the desired class.
1381 * A pointer to the newly-allocated, uninitialized object on success;
1382 * <code>NULL</code> on failure.
1385 * Kernel extensions should not need to use this function
1386 * directly, instead using static instance-creation functions
1387 * defined by classes.
1389 * This function consults the run-time type information system
1390 * to find the metaclass for the named class.
1391 * If it exists, it calls the metaclass's <code>@link alloc alloc@/link</code>
1392 * function and returns the result.
1394 static OSObject
* allocClassWithName(const OSString
* name
);
1398 * function allocClassWithName
1401 * Allocates an instance of a named OSObject-derived class.
1403 * @param name The name of the desired class.
1406 * A pointer to the newly-allocated, uninitialized object on success;
1407 * <code>NULL</code> on failure.
1410 * Kernel extensions should not need to use this function
1411 * directly, instead using static instance-creation functions
1412 * defined by classes.
1414 * This function consults the run-time type information system
1415 * to find the metaclass for the named class.
1416 * If it exists, it calls the metaclass's <code>@link alloc alloc@/link</code>
1417 * function and returns the result.
1419 static OSObject
* allocClassWithName(const char * name
);
1423 * @function checkMetaCastWithName
1426 * Search the metaclass inheritance hierarchy by name for an object instance.
1428 * @param className The name of the desired class or superclass.
1429 * @param object The object whose metaclass begins the search.
1432 * <code>object</code> if it's derived from <code>className</code>;
1433 * <code>NULL</code> otherwise.
1436 * This function is the basis of the Libkern run-time type-checking system.
1437 * Kernel extensions should not use it directly,
1438 * instead using <code>@link OSDynamicCast OSDynamicCast@/link</code> or
1439 * <code>@link OSCheckTypeInst OSCheckTypeInst@/link</code>.
1441 static OSMetaClassBase
* checkMetaCastWithName(
1442 const OSSymbol
* className
,
1443 const OSMetaClassBase
* object
);
1446 * @function checkMetaCastWithName
1449 * Search the metaclass inheritance hierarchy by name for an object instance.
1451 * @param className The name of the desired class or superclass.
1452 * @param object The object whose metaclass begins the search.
1455 * <code>object</code> if it's derived from <code>className</code>;
1456 * <code>NULL</code> otherwise.
1459 * Kernel extensions should not use this function directly,
1460 * instead using <code>@link OSDynamicCast OSDynamicCast@/link</code> or
1461 * <code>@link OSCheckTypeInst OSCheckTypeInst@/link</code>.
1463 static OSMetaClassBase
* checkMetaCastWithName(
1464 const OSString
* className
,
1465 const OSMetaClassBase
* object
);
1468 * @function checkMetaCastWithName
1471 * Search the metaclass inheritance hierarchy by name for an object instance.
1473 * @param className The name of the desired class or superclass.
1474 * @param object The object whose metaclass begins the search.
1477 * <code>object</code> if it's derived from <code>className</code>;
1478 * <code>NULL</code> otherwise.
1481 * Kernel extensions should not use this function directly,
1482 * instead using <code>@link OSDynamicCast OSDynamicCast@/link</code> or
1483 * <code>@link OSCheckTypeInst OSCheckTypeInst@/link</code>.
1485 static OSMetaClassBase
* checkMetaCastWithName(
1486 const char * className
,
1487 const OSMetaClassBase
* object
);
1491 * @function instanceConstructed
1494 * Counts the instances of the class managed by this metaclass.
1497 * <i>Not for use by kernel extensions.</i>
1499 * Every non-abstract class that inherits from OSObject
1500 * has a default constructor that calls it's own metaclass's
1501 * <code>instanceConstructed</code> function.
1502 * This constructor is defined by the
1504 * OSDefineMetaClassAndStructors
1505 * OSDefineMetaClassAndStructors@/link</code>
1506 * macro that all OSObject subclasses must use.
1508 * If a class's instance count goes from 0 to 1--that is,
1509 * upon the creation of the first instance of that class--the
1510 * superclass's instance count is also incremented.
1511 * This propagates reference counts up the inheritance chain so that
1512 * superclasses are counted as "in use" when subclasses have instances.
1514 void instanceConstructed() const;
1518 * @function instanceDestructed
1521 * Counts the instances of the class managed by this metaclass.
1524 * Every non-abstract class that inherits from OSObject
1525 * has a default destructor that calls it's own metaclass's
1526 * <code>instanceDestructed</code> function.
1527 * This constructor is defined by the
1528 * @link OSDefineMetaClassAndStructors OSDefineMetaClassAndStructors@/link
1529 * macro that all OSObject subclasses must use.
1531 * If a class's instance count goes from 1 to 0--that is,
1532 * upon the destruction of the last instance of that class--the
1533 * superclass's instance count is also decremented.
1534 * This reduces "in use" counts from superclasses when their subclasses
1535 * no longer have instances.
1537 void instanceDestructed() const;
1541 * @function checkMetaCast
1544 * Check whether a given object is an instance of the receiving
1545 * metaclass's class or one derived from it.
1547 * @param object The object to check for inheritance.
1550 * <code>object</code> if it is derived from the receiver's class,
1551 * <code>NULL</code> if not.
1553 OSMetaClassBase
* checkMetaCast(const OSMetaClassBase
* object
) const;
1557 * @function getInstanceCount
1560 * Returns the number of existing instances of the metaclass's class.
1563 * The number of existing instances of the metaclass's class,
1564 * plus 1 for each subclass with any instance.
1566 unsigned int getInstanceCount() const;
1570 * @function getSuperClass
1573 * Returns the super-metaclass of the receiver.
1576 * Returns a pointer to the super-metaclass of the receiving
1577 * OSMetaClass, or <code>NULL</code> for OSObject's metaclass.
1579 const OSMetaClass
* getSuperClass() const;
1582 * @function getKmodName
1585 * Returns the bundle identifier of the kernel extension
1586 * that defines this metaclass.
1589 * The bundle identifier of the kernel extension that defines this metaclass.
1592 * "Kmod" is an older term for kernel extension.
1594 const OSSymbol
* getKmodName() const;
1598 * @function getClassName
1601 * Returns the name of the C++ class managed by this metaclass.
1604 * Returns the name of the C++ class managed by this metaclass.
1606 const char * getClassName() const;
1607 const OSSymbol
* getClassNameSymbol() const;
1611 * @function getClassSize
1614 * Returns the allocation size of the C++ class managed by this metaclass.
1617 * The allocation size of the C++ class managed by this metaclass.
1619 unsigned int getClassSize() const;
1626 * Allocates an instance of the C++ class managed by this metaclass.
1629 * A pointer to the newly allocated, uninitialized instance,
1630 * with a retain count of 1; <code>NULL</code> on allocation failure.
1633 * This function is automatically created by the metaclass-registration macros
1634 * to enable dynamic instance allocation.
1636 virtual OSObject
* alloc() const = 0;
1638 #ifdef XNU_KERNEL_PRIVATE
1639 void addInstance(const OSObject
* instance
, bool super
= false) const;
1640 void removeInstance(const OSObject
* instance
, bool super
= false) const;
1641 void applyToInstances(OSMetaClassInstanceApplierFunction applier
,
1642 void * context
) const;
1643 static void applyToInstancesOfClassName(
1644 const OSSymbol
* name
,
1645 OSMetaClassInstanceApplierFunction applier
,
1648 static void applyToInstances(OSOrderedSet
* set
,
1649 OSMetaClassInstanceApplierFunction applier
,
1652 #endif /* XNU_KERNEL_PRIVATE */
1654 /* Not to be included in headerdoc.
1656 * @define OSDeclareCommonStructors
1660 * Helper macro for for the standard metaclass-registration macros.
1663 * @param className The name of the C++ class, as a raw token,
1664 * <i>not</i> a string or macro.
1666 #define OSDeclareCommonStructors(className) \
1668 static const OSMetaClass * const superClass; \
1670 static const OSMetaClass * const metaClass; \
1671 static class MetaClass : public OSMetaClass { \
1674 virtual OSObject *alloc() const; \
1676 friend class className ::MetaClass; \
1677 virtual const OSMetaClass * getMetaClass() const APPLE_KEXT_OVERRIDE; \
1679 className (const OSMetaClass *); \
1680 virtual ~ className ()
1684 * @define OSDeclareDefaultStructors
1688 * Declares run-time type information and functions
1689 * for a concrete Libkern C++ class.
1691 * @param className The name of the C++ class, as a raw token,
1692 * <i>not</i> a string or macro.
1695 * Concrete Libkern C++ classes should "call" this macro
1696 * immediately after the opening brace in a class declaration.
1697 * It leaves the current privacy state as <code>protected:</code>.
1699 #define OSDeclareDefaultStructors(className) \
1700 OSDeclareCommonStructors(className); \
1707 * @define OSDeclareAbstractStructors
1711 * Declares run-time type information and functions
1712 * for an abstract Libkern C++ class.
1714 * @param className The name of the C++ class, as a raw token,
1715 * <i>not</i> a string or macro.
1718 * Abstract Libkern C++ classes--those with at least one
1719 * pure virtual method--should "call" this macro
1720 * immediately after the opening brace in a class declaration.
1721 * It leaves the current privacy state as <code>protected:</code>.
1723 #define OSDeclareAbstractStructors(className) \
1724 OSDeclareCommonStructors(className); \
1726 className (); /* Make primary constructor private in abstract */ \
1730 * @define OSDeclareFinalStructors
1734 * Declares run-time type information and functions
1735 * for a final (non-subclassable) Libkern C++ class.
1737 * @param className The name of the C++ class, as a raw token,
1738 * <i>not</i> a string or macro.
1741 * Final Libkern C++ classes--those that do not allow subclassing--should
1742 * "call" this macro immediately after the opening brace in a class declaration.
1743 * (Final classes in the kernel may actually have subclasses in the kernel,
1744 * but kexts cannot define any subclasses of a final class.)
1745 * It leaves the current privacy state as <code>protected:</code>.
1747 * <b>Note:</b> If the class is exported by a pseudokext (symbol set),
1748 * the final symbol generated by this macro must be exported
1749 * for the final-class attribute to be enforced.
1751 * <b>Warning:</b> Changing a class from "Default" to "Final" will break
1752 * binary compatibility.
1754 #define OSDeclareFinalStructors(className) \
1755 OSDeclareDefaultStructors(className) \
1757 void __OSFinalClass(void); \
1761 /* Not to be included in headerdoc.
1763 * @define OSDefineMetaClassWithInit
1767 * Helper macro for for the standard metaclass-registration macros.
1770 * @param className The name of the C++ class, as a raw token,
1771 * <i>not</i> a string or macro.
1772 * @param superclassName The name of the superclass of the C++ class,
1774 * <i>not</i> a string or macro.
1775 * @param init A function to call in the constructor
1776 * of the class's OSMetaClass.
1778 #define OSDefineMetaClassWithInit(className, superclassName, init) \
1779 /* Class global data */ \
1780 className ::MetaClass className ::gMetaClass; \
1781 const OSMetaClass * const className ::metaClass = \
1782 & className ::gMetaClass; \
1783 const OSMetaClass * const className ::superClass = \
1784 & superclassName ::gMetaClass; \
1785 /* Class member functions */ \
1786 className :: className(const OSMetaClass *meta) \
1787 : superclassName (meta) { } \
1788 className ::~ className() { } \
1789 const OSMetaClass * className ::getMetaClass() const \
1790 { return &gMetaClass; } \
1791 /* The ::MetaClass constructor */ \
1792 className ::MetaClass::MetaClass() \
1793 : OSMetaClass(#className, className::superClass, sizeof(className)) \
1797 /* Not to be included in headerdoc.
1799 * @define OSDefineAbstractStructors
1803 * Helper macro for for the standard metaclass-registration macros.
1806 * @param className The name of the C++ class, as a raw token,
1807 * <i>not</i> a string or macro.
1808 * @param superclassName The name of the superclass of the C++ class,
1810 * <i>not</i> a string or macro.
1812 #define OSDefineAbstractStructors(className, superclassName) \
1813 OSObject * className ::MetaClass::alloc() const { return 0; }
1816 /* Not to be included in headerdoc.
1818 * @define OSDefineDefaultStructors
1822 * Helper macro for for the standard metaclass-registration macros.
1825 * @param className The name of the C++ class, as a raw token,
1826 * <i>not</i> a string or macro.
1827 * @param superclassName The name of the superclass of the C++ class,
1829 * <i>not</i> a string or macro.
1831 #define OSDefineDefaultStructors(className, superclassName) \
1832 OSObject * className ::MetaClass::alloc() const \
1833 { return new className; } \
1834 className :: className () : superclassName (&gMetaClass) \
1835 { gMetaClass.instanceConstructed(); }
1837 /* Not to be included in headerdoc.
1839 * @define OSDefineDefaultStructors
1843 * Helper macro for for the standard metaclass-registration macros.
1846 * @param className The name of the C++ class, as a raw token,
1847 * <i>not</i> a string or macro.
1848 * @param superclassName The name of the superclass of the C++ class,
1850 * <i>not</i> a string or macro.
1852 #define OSDefineFinalStructors(className, superclassName) \
1853 OSDefineDefaultStructors(className, superclassName) \
1854 void className ::__OSFinalClass(void) { }
1857 /* Not to be included in headerdoc.
1859 * @define OSDefineMetaClassAndStructorsWithInit
1863 * Helper macro for for the standard metaclass-registration macros.
1866 * @param className The name of the C++ class, as a raw token,
1867 * <i>not</i> a string or macro.
1868 * @param superclassName The name of the superclass of the C++ class,
1870 * <i>not</i> a string or macro.
1871 * @param init A function to call in the constructor
1872 * of the class's OSMetaClass.
1874 #define OSDefineMetaClassAndStructorsWithInit(className, superclassName, init) \
1875 OSDefineMetaClassWithInit(className, superclassName, init) \
1876 OSDefineDefaultStructors(className, superclassName)
1879 /* Not to be included in headerdoc.
1881 * @define OSDefineMetaClassAndAbstractStructorsWithInit
1885 * Helper macro for for the standard metaclass-registration macros.
1888 * @param className The name of the C++ class, as a raw token,
1889 * <i>not</i> a string or macro.
1890 * @param superclassName The name of the superclass of the C++ class,
1892 * <i>not</i> a string or macro.
1893 * @param init A function to call in the constructor
1894 * of the class's OSMetaClass.
1896 #define OSDefineMetaClassAndAbstractStructorsWithInit(className, superclassName, init) \
1897 OSDefineMetaClassWithInit(className, superclassName, init) \
1898 OSDefineAbstractStructors(className, superclassName)
1901 /* Not to be included in headerdoc.
1903 * @define OSDefineMetaClassAndFinalStructorsWithInit
1907 * Helper macro for for the standard metaclass-registration macros.
1910 * @param className The name of the C++ class, as a raw token,
1911 * <i>not</i> a string or macro.
1912 * @param superclassName The name of the superclass of the C++ class,
1914 * <i>not</i> a string or macro.
1915 * @param init A function to call in the constructor
1916 * of the class's OSMetaClass.
1918 #define OSDefineMetaClassAndFinalStructorsWithInit(className, superclassName, init) \
1919 OSDefineMetaClassWithInit(className, superclassName, init) \
1920 OSDefineFinalStructors(className, superclassName)
1925 /* Not to be included in headerdoc.
1927 * @define OSDefineMetaClass
1931 * Helper macro for for the standard metaclass-registration macros.
1934 * @param className The name of the C++ class, as a raw token,
1935 * <i>not</i> a string or macro.
1936 * @param superclassName The name of the superclass of the C++ class,
1938 * <i>not</i> a string or macro.
1939 * @param init A function to call in the constructor
1940 * of the class's OSMetaClass.
1942 #define OSDefineMetaClass(className, superclassName) \
1943 OSDefineMetaClassWithInit(className, superclassName, )
1947 * @define OSDefineMetaClassAndStructors
1951 * Defines an OSMetaClass and associated routines
1952 * for a concrete Libkern C++ class.
1954 * @param className The name of the C++ class, as a raw token,
1955 * <i>not</i> a string or macro.
1956 * @param superclassName The name of the superclass of the C++ class,
1958 * <i>not</i> a string or macro.
1961 * Concrete Libkern C++ classes should "call" this macro
1962 * at the beginning of their implementation files,
1963 * before any function implementations for the class.
1965 #define OSDefineMetaClassAndStructors(className, superclassName) \
1966 OSDefineMetaClassAndStructorsWithInit(className, superclassName, )
1970 * @define OSDefineMetaClassAndAbstractStructors
1974 * Defines an OSMetaClass and associated routines
1975 * for an abstract Libkern C++ class.
1977 * @param className The name of the C++ class, as a raw token,
1978 * <i>not</i> a string or macro.
1979 * @param superclassName The name of the superclass of the C++ class,
1981 * <i>not</i> a string or macro.
1984 * Abstract Libkern C++ classes--those with at least one
1985 * pure virtual method--should "call" this macro
1986 * at the beginning of their implementation files,
1987 * before any function implementations for the class.
1989 #define OSDefineMetaClassAndAbstractStructors(className, superclassName) \
1990 OSDefineMetaClassAndAbstractStructorsWithInit (className, superclassName, )
1994 * @define OSDefineMetaClassAndFinalStructors
1998 * Defines an OSMetaClass and associated routines
1999 * for a final (non-subclassable) Libkern C++ class.
2001 * @param className The name of the C++ class, as a raw token,
2002 * <i>not</i> a string or macro.
2003 * @param superclassName The name of the superclass of the C++ class,
2005 * <i>not</i> a string or macro.
2008 * Final Libkern C++ classes--those that do not allow
2009 * subclassing--should "call" this macro at the beginning
2010 * of their implementation files,
2011 * before any function implementations for the class.
2012 * (Final classes in the kernel may actually have subclasses in the kernel,
2013 * but kexts cannot define any subclasses of a final class.)
2015 * <b>Note:</b> If the class is exported by a pseudokext (symbol set),
2016 * the final symbol generated by this macro must be exported
2017 * for the final-class attribute to be enforced.
2019 * <b>Warning:</b> Changing a class from "Default" to "Final" will break
2020 * binary compatibility.
2022 #define OSDefineMetaClassAndFinalStructors(className, superclassName) \
2023 OSDefineMetaClassAndFinalStructorsWithInit(className, superclassName, )
2026 // Dynamic vtable patchup support routines and types
2027 void reservedCalled(int ind
) const;
2031 * @define OSMetaClassDeclareReservedUnused
2035 * Reserves vtable space for new virtual functions
2036 * in a Libkern C++ class.
2038 * @param className The name of the C++ class, as a raw token,
2039 * <i>not</i> a string or macro.
2040 * @param index The numeric index of the vtable slot,
2041 * as a raw constant, beginning from 0.
2044 * Libkern C++ classes in kernel extensions that can be used as libraries
2045 * can provide for backward compatibility by declaring a number
2046 * of reserved vtable slots
2047 * that can be replaced with new functions as they are added.
2048 * Each reserved declaration must be accompanied in the implementation
2049 * by a corresponding reference to
2050 * <code>@link OSMetaClassDefineReservedUnused
2051 * OSMetaClassDefineReservedUnused@/link</code>.
2053 * When replacing a reserved slot, change the macro from "Unused"
2054 * to "Used" to document the fact that the slot used to be reserved,
2055 * and declare the new function immediately after the "Used" macro
2056 * to preserve vtable ordering.
2058 * <code>@link OSMetaClassDeclareReservedUsed
2059 * OSMetaClassDeclareReservedUsed@/link</code>.
2061 #if APPLE_KEXT_VTABLE_PADDING
2062 #define OSMetaClassDeclareReservedUnused(className, index) \
2064 virtual void _RESERVED ## className ## index ()
2066 #define OSMetaClassDeclareReservedUnused(className, index)
2071 * @define OSMetaClassDeclareReservedUsed
2075 * Documents use of reserved vtable space for new virtual functions
2076 * in a Libkern C++ class.
2078 * @param className The name of the C++ class, as a raw token,
2079 * <i>not</i> a string or macro.
2080 * @param index The numeric index of the vtable slot,
2081 * as a raw constant, beginning from 0.
2084 * This macro evaluates to nothing, and is used to document reserved
2085 * vtable slots as they are filled.
2087 * <code>@link OSMetaClassDeclareReservedUnused
2088 * OSMetaClassDeclareReservedUnused@/link</code>.
2090 #define OSMetaClassDeclareReservedUsed(className, index)
2094 * @define OSMetaClassDefineReservedUnused
2098 * Defines a reserved vtable slot for a Libkern C++ class.
2100 * @param className The name of the C++ class, as a raw token,
2101 * <i>not</i> a string or macro.
2102 * @param index The numeric index of the vtable slot,
2103 * as a raw constant, beginning from 0.
2106 * Libkern C++ classes in kernel extensions that can be used as libraries
2107 * can provide for backward compatibility by declaring a number
2108 * of reserved vtable slots
2109 * that can be replaced with new functions as they are added.
2110 * Each reserved defintion accompanies
2111 * a corresponding declaration created with
2112 * <code>@link OSMetaClassDeclareReservedUnused
2113 * OSMetaClassDeclareReservedUnused@/link</code>.
2115 * This macro is used in the implementation file
2116 * to provide a placeholder definition for the reserved vtable slot,
2117 * as a function that calls <code>panic</code> with an error message.
2119 * When replacing a reserved slot, change the macro from "Unused"
2120 * to "Used" to document the fact that the slot used to be reserved,
2121 * and declare the new function immediately after the "Used" macro
2122 * to preserve vtable ordering.
2124 * <code>@link OSMetaClassDefineReservedUsed
2125 * OSMetaClassDefineReservedUsed@/link</code>.
2127 #if APPLE_KEXT_VTABLE_PADDING
2128 #define OSMetaClassDefineReservedUnused(className, index) \
2129 void className ::_RESERVED ## className ## index () \
2130 { gMetaClass.reservedCalled(index); }
2132 #define OSMetaClassDefineReservedUnused(className, index)
2137 * @define OSMetaClassDefineReservedUsed
2141 * Reserves vtable space for new virtual functions in a Libkern C++ class.
2143 * @param className The name of the C++ class, as a raw token,
2144 * <i>not</i> a string or macro.
2145 * @param index The numeric index of the vtable slot,
2146 * as a raw constant, beginning from 0.
2149 * This macro evaluates to nothing, and is used to document reserved
2150 * vtable slots as they are filled.
2152 * <code>@link OSMetaClassDefineReservedUnused
2153 * OSMetaClassDefineReservedUnused@/link</code>.
2155 #define OSMetaClassDefineReservedUsed(className, index)
2157 // I/O Kit debug internal routines.
2158 static void printInstanceCounts();
2159 static void serializeClassDictionary(OSDictionary
* dict
);
2160 #ifdef XNU_KERNEL_PRIVATE
2163 static void * trackedNew(size_t size
);
2164 static void trackedDelete(void * mem
, size_t size
);
2165 void trackedInstance(OSObject
* instance
) const;
2166 void trackedFree(OSObject
* instance
) const;
2167 void trackedAccumSize(OSObject
* instance
, size_t size
) const;
2168 struct IOTrackingQueue
* getTracking() const;
2169 #endif /* IOTRACKING */
2170 #endif /* XNU_KERNEL_PRIVATE */
2174 static OSDictionary
* getClassDictionary();
2175 virtual bool serialize(OSSerialize
* serializer
) const;
2177 // Virtual Padding functions for MetaClass's
2178 OSMetaClassDeclareReservedUnused(OSMetaClass
, 0);
2179 OSMetaClassDeclareReservedUnused(OSMetaClass
, 1);
2180 OSMetaClassDeclareReservedUnused(OSMetaClass
, 2);
2181 OSMetaClassDeclareReservedUnused(OSMetaClass
, 3);
2182 OSMetaClassDeclareReservedUnused(OSMetaClass
, 4);
2183 OSMetaClassDeclareReservedUnused(OSMetaClass
, 5);
2184 OSMetaClassDeclareReservedUnused(OSMetaClass
, 6);
2185 OSMetaClassDeclareReservedUnused(OSMetaClass
, 7);
2188 #endif /* !_LIBKERN_OSMETACLASS_H */