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
117 #define APPLE_KEXT_WSHADOW_PUSH _Pragma("clang diagnostic push"); \
118 _Pragma("clang diagnostic ignored \"-Wunknown-warning-option\"") \
119 _Pragma("clang diagnostic ignored \"-Wshadow-field\"")
121 #define APPLE_KEXT_WSHADOW_POP _Pragma("clang diagnostic pop")
125 * @class OSMetaClassBase
128 * OSMetaClassBase is the abstract bootstrap class
129 * for the Libkern and I/O Kit run-time type information system.
132 * OSMetaClassBase is the abstract C++ root class
133 * underlying the entire Libkern and I/O Kit class hierarchy.
134 * It defines the run-time type information system,
135 * including dynamic class allocation and safe type-casting,
136 * as well as the abstract interface for reference counting
137 * and a few other utility functions.
138 * OSMetaClassBase is the immediate superclass of
139 * @link //apple_ref/doc/class/OSObject OSObject@/link and
140 * @link //apple_ref/doc/class/OSMetaClass OSMetaClass@/link;
141 * no other class should derive from OSMetaClassBase.
143 * For more information, see
144 * <i>@link //apple_ref/doc/uid/TP40002799
145 * I/O Kit Device Driver Design Guidelines@/link</i>.
147 * <b>Use by Kernel Extensions</b>
149 * Kernel Extensions should never interact directly with OSMetaClassBase,
150 * but they will find useful several macros that tie in
151 * to the run-time type information system, specifically:
153 * <li><code>@link OSTypeAlloc OSTypeAlloc@/link</code> - allocation of new instances</li>
154 * <li><code>@link OSDynamicCast OSDynamicCast@/link</code> - safe type casting</li>
155 * <li><code>@link OSCheckTypeInst OSCheckTypeInst@/link</code> -
156 * checking for inheritance/derivation</li>
157 * <li><code>@link OSMemberFunctionCast OSMemberFunctionCast@/link</code> -
158 * casting C++ member functions to C function pointers
159 * for registration as callbacks</li>
162 * See @link //apple_ref/doc/class/OSMetaClass OSMetaClass@/link
163 * for more run-time type information interfaces.
165 * <b>Use Restrictions</b>
167 * OSMetaClassBase should not be subclassed by kernel extensions,
168 * nor should kernel extensions call its run-time type functions directly.
170 * The run-time type functions and macros are <b>not safe</b>
171 * to call in a primary interrupt context.
173 * <b>Concurrency Protection</b>
175 * The run-time type macros and functions of OSMetaClassBase are thread-safe.
177 class OSMetaClassBase
183 * @define OSTypeAlloc
187 * Allocates an instance of the named object class.
189 * @param type The name of the desired class to be created,
190 * as a raw token, <i>not</i> a string or macro.
193 * A pointer to the new, uninitialized object on success;
194 * <code>NULL</code> on failure.
199 * //apple_ref/cpp/clm/OSMetaClass/allocClassWithName/staticOSObject*\/(constchar*)
200 * OSMetaClass::allocClassWithName(const char *)@/link</code>
203 * //apple_ref/cpp/instm/OSMetaClass/alloc/virtualOSObject*\/()
204 * OSMetaClass::alloc@/link</code>.
206 * The OSTypeAlloc macro is used to avoid binary compatibility difficulties
207 * presented by the C++ <code>new</code> operator.
209 #define OSTypeAlloc(type) ((type *) ((type::metaClass)->alloc()))
217 * Returns the type ID (metaclass) of a class based on its name.
219 * @param type The name of the desired class, as a raw token,
220 * <i>not</i> a string or macro.
223 * The unique type ID (metaclass) for the class.
226 * It is typically more useful to determine whether a class is derived
228 * <code>@link //apple_ref/cpp/macro/OSDynamicCast OSDynamicCast@/link</code>
230 * <code>@link //apple_ref/cpp/macro/OSCheckTypeInst OSCheckTypeInst@/link</code>.
232 #define OSTypeID(type) (type::metaClass)
236 * @define OSTypeIDInst
240 * Returns the type ID (metaclass) for the class of an object instance.
242 * @param typeinst An instance of an OSObject subclass.
245 * The type ID of that object's class; that is, its metaclass.
248 * It is typically more useful to determine whether an object is derived
249 * from a particular class; see
250 * <code>@link //apple_ref/cpp/macro/OSDynamicCast OSDynamicCast@/link</code>
252 * <code>@link //apple_ref/cpp/macro/OSCheckTypeInst OSCheckTypeInst@/link</code>.
254 #define OSTypeIDInst(typeinst) ((typeinst)->getMetaClass())
258 * @define OSDynamicCast
262 * Safe type-casting for Libkern C++ objects.
264 * @param type The name of the desired class type, as a raw token,
265 * <i>not</i> a string or macro.
266 * It is assumed you intend to cast to a pointer
267 * to an object of this type.
268 * Type qualifiers, such as <code>const</code>,
269 * are not recognized and will cause
270 * a (usually obscure) compile error.
271 * @param inst A pointer to the object instance to be cast.
272 * May be <code>NULL</code>.
275 * <code>inst</code> if it is non-<code>NULL</code>
276 * and derived from <code>type</code>;
277 * otherwise <code>NULL</code>.
280 * <code>OSDynamicCast</code> is a rough equivalent
281 * to the standard C++ RTTI <code>dynamic_cast<T></code> operator.
282 * Your code should use this instead of raw C type-casting,
283 * and check the resulting value.
284 * If the result is non-<code>NULL</code>,
285 * the object is safe to use as the type-cast class;
286 * if the result is <code>NULL</code>,
287 * the object does not derive from the type-cast class
288 * and your code should take appropriate steps to handle the error.
290 #define OSDynamicCast(type, inst) \
291 ((type *) OSMetaClassBase::safeMetaCast((inst), OSTypeID(type)))
295 * @define OSCheckTypeInst
299 * Checks whether two objects are type-compatible.
301 * @param typeinst The reference object.
302 * @param inst The object to check for type compatibility.
305 * <code>true</code> if both <code>inst</code> and
306 * <code>typeinst</code> are non-<code>NULL</code>
307 * and <code>inst</code> is derived from the class of <code>typeinst</code>;
308 * otherwise <code>false</code>.
310 #define OSCheckTypeInst(typeinst, inst) \
311 OSMetaClassBase::checkTypeInst(inst, typeinst)
313 #define OSSafeRelease(inst) \
314 do { int OSSafeRelease __attribute__ ((deprecated("Use OSSafeReleaseNULL"))); (OSSafeRelease); \
315 if (inst) (inst)->release(); } while (0)
317 /*! @function OSSafeReleaseNULL
318 * @abstract Release an object if not <code>NULL</code>, then set it to <code>NULL</code>.
319 * @param inst Instance of an OSObject, may be <code>NULL</code>.
321 #define OSSafeReleaseNULL(inst) do { if (inst) (inst)->release(); (inst) = NULL; } while (0)
323 typedef void (*_ptf_t
)(void);
325 #if APPLE_KEXT_LEGACY_ABI
327 // Arcane evil code interprets a C++ pointer to function as specified in the
328 // -fapple-kext ABI, i.e. the gcc-2.95 generated code. IT DOES NOT ALLOW
329 // the conversion of functions that are from MULTIPLY inherited classes.
332 _ptmf2ptf(const OSMetaClassBase
*self
, void (OSMetaClassBase::*func
)(void))
335 void (OSMetaClassBase::*fIn
)(void);
336 struct { // Pointer to member function 2.95
337 unsigned short fToff
;
347 if (map
.fptmf2
.fToff
) {
348 panic("Multiple inheritance is not supported");
350 } else if (map
.fptmf2
.fVInd
< 0) {
351 // Not virtual, i.e. plain member func
352 return map
.fptmf2
.u
.fPFN
;
355 const OSMetaClassBase
*fObj
;
360 // Virtual member function so dereference vtable
361 return (*u
.vtablep
)[map
.fptmf2
.fVInd
- 1];
365 #else /* !APPLE_KEXT_LEGACY_ABI */
366 #if defined(__arm__) || defined(__arm64__)
367 typedef long int ptrdiff_t;
369 * Ugly reverse engineered ABI. Where does it come from? Nobody knows.
370 * <rdar://problem/5641129> gcc 4.2-built ARM kernel panics with multiple inheritance (no, really)
373 _ptmf2ptf(const OSMetaClassBase
*self
, void (OSMetaClassBase::*func
)(void))
380 void (OSMetaClassBase::*fIn
)(void);
387 if (map
.pTMF
.delta
& 1) {
390 const OSMetaClassBase
*fObj
;
395 // Virtual member function so dereference table
396 return *(_ptf_t
*)(((uintptr_t)*u
.vtablep
) + (uintptr_t)map
.pTMF
.fPFN
);
398 // Not virtual, i.e. plain member func
399 return map
.pTMF
.fPFN
;
402 #elif defined(__i386__) || defined(__x86_64__)
404 // Slightly less arcane and slightly less evil code to do
405 // the same for kexts compiled with the standard Itanium C++
409 _ptmf2ptf(const OSMetaClassBase
*self
, void (OSMetaClassBase::*func
)(void))
412 void (OSMetaClassBase::*fIn
)(void);
419 if (map
.fVTOffset
& 1) {
422 const OSMetaClassBase
*fObj
;
427 // Virtual member function so dereference vtable
428 return *(_ptf_t
*)(((uintptr_t)*u
.vtablep
) + map
.fVTOffset
- 1);
430 // Not virtual, i.e. plain member func
436 #error Unknown architecture.
439 #endif /* !APPLE_KEXT_LEGACY_ABI */
442 * @define OSMemberFunctionCast
446 * Converts a C++ member function pointer, relative to an instance,
447 * to a C-style pointer to function.
449 * @param cptrtype The function type declaration to cast to
450 * (typically provided as a <code>typedef</code> by I/O KitKit classes).
451 * @param self The <code>this</code> pointer of the object whose function
453 * @param func The pointer to the member function itself,
454 * something like <code>&Class::function</code>.
457 * A pointer to a function of the given type referencing <code>self</code>.
460 * This function is used to generate pointers to C++ functions for instances,
461 * such that they can be registered as callbacks with I/O Kit objects.
463 * No warnings are generated.
465 * This function will panic if an attempt is made to call it
466 * with a multiply-inheriting class.
468 #define OSMemberFunctionCast(cptrtype, self, func) \
469 (cptrtype) OSMetaClassBase:: \
470 _ptmf2ptf(self, (void (OSMetaClassBase::*)(void)) func)
474 virtual ~OSMetaClassBase();
477 // Disable copy constructors of OSMetaClassBase based objects
478 /* Not to be included in headerdoc.
480 * @function operator =
483 * Disable implicit copy constructor by making private
485 * @param src Reference to source object that isn't allowed to be copied.
487 void operator =(OSMetaClassBase
&src
);
489 /* Not to be included in headerdoc.
491 * @function OSMetaClassBase
494 * Disable implicit copy constructor by making private
496 * @param src Reference to source object that isn't allowed to be copied.
498 OSMetaClassBase(OSMetaClassBase
&src
);
502 // xx-review: the original comment for this makes it sound to me like we don't
503 // xx-review: catch over-releasing an object...?
509 * Abstract declaration of
511 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/(int)
512 * release(int freeWhen)@/link</code>.
517 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/(int)
518 * release(int freeWhen)@/link</code>.
520 virtual void release(int freeWhen
) const = 0;
524 * @function getRetainCount
527 * Abstract declaration of
529 * //apple_ref/cpp/instm/OSObject/getRetainCount/virtualint/()
530 * getRetainCount()@/link</code>.
535 * //apple_ref/cpp/instm/OSObject/getRetainCount/virtualint/()
536 * OSObject::getRetainCount()@/link</code>.
538 virtual int getRetainCount() const = 0;
545 * Abstract declaration of
547 * //apple_ref/cpp/instm/OSObject/retain/virtualvoid/()
548 * retain()@/link</code>.
553 * //apple_ref/cpp/instm/OSObject/retain/virtualvoid/()
554 * OSObject::retain()@/link</code>.
556 virtual void retain() const = 0;
563 * Abstract declaration of
565 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/()
566 * release@/link</code>.
571 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/()
572 * OSObject::release@/link</code>.
574 virtual void release() const = 0;
578 * @function serialize
581 * Abstract declaration of
583 * //apple_ref/cpp/instm/OSObject/serialize/virtualbool/(OSSerialize*)
584 * serialize@/link</code>.
589 * //apple_ref/cpp/instm/OSObject/serialize/virtualbool/(OSSerialize*)
590 * OSObject::serialize@/link</code>.
592 virtual bool serialize(OSSerialize
* serializer
) const = 0;
596 * @function getMetaClass
599 * Returns the OSMetaClass representing
600 * an OSMetaClassBase subclass.
603 * OSObject overrides this abstract member function
604 * to return the OSMetaClass object that represents
605 * each class for run-time typing.
607 virtual const OSMetaClass
* getMetaClass() const = 0;
611 * @function isEqualTo
614 * Checks whether another object is equal to the receiver.
616 * @param anObject The object to copmare to the receiver.
619 * <code>true</code> if the objects are equal, <code>false</code> otherwise.
622 * OSMetaClassBase implements this as a direct pointer comparison,
623 * since it has no other information to judge equality by.
624 * Subclasses generally override this function
625 * to do a more meaningful comparison.
626 * For example, OSString implements it to return
627 * <code>true</code> if <code>anObject</code>
628 * is derived from OSString and represents the same C string.
630 virtual bool isEqualTo(const OSMetaClassBase
* anObject
) const;
637 * Casts this object is to the class managed by the given OSMetaClass.
639 * @param toMeta A pointer to a constant OSMetaClass
640 * for the desired target type.
643 * <code>this</code> if the object is derived
644 * from the class managed by <code>toMeta</code>,
645 * otherwise <code>NULL</code>.
648 * It is far more convenient to use
649 * <code>@link OSDynamicCast OSDynamicCast@/link</code>.
651 OSMetaClassBase
* metaCast(const OSMetaClass
* toMeta
) const;
658 * Casts this object is to the class managed by the named OSMetaClass.
660 * @param toMeta An OSSymbol naming the desired target type.
663 * <code>this</code> if the object is derived
664 * from the class named by <code>toMeta</code>,
665 * otherwise <code>NULL</code>.
668 * It is far more convenient to use
669 * <code>@link OSDynamicCast OSDynamicCast@/link</code>.
671 OSMetaClassBase
* metaCast(const OSSymbol
* toMeta
) const;
678 * Casts this object is to the class managed by the named OSMetaClass.
680 * @param toMeta An OSString naming the desired target type.
682 * <code>this</code> if the object is derived
683 * from the class named by <code>toMeta</code>,
684 * otherwise <code>NULL</code>.
687 * It is far more convenient to use
688 * <code>@link OSDynamicCast OSDynamicCast@/link</code>.
690 OSMetaClassBase
* metaCast(const OSString
* toMeta
) const;
697 * Casts this object is to the class managed by the named OSMetaClass.
699 * @param toMeta A C string naming the desired target type.
701 * <code>this</code> if the object is derived
702 * from the class named by <code>toMeta</code>,
703 * otherwise <code>NULL</code>.
706 * It is far more convenient to use
707 * <code>@link OSDynamicCast OSDynamicCast@/link</code>.
709 OSMetaClassBase
* metaCast(const char * toMeta
) const;
711 // Helper inlines for run-time type preprocessor macros
713 * @function safeMetaCast
716 * Casts an object is to the class managed by the given OSMetaClass.
718 * @param anObject A pointer to the object to be cast.
719 * @param toMeta A pointer to a constant OSMetaClass
720 * for the desired target type.
723 * <code>anObject</code> if the object is derived
724 * from the class managed by <code>toMeta</code>,
725 * otherwise <code>NULL</code>.
728 * It is far more convenient to use
729 * <code>@link OSDynamicCast OSDynamicCast@/link</code>.
731 static OSMetaClassBase
* safeMetaCast(
732 const OSMetaClassBase
* anObject
,
733 const OSMetaClass
* toMeta
);
736 * @function checkTypeInst
739 * Checks whether an object instance is of the same class
740 * as another object instance (or a subclass of that class).
742 * @param inst A pointer to the object to check.
743 * @param typeinst A pointer to an object of the class being checked.
746 * <code>true</code> if the object is derived
747 * from the class of <code>typeinst</code>
748 * or a subclass of that class,
749 * otherwise <code>false</code>.
752 * It is far more convenient to use
753 * <code>@link OSCheckTypeInst OSCheckTypeInst@/link</code>.
755 static bool checkTypeInst(
756 const OSMetaClassBase
* inst
,
757 const OSMetaClassBase
* typeinst
);
759 static void initialize(void);
764 * @function taggedRetain
767 * Abstract declaration of
769 * //apple_ref/cpp/instm/OSObject/taggedRetain/virtualvoid/(constvoid*)
770 * taggedRetain(const void *)@/link</code>.
775 * //apple_ref/cpp/instm/OSObject/taggedRetain/virtualvoid/(constvoid*)
776 * OSObject::taggedRetain(const void *)@/link</code>.
778 // WAS: virtual void _RESERVEDOSMetaClassBase0();
779 virtual void taggedRetain(const void * tag
= 0) const = 0;
783 * @function taggedRelease
786 * Abstract declaration of
788 * //apple_ref/cpp/instm/OSObject/taggedRelease/virtualvoid/(constvoid*)
789 * taggedRelease(const void *)@/link</code>.
794 * //apple_ref/cpp/instm/OSObject/taggedRelease/virtualvoid/(constvoid*)
795 * OSObject::taggedRelease(const void *)@/link</code>.
797 // WAS: virtual void _RESERVEDOSMetaClassBase1();
798 virtual void taggedRelease(const void * tag
= 0) const = 0;
802 * @function taggedRelease
805 * Abstract declaration of
807 * //apple_ref/cpp/instm/OSObject/taggedRelease/virtualvoid/(constvoid*,constint)
808 * taggedRelease(const void *, const int freeWhen)@/link</code>.
813 * //apple_ref/cpp/instm/OSObject/taggedRelease/virtualvoid/(constvoid*,constint)
814 * OSObject::taggedRelease(const void *, const int freeWhen)@/link</code>.
816 // WAS: virtual void _RESERVEDOSMetaClassBase2();
817 virtual void taggedRelease(
819 const int freeWhen
) const = 0;
822 #if APPLE_KEXT_VTABLE_PADDING
824 virtual void _RESERVEDOSMetaClassBase3();
825 virtual void _RESERVEDOSMetaClassBase4();
826 virtual void _RESERVEDOSMetaClassBase5();
827 virtual void _RESERVEDOSMetaClassBase6();
828 virtual void _RESERVEDOSMetaClassBase7();
830 } APPLE_KEXT_COMPATIBILITY
;
833 #ifdef XNU_KERNEL_PRIVATE
834 typedef bool (*OSMetaClassInstanceApplierFunction
)(const OSObject
* instance
,
836 #endif /* XNU_KERNEL_PRIVATE */
842 * OSMetaClass manages run-time type information
843 * for Libkern and I/O Kit C++ classes.
845 * @discussion OSMetaClass manages run-time type information
846 * for Libkern and I/O Kit C++ classes.
847 * An instance of OSMetaClass exists for (nearly) every such C++ class,
848 * keeping track of inheritance relationships, class lookup by name,
849 * instance counts, and more.
850 * OSMetaClass operates almost entirely behind the scenes,
851 * and kernel extensions should rarely, if ever,
852 * have to interact directly with OSMetaClass.
854 * <b>Use by Kernel Extensions</b>
856 * While kernel extensions rarey interact directly with OSMetaClass at run time,
857 * they must register their classes with the metaclass system
858 * using the macros declared here.
859 * The class declaration should use one of these two macros
860 * before its first member function declaration:
862 * <li><code>@link OSDeclareDefaultStructors OSDeclareDefaultStructors@/link</code> -
863 * for classes with no abstract member function declarations</li>
864 * <li><code>@link OSDeclareAbstractStructors OSDeclareAbstractStructors@/link</code> -
865 * for classes with at least one abstract member function declaration</li>
866 * <li><code>@link OSDeclareFinalStructors OSDeclareFinalStructors@/link</code> -
867 * for classes that should not be subclassable by another kext</li>
870 * The class implementation should then use one of these macros:
872 * <li><code>@link OSDefineMetaClassAndStructors
873 * OSDefineMetaClassAndStructors@/link</code> -
874 * for classes with no abstract member function declarations</li>
875 * <li><code>@link OSDefineMetaClassAndAbstractStructors
876 * OSDefineMetaClassAndAbstractStructors@/link</code> -
877 * for classes with at least one abstract member function declaration</li>
878 * <li><code>@link OSDefineMetaClassAndFinalStructors
879 * OSDefineMetaClassAndFinalStructors@/link</code> -
880 * for classes that should not be subclassable by another kext</li>
883 * Classes in kernel extensions that are intended for use as libraries
884 * may need to reserve vtable slots to preserve binary compatibility
885 * as new functions are added. They may do so with these macros:
887 * <li><code>@link OSMetaClassDeclareReservedUnused
888 * OSMetaClassDeclareReservedUnused@/link</code> -
889 * reserves a vtable slot</li>
890 * <li><code>@link OSMetaClassDefineReservedUnused
891 * OSMetaClassDefineReservedUnused@/link</code> -
892 * defines the reserved vtable slot as an unimplemented function</li>
893 * <li><code>@link OSMetaClassDeclareReservedUsed
894 * OSMetaClassDeclareReservedUsed@/link</code> -
895 * documents that a formerly reserved slot is now used</li>
896 * <li><code>@link OSMetaClassDefineReservedUsed
897 * OSMetaClassDefineReservedUsed@/link</code> -
898 * documents that a formerly reserved slot is now used</li>
901 * <b>Use Restrictions</b>
903 * OSMetaClass should not be explicitly subclassed by kernel extensions
904 * (the declare/define macros do that),
905 * nor should kernel extensions call its run-time type functions directly.
907 * OSMetaClass functions should be considered
908 * <b>unsafe</b> to call in a primary interrupt context.
910 * <b>Concurrency Protection</b>
912 * Kernel extensions should in general not interact
913 * with OSMetaClass objects directly,
914 * instead using the run-time type macros.
915 * Much of OSMetaClass's interface is intended for use
916 * by the run-time type information system,
917 * which handles concurrency and locking internally.
919 class OSMetaClass
: private OSMetaClassBase
923 friend class IOStatistics
;
927 // Can never be allocated must be created at compile time
928 static void * operator new(size_t size
);
930 /* Reserved for future use. (Internal use only) */
931 struct ExpansionData
*reserved
;
933 /* superClass Handle to the superclass's meta class. */
934 const OSMetaClass
*superClassLink
;
936 /* className OSSymbol of the class' name. */
937 const OSSymbol
*className
;
939 /* classSize How big is a single instance of this class. */
940 unsigned int classSize
;
942 /* instanceCount Roughly number of instances of the object,
943 * +1 for each direct subclass with a nonzero refcount.
944 * Used primarily as a code-in-use flag.
946 mutable unsigned int instanceCount
;
948 /* Not to be included in headerdoc.
950 * @function OSMetaClass
953 * The default private constructor.
957 // Called by postModLoad
958 /* Not to be included in headerdoc.
963 * Logs an error string for an <code>OSReturn</code> value
964 * using <code>printf</code>.
966 * @param result The <code>OSReturn</code> value for which to log a message.
969 * This function is used to log errors loading kernel extensions.
970 * Kernel extensions themselves should not call it.
972 static void logError(OSReturn result
);
977 * @function getMetaClassWithName
980 * Look up a metaclass in the run-time type information system.
982 * @param name The name of the desired class's metaclass.
985 * A pointer to the metaclass object if found, <code>NULL</code> otherwise.
987 static const OSMetaClass
* getMetaClassWithName(const OSSymbol
* name
);
989 #if XNU_KERNEL_PRIVATE
992 * @function copyMetaClassWithName
995 * Look up a metaclass in the run-time type information system.
997 * @param name The name of the desired class's metaclass.
1000 * A pointer to the metaclass object if found, <code>NULL</code> otherwise.
1001 * The metaclass will be protected from unloading until releaseMetaClass()
1004 static const OSMetaClass
* copyMetaClassWithName(const OSSymbol
* name
);
1006 * @function releaseMetaClass
1009 * Releases reference obtained from copyMetaClassWithName().
1012 * The metaclass will be protected from unloading until releaseMetaClass()
1015 void releaseMetaClass() const;
1017 #endif /* XNU_KERNEL_PRIVATE */
1024 * Implements the abstract <code>retain</code> function to do nothing.
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 retain() const;
1038 * Implements the abstract <code>release</code> function to do nothing.
1041 * Since an OSMetaClass instance must remain in existence
1042 * for as long as its kernel extension is loaded,
1043 * OSMetaClass does not use reference-counting.
1045 virtual void release() const;
1052 * Implements the abstract <code>release(int freeWhen)</code>
1053 * function to do nothing.
1055 * @param freeWhen Unused.
1058 * Since an OSMetaClass instance must remain in existence
1059 * for as long as its kernel extension is loaded,
1060 * OSMetaClass does not use reference-counting.
1062 virtual void release(int freeWhen
) const;
1066 * @function taggedRetain
1069 * Implements the abstract <code>taggedRetain(const void *)</code>
1070 * function to do nothing.
1072 * @param tag Unused.
1075 * Since an OSMetaClass instance must remain in existence
1076 * for as long as its kernel extension is loaded,
1077 * OSMetaClass does not use reference-counting.
1079 virtual void taggedRetain(const void * tag
= 0) const;
1083 * @function taggedRelease
1086 * Implements the abstract <code>taggedRelease(const void *)</code>
1087 * function to do nothing.
1089 * @param tag Unused.
1092 * Since an OSMetaClass instance must remain in existence
1093 * for as long as its kernel extension is loaded,
1094 * OSMetaClass does not use reference-counting.
1096 virtual void taggedRelease(const void * tag
= 0) const;
1100 * @function taggedRelease
1103 * Implements the abstract <code>taggedRelease(const void *, cont int)</code>
1104 * function to do nothing.
1106 * @param tag Unused.
1107 * @param freeWhen Unused.
1110 * Since an OSMetaClass instance must remain in existence
1111 * for as long as its kernel extension is loaded,
1112 * OSMetaClass does not use reference-counting.
1114 virtual void taggedRelease(
1116 const int freeWhen
) const;
1120 * @function getRetainCount
1123 * Implements the abstract <code>getRetainCount</code>
1124 * function to return 0.
1130 * Since an OSMetaClass instance must remain in existence
1131 * for as long as its kernel extension is loaded,
1132 * OSMetaClass does not use reference-counting.
1134 virtual int getRetainCount() const;
1137 /* Not to be included in headerdoc.
1139 * @function getMetaClass
1142 * Returns the meta-metaclass.
1145 * The metaclass of the OSMetaClass object.
1147 virtual const OSMetaClass
* getMetaClass() const;
1151 * @function OSMetaClass
1154 * Constructor for OSMetaClass objects.
1156 * @param className A C string naming the C++ class
1157 * that this OSMetaClass represents.
1158 * @param superclass The OSMetaClass object representing the superclass
1159 * of this metaclass's class.
1160 * @param classSize The allocation size of the represented C++ class.
1163 * This constructor is protected and cannot be used
1164 * to instantiate OSMetaClass directly, as OSMetaClass is an abstract class.
1165 * This function is called during kext loading
1166 * to queue C++ classes for registration.
1167 * See <code>@link preModLoad preModLoad@/link</code> and
1168 * <code>@link postModLoad postModLoad@/link</code>.
1170 OSMetaClass(const char * className
,
1171 const OSMetaClass
* superclass
,
1172 unsigned int classSize
);
1176 * @function ~OSMetaClass
1179 * Destructor for OSMetaClass objects.
1182 * This function is called when the kernel extension that implements
1183 * the metaclass's class is unloaded.
1184 * The destructor removes all references to the class
1185 * from the run-time type information system.
1187 virtual ~OSMetaClass();
1189 // Needs to be overriden as NULL as all OSMetaClass objects are allocated
1190 // statically at compile time, don't accidently try to free them.
1191 void operator delete(void *, size_t) { }
1194 static const OSMetaClass
* const metaClass
;
1197 * @function preModLoad
1200 * Prepares the run-time type system
1201 * for the creation of new metaclasses
1202 * during loading of a kernel extension (module).
1204 * @param kextID The bundle ID of the kext being loaded.
1207 * An opaque handle to the load context
1208 * for the kernel extension on success;
1209 * <code>NULL</code> on failure.
1212 * <i>Not for use by kernel extensions.</i>
1214 * Prepares the run-time type information system to record and register
1215 * metaclasses created by static constructors until a subsequent call to
1216 * <code>@link postModLoad postModLoad@/link</code>.
1217 * <code>preModLoad</code> takes a lock to ensure processing of a single
1218 * load operation at a time; the lock is released by
1219 * <code>@link postModLoad postModLoad@/link</code>.
1220 * Any OSMetaClass constructed between these two function calls
1221 * will be associated with <code>kextID</code>.
1223 static void * preModLoad(const char * kextID
);
1227 * @function checkModLoad
1230 * Checks whether the current kext load operation can proceed.
1232 * @param loadHandle The opaque handle returned
1233 * by <code>@link preModLoad preModLoad@/link</code>.
1235 * <code>true</code> if no errors are outstanding
1236 * and the system is ready to process more metaclasses.
1239 * <i>Not for use by kernel extensions.</i>
1241 static bool checkModLoad(void * loadHandle
);
1245 * @function postModLoad
1248 * Registers the metaclasses created during loading of a kernel extension.
1250 * @param loadHandle The opaque handle returned
1251 * by <code>@link preModLoad preModLoad@/link</code>.
1253 * The error code of the first error encountered,
1256 * //apple_ref/cpp/macro/kOSReturnSuccess
1257 * kOSReturnSuccess@/link</code>
1258 * if no error occurred.
1261 * <i>Not for use by kernel extensions.</i>
1263 * Called after all static constructors in a kernel extension
1264 * have created metaclasses,
1265 * this function checks for duplicate class names,
1266 * then registers the new metaclasses under the kext ID
1267 * that @link preModLoad preModLoad@/link was called with,
1268 * so that they can be dynamically allocated
1269 * and have their instance counts tracked.
1270 * <code>postModLoad</code> releases the lock taken by
1271 * <code>@link preModLoad preModLoad@/link</code>.
1273 static OSReturn
postModLoad(void * loadHandle
);
1276 * @function modHasInstance
1279 * Returns whether any classes defined by the named
1280 * kernel extension (or their subclasses) have existing instances.
1282 * @param kextID The bundle ID of the kernel extension to check.
1285 * <code>true</code> if the kext is found and
1286 * if any class defined by that kext
1287 * has a nonzero instance count,
1288 * <code>false</code> otherwise.
1291 * This function is called before a kernel extension's static destructors
1292 * are invoked, prior to unloading the extension.
1293 * If any classes stil have instances or subclasses with instances,
1294 * those classes are logged
1295 * (using <code>@link reportModInstances reportModInstances@/link</code>) and
1296 * the kernel extension is not be unloaded.
1298 static bool modHasInstance(const char * kextID
);
1302 * @function reportModInstances
1305 * Logs the instance counts for classes
1306 * defined by a kernel extension.
1308 * @param kextID The bundle ID of the kernel extension to report on.
1311 * This function prints the names and instance counts
1312 * of any class defined by <code>kextID</code>
1313 * that has a nonzero instance count.
1314 * It's called by <code>@link modHasInstance modHasInstance@/link</code>
1315 * to help diagnose problems unloading kernel extensions.
1317 static void reportModInstances(const char * kextID
);
1321 * @function considerUnloads
1324 * Schedule automatic unloading of unused kernel extensions.
1327 * This function schedules a check for kernel extensions
1328 * that can be automatically unloaded,
1329 * canceling any currently scheduled check.
1330 * At that time, any such kexts with no Libkern C++ instances
1331 * and no external references are unloaded.
1333 * The I/O Kit calls this function when matching goes idle.
1335 * Kernel extensions that define subclasses of
1336 * @link //apple_ref/doc/class/IOService IOService@/link
1337 * are eligible for automatic unloading.
1339 * (On releases of Mac OS X prior to Snow Leopard (10.6),
1340 * any kernel extension defining any Libkern C++ class
1341 * was eligible for automatic unloading,
1342 * but that unload did not call the module stop routine.
1343 * Non-I/O Kit kernel extensions that define Libkern C++ subclasses
1344 * should be sure to have OSBundleLibraries declarations that ensure
1345 * they will not load on releases prior to Snow Leopard.)
1347 static void considerUnloads();
1349 #if XNU_KERNEL_PRIVATE
1350 static bool removeClasses(OSCollection
* metaClasses
);
1351 #endif /* XNU_KERNEL_PRIVATE */
1354 * @function allocClassWithName
1357 * Allocates an instance of a named OSObject-derived class.
1359 * @param name The name of the desired class.
1362 * A pointer to the newly-allocated, uninitialized object on success;
1363 * <code>NULL</code> on failure.
1366 * Kernel extensions should not need to use this function
1367 * directly, instead using static instance-creation functions
1368 * defined by classes.
1370 * This function consults the run-time type information system
1371 * to find the metaclass for the named class.
1372 * If it exists, it calls the metaclass's <code>@link alloc alloc@/link</code>
1373 * function and returns the result.
1375 static OSObject
* allocClassWithName(const OSSymbol
* name
);
1379 * function allocClassWithName
1382 * Allocates an instance of a named OSObject-derived class.
1384 * @param name The name of the desired class.
1387 * A pointer to the newly-allocated, uninitialized object on success;
1388 * <code>NULL</code> on failure.
1391 * Kernel extensions should not need to use this function
1392 * directly, instead using static instance-creation functions
1393 * defined by classes.
1395 * This function consults the run-time type information system
1396 * to find the metaclass for the named class.
1397 * If it exists, it calls the metaclass's <code>@link alloc alloc@/link</code>
1398 * function and returns the result.
1400 static OSObject
* allocClassWithName(const OSString
* name
);
1404 * function allocClassWithName
1407 * Allocates an instance of a named OSObject-derived class.
1409 * @param name The name of the desired class.
1412 * A pointer to the newly-allocated, uninitialized object on success;
1413 * <code>NULL</code> on failure.
1416 * Kernel extensions should not need to use this function
1417 * directly, instead using static instance-creation functions
1418 * defined by classes.
1420 * This function consults the run-time type information system
1421 * to find the metaclass for the named class.
1422 * If it exists, it calls the metaclass's <code>@link alloc alloc@/link</code>
1423 * function and returns the result.
1425 static OSObject
* allocClassWithName(const char * name
);
1429 * @function checkMetaCastWithName
1432 * Search the metaclass inheritance hierarchy by name for an object instance.
1434 * @param className The name of the desired class or superclass.
1435 * @param object The object whose metaclass begins the search.
1438 * <code>object</code> if it's derived from <code>className</code>;
1439 * <code>NULL</code> otherwise.
1442 * This function is the basis of the Libkern run-time type-checking system.
1443 * Kernel extensions should not use it directly,
1444 * instead using <code>@link OSDynamicCast OSDynamicCast@/link</code> or
1445 * <code>@link OSCheckTypeInst OSCheckTypeInst@/link</code>.
1447 static OSMetaClassBase
* checkMetaCastWithName(
1448 const OSSymbol
* className
,
1449 const OSMetaClassBase
* object
);
1452 * @function checkMetaCastWithName
1455 * Search the metaclass inheritance hierarchy by name for an object instance.
1457 * @param className The name of the desired class or superclass.
1458 * @param object The object whose metaclass begins the search.
1461 * <code>object</code> if it's derived from <code>className</code>;
1462 * <code>NULL</code> otherwise.
1465 * Kernel extensions should not use this function directly,
1466 * instead using <code>@link OSDynamicCast OSDynamicCast@/link</code> or
1467 * <code>@link OSCheckTypeInst OSCheckTypeInst@/link</code>.
1469 static OSMetaClassBase
* checkMetaCastWithName(
1470 const OSString
* className
,
1471 const OSMetaClassBase
* object
);
1474 * @function checkMetaCastWithName
1477 * Search the metaclass inheritance hierarchy by name for an object instance.
1479 * @param className The name of the desired class or superclass.
1480 * @param object The object whose metaclass begins the search.
1483 * <code>object</code> if it's derived from <code>className</code>;
1484 * <code>NULL</code> otherwise.
1487 * Kernel extensions should not use this function directly,
1488 * instead using <code>@link OSDynamicCast OSDynamicCast@/link</code> or
1489 * <code>@link OSCheckTypeInst OSCheckTypeInst@/link</code>.
1491 static OSMetaClassBase
* checkMetaCastWithName(
1492 const char * className
,
1493 const OSMetaClassBase
* object
);
1497 * @function instanceConstructed
1500 * Counts the instances of the class managed by this metaclass.
1503 * <i>Not for use by kernel extensions.</i>
1505 * Every non-abstract class that inherits from OSObject
1506 * has a default constructor that calls it's own metaclass's
1507 * <code>instanceConstructed</code> function.
1508 * This constructor is defined by the
1510 * OSDefineMetaClassAndStructors
1511 * OSDefineMetaClassAndStructors@/link</code>
1512 * macro that all OSObject subclasses must use.
1514 * If a class's instance count goes from 0 to 1--that is,
1515 * upon the creation of the first instance of that class--the
1516 * superclass's instance count is also incremented.
1517 * This propagates reference counts up the inheritance chain so that
1518 * superclasses are counted as "in use" when subclasses have instances.
1520 void instanceConstructed() const;
1524 * @function instanceDestructed
1527 * Counts the instances of the class managed by this metaclass.
1530 * Every non-abstract class that inherits from OSObject
1531 * has a default destructor that calls it's own metaclass's
1532 * <code>instanceDestructed</code> function.
1533 * This constructor is defined by the
1534 * @link OSDefineMetaClassAndStructors OSDefineMetaClassAndStructors@/link
1535 * macro that all OSObject subclasses must use.
1537 * If a class's instance count goes from 1 to 0--that is,
1538 * upon the destruction of the last instance of that class--the
1539 * superclass's instance count is also decremented.
1540 * This reduces "in use" counts from superclasses when their subclasses
1541 * no longer have instances.
1543 void instanceDestructed() const;
1547 * @function checkMetaCast
1550 * Check whether a given object is an instance of the receiving
1551 * metaclass's class or one derived from it.
1553 * @param object The object to check for inheritance.
1556 * <code>object</code> if it is derived from the receiver's class,
1557 * <code>NULL</code> if not.
1559 OSMetaClassBase
* checkMetaCast(const OSMetaClassBase
* object
) const;
1563 * @function getInstanceCount
1566 * Returns the number of existing instances of the metaclass's class.
1569 * The number of existing instances of the metaclass's class,
1570 * plus 1 for each subclass with any instance.
1572 unsigned int getInstanceCount() const;
1576 * @function getSuperClass
1579 * Returns the super-metaclass of the receiver.
1582 * Returns a pointer to the super-metaclass of the receiving
1583 * OSMetaClass, or <code>NULL</code> for OSObject's metaclass.
1585 const OSMetaClass
* getSuperClass() const;
1588 * @function getKmodName
1591 * Returns the bundle identifier of the kernel extension
1592 * that defines this metaclass.
1595 * The bundle identifier of the kernel extension that defines this metaclass.
1598 * "Kmod" is an older term for kernel extension.
1600 const OSSymbol
* getKmodName() const;
1604 * @function getClassName
1607 * Returns the name of the C++ class managed by this metaclass.
1610 * Returns the name of the C++ class managed by this metaclass.
1612 const char * getClassName() const;
1613 const OSSymbol
* getClassNameSymbol() const;
1617 * @function getClassSize
1620 * Returns the allocation size of the C++ class managed by this metaclass.
1623 * The allocation size of the C++ class managed by this metaclass.
1625 unsigned int getClassSize() const;
1632 * Allocates an instance of the C++ class managed by this metaclass.
1635 * A pointer to the newly allocated, uninitialized instance,
1636 * with a retain count of 1; <code>NULL</code> on allocation failure.
1639 * This function is automatically created by the metaclass-registration macros
1640 * to enable dynamic instance allocation.
1642 virtual OSObject
* alloc() const = 0;
1644 #ifdef XNU_KERNEL_PRIVATE
1645 void addInstance(const OSObject
* instance
, bool super
= false) const;
1646 void removeInstance(const OSObject
* instance
, bool super
= false) const;
1647 void applyToInstances(OSMetaClassInstanceApplierFunction applier
,
1648 void * context
) const;
1649 static void applyToInstancesOfClassName(
1650 const OSSymbol
* name
,
1651 OSMetaClassInstanceApplierFunction applier
,
1654 static void applyToInstances(OSOrderedSet
* set
,
1655 OSMetaClassInstanceApplierFunction applier
,
1658 #endif /* XNU_KERNEL_PRIVATE */
1660 /* Not to be included in headerdoc.
1662 * @define OSDeclareCommonStructors
1666 * Helper macro for for the standard metaclass-registration macros.
1669 * @param className The name of the C++ class, as a raw token,
1670 * <i>not</i> a string or macro.
1672 #define OSDeclareCommonStructors(className) \
1674 static const OSMetaClass * const superClass; \
1676 static const OSMetaClass * const metaClass; \
1677 static class MetaClass : public OSMetaClass { \
1680 virtual OSObject *alloc() const; \
1682 friend class className ::MetaClass; \
1683 virtual const OSMetaClass * getMetaClass() const APPLE_KEXT_OVERRIDE; \
1685 className (const OSMetaClass *); \
1686 virtual ~ className () APPLE_KEXT_OVERRIDE
1690 * @define OSDeclareDefaultStructors
1694 * Declares run-time type information and functions
1695 * for a concrete Libkern C++ class.
1697 * @param className The name of the C++ class, as a raw token,
1698 * <i>not</i> a string or macro.
1701 * Concrete Libkern C++ classes should "call" this macro
1702 * immediately after the opening brace in a class declaration.
1703 * It leaves the current privacy state as <code>protected:</code>.
1705 #define OSDeclareDefaultStructors(className) \
1706 OSDeclareCommonStructors(className); \
1713 * @define OSDeclareAbstractStructors
1717 * Declares run-time type information and functions
1718 * for an abstract Libkern C++ class.
1720 * @param className The name of the C++ class, as a raw token,
1721 * <i>not</i> a string or macro.
1724 * Abstract Libkern C++ classes--those with at least one
1725 * pure virtual method--should "call" this macro
1726 * immediately after the opening brace in a class declaration.
1727 * It leaves the current privacy state as <code>protected:</code>.
1729 #define OSDeclareAbstractStructors(className) \
1730 OSDeclareCommonStructors(className); \
1732 className (); /* Make primary constructor private in abstract */ \
1736 * @define OSDeclareFinalStructors
1740 * Declares run-time type information and functions
1741 * for a final (non-subclassable) Libkern C++ class.
1743 * @param className The name of the C++ class, as a raw token,
1744 * <i>not</i> a string or macro.
1747 * Final Libkern C++ classes--those that do not allow subclassing--should
1748 * "call" this macro immediately after the opening brace in a class declaration.
1749 * (Final classes in the kernel may actually have subclasses in the kernel,
1750 * but kexts cannot define any subclasses of a final class.)
1751 * It leaves the current privacy state as <code>protected:</code>.
1753 * <b>Note:</b> If the class is exported by a pseudokext (symbol set),
1754 * the final symbol generated by this macro must be exported
1755 * for the final-class attribute to be enforced.
1757 * <b>Warning:</b> Changing a class from "Default" to "Final" will break
1758 * binary compatibility.
1760 #define OSDeclareFinalStructors(className) \
1761 OSDeclareDefaultStructors(className) \
1763 void __OSFinalClass(void); \
1767 /* Not to be included in headerdoc.
1769 * @define OSDefineMetaClassWithInit
1773 * Helper macro for for the standard metaclass-registration macros.
1776 * @param className The name of the C++ class, as a raw token,
1777 * <i>not</i> a string or macro.
1778 * @param superclassName The name of the superclass of the C++ class,
1780 * <i>not</i> a string or macro.
1781 * @param init A function to call in the constructor
1782 * of the class's OSMetaClass.
1784 #define OSDefineMetaClassWithInit(className, superclassName, init) \
1785 /* Class global data */ \
1786 className ::MetaClass className ::gMetaClass; \
1787 const OSMetaClass * const className ::metaClass = \
1788 & className ::gMetaClass; \
1789 const OSMetaClass * const className ::superClass = \
1790 & superclassName ::gMetaClass; \
1791 /* Class member functions */ \
1792 className :: className(const OSMetaClass *meta) \
1793 : superclassName (meta) { } \
1794 className ::~ className() { } \
1795 const OSMetaClass * className ::getMetaClass() const \
1796 { return &gMetaClass; } \
1797 /* The ::MetaClass constructor */ \
1798 className ::MetaClass::MetaClass() \
1799 : OSMetaClass(#className, className::superClass, sizeof(className)) \
1803 /* Not to be included in headerdoc.
1805 * @define OSDefineAbstractStructors
1809 * Helper macro for for the standard metaclass-registration macros.
1812 * @param className The name of the C++ class, as a raw token,
1813 * <i>not</i> a string or macro.
1814 * @param superclassName The name of the superclass of the C++ class,
1816 * <i>not</i> a string or macro.
1818 #define OSDefineAbstractStructors(className, superclassName) \
1819 OSObject * className ::MetaClass::alloc() const { return 0; }
1822 /* Not to be included in headerdoc.
1824 * @define OSDefineDefaultStructors
1828 * Helper macro for for the standard metaclass-registration macros.
1831 * @param className The name of the C++ class, as a raw token,
1832 * <i>not</i> a string or macro.
1833 * @param superclassName The name of the superclass of the C++ class,
1835 * <i>not</i> a string or macro.
1837 #define OSDefineDefaultStructors(className, superclassName) \
1838 OSObject * className ::MetaClass::alloc() const \
1839 { return new className; } \
1840 className :: className () : superclassName (&gMetaClass) \
1841 { gMetaClass.instanceConstructed(); }
1843 /* Not to be included in headerdoc.
1845 * @define OSDefineDefaultStructors
1849 * Helper macro for for the standard metaclass-registration macros.
1852 * @param className The name of the C++ class, as a raw token,
1853 * <i>not</i> a string or macro.
1854 * @param superclassName The name of the superclass of the C++ class,
1856 * <i>not</i> a string or macro.
1858 #define OSDefineFinalStructors(className, superclassName) \
1859 OSDefineDefaultStructors(className, superclassName) \
1860 void className ::__OSFinalClass(void) { }
1863 /* Not to be included in headerdoc.
1865 * @define OSDefineMetaClassAndStructorsWithInit
1869 * Helper macro for for the standard metaclass-registration macros.
1872 * @param className The name of the C++ class, as a raw token,
1873 * <i>not</i> a string or macro.
1874 * @param superclassName The name of the superclass of the C++ class,
1876 * <i>not</i> a string or macro.
1877 * @param init A function to call in the constructor
1878 * of the class's OSMetaClass.
1880 #define OSDefineMetaClassAndStructorsWithInit(className, superclassName, init) \
1881 OSDefineMetaClassWithInit(className, superclassName, init) \
1882 OSDefineDefaultStructors(className, superclassName)
1885 /* Not to be included in headerdoc.
1887 * @define OSDefineMetaClassAndAbstractStructorsWithInit
1891 * Helper macro for for the standard metaclass-registration macros.
1894 * @param className The name of the C++ class, as a raw token,
1895 * <i>not</i> a string or macro.
1896 * @param superclassName The name of the superclass of the C++ class,
1898 * <i>not</i> a string or macro.
1899 * @param init A function to call in the constructor
1900 * of the class's OSMetaClass.
1902 #define OSDefineMetaClassAndAbstractStructorsWithInit(className, superclassName, init) \
1903 OSDefineMetaClassWithInit(className, superclassName, init) \
1904 OSDefineAbstractStructors(className, superclassName)
1907 /* Not to be included in headerdoc.
1909 * @define OSDefineMetaClassAndFinalStructorsWithInit
1913 * Helper macro for for the standard metaclass-registration macros.
1916 * @param className The name of the C++ class, as a raw token,
1917 * <i>not</i> a string or macro.
1918 * @param superclassName The name of the superclass of the C++ class,
1920 * <i>not</i> a string or macro.
1921 * @param init A function to call in the constructor
1922 * of the class's OSMetaClass.
1924 #define OSDefineMetaClassAndFinalStructorsWithInit(className, superclassName, init) \
1925 OSDefineMetaClassWithInit(className, superclassName, init) \
1926 OSDefineFinalStructors(className, superclassName)
1931 /* Not to be included in headerdoc.
1933 * @define OSDefineMetaClass
1937 * Helper macro for for the standard metaclass-registration macros.
1940 * @param className The name of the C++ class, as a raw token,
1941 * <i>not</i> a string or macro.
1942 * @param superclassName The name of the superclass of the C++ class,
1944 * <i>not</i> a string or macro.
1945 * @param init A function to call in the constructor
1946 * of the class's OSMetaClass.
1948 #define OSDefineMetaClass(className, superclassName) \
1949 OSDefineMetaClassWithInit(className, superclassName, )
1953 * @define OSDefineMetaClassAndStructors
1957 * Defines an OSMetaClass and associated routines
1958 * for a concrete Libkern C++ class.
1960 * @param className The name of the C++ class, as a raw token,
1961 * <i>not</i> a string or macro.
1962 * @param superclassName The name of the superclass of the C++ class,
1964 * <i>not</i> a string or macro.
1967 * Concrete Libkern C++ classes should "call" this macro
1968 * at the beginning of their implementation files,
1969 * before any function implementations for the class.
1971 #define OSDefineMetaClassAndStructors(className, superclassName) \
1972 OSDefineMetaClassAndStructorsWithInit(className, superclassName, )
1976 * @define OSDefineMetaClassAndAbstractStructors
1980 * Defines an OSMetaClass and associated routines
1981 * for an abstract Libkern C++ class.
1983 * @param className The name of the C++ class, as a raw token,
1984 * <i>not</i> a string or macro.
1985 * @param superclassName The name of the superclass of the C++ class,
1987 * <i>not</i> a string or macro.
1990 * Abstract Libkern C++ classes--those with at least one
1991 * pure virtual method--should "call" this macro
1992 * at the beginning of their implementation files,
1993 * before any function implementations for the class.
1995 #define OSDefineMetaClassAndAbstractStructors(className, superclassName) \
1996 OSDefineMetaClassAndAbstractStructorsWithInit (className, superclassName, )
2000 * @define OSDefineMetaClassAndFinalStructors
2004 * Defines an OSMetaClass and associated routines
2005 * for a final (non-subclassable) Libkern C++ class.
2007 * @param className The name of the C++ class, as a raw token,
2008 * <i>not</i> a string or macro.
2009 * @param superclassName The name of the superclass of the C++ class,
2011 * <i>not</i> a string or macro.
2014 * Final Libkern C++ classes--those that do not allow
2015 * subclassing--should "call" this macro at the beginning
2016 * of their implementation files,
2017 * before any function implementations for the class.
2018 * (Final classes in the kernel may actually have subclasses in the kernel,
2019 * but kexts cannot define any subclasses of a final class.)
2021 * <b>Note:</b> If the class is exported by a pseudokext (symbol set),
2022 * the final symbol generated by this macro must be exported
2023 * for the final-class attribute to be enforced.
2025 * <b>Warning:</b> Changing a class from "Default" to "Final" will break
2026 * binary compatibility.
2028 #define OSDefineMetaClassAndFinalStructors(className, superclassName) \
2029 OSDefineMetaClassAndFinalStructorsWithInit(className, superclassName, )
2032 // Dynamic vtable patchup support routines and types
2033 void reservedCalled(int ind
) const;
2037 * @define OSMetaClassDeclareReservedUnused
2041 * Reserves vtable space for new virtual functions
2042 * in a Libkern C++ class.
2044 * @param className The name of the C++ class, as a raw token,
2045 * <i>not</i> a string or macro.
2046 * @param index The numeric index of the vtable slot,
2047 * as a raw constant, beginning from 0.
2050 * Libkern C++ classes in kernel extensions that can be used as libraries
2051 * can provide for backward compatibility by declaring a number
2052 * of reserved vtable slots
2053 * that can be replaced with new functions as they are added.
2054 * Each reserved declaration must be accompanied in the implementation
2055 * by a corresponding reference to
2056 * <code>@link OSMetaClassDefineReservedUnused
2057 * OSMetaClassDefineReservedUnused@/link</code>.
2059 * When replacing a reserved slot, change the macro from "Unused"
2060 * to "Used" to document the fact that the slot used to be reserved,
2061 * and declare the new function immediately after the "Used" macro
2062 * to preserve vtable ordering.
2064 * <code>@link OSMetaClassDeclareReservedUsed
2065 * OSMetaClassDeclareReservedUsed@/link</code>.
2067 #if APPLE_KEXT_VTABLE_PADDING
2068 #define OSMetaClassDeclareReservedUnused(className, index) \
2070 virtual void _RESERVED ## className ## index ()
2072 #define OSMetaClassDeclareReservedUnused(className, index)
2077 * @define OSMetaClassDeclareReservedUsed
2081 * Documents use of reserved vtable space for new virtual functions
2082 * in a Libkern C++ class.
2084 * @param className The name of the C++ class, as a raw token,
2085 * <i>not</i> a string or macro.
2086 * @param index The numeric index of the vtable slot,
2087 * as a raw constant, beginning from 0.
2090 * This macro evaluates to nothing, and is used to document reserved
2091 * vtable slots as they are filled.
2093 * <code>@link OSMetaClassDeclareReservedUnused
2094 * OSMetaClassDeclareReservedUnused@/link</code>.
2096 #define OSMetaClassDeclareReservedUsed(className, index)
2100 * @define OSMetaClassDefineReservedUnused
2104 * Defines a reserved vtable slot for a Libkern C++ class.
2106 * @param className The name of the C++ class, as a raw token,
2107 * <i>not</i> a string or macro.
2108 * @param index The numeric index of the vtable slot,
2109 * as a raw constant, beginning from 0.
2112 * Libkern C++ classes in kernel extensions that can be used as libraries
2113 * can provide for backward compatibility by declaring a number
2114 * of reserved vtable slots
2115 * that can be replaced with new functions as they are added.
2116 * Each reserved defintion accompanies
2117 * a corresponding declaration created with
2118 * <code>@link OSMetaClassDeclareReservedUnused
2119 * OSMetaClassDeclareReservedUnused@/link</code>.
2121 * This macro is used in the implementation file
2122 * to provide a placeholder definition for the reserved vtable slot,
2123 * as a function that calls <code>panic</code> with an error message.
2125 * When replacing a reserved slot, change the macro from "Unused"
2126 * to "Used" to document the fact that the slot used to be reserved,
2127 * and declare the new function immediately after the "Used" macro
2128 * to preserve vtable ordering.
2130 * <code>@link OSMetaClassDefineReservedUsed
2131 * OSMetaClassDefineReservedUsed@/link</code>.
2133 #if APPLE_KEXT_VTABLE_PADDING
2134 #define OSMetaClassDefineReservedUnused(className, index) \
2135 void className ::_RESERVED ## className ## index () \
2136 { gMetaClass.reservedCalled(index); }
2138 #define OSMetaClassDefineReservedUnused(className, index)
2143 * @define OSMetaClassDefineReservedUsed
2147 * Reserves vtable space for new virtual functions in a Libkern C++ class.
2149 * @param className The name of the C++ class, as a raw token,
2150 * <i>not</i> a string or macro.
2151 * @param index The numeric index of the vtable slot,
2152 * as a raw constant, beginning from 0.
2155 * This macro evaluates to nothing, and is used to document reserved
2156 * vtable slots as they are filled.
2158 * <code>@link OSMetaClassDefineReservedUnused
2159 * OSMetaClassDefineReservedUnused@/link</code>.
2161 #define OSMetaClassDefineReservedUsed(className, index)
2163 // I/O Kit debug internal routines.
2164 static void printInstanceCounts();
2165 static void serializeClassDictionary(OSDictionary
* dict
);
2166 #ifdef XNU_KERNEL_PRIVATE
2169 static void * trackedNew(size_t size
);
2170 static void trackedDelete(void * mem
, size_t size
);
2171 void trackedInstance(OSObject
* instance
) const;
2172 void trackedFree(OSObject
* instance
) const;
2173 void trackedAccumSize(OSObject
* instance
, size_t size
) const;
2174 struct IOTrackingQueue
* getTracking() const;
2175 #endif /* IOTRACKING */
2176 #endif /* XNU_KERNEL_PRIVATE */
2180 static OSDictionary
* getClassDictionary();
2181 virtual bool serialize(OSSerialize
* serializer
) const;
2183 // Virtual Padding functions for MetaClass's
2184 OSMetaClassDeclareReservedUnused(OSMetaClass
, 0);
2185 OSMetaClassDeclareReservedUnused(OSMetaClass
, 1);
2186 OSMetaClassDeclareReservedUnused(OSMetaClass
, 2);
2187 OSMetaClassDeclareReservedUnused(OSMetaClass
, 3);
2188 OSMetaClassDeclareReservedUnused(OSMetaClass
, 4);
2189 OSMetaClassDeclareReservedUnused(OSMetaClass
, 5);
2190 OSMetaClassDeclareReservedUnused(OSMetaClass
, 6);
2191 OSMetaClassDeclareReservedUnused(OSMetaClass
, 7);
2194 #endif /* !_LIBKERN_OSMETACLASS_H */