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>
37 * LIBKERN_ macros below can be used to describe the ownership semantics
38 * of functions handling subclasses of OSObject.
39 * The attributes propagate with inheritance, but can be overriden.
40 * New versions of the Clang Static Analyzer can use this knowledge to
41 * check the code for leaks or uses-after-free.
45 * By default, methods returning OSObjects are assumed to have the following
47 * - Methods which start with "get" are "Get" and which are not returning
48 * a subclass of OSIterator are assumed to be getters.
49 * They return at "+0" and the caller is not responsible for releasing the
52 * - All other methods are assumed to return at "+1", and the caller is
53 * responsible for releasing the returned object.
55 * The semantics implied by the naming convention described above can be
56 * overriden using either LIBKERN_RETURNS_RETAINED or LIBKERN_RETURNS_NOT_RETAINED
57 * attribute applied to a function.
58 * In the former case, it stipulates that the function is returning at "+1",
59 * and in the latter case "+0".
61 #if __has_attribute(os_returns_retained)
62 #define LIBKERN_RETURNS_RETAINED __attribute__((os_returns_retained))
64 #define LIBKERN_RETURNS_RETAINED
66 #if __has_attribute(os_returns_not_retained)
67 #define LIBKERN_RETURNS_NOT_RETAINED __attribute__((os_returns_not_retained))
69 #define LIBKERN_RETURNS_NOT_RETAINED
73 * LIBKERN_CONSUMED attribute can be applied to parameters.
74 * It specifies that this function call would consume the reference to the
75 * annotated parameter.
77 #if __has_attribute(os_consumed)
78 #define LIBKERN_CONSUMED __attribute__((os_consumed))
80 #define LIBKERN_CONSUMED
84 * LIBKERN_CONSUMES_THIS attribute can be applied to methods.
85 * It specifies that this method call consumes a reference to "this" (e.g.
86 * by storing a reference to "this" in a passed parameter).
88 #if __has_attribute(os_consumes_this)
89 #define LIBKERN_CONSUMES_THIS __attribute__((os_consumes_this))
91 #define LIBKERN_CONSUMES_THIS
100 #ifdef XNU_KERNEL_PRIVATE
103 #endif /* XNU_KERNEL_PRIVATE */
110 * This header declares the OSMetaClassBase and OSMetaClass classes,
111 * which together form the basis of the Libkern and I/O Kit C++ class hierarchy
112 * and run-time type information facility.
117 #define APPLE_KEXT_COMPATIBILITY
119 #ifdef XNU_KERNEL_PRIVATE
121 #ifdef CONFIG_EMBEDDED
122 #define APPLE_KEXT_VTABLE_PADDING 0
123 #else /* CONFIG_EMBEDDED */
125 #define APPLE_KEXT_VTABLE_PADDING 1
126 #endif /* CONFIG_EMBEDDED */
128 #else /* XNU_KERNEL_PRIVATE */
129 #include <TargetConditionals.h>
131 #if TARGET_OS_EMBEDDED
132 #define APPLE_KEXT_VTABLE_PADDING 0
133 #else /* TARGET_OS_EMBEDDED */
135 #define APPLE_KEXT_VTABLE_PADDING 1
136 #endif /* TARGET_OS_EMBEDDED */
138 #endif /* XNU_KERNEL_PRIVATE */
140 #define APPLE_KEXT_ALIGN_CONTAINERS (0 == APPLE_KEXT_VTABLE_PADDING)
142 #if defined(__LP64__)
144 #define APPLE_KEXT_LEGACY_ABI 0
145 #elif defined(__arm__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2))
146 #define APPLE_KEXT_LEGACY_ABI 0
148 #define APPLE_KEXT_LEGACY_ABI 1
151 #if defined(__LP64__)
153 #define APPLE_KEXT_COMPATIBILITY_VIRTUAL
155 // private method made virtual only for binary compatibility
156 #define APPLE_KEXT_COMPATIBILITY_VIRTUAL virtual
160 #define APPLE_KEXT_DEPRECATED __attribute__((deprecated))
163 #if __cplusplus >= 201103L
164 #define APPLE_KEXT_OVERRIDE override
165 #if defined(__LP64__)
166 #define APPLE_KEXT_COMPATIBILITY_OVERRIDE
168 #define APPLE_KEXT_COMPATIBILITY_OVERRIDE APPLE_KEXT_OVERRIDE
171 #define APPLE_KEXT_OVERRIDE
172 #define APPLE_KEXT_COMPATIBILITY_OVERRIDE
175 #define APPLE_KEXT_WSHADOW_PUSH _Pragma("clang diagnostic push"); \
176 _Pragma("clang diagnostic ignored \"-Wunknown-warning-option\"") \
177 _Pragma("clang diagnostic ignored \"-Wshadow-field\"")
179 #define APPLE_KEXT_WSHADOW_POP _Pragma("clang diagnostic pop")
183 * @class OSMetaClassBase
186 * OSMetaClassBase is the abstract bootstrap class
187 * for the Libkern and I/O Kit run-time type information system.
190 * OSMetaClassBase is the abstract C++ root class
191 * underlying the entire Libkern and I/O Kit class hierarchy.
192 * It defines the run-time type information system,
193 * including dynamic class allocation and safe type-casting,
194 * as well as the abstract interface for reference counting
195 * and a few other utility functions.
196 * OSMetaClassBase is the immediate superclass of
197 * @link //apple_ref/doc/class/OSObject OSObject@/link and
198 * @link //apple_ref/doc/class/OSMetaClass OSMetaClass@/link;
199 * no other class should derive from OSMetaClassBase.
201 * For more information, see
202 * <i>@link //apple_ref/doc/uid/TP40002799
203 * I/O Kit Device Driver Design Guidelines@/link</i>.
205 * <b>Use by Kernel Extensions</b>
207 * Kernel Extensions should never interact directly with OSMetaClassBase,
208 * but they will find useful several macros that tie in
209 * to the run-time type information system, specifically:
211 * <li><code>@link OSTypeAlloc OSTypeAlloc@/link</code> - allocation of new instances</li>
212 * <li><code>@link OSDynamicCast OSDynamicCast@/link</code> - safe type casting</li>
213 * <li><code>@link OSCheckTypeInst OSCheckTypeInst@/link</code> -
214 * checking for inheritance/derivation</li>
215 * <li><code>@link OSMemberFunctionCast OSMemberFunctionCast@/link</code> -
216 * casting C++ member functions to C function pointers
217 * for registration as callbacks</li>
220 * See @link //apple_ref/doc/class/OSMetaClass OSMetaClass@/link
221 * for more run-time type information interfaces.
223 * <b>Use Restrictions</b>
225 * OSMetaClassBase should not be subclassed by kernel extensions,
226 * nor should kernel extensions call its run-time type functions directly.
228 * The run-time type functions and macros are <b>not safe</b>
229 * to call in a primary interrupt context.
231 * <b>Concurrency Protection</b>
233 * The run-time type macros and functions of OSMetaClassBase are thread-safe.
235 class OSMetaClassBase
241 * @define OSTypeAlloc
245 * Allocates an instance of the named object class.
247 * @param type The name of the desired class to be created,
248 * as a raw token, <i>not</i> a string or macro.
251 * A pointer to the new, uninitialized object on success;
252 * <code>NULL</code> on failure.
257 * //apple_ref/cpp/clm/OSMetaClass/allocClassWithName/staticOSObject*\/(constchar*)
258 * OSMetaClass::allocClassWithName(const char *)@/link</code>
261 * //apple_ref/cpp/instm/OSMetaClass/alloc/virtualOSObject*\/()
262 * OSMetaClass::alloc@/link</code>.
264 * The OSTypeAlloc macro is used to avoid binary compatibility difficulties
265 * presented by the C++ <code>new</code> operator.
267 #define OSTypeAlloc(type) ((type *) ((type::metaClass)->alloc()))
275 * Returns the type ID (metaclass) of a class based on its name.
277 * @param type The name of the desired class, as a raw token,
278 * <i>not</i> a string or macro.
281 * The unique type ID (metaclass) for the class.
284 * It is typically more useful to determine whether a class is derived
286 * <code>@link //apple_ref/cpp/macro/OSDynamicCast OSDynamicCast@/link</code>
288 * <code>@link //apple_ref/cpp/macro/OSCheckTypeInst OSCheckTypeInst@/link</code>.
290 #define OSTypeID(type) (type::metaClass)
294 * @define OSTypeIDInst
298 * Returns the type ID (metaclass) for the class of an object instance.
300 * @param typeinst An instance of an OSObject subclass.
303 * The type ID of that object's class; that is, its metaclass.
306 * It is typically more useful to determine whether an object is derived
307 * from a particular class; see
308 * <code>@link //apple_ref/cpp/macro/OSDynamicCast OSDynamicCast@/link</code>
310 * <code>@link //apple_ref/cpp/macro/OSCheckTypeInst OSCheckTypeInst@/link</code>.
312 #define OSTypeIDInst(typeinst) ((typeinst)->getMetaClass())
316 * @define OSDynamicCast
320 * Safe type-casting for Libkern C++ objects.
322 * @param type The name of the desired class type, as a raw token,
323 * <i>not</i> a string or macro.
324 * It is assumed you intend to cast to a pointer
325 * to an object of this type.
326 * Type qualifiers, such as <code>const</code>,
327 * are not recognized and will cause
328 * a (usually obscure) compile error.
329 * @param inst A pointer to the object instance to be cast.
330 * May be <code>NULL</code>.
333 * <code>inst</code> if it is non-<code>NULL</code>
334 * and derived from <code>type</code>;
335 * otherwise <code>NULL</code>.
338 * <code>OSDynamicCast</code> is a rough equivalent
339 * to the standard C++ RTTI <code>dynamic_cast<T></code> operator.
340 * Your code should use this instead of raw C type-casting,
341 * and check the resulting value.
342 * If the result is non-<code>NULL</code>,
343 * the object is safe to use as the type-cast class;
344 * if the result is <code>NULL</code>,
345 * the object does not derive from the type-cast class
346 * and your code should take appropriate steps to handle the error.
348 #define OSDynamicCast(type, inst) \
349 ((type *) OSMetaClassBase::safeMetaCast((inst), OSTypeID(type)))
353 * @define OSCheckTypeInst
357 * Checks whether two objects are type-compatible.
359 * @param typeinst The reference object.
360 * @param inst The object to check for type compatibility.
363 * <code>true</code> if both <code>inst</code> and
364 * <code>typeinst</code> are non-<code>NULL</code>
365 * and <code>inst</code> is derived from the class of <code>typeinst</code>;
366 * otherwise <code>false</code>.
368 #define OSCheckTypeInst(typeinst, inst) \
369 OSMetaClassBase::checkTypeInst(inst, typeinst)
371 #define OSSafeRelease(inst) \
372 do { int OSSafeRelease __attribute__ ((deprecated("Use OSSafeReleaseNULL"))); (OSSafeRelease); \
373 if (inst) (inst)->release(); } while (0)
375 /*! @function OSSafeReleaseNULL
376 * @abstract Release an object if not <code>NULL</code>, then set it to <code>NULL</code>.
377 * @param inst Instance of an OSObject, may be <code>NULL</code>.
379 #define OSSafeReleaseNULL(inst) do { if (inst != NULL) (inst)->release(); (inst) = NULL; } while (0)
381 typedef void (*_ptf_t
)(void);
383 #if defined(__arm__) || defined(__arm64__)
385 static _ptf_t
_ptmf2ptf(const OSMetaClassBase
* self
, void (OSMetaClassBase::*func
)(void));
387 #elif defined(__i386__) || defined(__x86_64__)
389 // Slightly less arcane and slightly less evil code to do
390 // the same for kexts compiled with the standard Itanium C++
394 _ptmf2ptf(const OSMetaClassBase
*self
, void (OSMetaClassBase::*func
)(void))
397 void (OSMetaClassBase::*fIn
)(void);
404 if (map
.fVTOffset
& 1) {
407 const OSMetaClassBase
*fObj
;
412 // Virtual member function so dereference vtable
413 return *(_ptf_t
*)(((uintptr_t)*u
.vtablep
) + map
.fVTOffset
- 1);
415 // Not virtual, i.e. plain member func
421 #error Unknown architecture.
426 * @define OSMemberFunctionCast
430 * Converts a C++ member function pointer, relative to an instance,
431 * to a C-style pointer to function.
433 * @param cptrtype The function type declaration to cast to
434 * (typically provided as a <code>typedef</code> by I/O KitKit classes).
435 * @param self The <code>this</code> pointer of the object whose function
437 * @param func The pointer to the member function itself,
438 * something like <code>&Class::function</code>.
441 * A pointer to a function of the given type referencing <code>self</code>.
444 * This function is used to generate pointers to C++ functions for instances,
445 * such that they can be registered as callbacks with I/O Kit objects.
447 * No warnings are generated.
449 * This function will panic if an attempt is made to call it
450 * with a multiply-inheriting class.
452 #define OSMemberFunctionCast(cptrtype, self, func) \
453 (cptrtype) OSMetaClassBase:: \
454 _ptmf2ptf(self, (void (OSMetaClassBase::*)(void)) func)
462 // Disable copy constructors of OSMetaClassBase based objects
463 /* Not to be included in headerdoc.
465 * @function operator =
468 * Disable implicit copy constructor by making private
470 * @param src Reference to source object that isn't allowed to be copied.
472 void operator =(OSMetaClassBase
&src
);
474 /* Not to be included in headerdoc.
476 * @function OSMetaClassBase
479 * Disable implicit copy constructor by making private
481 * @param src Reference to source object that isn't allowed to be copied.
483 OSMetaClassBase(OSMetaClassBase
&src
);
487 // xx-review: the original comment for this makes it sound to me like we don't
488 // xx-review: catch over-releasing an object...?
494 * Abstract declaration of
496 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/(int)
497 * release(int freeWhen)@/link</code>.
502 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/(int)
503 * release(int freeWhen)@/link</code>.
505 virtual void release(int freeWhen
) const = 0;
509 * @function getRetainCount
512 * Abstract declaration of
514 * //apple_ref/cpp/instm/OSObject/getRetainCount/virtualint/()
515 * getRetainCount()@/link</code>.
520 * //apple_ref/cpp/instm/OSObject/getRetainCount/virtualint/()
521 * OSObject::getRetainCount()@/link</code>.
523 virtual int getRetainCount() const = 0;
530 * Abstract declaration of
532 * //apple_ref/cpp/instm/OSObject/retain/virtualvoid/()
533 * retain()@/link</code>.
538 * //apple_ref/cpp/instm/OSObject/retain/virtualvoid/()
539 * OSObject::retain()@/link</code>.
541 virtual void retain() const = 0;
548 * Abstract declaration of
550 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/()
551 * release@/link</code>.
556 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/()
557 * OSObject::release@/link</code>.
559 virtual void release() const = 0;
563 * @function serialize
566 * Abstract declaration of
568 * //apple_ref/cpp/instm/OSObject/serialize/virtualbool/(OSSerialize*)
569 * serialize@/link</code>.
574 * //apple_ref/cpp/instm/OSObject/serialize/virtualbool/(OSSerialize*)
575 * OSObject::serialize@/link</code>.
577 virtual bool serialize(OSSerialize
* serializer
) const = 0;
581 * @function getMetaClass
584 * Returns the OSMetaClass representing
585 * an OSMetaClassBase subclass.
588 * OSObject overrides this abstract member function
589 * to return the OSMetaClass object that represents
590 * each class for run-time typing.
592 virtual const OSMetaClass
* getMetaClass() const = 0;
596 * @function isEqualTo
599 * Checks whether another object is equal to the receiver.
601 * @param anObject The object to copmare to the receiver.
604 * <code>true</code> if the objects are equal, <code>false</code> otherwise.
607 * OSMetaClassBase implements this as a direct pointer comparison,
608 * since it has no other information to judge equality by.
609 * Subclasses generally override this function
610 * to do a more meaningful comparison.
611 * For example, OSString implements it to return
612 * <code>true</code> if <code>anObject</code>
613 * is derived from OSString and represents the same C string.
615 virtual bool isEqualTo(const OSMetaClassBase
* anObject
) const;
622 * Casts this object is to the class managed by the given OSMetaClass.
624 * @param toMeta A pointer to a constant OSMetaClass
625 * for the desired target type.
628 * <code>this</code> if the object is derived
629 * from the class managed by <code>toMeta</code>,
630 * otherwise <code>NULL</code>.
633 * It is far more convenient to use
634 * <code>@link OSDynamicCast OSDynamicCast@/link</code>.
636 OSMetaClassBase
* metaCast(const OSMetaClass
* toMeta
) const;
643 * Casts this object is to the class managed by the named OSMetaClass.
645 * @param toMeta An OSSymbol naming the desired target type.
648 * <code>this</code> if the object is derived
649 * from the class named by <code>toMeta</code>,
650 * otherwise <code>NULL</code>.
653 * It is far more convenient to use
654 * <code>@link OSDynamicCast OSDynamicCast@/link</code>.
656 OSMetaClassBase
* metaCast(const OSSymbol
* toMeta
) const;
663 * Casts this object is to the class managed by the named OSMetaClass.
665 * @param toMeta An OSString naming the desired target type.
667 * <code>this</code> if the object is derived
668 * from the class named by <code>toMeta</code>,
669 * otherwise <code>NULL</code>.
672 * It is far more convenient to use
673 * <code>@link OSDynamicCast OSDynamicCast@/link</code>.
675 OSMetaClassBase
* metaCast(const OSString
* toMeta
) const;
682 * Casts this object is to the class managed by the named OSMetaClass.
684 * @param toMeta A C string naming the desired target type.
686 * <code>this</code> if the object is derived
687 * from the class named by <code>toMeta</code>,
688 * otherwise <code>NULL</code>.
691 * It is far more convenient to use
692 * <code>@link OSDynamicCast OSDynamicCast@/link</code>.
694 OSMetaClassBase
* metaCast(const char * toMeta
) const;
696 // Helper inlines for run-time type preprocessor macros
698 * @function safeMetaCast
701 * Casts an object is to the class managed by the given OSMetaClass.
703 * @param anObject A pointer to the object to be cast.
704 * @param toMeta A pointer to a constant OSMetaClass
705 * for the desired target type.
708 * <code>anObject</code> if the object is derived
709 * from the class managed by <code>toMeta</code>,
710 * otherwise <code>NULL</code>.
713 * It is far more convenient to use
714 * <code>@link OSDynamicCast OSDynamicCast@/link</code>.
716 static OSMetaClassBase
* safeMetaCast(
717 const OSMetaClassBase
* anObject
,
718 const OSMetaClass
* toMeta
);
721 * @function checkTypeInst
724 * Checks whether an object instance is of the same class
725 * as another object instance (or a subclass of that class).
727 * @param inst A pointer to the object to check.
728 * @param typeinst A pointer to an object of the class being checked.
731 * <code>true</code> if the object is derived
732 * from the class of <code>typeinst</code>
733 * or a subclass of that class,
734 * otherwise <code>false</code>.
737 * It is far more convenient to use
738 * <code>@link OSCheckTypeInst OSCheckTypeInst@/link</code>.
740 static bool checkTypeInst(
741 const OSMetaClassBase
* inst
,
742 const OSMetaClassBase
* typeinst
);
744 static void initialize(void);
749 * @function taggedRetain
752 * Abstract declaration of
754 * //apple_ref/cpp/instm/OSObject/taggedRetain/virtualvoid/(constvoid*)
755 * taggedRetain(const void *)@/link</code>.
760 * //apple_ref/cpp/instm/OSObject/taggedRetain/virtualvoid/(constvoid*)
761 * OSObject::taggedRetain(const void *)@/link</code>.
763 // WAS: virtual void _RESERVEDOSMetaClassBase0();
764 virtual void taggedRetain(const void * tag
= 0) const = 0;
768 * @function taggedRelease
771 * Abstract declaration of
773 * //apple_ref/cpp/instm/OSObject/taggedRelease/virtualvoid/(constvoid*)
774 * taggedRelease(const void *)@/link</code>.
779 * //apple_ref/cpp/instm/OSObject/taggedRelease/virtualvoid/(constvoid*)
780 * OSObject::taggedRelease(const void *)@/link</code>.
782 // WAS: virtual void _RESERVEDOSMetaClassBase1();
783 virtual void taggedRelease(const void * tag
= 0) const = 0;
787 * @function taggedRelease
790 * Abstract declaration of
792 * //apple_ref/cpp/instm/OSObject/taggedRelease/virtualvoid/(constvoid*,constint)
793 * taggedRelease(const void *, const int freeWhen)@/link</code>.
798 * //apple_ref/cpp/instm/OSObject/taggedRelease/virtualvoid/(constvoid*,constint)
799 * OSObject::taggedRelease(const void *, const int freeWhen)@/link</code>.
801 // WAS: virtual void _RESERVEDOSMetaClassBase2();
802 virtual void taggedRelease(
804 const int freeWhen
) const = 0;
807 #if APPLE_KEXT_VTABLE_PADDING
809 virtual void _RESERVEDOSMetaClassBase3();
810 virtual void _RESERVEDOSMetaClassBase4();
811 virtual void _RESERVEDOSMetaClassBase5();
812 virtual void _RESERVEDOSMetaClassBase6();
813 virtual void _RESERVEDOSMetaClassBase7();
815 } APPLE_KEXT_COMPATIBILITY
;
818 #ifdef XNU_KERNEL_PRIVATE
819 typedef bool (*OSMetaClassInstanceApplierFunction
)(const OSObject
* instance
,
821 #endif /* XNU_KERNEL_PRIVATE */
827 * OSMetaClass manages run-time type information
828 * for Libkern and I/O Kit C++ classes.
830 * @discussion OSMetaClass manages run-time type information
831 * for Libkern and I/O Kit C++ classes.
832 * An instance of OSMetaClass exists for (nearly) every such C++ class,
833 * keeping track of inheritance relationships, class lookup by name,
834 * instance counts, and more.
835 * OSMetaClass operates almost entirely behind the scenes,
836 * and kernel extensions should rarely, if ever,
837 * have to interact directly with OSMetaClass.
839 * <b>Use by Kernel Extensions</b>
841 * While kernel extensions rarey interact directly with OSMetaClass at run time,
842 * they must register their classes with the metaclass system
843 * using the macros declared here.
844 * The class declaration should use one of these two macros
845 * before its first member function declaration:
847 * <li><code>@link OSDeclareDefaultStructors OSDeclareDefaultStructors@/link</code> -
848 * for classes with no abstract member function declarations</li>
849 * <li><code>@link OSDeclareAbstractStructors OSDeclareAbstractStructors@/link</code> -
850 * for classes with at least one abstract member function declaration</li>
851 * <li><code>@link OSDeclareFinalStructors OSDeclareFinalStructors@/link</code> -
852 * for classes that should not be subclassable by another kext</li>
855 * The class implementation should then use one of these macros:
857 * <li><code>@link OSDefineMetaClassAndStructors
858 * OSDefineMetaClassAndStructors@/link</code> -
859 * for classes with no abstract member function declarations</li>
860 * <li><code>@link OSDefineMetaClassAndAbstractStructors
861 * OSDefineMetaClassAndAbstractStructors@/link</code> -
862 * for classes with at least one abstract member function declaration</li>
863 * <li><code>@link OSDefineMetaClassAndFinalStructors
864 * OSDefineMetaClassAndFinalStructors@/link</code> -
865 * for classes that should not be subclassable by another kext</li>
868 * Classes in kernel extensions that are intended for use as libraries
869 * may need to reserve vtable slots to preserve binary compatibility
870 * as new functions are added. They may do so with these macros:
872 * <li><code>@link OSMetaClassDeclareReservedUnused
873 * OSMetaClassDeclareReservedUnused@/link</code> -
874 * reserves a vtable slot</li>
875 * <li><code>@link OSMetaClassDefineReservedUnused
876 * OSMetaClassDefineReservedUnused@/link</code> -
877 * defines the reserved vtable slot as an unimplemented function</li>
878 * <li><code>@link OSMetaClassDeclareReservedUsed
879 * OSMetaClassDeclareReservedUsed@/link</code> -
880 * documents that a formerly reserved slot is now used</li>
881 * <li><code>@link OSMetaClassDefineReservedUsed
882 * OSMetaClassDefineReservedUsed@/link</code> -
883 * documents that a formerly reserved slot is now used</li>
886 * <b>Use Restrictions</b>
888 * OSMetaClass should not be explicitly subclassed by kernel extensions
889 * (the declare/define macros do that),
890 * nor should kernel extensions call its run-time type functions directly.
892 * OSMetaClass functions should be considered
893 * <b>unsafe</b> to call in a primary interrupt context.
895 * <b>Concurrency Protection</b>
897 * Kernel extensions should in general not interact
898 * with OSMetaClass objects directly,
899 * instead using the run-time type macros.
900 * Much of OSMetaClass's interface is intended for use
901 * by the run-time type information system,
902 * which handles concurrency and locking internally.
904 class OSMetaClass
: private OSMetaClassBase
908 friend class IOStatistics
;
912 // Can never be allocated must be created at compile time
913 static void * operator new(size_t size
);
915 /* Reserved for future use. (Internal use only) */
916 struct ExpansionData
*reserved
;
918 /* superClass Handle to the superclass's meta class. */
919 const OSMetaClass
*superClassLink
;
921 /* className OSSymbol of the class' name. */
922 const OSSymbol
*className
;
924 /* classSize How big is a single instance of this class. */
925 unsigned int classSize
;
927 /* instanceCount Roughly number of instances of the object,
928 * +1 for each direct subclass with a nonzero refcount.
929 * Used primarily as a code-in-use flag.
931 mutable unsigned int instanceCount
;
933 /* Not to be included in headerdoc.
935 * @function OSMetaClass
938 * The default private constructor.
942 // Called by postModLoad
943 /* Not to be included in headerdoc.
948 * Logs an error string for an <code>OSReturn</code> value
949 * using <code>printf</code>.
951 * @param result The <code>OSReturn</code> value for which to log a message.
954 * This function is used to log errors loading kernel extensions.
955 * Kernel extensions themselves should not call it.
957 static void logError(OSReturn result
);
962 * @function getMetaClassWithName
965 * Look up a metaclass in the run-time type information system.
967 * @param name The name of the desired class's metaclass.
970 * A pointer to the metaclass object if found, <code>NULL</code> otherwise.
972 static const OSMetaClass
* getMetaClassWithName(const OSSymbol
* name
);
974 #if XNU_KERNEL_PRIVATE
977 * @function copyMetaClassWithName
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.
986 * The metaclass will be protected from unloading until releaseMetaClass()
989 static const OSMetaClass
* copyMetaClassWithName(const OSSymbol
* name
);
991 * @function releaseMetaClass
994 * Releases reference obtained from copyMetaClassWithName().
997 * The metaclass will be protected from unloading until releaseMetaClass()
1000 void releaseMetaClass() const;
1002 #endif /* XNU_KERNEL_PRIVATE */
1009 * Implements the abstract <code>retain</code> function to do nothing.
1012 * Since an OSMetaClass instance must remain in existence
1013 * for as long as its kernel extension is loaded,
1014 * OSMetaClass does not use reference-counting.
1016 virtual void retain() const;
1023 * Implements the abstract <code>release</code> function to do nothing.
1026 * Since an OSMetaClass instance must remain in existence
1027 * for as long as its kernel extension is loaded,
1028 * OSMetaClass does not use reference-counting.
1030 virtual void release() const;
1037 * Implements the abstract <code>release(int freeWhen)</code>
1038 * function to do nothing.
1040 * @param freeWhen Unused.
1043 * Since an OSMetaClass instance must remain in existence
1044 * for as long as its kernel extension is loaded,
1045 * OSMetaClass does not use reference-counting.
1047 virtual void release(int freeWhen
) const;
1051 * @function taggedRetain
1054 * Implements the abstract <code>taggedRetain(const void *)</code>
1055 * function to do nothing.
1057 * @param tag Unused.
1060 * Since an OSMetaClass instance must remain in existence
1061 * for as long as its kernel extension is loaded,
1062 * OSMetaClass does not use reference-counting.
1064 virtual void taggedRetain(const void * tag
= 0) const;
1068 * @function taggedRelease
1071 * Implements the abstract <code>taggedRelease(const void *)</code>
1072 * function to do nothing.
1074 * @param tag Unused.
1077 * Since an OSMetaClass instance must remain in existence
1078 * for as long as its kernel extension is loaded,
1079 * OSMetaClass does not use reference-counting.
1081 virtual void taggedRelease(const void * tag
= 0) const;
1085 * @function taggedRelease
1088 * Implements the abstract <code>taggedRelease(const void *, cont int)</code>
1089 * function to do nothing.
1091 * @param tag Unused.
1092 * @param freeWhen Unused.
1095 * Since an OSMetaClass instance must remain in existence
1096 * for as long as its kernel extension is loaded,
1097 * OSMetaClass does not use reference-counting.
1099 virtual void taggedRelease(
1101 const int freeWhen
) const;
1105 * @function getRetainCount
1108 * Implements the abstract <code>getRetainCount</code>
1109 * function to return 0.
1115 * Since an OSMetaClass instance must remain in existence
1116 * for as long as its kernel extension is loaded,
1117 * OSMetaClass does not use reference-counting.
1119 virtual int getRetainCount() const;
1122 /* Not to be included in headerdoc.
1124 * @function getMetaClass
1127 * Returns the meta-metaclass.
1130 * The metaclass of the OSMetaClass object.
1132 virtual const OSMetaClass
* getMetaClass() const;
1136 * @function OSMetaClass
1139 * Constructor for OSMetaClass objects.
1141 * @param className A C string naming the C++ class
1142 * that this OSMetaClass represents.
1143 * @param superclass The OSMetaClass object representing the superclass
1144 * of this metaclass's class.
1145 * @param classSize The allocation size of the represented C++ class.
1148 * This constructor is protected and cannot be used
1149 * to instantiate OSMetaClass directly, as OSMetaClass is an abstract class.
1150 * This function is called during kext loading
1151 * to queue C++ classes for registration.
1152 * See <code>@link preModLoad preModLoad@/link</code> and
1153 * <code>@link postModLoad postModLoad@/link</code>.
1155 OSMetaClass(const char * className
,
1156 const OSMetaClass
* superclass
,
1157 unsigned int classSize
);
1161 * @function ~OSMetaClass
1164 * Destructor for OSMetaClass objects.
1167 * This function is called when the kernel extension that implements
1168 * the metaclass's class is unloaded.
1169 * The destructor removes all references to the class
1170 * from the run-time type information system.
1175 // Needs to be overriden as NULL as all OSMetaClass objects are allocated
1176 // statically at compile time, don't accidently try to free them.
1178 operator delete(void *, size_t)
1183 static const OSMetaClass
* const metaClass
;
1186 * @function preModLoad
1189 * Prepares the run-time type system
1190 * for the creation of new metaclasses
1191 * during loading of a kernel extension (module).
1193 * @param kextID The bundle ID of the kext being loaded.
1196 * An opaque handle to the load context
1197 * for the kernel extension on success;
1198 * <code>NULL</code> on failure.
1201 * <i>Not for use by kernel extensions.</i>
1203 * Prepares the run-time type information system to record and register
1204 * metaclasses created by static constructors until a subsequent call to
1205 * <code>@link postModLoad postModLoad@/link</code>.
1206 * <code>preModLoad</code> takes a lock to ensure processing of a single
1207 * load operation at a time; the lock is released by
1208 * <code>@link postModLoad postModLoad@/link</code>.
1209 * Any OSMetaClass constructed between these two function calls
1210 * will be associated with <code>kextID</code>.
1212 static void * preModLoad(const char * kextID
);
1216 * @function checkModLoad
1219 * Checks whether the current kext load operation can proceed.
1221 * @param loadHandle The opaque handle returned
1222 * by <code>@link preModLoad preModLoad@/link</code>.
1224 * <code>true</code> if no errors are outstanding
1225 * and the system is ready to process more metaclasses.
1228 * <i>Not for use by kernel extensions.</i>
1230 static bool checkModLoad(void * loadHandle
);
1234 * @function postModLoad
1237 * Registers the metaclasses created during loading of a kernel extension.
1239 * @param loadHandle The opaque handle returned
1240 * by <code>@link preModLoad preModLoad@/link</code>.
1242 * The error code of the first error encountered,
1245 * //apple_ref/cpp/macro/kOSReturnSuccess
1246 * kOSReturnSuccess@/link</code>
1247 * if no error occurred.
1250 * <i>Not for use by kernel extensions.</i>
1252 * Called after all static constructors in a kernel extension
1253 * have created metaclasses,
1254 * this function checks for duplicate class names,
1255 * then registers the new metaclasses under the kext ID
1256 * that @link preModLoad preModLoad@/link was called with,
1257 * so that they can be dynamically allocated
1258 * and have their instance counts tracked.
1259 * <code>postModLoad</code> releases the lock taken by
1260 * <code>@link preModLoad preModLoad@/link</code>.
1262 static OSReturn
postModLoad(void * loadHandle
);
1265 * @function modHasInstance
1268 * Returns whether any classes defined by the named
1269 * kernel extension (or their subclasses) have existing instances.
1271 * @param kextID The bundle ID of the kernel extension to check.
1274 * <code>true</code> if the kext is found and
1275 * if any class defined by that kext
1276 * has a nonzero instance count,
1277 * <code>false</code> otherwise.
1280 * This function is called before a kernel extension's static destructors
1281 * are invoked, prior to unloading the extension.
1282 * If any classes stil have instances or subclasses with instances,
1283 * those classes are logged
1284 * (using <code>@link reportModInstances reportModInstances@/link</code>) and
1285 * the kernel extension is not be unloaded.
1287 static bool modHasInstance(const char * kextID
);
1291 * @function reportModInstances
1294 * Logs the instance counts for classes
1295 * defined by a kernel extension.
1297 * @param kextID The bundle ID of the kernel extension to report on.
1300 * This function prints the names and instance counts
1301 * of any class defined by <code>kextID</code>
1302 * that has a nonzero instance count.
1303 * It's called by <code>@link modHasInstance modHasInstance@/link</code>
1304 * to help diagnose problems unloading kernel extensions.
1306 static void reportModInstances(const char * kextID
);
1310 * @function considerUnloads
1313 * Schedule automatic unloading of unused kernel extensions.
1316 * This function schedules a check for kernel extensions
1317 * that can be automatically unloaded,
1318 * canceling any currently scheduled check.
1319 * At that time, any such kexts with no Libkern C++ instances
1320 * and no external references are unloaded.
1322 * The I/O Kit calls this function when matching goes idle.
1324 * Kernel extensions that define subclasses of
1325 * @link //apple_ref/doc/class/IOService IOService@/link
1326 * are eligible for automatic unloading.
1328 * (On releases of Mac OS X prior to Snow Leopard (10.6),
1329 * any kernel extension defining any Libkern C++ class
1330 * was eligible for automatic unloading,
1331 * but that unload did not call the module stop routine.
1332 * Non-I/O Kit kernel extensions that define Libkern C++ subclasses
1333 * should be sure to have OSBundleLibraries declarations that ensure
1334 * they will not load on releases prior to Snow Leopard.)
1336 static void considerUnloads();
1338 #if XNU_KERNEL_PRIVATE
1339 static bool removeClasses(OSCollection
* metaClasses
);
1340 #endif /* XNU_KERNEL_PRIVATE */
1343 * @function allocClassWithName
1346 * Allocates an instance of a named OSObject-derived class.
1348 * @param name The name of the desired class.
1351 * A pointer to the newly-allocated, uninitialized object on success;
1352 * <code>NULL</code> on failure.
1355 * Kernel extensions should not need to use this function
1356 * directly, instead using static instance-creation functions
1357 * defined by classes.
1359 * This function consults the run-time type information system
1360 * to find the metaclass for the named class.
1361 * If it exists, it calls the metaclass's <code>@link alloc alloc@/link</code>
1362 * function and returns the result.
1364 static OSObject
* allocClassWithName(const OSSymbol
* name
);
1368 * function allocClassWithName
1371 * Allocates an instance of a named OSObject-derived class.
1373 * @param name The name of the desired class.
1376 * A pointer to the newly-allocated, uninitialized object on success;
1377 * <code>NULL</code> on failure.
1380 * Kernel extensions should not need to use this function
1381 * directly, instead using static instance-creation functions
1382 * defined by classes.
1384 * This function consults the run-time type information system
1385 * to find the metaclass for the named class.
1386 * If it exists, it calls the metaclass's <code>@link alloc alloc@/link</code>
1387 * function and returns the result.
1389 static OSObject
* allocClassWithName(const OSString
* name
);
1393 * function allocClassWithName
1396 * Allocates an instance of a named OSObject-derived class.
1398 * @param name The name of the desired class.
1401 * A pointer to the newly-allocated, uninitialized object on success;
1402 * <code>NULL</code> on failure.
1405 * Kernel extensions should not need to use this function
1406 * directly, instead using static instance-creation functions
1407 * defined by classes.
1409 * This function consults the run-time type information system
1410 * to find the metaclass for the named class.
1411 * If it exists, it calls the metaclass's <code>@link alloc alloc@/link</code>
1412 * function and returns the result.
1414 static OSObject
* allocClassWithName(const char * name
);
1418 * @function checkMetaCastWithName
1421 * Search the metaclass inheritance hierarchy by name for an object instance.
1423 * @param className The name of the desired class or superclass.
1424 * @param object The object whose metaclass begins the search.
1427 * <code>object</code> if it's derived from <code>className</code>;
1428 * <code>NULL</code> otherwise.
1431 * This function is the basis of the Libkern run-time type-checking system.
1432 * Kernel extensions should not use it directly,
1433 * instead using <code>@link OSDynamicCast OSDynamicCast@/link</code> or
1434 * <code>@link OSCheckTypeInst OSCheckTypeInst@/link</code>.
1436 static OSMetaClassBase
* checkMetaCastWithName(
1437 const OSSymbol
* className
,
1438 const OSMetaClassBase
* object
);
1441 * @function checkMetaCastWithName
1444 * Search the metaclass inheritance hierarchy by name for an object instance.
1446 * @param className The name of the desired class or superclass.
1447 * @param object The object whose metaclass begins the search.
1450 * <code>object</code> if it's derived from <code>className</code>;
1451 * <code>NULL</code> otherwise.
1454 * Kernel extensions should not use this function directly,
1455 * instead using <code>@link OSDynamicCast OSDynamicCast@/link</code> or
1456 * <code>@link OSCheckTypeInst OSCheckTypeInst@/link</code>.
1458 static OSMetaClassBase
* checkMetaCastWithName(
1459 const OSString
* className
,
1460 const OSMetaClassBase
* object
);
1463 * @function checkMetaCastWithName
1466 * Search the metaclass inheritance hierarchy by name for an object instance.
1468 * @param className The name of the desired class or superclass.
1469 * @param object The object whose metaclass begins the search.
1472 * <code>object</code> if it's derived from <code>className</code>;
1473 * <code>NULL</code> otherwise.
1476 * Kernel extensions should not use this function directly,
1477 * instead using <code>@link OSDynamicCast OSDynamicCast@/link</code> or
1478 * <code>@link OSCheckTypeInst OSCheckTypeInst@/link</code>.
1480 static OSMetaClassBase
* checkMetaCastWithName(
1481 const char * className
,
1482 const OSMetaClassBase
* object
);
1486 * @function instanceConstructed
1489 * Counts the instances of the class managed by this metaclass.
1492 * <i>Not for use by kernel extensions.</i>
1494 * Every non-abstract class that inherits from OSObject
1495 * has a default constructor that calls it's own metaclass's
1496 * <code>instanceConstructed</code> function.
1497 * This constructor is defined by the
1499 * OSDefineMetaClassAndStructors
1500 * OSDefineMetaClassAndStructors@/link</code>
1501 * macro that all OSObject subclasses must use.
1503 * If a class's instance count goes from 0 to 1--that is,
1504 * upon the creation of the first instance of that class--the
1505 * superclass's instance count is also incremented.
1506 * This propagates reference counts up the inheritance chain so that
1507 * superclasses are counted as "in use" when subclasses have instances.
1509 void instanceConstructed() const;
1513 * @function instanceDestructed
1516 * Counts the instances of the class managed by this metaclass.
1519 * Every non-abstract class that inherits from OSObject
1520 * has a default destructor that calls it's own metaclass's
1521 * <code>instanceDestructed</code> function.
1522 * This constructor is defined by the
1523 * @link OSDefineMetaClassAndStructors OSDefineMetaClassAndStructors@/link
1524 * macro that all OSObject subclasses must use.
1526 * If a class's instance count goes from 1 to 0--that is,
1527 * upon the destruction of the last instance of that class--the
1528 * superclass's instance count is also decremented.
1529 * This reduces "in use" counts from superclasses when their subclasses
1530 * no longer have instances.
1532 void instanceDestructed() const;
1536 * @function checkMetaCast
1539 * Check whether a given object is an instance of the receiving
1540 * metaclass's class or one derived from it.
1542 * @param object The object to check for inheritance.
1545 * <code>object</code> if it is derived from the receiver's class,
1546 * <code>NULL</code> if not.
1548 OSMetaClassBase
* checkMetaCast(const OSMetaClassBase
* object
) const;
1552 * @function getInstanceCount
1555 * Returns the number of existing instances of the metaclass's class.
1558 * The number of existing instances of the metaclass's class,
1559 * plus 1 for each subclass with any instance.
1561 unsigned int getInstanceCount() const;
1565 * @function getSuperClass
1568 * Returns the super-metaclass of the receiver.
1571 * Returns a pointer to the super-metaclass of the receiving
1572 * OSMetaClass, or <code>NULL</code> for OSObject's metaclass.
1574 const OSMetaClass
* getSuperClass() const;
1577 * @function getKmodName
1580 * Returns the bundle identifier of the kernel extension
1581 * that defines this metaclass.
1584 * The bundle identifier of the kernel extension that defines this metaclass.
1587 * "Kmod" is an older term for kernel extension.
1589 const OSSymbol
* getKmodName() const;
1593 * @function getClassName
1596 * Returns the name of the C++ class managed by this metaclass.
1599 * Returns the name of the C++ class managed by this metaclass.
1601 const char * getClassName() const;
1602 const OSSymbol
* getClassNameSymbol() const;
1606 * @function getClassSize
1609 * Returns the allocation size of the C++ class managed by this metaclass.
1612 * The allocation size of the C++ class managed by this metaclass.
1614 unsigned int getClassSize() const;
1621 * Allocates an instance of the C++ class managed by this metaclass.
1624 * A pointer to the newly allocated, uninitialized instance,
1625 * with a retain count of 1; <code>NULL</code> on allocation failure.
1628 * This function is automatically created by the metaclass-registration macros
1629 * to enable dynamic instance allocation.
1631 virtual OSObject
* alloc() const = 0;
1633 #ifdef XNU_KERNEL_PRIVATE
1634 void addInstance(const OSObject
* instance
, bool super
= false) const;
1635 void removeInstance(const OSObject
* instance
, bool super
= false) const;
1636 void applyToInstances(OSMetaClassInstanceApplierFunction applier
,
1637 void * context
) const;
1638 static void applyToInstancesOfClassName(
1639 const OSSymbol
* name
,
1640 OSMetaClassInstanceApplierFunction applier
,
1643 static void applyToInstances(OSOrderedSet
* set
,
1644 OSMetaClassInstanceApplierFunction applier
,
1647 #endif /* XNU_KERNEL_PRIVATE */
1649 /* Not to be included in headerdoc.
1651 * @define OSDeclareCommonStructors
1655 * Helper macro for for the standard metaclass-registration macros.
1658 * @param className The name of the C++ class, as a raw token,
1659 * <i>not</i> a string or macro.
1661 #define OSDeclareCommonStructors(className) \
1663 static const OSMetaClass * const superClass; \
1665 static const OSMetaClass * const metaClass; \
1666 static class MetaClass : public OSMetaClass { \
1669 virtual OSObject *alloc() const; \
1671 friend class className ::MetaClass; \
1672 virtual const OSMetaClass * getMetaClass() const APPLE_KEXT_OVERRIDE; \
1674 className (const OSMetaClass *); \
1675 virtual ~ className () APPLE_KEXT_OVERRIDE
1679 * @define OSDeclareDefaultStructors
1683 * Declares run-time type information and functions
1684 * for a concrete Libkern C++ class.
1686 * @param className The name of the C++ class, as a raw token,
1687 * <i>not</i> a string or macro.
1690 * Concrete Libkern C++ classes should "call" this macro
1691 * immediately after the opening brace in a class declaration.
1692 * It leaves the current privacy state as <code>protected:</code>.
1694 #define OSDeclareDefaultStructors(className) \
1695 OSDeclareCommonStructors(className); \
1702 * @define OSDeclareAbstractStructors
1706 * Declares run-time type information and functions
1707 * for an abstract Libkern C++ class.
1709 * @param className The name of the C++ class, as a raw token,
1710 * <i>not</i> a string or macro.
1713 * Abstract Libkern C++ classes--those with at least one
1714 * pure virtual method--should "call" this macro
1715 * immediately after the opening brace in a class declaration.
1716 * It leaves the current privacy state as <code>protected:</code>.
1718 #define OSDeclareAbstractStructors(className) \
1719 OSDeclareCommonStructors(className); \
1721 className (); /* Make primary constructor private in abstract */ \
1725 * @define OSDeclareFinalStructors
1729 * Declares run-time type information and functions
1730 * for a final (non-subclassable) Libkern C++ class.
1732 * @param className The name of the C++ class, as a raw token,
1733 * <i>not</i> a string or macro.
1736 * Final Libkern C++ classes--those that do not allow subclassing--should
1737 * "call" this macro immediately after the opening brace in a class declaration.
1738 * (Final classes in the kernel may actually have subclasses in the kernel,
1739 * but kexts cannot define any subclasses of a final class.)
1740 * It leaves the current privacy state as <code>protected:</code>.
1742 * <b>Note:</b> If the class is exported by a pseudokext (symbol set),
1743 * the final symbol generated by this macro must be exported
1744 * for the final-class attribute to be enforced.
1746 * <b>Warning:</b> Changing a class from "Default" to "Final" will break
1747 * binary compatibility.
1749 #define OSDeclareFinalStructors(className) \
1750 OSDeclareDefaultStructors(className) \
1752 void __OSFinalClass(void); \
1756 /* Not to be included in headerdoc.
1758 * @define OSDefineMetaClassWithInit
1762 * Helper macro for for the standard metaclass-registration macros.
1765 * @param className The name of the C++ class, as a raw token,
1766 * <i>not</i> a string or macro.
1767 * @param superclassName The name of the superclass of the C++ class,
1769 * <i>not</i> a string or macro.
1770 * @param init A function to call in the constructor
1771 * of the class's OSMetaClass.
1773 #define OSDefineMetaClassWithInit(className, superclassName, init) \
1774 /* Class global data */ \
1775 className ::MetaClass className ::gMetaClass; \
1776 const OSMetaClass * const className ::metaClass = \
1777 & className ::gMetaClass; \
1778 const OSMetaClass * const className ::superClass = \
1779 & superclassName ::gMetaClass; \
1780 /* Class member functions */ \
1781 className :: className(const OSMetaClass *meta) \
1782 : superclassName (meta) { } \
1783 className ::~ className() { } \
1784 const OSMetaClass * className ::getMetaClass() const \
1785 { return &gMetaClass; } \
1786 /* The ::MetaClass constructor */ \
1787 className ::MetaClass::MetaClass() \
1788 : OSMetaClass(#className, className::superClass, sizeof(className)) \
1792 /* Not to be included in headerdoc.
1794 * @define OSDefineAbstractStructors
1798 * Helper macro for for the standard metaclass-registration macros.
1801 * @param className The name of the C++ class, as a raw token,
1802 * <i>not</i> a string or macro.
1803 * @param superclassName The name of the superclass of the C++ class,
1805 * <i>not</i> a string or macro.
1807 #define OSDefineAbstractStructors(className, superclassName) \
1808 OSObject * className ::MetaClass::alloc() const { return 0; }
1811 /* Not to be included in headerdoc.
1813 * @define OSDefineDefaultStructors
1817 * Helper macro for for the standard metaclass-registration macros.
1820 * @param className The name of the C++ class, as a raw token,
1821 * <i>not</i> a string or macro.
1822 * @param superclassName The name of the superclass of the C++ class,
1824 * <i>not</i> a string or macro.
1826 #define OSDefineDefaultStructors(className, superclassName) \
1827 OSObject * className ::MetaClass::alloc() const \
1828 { return new className; } \
1829 className :: className () : superclassName (&gMetaClass) \
1830 { gMetaClass.instanceConstructed(); }
1832 /* Not to be included in headerdoc.
1834 * @define OSDefineDefaultStructors
1838 * Helper macro for for the standard metaclass-registration macros.
1841 * @param className The name of the C++ class, as a raw token,
1842 * <i>not</i> a string or macro.
1843 * @param superclassName The name of the superclass of the C++ class,
1845 * <i>not</i> a string or macro.
1847 #define OSDefineFinalStructors(className, superclassName) \
1848 OSDefineDefaultStructors(className, superclassName) \
1849 void className ::__OSFinalClass(void) { }
1852 /* Not to be included in headerdoc.
1854 * @define OSDefineMetaClassAndStructorsWithInit
1858 * Helper macro for for the standard metaclass-registration macros.
1861 * @param className The name of the C++ class, as a raw token,
1862 * <i>not</i> a string or macro.
1863 * @param superclassName The name of the superclass of the C++ class,
1865 * <i>not</i> a string or macro.
1866 * @param init A function to call in the constructor
1867 * of the class's OSMetaClass.
1869 #define OSDefineMetaClassAndStructorsWithInit(className, superclassName, init) \
1870 OSDefineMetaClassWithInit(className, superclassName, init) \
1871 OSDefineDefaultStructors(className, superclassName)
1874 /* Not to be included in headerdoc.
1876 * @define OSDefineMetaClassAndAbstractStructorsWithInit
1880 * Helper macro for for the standard metaclass-registration macros.
1883 * @param className The name of the C++ class, as a raw token,
1884 * <i>not</i> a string or macro.
1885 * @param superclassName The name of the superclass of the C++ class,
1887 * <i>not</i> a string or macro.
1888 * @param init A function to call in the constructor
1889 * of the class's OSMetaClass.
1891 #define OSDefineMetaClassAndAbstractStructorsWithInit(className, superclassName, init) \
1892 OSDefineMetaClassWithInit(className, superclassName, init) \
1893 OSDefineAbstractStructors(className, superclassName)
1896 /* Not to be included in headerdoc.
1898 * @define OSDefineMetaClassAndFinalStructorsWithInit
1902 * Helper macro for for the standard metaclass-registration macros.
1905 * @param className The name of the C++ class, as a raw token,
1906 * <i>not</i> a string or macro.
1907 * @param superclassName The name of the superclass of the C++ class,
1909 * <i>not</i> a string or macro.
1910 * @param init A function to call in the constructor
1911 * of the class's OSMetaClass.
1913 #define OSDefineMetaClassAndFinalStructorsWithInit(className, superclassName, init) \
1914 OSDefineMetaClassWithInit(className, superclassName, init) \
1915 OSDefineFinalStructors(className, superclassName)
1920 /* Not to be included in headerdoc.
1922 * @define OSDefineMetaClass
1926 * Helper macro for for the standard metaclass-registration macros.
1929 * @param className The name of the C++ class, as a raw token,
1930 * <i>not</i> a string or macro.
1931 * @param superclassName The name of the superclass of the C++ class,
1933 * <i>not</i> a string or macro.
1934 * @param init A function to call in the constructor
1935 * of the class's OSMetaClass.
1937 #define OSDefineMetaClass(className, superclassName) \
1938 OSDefineMetaClassWithInit(className, superclassName, )
1942 * @define OSDefineMetaClassAndStructors
1946 * Defines an OSMetaClass and associated routines
1947 * for a concrete Libkern C++ class.
1949 * @param className The name of the C++ class, as a raw token,
1950 * <i>not</i> a string or macro.
1951 * @param superclassName The name of the superclass of the C++ class,
1953 * <i>not</i> a string or macro.
1956 * Concrete Libkern C++ classes should "call" this macro
1957 * at the beginning of their implementation files,
1958 * before any function implementations for the class.
1960 #define OSDefineMetaClassAndStructors(className, superclassName) \
1961 OSDefineMetaClassAndStructorsWithInit(className, superclassName, )
1965 * @define OSDefineMetaClassAndAbstractStructors
1969 * Defines an OSMetaClass and associated routines
1970 * for an abstract Libkern C++ class.
1972 * @param className The name of the C++ class, as a raw token,
1973 * <i>not</i> a string or macro.
1974 * @param superclassName The name of the superclass of the C++ class,
1976 * <i>not</i> a string or macro.
1979 * Abstract Libkern C++ classes--those with at least one
1980 * pure virtual method--should "call" this macro
1981 * at the beginning of their implementation files,
1982 * before any function implementations for the class.
1984 #define OSDefineMetaClassAndAbstractStructors(className, superclassName) \
1985 OSDefineMetaClassAndAbstractStructorsWithInit (className, superclassName, )
1989 * @define OSDefineMetaClassAndFinalStructors
1993 * Defines an OSMetaClass and associated routines
1994 * for a final (non-subclassable) Libkern C++ class.
1996 * @param className The name of the C++ class, as a raw token,
1997 * <i>not</i> a string or macro.
1998 * @param superclassName The name of the superclass of the C++ class,
2000 * <i>not</i> a string or macro.
2003 * Final Libkern C++ classes--those that do not allow
2004 * subclassing--should "call" this macro at the beginning
2005 * of their implementation files,
2006 * before any function implementations for the class.
2007 * (Final classes in the kernel may actually have subclasses in the kernel,
2008 * but kexts cannot define any subclasses of a final class.)
2010 * <b>Note:</b> If the class is exported by a pseudokext (symbol set),
2011 * the final symbol generated by this macro must be exported
2012 * for the final-class attribute to be enforced.
2014 * <b>Warning:</b> Changing a class from "Default" to "Final" will break
2015 * binary compatibility.
2017 #define OSDefineMetaClassAndFinalStructors(className, superclassName) \
2018 OSDefineMetaClassAndFinalStructorsWithInit(className, superclassName, )
2021 // Dynamic vtable patchup support routines and types
2022 void reservedCalled(int ind
) const;
2026 * @define OSMetaClassDeclareReservedUnused
2030 * Reserves vtable space for new virtual functions
2031 * in a Libkern C++ class.
2033 * @param className The name of the C++ class, as a raw token,
2034 * <i>not</i> a string or macro.
2035 * @param index The numeric index of the vtable slot,
2036 * as a raw constant, beginning from 0.
2039 * Libkern C++ classes in kernel extensions that can be used as libraries
2040 * can provide for backward compatibility by declaring a number
2041 * of reserved vtable slots
2042 * that can be replaced with new functions as they are added.
2043 * Each reserved declaration must be accompanied in the implementation
2044 * by a corresponding reference to
2045 * <code>@link OSMetaClassDefineReservedUnused
2046 * OSMetaClassDefineReservedUnused@/link</code>.
2048 * When replacing a reserved slot, change the macro from "Unused"
2049 * to "Used" to document the fact that the slot used to be reserved,
2050 * and declare the new function immediately after the "Used" macro
2051 * to preserve vtable ordering.
2053 * <code>@link OSMetaClassDeclareReservedUsed
2054 * OSMetaClassDeclareReservedUsed@/link</code>.
2056 #if APPLE_KEXT_VTABLE_PADDING
2057 #define OSMetaClassDeclareReservedUnused(className, index) \
2059 virtual void _RESERVED ## className ## index ()
2061 #define OSMetaClassDeclareReservedUnused(className, index)
2066 * @define OSMetaClassDeclareReservedUsed
2070 * Documents use of reserved vtable space for new virtual functions
2071 * in a Libkern C++ class.
2073 * @param className The name of the C++ class, as a raw token,
2074 * <i>not</i> a string or macro.
2075 * @param index The numeric index of the vtable slot,
2076 * as a raw constant, beginning from 0.
2079 * This macro evaluates to nothing, and is used to document reserved
2080 * vtable slots as they are filled.
2082 * <code>@link OSMetaClassDeclareReservedUnused
2083 * OSMetaClassDeclareReservedUnused@/link</code>.
2085 #define OSMetaClassDeclareReservedUsed(className, index)
2089 * @define OSMetaClassDefineReservedUnused
2093 * Defines a reserved vtable slot for a Libkern C++ class.
2095 * @param className The name of the C++ class, as a raw token,
2096 * <i>not</i> a string or macro.
2097 * @param index The numeric index of the vtable slot,
2098 * as a raw constant, beginning from 0.
2101 * Libkern C++ classes in kernel extensions that can be used as libraries
2102 * can provide for backward compatibility by declaring a number
2103 * of reserved vtable slots
2104 * that can be replaced with new functions as they are added.
2105 * Each reserved defintion accompanies
2106 * a corresponding declaration created with
2107 * <code>@link OSMetaClassDeclareReservedUnused
2108 * OSMetaClassDeclareReservedUnused@/link</code>.
2110 * This macro is used in the implementation file
2111 * to provide a placeholder definition for the reserved vtable slot,
2112 * as a function that calls <code>panic</code> with an error message.
2114 * When replacing a reserved slot, change the macro from "Unused"
2115 * to "Used" to document the fact that the slot used to be reserved,
2116 * and declare the new function immediately after the "Used" macro
2117 * to preserve vtable ordering.
2119 * <code>@link OSMetaClassDefineReservedUsed
2120 * OSMetaClassDefineReservedUsed@/link</code>.
2122 #if APPLE_KEXT_VTABLE_PADDING
2123 #define OSMetaClassDefineReservedUnused(className, index) \
2124 void className ::_RESERVED ## className ## index () \
2125 { gMetaClass.reservedCalled(index); }
2127 #define OSMetaClassDefineReservedUnused(className, index)
2132 * @define OSMetaClassDefineReservedUsed
2136 * Reserves vtable space for new virtual functions in a Libkern C++ class.
2138 * @param className The name of the C++ class, as a raw token,
2139 * <i>not</i> a string or macro.
2140 * @param index The numeric index of the vtable slot,
2141 * as a raw constant, beginning from 0.
2144 * This macro evaluates to nothing, and is used to document reserved
2145 * vtable slots as they are filled.
2147 * <code>@link OSMetaClassDefineReservedUnused
2148 * OSMetaClassDefineReservedUnused@/link</code>.
2150 #define OSMetaClassDefineReservedUsed(className, index)
2152 // I/O Kit debug internal routines.
2153 static void printInstanceCounts();
2154 static void serializeClassDictionary(OSDictionary
* dict
);
2155 #ifdef XNU_KERNEL_PRIVATE
2158 static void * trackedNew(size_t size
);
2159 static void trackedDelete(void * mem
, size_t size
);
2160 void trackedInstance(OSObject
* instance
) const;
2161 void trackedFree(OSObject
* instance
) const;
2162 void trackedAccumSize(OSObject
* instance
, size_t size
) const;
2163 struct IOTrackingQueue
* getTracking() const;
2164 #endif /* IOTRACKING */
2165 #endif /* XNU_KERNEL_PRIVATE */
2169 static OSDictionary
* getClassDictionary();
2170 virtual bool serialize(OSSerialize
* serializer
) const;
2172 // Virtual Padding functions for MetaClass's
2173 OSMetaClassDeclareReservedUnused(OSMetaClass
, 0);
2174 OSMetaClassDeclareReservedUnused(OSMetaClass
, 1);
2175 OSMetaClassDeclareReservedUnused(OSMetaClass
, 2);
2176 OSMetaClassDeclareReservedUnused(OSMetaClass
, 3);
2177 OSMetaClassDeclareReservedUnused(OSMetaClass
, 4);
2178 OSMetaClassDeclareReservedUnused(OSMetaClass
, 5);
2179 OSMetaClassDeclareReservedUnused(OSMetaClass
, 6);
2180 OSMetaClassDeclareReservedUnused(OSMetaClass
, 7);
2183 #endif /* !_LIBKERN_OSMETACLASS_H */