2 * Copyright (c) 2000-2019 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>
38 * LIBKERN_ macros below can be used to describe the ownership semantics
39 * of functions handling subclasses of OSObject.
40 * The attributes propagate with inheritance, but can be overriden.
41 * New versions of the Clang Static Analyzer can use this knowledge to
42 * check the code for leaks or uses-after-free.
46 * By default, methods returning OSObjects are assumed to have the following
48 * - Methods which start with "get" are "Get" and which are not returning
49 * a subclass of OSIterator are assumed to be getters.
50 * They return at "+0" and the caller is not responsible for releasing the
53 * - All other methods are assumed to return at "+1", and the caller is
54 * responsible for releasing the returned object.
56 * The semantics implied by the naming convention described above can be
57 * overriden using either LIBKERN_RETURNS_RETAINED or LIBKERN_RETURNS_NOT_RETAINED
58 * attribute applied to a function.
59 * In the former case, it stipulates that the function is returning at "+1",
60 * and in the latter case "+0".
62 * LIBKERN_RETURNS_RETAINED and LIBKERN_RETURNS_NOT_RETAINED attributes
63 * can be also applied to out parameters, in which case they specify
64 * that an out parameter is written into at +1 or +0 respectively.
65 * For out parameters of non-void functions an assumption is
66 * that an out parameter is written into iff the return value is non-zero
67 * unless the function returns a typedef to kern_return_t,
68 * in which case it is assumed to be written into on zero value
70 * This can be customized using the attributes
71 * LIBKERN_RETURNS_RETAINED_ON_ZERO and LIBKERN_RETURNS_RETAINED_ON_NONZERO.
73 #if __has_attribute(os_returns_retained)
74 #define LIBKERN_RETURNS_RETAINED __attribute__((os_returns_retained))
76 #define LIBKERN_RETURNS_RETAINED
78 #if __has_attribute(os_returns_not_retained)
79 #define LIBKERN_RETURNS_NOT_RETAINED __attribute__((os_returns_not_retained))
81 #define LIBKERN_RETURNS_NOT_RETAINED
85 * LIBKERN_CONSUMED attribute can be applied to parameters.
86 * It specifies that this function call would consume the reference to the
87 * annotated parameter.
89 #if __has_attribute(os_consumed)
90 #define LIBKERN_CONSUMED __attribute__((os_consumed))
92 #define LIBKERN_CONSUMED
96 * LIBKERN_CONSUMES_THIS attribute can be applied to methods.
97 * It specifies that this method call consumes a reference to "this" (e.g.
98 * by storing a reference to "this" in a passed parameter).
100 #if __has_attribute(os_consumes_this)
101 #define LIBKERN_CONSUMES_THIS __attribute__((os_consumes_this))
103 #define LIBKERN_CONSUMES_THIS
107 * LIBKERN_RETURNS_RETAINED_ON_ZERO is an attribute applicable to out
109 * It specifies that an out parameter at +1 is written into an argument iff
110 * the function returns a zero return value.
112 #if __has_attribute(os_returns_retained_on_zero)
113 #define LIBKERN_RETURNS_RETAINED_ON_ZERO __attribute__((os_returns_retained_on_zero))
115 #define LIBKERN_RETURNS_RETAINED_ON_ZERO
119 * LIBKERN_RETURNS_RETAINED_ON_NON_ZERO is an attribute applicable to out
121 * It specifies that an out parameter at +1 is written into an argument iff
122 * the function returns a non-zero return value.
124 #if __has_attribute(os_returns_retained_on_non_zero)
125 #define LIBKERN_RETURNS_RETAINED_ON_NONZERO __attribute__((os_returns_retained_on_non_zero))
127 #define LIBKERN_RETURNS_RETAINED_ON_NONZERO
136 #ifdef XNU_KERNEL_PRIVATE
139 #endif /* XNU_KERNEL_PRIVATE */
149 * This header declares the OSMetaClassBase and OSMetaClass classes,
150 * which together form the basis of the Libkern and I/O Kit C++ class hierarchy
151 * and run-time type information facility.
156 #define APPLE_KEXT_COMPATIBILITY
158 #ifdef XNU_KERNEL_PRIVATE
160 #ifdef CONFIG_EMBEDDED
161 #define APPLE_KEXT_VTABLE_PADDING 0
162 #else /* CONFIG_EMBEDDED */
164 #define APPLE_KEXT_VTABLE_PADDING 1
165 #endif /* CONFIG_EMBEDDED */
167 #else /* XNU_KERNEL_PRIVATE */
168 #include <TargetConditionals.h>
170 #if (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)
171 #define APPLE_KEXT_VTABLE_PADDING 0
172 #else /* (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR) */
174 #define APPLE_KEXT_VTABLE_PADDING 1
175 #endif /* (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR) */
177 #endif /* XNU_KERNEL_PRIVATE */
179 #define APPLE_KEXT_ALIGN_CONTAINERS (0 == APPLE_KEXT_VTABLE_PADDING)
181 #if defined(__LP64__)
183 #define APPLE_KEXT_LEGACY_ABI 0
184 #elif defined(__arm__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2))
185 #define APPLE_KEXT_LEGACY_ABI 0
187 #define APPLE_KEXT_LEGACY_ABI 1
190 #if defined(__LP64__)
192 #define APPLE_KEXT_COMPATIBILITY_VIRTUAL
194 // private method made virtual only for binary compatibility
195 #define APPLE_KEXT_COMPATIBILITY_VIRTUAL virtual
199 #define APPLE_KEXT_DEPRECATED __attribute__((deprecated))
203 * <rdar://problem/44872498> AppleUSBAudio builds xnu's libkern headers in user space
205 #if !defined(BUILD_FOR_USERSPACE) && (__cplusplus >= 201103L)
206 #define APPLE_KEXT_OVERRIDE override
207 #if defined(__LP64__)
208 #define APPLE_KEXT_COMPATIBILITY_OVERRIDE
210 #define APPLE_KEXT_COMPATIBILITY_OVERRIDE APPLE_KEXT_OVERRIDE
213 #define APPLE_KEXT_OVERRIDE
214 #define APPLE_KEXT_COMPATIBILITY_OVERRIDE
217 #define APPLE_KEXT_WSHADOW_PUSH _Pragma("clang diagnostic push") \
218 _Pragma("clang diagnostic ignored \"-Wunknown-warning-option\"") \
219 _Pragma("clang diagnostic ignored \"-Wshadow-field\"")
221 #define APPLE_KEXT_WSHADOW_POP _Pragma("clang diagnostic pop")
225 * @class OSMetaClassBase
228 * OSMetaClassBase is the abstract bootstrap class
229 * for the Libkern and I/O Kit run-time type information system.
232 * OSMetaClassBase is the abstract C++ root class
233 * underlying the entire Libkern and I/O Kit class hierarchy.
234 * It defines the run-time type information system,
235 * including dynamic class allocation and safe type-casting,
236 * as well as the abstract interface for reference counting
237 * and a few other utility functions.
238 * OSMetaClassBase is the immediate superclass of
239 * @link //apple_ref/doc/class/OSObject OSObject@/link and
240 * @link //apple_ref/doc/class/OSMetaClass OSMetaClass@/link;
241 * no other class should derive from OSMetaClassBase.
243 * For more information, see
244 * <i>@link //apple_ref/doc/uid/TP40002799
245 * I/O Kit Device Driver Design Guidelines@/link</i>.
247 * <b>Use by Kernel Extensions</b>
249 * Kernel Extensions should never interact directly with OSMetaClassBase,
250 * but they will find useful several macros that tie in
251 * to the run-time type information system, specifically:
253 * <li><code>@link OSTypeAlloc OSTypeAlloc@/link</code> - allocation of new instances</li>
254 * <li><code>@link OSDynamicCast OSDynamicCast@/link</code> - safe type casting</li>
255 * <li><code>@link OSCheckTypeInst OSCheckTypeInst@/link</code> -
256 * checking for inheritance/derivation</li>
257 * <li><code>@link OSMemberFunctionCast OSMemberFunctionCast@/link</code> -
258 * casting C++ member functions to C function pointers
259 * for registration as callbacks</li>
262 * See @link //apple_ref/doc/class/OSMetaClass OSMetaClass@/link
263 * for more run-time type information interfaces.
265 * <b>Use Restrictions</b>
267 * OSMetaClassBase should not be subclassed by kernel extensions,
268 * nor should kernel extensions call its run-time type functions directly.
270 * The run-time type functions and macros are <b>not safe</b>
271 * to call in a primary interrupt context.
273 * <b>Concurrency Protection</b>
275 * The run-time type macros and functions of OSMetaClassBase are thread-safe.
278 class OSMetaClassBase
284 * @define OSTypeAlloc
288 * Allocates an instance of the named object class.
290 * @param type The name of the desired class to be created,
291 * as a raw token, <i>not</i> a string or macro.
294 * A pointer to the new, uninitialized object on success;
295 * <code>NULL</code> on failure.
300 * //apple_ref/cpp/clm/OSMetaClass/allocClassWithName/staticOSObject*\/(constchar*)
301 * OSMetaClass::allocClassWithName(const char *)@/link</code>
304 * //apple_ref/cpp/instm/OSMetaClass/alloc/virtualOSObject*\/()
305 * OSMetaClass::alloc@/link</code>.
307 * The OSTypeAlloc macro is used to avoid binary compatibility difficulties
308 * presented by the C++ <code>new</code> operator.
310 #define OSTypeAlloc(type) ((type *) ((type::metaClass)->alloc()))
318 * Returns the type ID (metaclass) of a class based on its name.
320 * @param type The name of the desired class, as a raw token,
321 * <i>not</i> a string or macro.
324 * The unique type ID (metaclass) for the class.
327 * It is typically more useful to determine whether a class is derived
329 * <code>@link //apple_ref/cpp/macro/OSDynamicCast OSDynamicCast@/link</code>
331 * <code>@link //apple_ref/cpp/macro/OSCheckTypeInst OSCheckTypeInst@/link</code>.
333 #define OSTypeID(type) (type::metaClass)
334 #define OSMTypeID(type) ((OSMetaClass *) type::metaClass)
338 * @define OSTypeIDInst
342 * Returns the type ID (metaclass) for the class of an object instance.
344 * @param typeinst An instance of an OSObject subclass.
347 * The type ID of that object's class; that is, its metaclass.
350 * It is typically more useful to determine whether an object is derived
351 * from a particular class; see
352 * <code>@link //apple_ref/cpp/macro/OSDynamicCast OSDynamicCast@/link</code>
354 * <code>@link //apple_ref/cpp/macro/OSCheckTypeInst OSCheckTypeInst@/link</code>.
356 #define OSTypeIDInst(typeinst) ((typeinst)->getMetaClass())
360 * @define OSDynamicCast
364 * Safe type-casting for Libkern C++ objects.
366 * @param type The name of the desired class type, as a raw token,
367 * <i>not</i> a string or macro.
368 * It is assumed you intend to cast to a pointer
369 * to an object of this type.
370 * Type qualifiers, such as <code>const</code>,
371 * are not recognized and will cause
372 * a (usually obscure) compile error.
373 * @param inst A pointer to the object instance to be cast.
374 * May be <code>NULL</code>.
377 * <code>inst</code> if it is non-<code>NULL</code>
378 * and derived from <code>type</code>;
379 * otherwise <code>NULL</code>.
382 * <code>OSDynamicCast</code> is a rough equivalent
383 * to the standard C++ RTTI <code>dynamic_cast<T></code> operator.
384 * Your code should use this instead of raw C type-casting,
385 * and check the resulting value.
386 * If the result is non-<code>NULL</code>,
387 * the object is safe to use as the type-cast class;
388 * if the result is <code>NULL</code>,
389 * the object does not derive from the type-cast class
390 * and your code should take appropriate steps to handle the error.
392 #define OSDynamicCast(type, inst) \
393 ((type *) OSMetaClassBase::safeMetaCast((inst), OSTypeID(type)))
396 * @define OSRequiredCast
400 * Safe type-casting for Libkern C++ objects; panics on failure.
401 * The input parameters are the same as for the {@code OSDynamicCast} macro.
403 * @result {@code inst} if it is NULL or derived from {@code type};
404 * otherwise triggers a kernel panic.
407 * This macro should be used in place of C-style casts or
408 * <code>@link OSDynamicCast OSDynamicCast@/link</code>.
409 * when the caller is absolutely sure that the passed
410 * argument is a subclass of a required type.
411 * It is equivalent to using {@code OSDynamicCast} and crashing with a kernel
412 * panic on cast failure.
414 #define OSRequiredCast(type, inst) \
415 (type *) OSMetaClassBase::requiredMetaCast((inst), OSTypeID(type))
418 * @define OSCheckTypeInst
422 * Checks whether two objects are type-compatible.
424 * @param typeinst The reference object.
425 * @param inst The object to check for type compatibility.
428 * <code>true</code> if both <code>inst</code> and
429 * <code>typeinst</code> are non-<code>NULL</code>
430 * and <code>inst</code> is derived from the class of <code>typeinst</code>;
431 * otherwise <code>false</code>.
433 #define OSCheckTypeInst(typeinst, inst) \
434 OSMetaClassBase::checkTypeInst(inst, typeinst)
436 #define OSSafeRelease(inst) \
437 do { int OSSafeRelease __attribute__ ((deprecated("Use OSSafeReleaseNULL"))); (OSSafeRelease); \
438 if (inst) (inst)->release(); } while (0)
440 /*! @function OSSafeReleaseNULL
441 * @abstract Release an object if not <code>NULL</code>, then set it to <code>NULL</code>.
442 * @param inst Instance of an OSObject, may be <code>NULL</code>.
444 #define OSSafeReleaseNULL(inst) do { if (inst != NULL) (inst)->release(); (inst) = NULL; } while (0)
446 typedef void (*_ptf_t
)(void);
448 #if defined(__arm__) || defined(__arm64__)
450 static _ptf_t
_ptmf2ptf(const OSMetaClassBase
* self
, void (OSMetaClassBase::*func
)(void), uintptr_t typeDisc
);
452 #elif defined(__i386__) || defined(__x86_64__)
454 // Slightly less arcane and slightly less evil code to do
455 // the same for kexts compiled with the standard Itanium C++
459 _ptmf2ptf(const OSMetaClassBase
*self
, void (OSMetaClassBase::*func
)(void),
460 uintptr_t typeDisc
__attribute__((unused
)))
463 void (OSMetaClassBase::*fIn
)(void);
470 if (map
.fVTOffset
& 1) {
473 const OSMetaClassBase
*fObj
;
478 // Virtual member function so dereference vtable
479 return *(_ptf_t
*)(((uintptr_t)*u
.vtablep
) + map
.fVTOffset
- 1);
481 // Not virtual, i.e. plain member func
487 #error Unknown architecture.
492 * @define OSMemberFunctionCast
496 * Converts a C++ member function pointer, relative to an instance,
497 * to a C-style pointer to function.
499 * @param cptrtype The function type declaration to cast to
500 * (typically provided as a <code>typedef</code> by I/O KitKit classes).
501 * @param self The <code>this</code> pointer of the object whose function
503 * @param func The pointer to the member function itself,
504 * something like <code>&Class::function</code>.
507 * A pointer to a function of the given type referencing <code>self</code>.
510 * This function is used to generate pointers to C++ functions for instances,
511 * such that they can be registered as callbacks with I/O Kit objects.
513 * No warnings are generated.
515 * This function will panic if an attempt is made to call it
516 * with a multiply-inheriting class.
518 #define OSMemberFunctionCast(cptrtype, self, func) \
519 (cptrtype) OSMetaClassBase:: \
520 _ptmf2ptf(self, (void (OSMetaClassBase::*)(void)) func, \
521 ptrauth_type_discriminator(__typeof__(func)))
529 // Disable copy constructors of OSMetaClassBase based objects
530 /* Not to be included in headerdoc.
532 * @function operator =
535 * Disable implicit copy constructor by making private
537 * @param src Reference to source object that isn't allowed to be copied.
539 void operator =(OSMetaClassBase
&src
);
541 /* Not to be included in headerdoc.
543 * @function OSMetaClassBase
546 * Disable implicit copy constructor by making private
548 * @param src Reference to source object that isn't allowed to be copied.
550 OSMetaClassBase(OSMetaClassBase
&src
);
554 // xx-review: the original comment for this makes it sound to me like we don't
555 // xx-review: catch over-releasing an object...?
561 * Abstract declaration of
563 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/(int)
564 * release(int freeWhen)@/link</code>.
569 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/(int)
570 * release(int freeWhen)@/link</code>.
572 virtual void release(int freeWhen
) const = 0;
576 * @function getRetainCount
579 * Abstract declaration of
581 * //apple_ref/cpp/instm/OSObject/getRetainCount/virtualint/()
582 * getRetainCount()@/link</code>.
587 * //apple_ref/cpp/instm/OSObject/getRetainCount/virtualint/()
588 * OSObject::getRetainCount()@/link</code>.
590 virtual int getRetainCount() const = 0;
597 * Abstract declaration of
599 * //apple_ref/cpp/instm/OSObject/retain/virtualvoid/()
600 * retain()@/link</code>.
605 * //apple_ref/cpp/instm/OSObject/retain/virtualvoid/()
606 * OSObject::retain()@/link</code>.
608 virtual void retain() const = 0;
615 * Abstract declaration of
617 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/()
618 * release@/link</code>.
623 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/()
624 * OSObject::release@/link</code>.
626 virtual void release() const = 0;
630 * @function serialize
633 * Abstract declaration of
635 * //apple_ref/cpp/instm/OSObject/serialize/virtualbool/(OSSerialize*)
636 * serialize@/link</code>.
641 * //apple_ref/cpp/instm/OSObject/serialize/virtualbool/(OSSerialize*)
642 * OSObject::serialize@/link</code>.
644 virtual bool serialize(OSSerialize
* serializer
) const = 0;
648 * @function getMetaClass
651 * Returns the OSMetaClass representing
652 * an OSMetaClassBase subclass.
655 * OSObject overrides this abstract member function
656 * to return the OSMetaClass object that represents
657 * each class for run-time typing.
659 virtual const OSMetaClass
* getMetaClass() const = 0;
663 * @function isEqualTo
666 * Checks whether another object is equal to the receiver.
668 * @param anObject The object to copmare to the receiver.
671 * <code>true</code> if the objects are equal, <code>false</code> otherwise.
674 * OSMetaClassBase implements this as a direct pointer comparison,
675 * since it has no other information to judge equality by.
676 * Subclasses generally override this function
677 * to do a more meaningful comparison.
678 * For example, OSString implements it to return
679 * <code>true</code> if <code>anObject</code>
680 * is derived from OSString and represents the same C string.
682 virtual bool isEqualTo(const OSMetaClassBase
* anObject
) const;
689 * Casts this object is to the class managed by the given OSMetaClass.
691 * @param toMeta A pointer to a constant OSMetaClass
692 * for the desired target type.
695 * <code>this</code> if the object is derived
696 * from the class managed by <code>toMeta</code>,
697 * otherwise <code>NULL</code>.
700 * It is far more convenient to use
701 * <code>@link OSDynamicCast OSDynamicCast@/link</code>.
703 OSMetaClassBase
* metaCast(const OSMetaClass
* toMeta
) const;
710 * Casts this object is to the class managed by the named OSMetaClass.
712 * @param toMeta An OSSymbol naming the desired target type.
715 * <code>this</code> if the object is derived
716 * from the class named by <code>toMeta</code>,
717 * otherwise <code>NULL</code>.
720 * It is far more convenient to use
721 * <code>@link OSDynamicCast OSDynamicCast@/link</code>.
723 OSMetaClassBase
* metaCast(const OSSymbol
* toMeta
) const;
730 * Casts this object is to the class managed by the named OSMetaClass.
732 * @param toMeta An OSString naming the desired target type.
734 * <code>this</code> if the object is derived
735 * from the class named by <code>toMeta</code>,
736 * otherwise <code>NULL</code>.
739 * It is far more convenient to use
740 * <code>@link OSDynamicCast OSDynamicCast@/link</code>.
742 OSMetaClassBase
* metaCast(const OSString
* toMeta
) const;
749 * Casts this object is to the class managed by the named OSMetaClass.
751 * @param toMeta A C string naming the desired target type.
753 * <code>this</code> if the object is derived
754 * from the class named by <code>toMeta</code>,
755 * otherwise <code>NULL</code>.
758 * It is far more convenient to use
759 * <code>@link OSDynamicCast OSDynamicCast@/link</code>.
761 OSMetaClassBase
* metaCast(const char * toMeta
) const;
763 // Helper inlines for run-time type preprocessor macros
765 * @function safeMetaCast
768 * Casts an object is to the class managed by the given OSMetaClass.
770 * @param anObject A pointer to the object to be cast.
771 * @param toMeta A pointer to a constant OSMetaClass
772 * for the desired target type.
775 * <code>anObject</code> if the object is derived
776 * from the class managed by <code>toMeta</code>,
777 * otherwise <code>NULL</code>.
780 * It is far more convenient to use
781 * <code>@link OSDynamicCast OSDynamicCast@/link</code>.
783 static OSMetaClassBase
* safeMetaCast(
784 const OSMetaClassBase
* anObject
,
785 const OSMetaClass
* toMeta
);
788 * @function requiredMetaCast
791 * Casts an object to the class managed by the given OSMetaClass or
792 * fails with a kernel panic if the cast does not succeed.
794 * @param anObject A pointer to the object to be cast.
795 * @param toMeta A pointer to a constant OSMetaClass
796 * for the desired target type.
799 * <code>anObject</code> if the object is derived
800 * from the class managed by <code>toMeta</code>,
801 * <code>NULL</code> if <code>anObject</code> was <code>NULL</code>,
802 * kernel panic otherwise.
805 * It is far more convenient to use
806 * <code>@link OSRequiredCast OSRequiredCast@/link</code>.
808 static OSMetaClassBase
*requiredMetaCast(
809 const OSMetaClassBase
* anObject
,
810 const OSMetaClass
* toMeta
);
813 * @function checkTypeInst
816 * Checks whether an object instance is of the same class
817 * as another object instance (or a subclass of that class).
819 * @param inst A pointer to the object to check.
820 * @param typeinst A pointer to an object of the class being checked.
823 * <code>true</code> if the object is derived
824 * from the class of <code>typeinst</code>
825 * or a subclass of that class,
826 * otherwise <code>false</code>.
829 * It is far more convenient to use
830 * <code>@link OSCheckTypeInst OSCheckTypeInst@/link</code>.
832 static bool checkTypeInst(
833 const OSMetaClassBase
* inst
,
834 const OSMetaClassBase
* typeinst
);
836 static void initialize(void);
841 * @function taggedRetain
844 * Abstract declaration of
846 * //apple_ref/cpp/instm/OSObject/taggedRetain/virtualvoid/(constvoid*)
847 * taggedRetain(const void *)@/link</code>.
852 * //apple_ref/cpp/instm/OSObject/taggedRetain/virtualvoid/(constvoid*)
853 * OSObject::taggedRetain(const void *)@/link</code>.
855 // WAS: virtual void _RESERVEDOSMetaClassBase0();
856 virtual void taggedRetain(const void * tag
= NULL
) const = 0;
860 * @function taggedRelease
863 * Abstract declaration of
865 * //apple_ref/cpp/instm/OSObject/taggedRelease/virtualvoid/(constvoid*)
866 * taggedRelease(const void *)@/link</code>.
871 * //apple_ref/cpp/instm/OSObject/taggedRelease/virtualvoid/(constvoid*)
872 * OSObject::taggedRelease(const void *)@/link</code>.
874 // WAS: virtual void _RESERVEDOSMetaClassBase1();
875 virtual void taggedRelease(const void * tag
= NULL
) const = 0;
879 * @function taggedRelease
882 * Abstract declaration of
884 * //apple_ref/cpp/instm/OSObject/taggedRelease/virtualvoid/(constvoid*,constint)
885 * taggedRelease(const void *, const int freeWhen)@/link</code>.
890 * //apple_ref/cpp/instm/OSObject/taggedRelease/virtualvoid/(constvoid*,constint)
891 * OSObject::taggedRelease(const void *, const int freeWhen)@/link</code>.
893 // WAS: virtual void _RESERVEDOSMetaClassBase2();
894 virtual void taggedRelease(
896 const int freeWhen
) const = 0;
899 virtual kern_return_t
900 Dispatch(const IORPC rpc
);
903 Invoke(const IORPC rpc
);
906 #if APPLE_KEXT_VTABLE_PADDING
908 virtual void _RESERVEDOSMetaClassBase4();
909 virtual void _RESERVEDOSMetaClassBase5();
910 virtual void _RESERVEDOSMetaClassBase6();
911 virtual void _RESERVEDOSMetaClassBase7();
913 } APPLE_KEXT_COMPATIBILITY
;
916 #ifdef XNU_KERNEL_PRIVATE
917 typedef bool (*OSMetaClassInstanceApplierFunction
)(const OSObject
* instance
,
919 #endif /* XNU_KERNEL_PRIVATE */
925 * OSMetaClass manages run-time type information
926 * for Libkern and I/O Kit C++ classes.
928 * @discussion OSMetaClass manages run-time type information
929 * for Libkern and I/O Kit C++ classes.
930 * An instance of OSMetaClass exists for (nearly) every such C++ class,
931 * keeping track of inheritance relationships, class lookup by name,
932 * instance counts, and more.
933 * OSMetaClass operates almost entirely behind the scenes,
934 * and kernel extensions should rarely, if ever,
935 * have to interact directly with OSMetaClass.
937 * <b>Use by Kernel Extensions</b>
939 * While kernel extensions rarey interact directly with OSMetaClass at run time,
940 * they must register their classes with the metaclass system
941 * using the macros declared here.
942 * The class declaration should use one of these two macros
943 * before its first member function declaration:
945 * <li><code>@link OSDeclareDefaultStructors OSDeclareDefaultStructors@/link</code> -
946 * for classes with no abstract member function declarations</li>
947 * <li><code>@link OSDeclareAbstractStructors OSDeclareAbstractStructors@/link</code> -
948 * for classes with at least one abstract member function declaration</li>
949 * <li><code>@link OSDeclareFinalStructors OSDeclareFinalStructors@/link</code> -
950 * for classes that should not be subclassable by another kext</li>
953 * The class implementation should then use one of these macros:
955 * <li><code>@link OSDefineMetaClassAndStructors
956 * OSDefineMetaClassAndStructors@/link</code> -
957 * for classes with no abstract member function declarations</li>
958 * <li><code>@link OSDefineMetaClassAndAbstractStructors
959 * OSDefineMetaClassAndAbstractStructors@/link</code> -
960 * for classes with at least one abstract member function declaration</li>
961 * <li><code>@link OSDefineMetaClassAndFinalStructors
962 * OSDefineMetaClassAndFinalStructors@/link</code> -
963 * for classes that should not be subclassable by another kext</li>
966 * Classes in kernel extensions that are intended for use as libraries
967 * may need to reserve vtable slots to preserve binary compatibility
968 * as new functions are added. They may do so with these macros:
970 * <li><code>@link OSMetaClassDeclareReservedUnused
971 * OSMetaClassDeclareReservedUnused@/link</code> -
972 * reserves a vtable slot</li>
973 * <li><code>@link OSMetaClassDefineReservedUnused
974 * OSMetaClassDefineReservedUnused@/link</code> -
975 * defines the reserved vtable slot as an unimplemented function</li>
976 * <li><code>@link OSMetaClassDeclareReservedUsed
977 * OSMetaClassDeclareReservedUsed@/link</code> -
978 * documents that a formerly reserved slot is now used</li>
979 * <li><code>@link OSMetaClassDefineReservedUsed
980 * OSMetaClassDefineReservedUsed@/link</code> -
981 * documents that a formerly reserved slot is now used</li>
984 * <b>Use Restrictions</b>
986 * OSMetaClass should not be explicitly subclassed by kernel extensions
987 * (the declare/define macros do that),
988 * nor should kernel extensions call its run-time type functions directly.
990 * OSMetaClass functions should be considered
991 * <b>unsafe</b> to call in a primary interrupt context.
993 * <b>Concurrency Protection</b>
995 * Kernel extensions should in general not interact
996 * with OSMetaClass objects directly,
997 * instead using the run-time type macros.
998 * Much of OSMetaClass's interface is intended for use
999 * by the run-time type information system,
1000 * which handles concurrency and locking internally.
1002 class OSMetaClass
: public OSMetaClassBase
1004 friend class OSKext
;
1006 friend class IOStatistics
;
1010 // Can never be allocated must be created at compile time
1011 static void * operator new(size_t size
);
1013 /* Reserved for future use. (Internal use only) */
1014 struct ExpansionData
*reserved
;
1016 /* superClass Handle to the superclass's meta class. */
1017 const OSMetaClass
*superClassLink
;
1019 /* className OSSymbol of the class' name. */
1020 const OSSymbol
*className
;
1022 /* classSize How big is a single instance of this class. */
1023 unsigned int classSize
;
1025 /* instanceCount Roughly number of instances of the object,
1026 * +1 for each direct subclass with a nonzero refcount.
1027 * Used primarily as a code-in-use flag.
1029 mutable unsigned int instanceCount
;
1031 /* Not to be included in headerdoc.
1033 * @function OSMetaClass
1036 * The default private constructor.
1040 // Called by postModLoad
1041 /* Not to be included in headerdoc.
1043 * @function logError
1046 * Logs an error string for an <code>OSReturn</code> value
1047 * using <code>printf</code>.
1049 * @param result The <code>OSReturn</code> value for which to log a message.
1052 * This function is used to log errors loading kernel extensions.
1053 * Kernel extensions themselves should not call it.
1055 static void logError(OSReturn result
);
1060 * @function getMetaClassWithName
1063 * Look up a metaclass in the run-time type information system.
1065 * @param name The name of the desired class's metaclass.
1068 * A pointer to the metaclass object if found, <code>NULL</code> otherwise.
1070 static const OSMetaClass
* getMetaClassWithName(const OSSymbol
* name
);
1072 #if XNU_KERNEL_PRIVATE
1075 * @function copyMetaClassWithName
1078 * Look up a metaclass in the run-time type information system.
1080 * @param name The name of the desired class's metaclass.
1083 * A pointer to the metaclass object if found, <code>NULL</code> otherwise.
1084 * The metaclass will be protected from unloading until releaseMetaClass()
1087 static const OSMetaClass
* copyMetaClassWithName(const OSSymbol
* name
);
1089 * @function releaseMetaClass
1092 * Releases reference obtained from copyMetaClassWithName().
1095 * The metaclass will be protected from unloading until releaseMetaClass()
1098 void releaseMetaClass() const;
1100 #endif /* XNU_KERNEL_PRIVATE */
1107 * Implements the abstract <code>retain</code> function to do nothing.
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 retain() const;
1121 * Implements the abstract <code>release</code> function to do nothing.
1124 * Since an OSMetaClass instance must remain in existence
1125 * for as long as its kernel extension is loaded,
1126 * OSMetaClass does not use reference-counting.
1128 virtual void release() const;
1135 * Implements the abstract <code>release(int freeWhen)</code>
1136 * function to do nothing.
1138 * @param freeWhen Unused.
1141 * Since an OSMetaClass instance must remain in existence
1142 * for as long as its kernel extension is loaded,
1143 * OSMetaClass does not use reference-counting.
1145 virtual void release(int freeWhen
) const;
1149 * @function taggedRetain
1152 * Implements the abstract <code>taggedRetain(const void *)</code>
1153 * function to do nothing.
1155 * @param tag Unused.
1158 * Since an OSMetaClass instance must remain in existence
1159 * for as long as its kernel extension is loaded,
1160 * OSMetaClass does not use reference-counting.
1162 virtual void taggedRetain(const void * tag
= NULL
) const;
1166 * @function taggedRelease
1169 * Implements the abstract <code>taggedRelease(const void *)</code>
1170 * function to do nothing.
1172 * @param tag Unused.
1175 * Since an OSMetaClass instance must remain in existence
1176 * for as long as its kernel extension is loaded,
1177 * OSMetaClass does not use reference-counting.
1179 virtual void taggedRelease(const void * tag
= NULL
) const;
1183 * @function taggedRelease
1186 * Implements the abstract <code>taggedRelease(const void *, cont int)</code>
1187 * function to do nothing.
1189 * @param tag Unused.
1190 * @param freeWhen Unused.
1193 * Since an OSMetaClass instance must remain in existence
1194 * for as long as its kernel extension is loaded,
1195 * OSMetaClass does not use reference-counting.
1197 virtual void taggedRelease(
1199 const int freeWhen
) const;
1203 * @function getRetainCount
1206 * Implements the abstract <code>getRetainCount</code>
1207 * function to return 0.
1213 * Since an OSMetaClass instance must remain in existence
1214 * for as long as its kernel extension is loaded,
1215 * OSMetaClass does not use reference-counting.
1217 virtual int getRetainCount() const;
1220 /* Not to be included in headerdoc.
1222 * @function getMetaClass
1225 * Returns the meta-metaclass.
1228 * The metaclass of the OSMetaClass object.
1230 virtual const OSMetaClass
* getMetaClass() const;
1234 * @function OSMetaClass
1237 * Constructor for OSMetaClass objects.
1239 * @param className A C string naming the C++ class
1240 * that this OSMetaClass represents.
1241 * @param superclass The OSMetaClass object representing the superclass
1242 * of this metaclass's class.
1243 * @param classSize The allocation size of the represented C++ class.
1246 * This constructor is protected and cannot be used
1247 * to instantiate OSMetaClass directly, as OSMetaClass is an abstract class.
1248 * This function is called during kext loading
1249 * to queue C++ classes for registration.
1250 * See <code>@link preModLoad preModLoad@/link</code> and
1251 * <code>@link postModLoad postModLoad@/link</code>.
1253 OSMetaClass(const char * className
,
1254 const OSMetaClass
* superclass
,
1255 unsigned int classSize
);
1259 * @function ~OSMetaClass
1262 * Destructor for OSMetaClass objects.
1265 * This function is called when the kernel extension that implements
1266 * the metaclass's class is unloaded.
1267 * The destructor removes all references to the class
1268 * from the run-time type information system.
1273 // Needs to be overriden as NULL as all OSMetaClass objects are allocated
1274 // statically at compile time, don't accidently try to free them.
1276 operator delete(void *, size_t)
1281 static const OSMetaClass
* const metaClass
;
1284 * @function preModLoad
1287 * Prepares the run-time type system
1288 * for the creation of new metaclasses
1289 * during loading of a kernel extension (module).
1291 * @param kextID The bundle ID of the kext being loaded.
1294 * An opaque handle to the load context
1295 * for the kernel extension on success;
1296 * <code>NULL</code> on failure.
1299 * <i>Not for use by kernel extensions.</i>
1301 * Prepares the run-time type information system to record and register
1302 * metaclasses created by static constructors until a subsequent call to
1303 * <code>@link postModLoad postModLoad@/link</code>.
1304 * <code>preModLoad</code> takes a lock to ensure processing of a single
1305 * load operation at a time; the lock is released by
1306 * <code>@link postModLoad postModLoad@/link</code>.
1307 * Any OSMetaClass constructed between these two function calls
1308 * will be associated with <code>kextID</code>.
1310 static void * preModLoad(const char * kextID
);
1314 * @function checkModLoad
1317 * Checks whether the current kext load operation can proceed.
1319 * @param loadHandle The opaque handle returned
1320 * by <code>@link preModLoad preModLoad@/link</code>.
1322 * <code>true</code> if no errors are outstanding
1323 * and the system is ready to process more metaclasses.
1326 * <i>Not for use by kernel extensions.</i>
1328 static bool checkModLoad(void * loadHandle
);
1332 * @function postModLoad
1335 * Registers the metaclasses created during loading of a kernel extension.
1337 * @param loadHandle The opaque handle returned
1338 * by <code>@link preModLoad preModLoad@/link</code>.
1340 * The error code of the first error encountered,
1343 * //apple_ref/cpp/macro/kOSReturnSuccess
1344 * kOSReturnSuccess@/link</code>
1345 * if no error occurred.
1348 * <i>Not for use by kernel extensions.</i>
1350 * Called after all static constructors in a kernel extension
1351 * have created metaclasses,
1352 * this function checks for duplicate class names,
1353 * then registers the new metaclasses under the kext ID
1354 * that @link preModLoad preModLoad@/link was called with,
1355 * so that they can be dynamically allocated
1356 * and have their instance counts tracked.
1357 * <code>postModLoad</code> releases the lock taken by
1358 * <code>@link preModLoad preModLoad@/link</code>.
1360 static OSReturn
postModLoad(void * loadHandle
);
1363 * @function modHasInstance
1366 * Returns whether any classes defined by the named
1367 * kernel extension (or their subclasses) have existing instances.
1369 * @param kextID The bundle ID of the kernel extension to check.
1372 * <code>true</code> if the kext is found and
1373 * if any class defined by that kext
1374 * has a nonzero instance count,
1375 * <code>false</code> otherwise.
1378 * This function is called before a kernel extension's static destructors
1379 * are invoked, prior to unloading the extension.
1380 * If any classes stil have instances or subclasses with instances,
1381 * those classes are logged
1382 * (using <code>@link reportModInstances reportModInstances@/link</code>) and
1383 * the kernel extension is not be unloaded.
1385 static bool modHasInstance(const char * kextID
);
1389 * @function reportModInstances
1392 * Logs the instance counts for classes
1393 * defined by a kernel extension.
1395 * @param kextID The bundle ID of the kernel extension to report on.
1398 * This function prints the names and instance counts
1399 * of any class defined by <code>kextID</code>
1400 * that has a nonzero instance count.
1401 * It's called by <code>@link modHasInstance modHasInstance@/link</code>
1402 * to help diagnose problems unloading kernel extensions.
1404 static void reportModInstances(const char * kextID
);
1408 * @function considerUnloads
1411 * Schedule automatic unloading of unused kernel extensions.
1414 * This function schedules a check for kernel extensions
1415 * that can be automatically unloaded,
1416 * canceling any currently scheduled check.
1417 * At that time, any such kexts with no Libkern C++ instances
1418 * and no external references are unloaded.
1420 * The I/O Kit calls this function when matching goes idle.
1422 * Kernel extensions that define subclasses of
1423 * @link //apple_ref/doc/class/IOService IOService@/link
1424 * are eligible for automatic unloading.
1426 * (On releases of Mac OS X prior to Snow Leopard (10.6),
1427 * any kernel extension defining any Libkern C++ class
1428 * was eligible for automatic unloading,
1429 * but that unload did not call the module stop routine.
1430 * Non-I/O Kit kernel extensions that define Libkern C++ subclasses
1431 * should be sure to have OSBundleLibraries declarations that ensure
1432 * they will not load on releases prior to Snow Leopard.)
1434 static void considerUnloads();
1436 #if XNU_KERNEL_PRIVATE
1437 static bool removeClasses(OSCollection
* metaClasses
);
1438 #endif /* XNU_KERNEL_PRIVATE */
1441 * @function allocClassWithName
1444 * Allocates an instance of a named OSObject-derived class.
1446 * @param name The name of the desired class.
1449 * A pointer to the newly-allocated, uninitialized object on success;
1450 * <code>NULL</code> on failure.
1453 * Kernel extensions should not need to use this function
1454 * directly, instead using static instance-creation functions
1455 * defined by classes.
1457 * This function consults the run-time type information system
1458 * to find the metaclass for the named class.
1459 * If it exists, it calls the metaclass's <code>@link alloc alloc@/link</code>
1460 * function and returns the result.
1462 static OSObject
* allocClassWithName(const OSSymbol
* name
);
1466 * function allocClassWithName
1469 * Allocates an instance of a named OSObject-derived class.
1471 * @param name The name of the desired class.
1474 * A pointer to the newly-allocated, uninitialized object on success;
1475 * <code>NULL</code> on failure.
1478 * Kernel extensions should not need to use this function
1479 * directly, instead using static instance-creation functions
1480 * defined by classes.
1482 * This function consults the run-time type information system
1483 * to find the metaclass for the named class.
1484 * If it exists, it calls the metaclass's <code>@link alloc alloc@/link</code>
1485 * function and returns the result.
1487 static OSObject
* allocClassWithName(const OSString
* name
);
1491 * function allocClassWithName
1494 * Allocates an instance of a named OSObject-derived class.
1496 * @param name The name of the desired class.
1499 * A pointer to the newly-allocated, uninitialized object on success;
1500 * <code>NULL</code> on failure.
1503 * Kernel extensions should not need to use this function
1504 * directly, instead using static instance-creation functions
1505 * defined by classes.
1507 * This function consults the run-time type information system
1508 * to find the metaclass for the named class.
1509 * If it exists, it calls the metaclass's <code>@link alloc alloc@/link</code>
1510 * function and returns the result.
1512 static OSObject
* allocClassWithName(const char * name
);
1516 * @function checkMetaCastWithName
1519 * Search the metaclass inheritance hierarchy by name for an object instance.
1521 * @param className The name of the desired class or superclass.
1522 * @param object The object whose metaclass begins the search.
1525 * <code>object</code> if it's derived from <code>className</code>;
1526 * <code>NULL</code> otherwise.
1529 * This function is the basis of the Libkern run-time type-checking system.
1530 * Kernel extensions should not use it directly,
1531 * instead using <code>@link OSDynamicCast OSDynamicCast@/link</code> or
1532 * <code>@link OSCheckTypeInst OSCheckTypeInst@/link</code>.
1534 static OSMetaClassBase
* checkMetaCastWithName(
1535 const OSSymbol
* className
,
1536 const OSMetaClassBase
* object
);
1539 * @function checkMetaCastWithName
1542 * Search the metaclass inheritance hierarchy by name for an object instance.
1544 * @param className The name of the desired class or superclass.
1545 * @param object The object whose metaclass begins the search.
1548 * <code>object</code> if it's derived from <code>className</code>;
1549 * <code>NULL</code> otherwise.
1552 * Kernel extensions should not use this function directly,
1553 * instead using <code>@link OSDynamicCast OSDynamicCast@/link</code> or
1554 * <code>@link OSCheckTypeInst OSCheckTypeInst@/link</code>.
1556 static OSMetaClassBase
* checkMetaCastWithName(
1557 const OSString
* className
,
1558 const OSMetaClassBase
* object
);
1561 * @function checkMetaCastWithName
1564 * Search the metaclass inheritance hierarchy by name for an object instance.
1566 * @param className The name of the desired class or superclass.
1567 * @param object The object whose metaclass begins the search.
1570 * <code>object</code> if it's derived from <code>className</code>;
1571 * <code>NULL</code> otherwise.
1574 * Kernel extensions should not use this function directly,
1575 * instead using <code>@link OSDynamicCast OSDynamicCast@/link</code> or
1576 * <code>@link OSCheckTypeInst OSCheckTypeInst@/link</code>.
1578 static OSMetaClassBase
* checkMetaCastWithName(
1579 const char * className
,
1580 const OSMetaClassBase
* object
);
1584 * @function instanceConstructed
1587 * Counts the instances of the class managed by this metaclass.
1590 * <i>Not for use by kernel extensions.</i>
1592 * Every non-abstract class that inherits from OSObject
1593 * has a default constructor that calls it's own metaclass's
1594 * <code>instanceConstructed</code> function.
1595 * This constructor is defined by the
1597 * OSDefineMetaClassAndStructors
1598 * OSDefineMetaClassAndStructors@/link</code>
1599 * macro that all OSObject subclasses must use.
1601 * If a class's instance count goes from 0 to 1--that is,
1602 * upon the creation of the first instance of that class--the
1603 * superclass's instance count is also incremented.
1604 * This propagates reference counts up the inheritance chain so that
1605 * superclasses are counted as "in use" when subclasses have instances.
1607 void instanceConstructed() const;
1611 * @function instanceDestructed
1614 * Counts the instances of the class managed by this metaclass.
1617 * Every non-abstract class that inherits from OSObject
1618 * has a default destructor that calls it's own metaclass's
1619 * <code>instanceDestructed</code> function.
1620 * This constructor is defined by the
1621 * @link OSDefineMetaClassAndStructors OSDefineMetaClassAndStructors@/link
1622 * macro that all OSObject subclasses must use.
1624 * If a class's instance count goes from 1 to 0--that is,
1625 * upon the destruction of the last instance of that class--the
1626 * superclass's instance count is also decremented.
1627 * This reduces "in use" counts from superclasses when their subclasses
1628 * no longer have instances.
1630 void instanceDestructed() const;
1634 * @function checkMetaCast
1637 * Check whether a given object is an instance of the receiving
1638 * metaclass's class or one derived from it.
1640 * @param object The object to check for inheritance.
1643 * <code>object</code> if it is derived from the receiver's class,
1644 * <code>NULL</code> if not.
1646 OSMetaClassBase
* checkMetaCast(const OSMetaClassBase
* object
) const;
1650 * @function getInstanceCount
1653 * Returns the number of existing instances of the metaclass's class.
1656 * The number of existing instances of the metaclass's class,
1657 * plus 1 for each subclass with any instance.
1659 unsigned int getInstanceCount() const;
1663 * @function getSuperClass
1666 * Returns the super-metaclass of the receiver.
1669 * Returns a pointer to the super-metaclass of the receiving
1670 * OSMetaClass, or <code>NULL</code> for OSObject's metaclass.
1672 const OSMetaClass
* getSuperClass() const;
1675 * @function getKmodName
1678 * Returns the bundle identifier of the kernel extension
1679 * that defines this metaclass.
1682 * The bundle identifier of the kernel extension that defines this metaclass.
1685 * "Kmod" is an older term for kernel extension.
1687 const OSSymbol
* getKmodName() const;
1691 * @function getClassName
1694 * Returns the name of the C++ class managed by this metaclass.
1697 * Returns the name of the C++ class managed by this metaclass.
1699 const char * getClassName() const;
1700 const OSSymbol
* getClassNameSymbol() const;
1704 * @function getClassSize
1707 * Returns the allocation size of the C++ class managed by this metaclass.
1710 * The allocation size of the C++ class managed by this metaclass.
1712 unsigned int getClassSize() const;
1719 * Allocates an instance of the C++ class managed by this metaclass.
1722 * A pointer to the newly allocated, uninitialized instance,
1723 * with a retain count of 1; <code>NULL</code> on allocation failure.
1726 * This function is automatically created by the metaclass-registration macros
1727 * to enable dynamic instance allocation.
1729 virtual OSObject
* alloc() const = 0;
1731 #ifdef XNU_KERNEL_PRIVATE
1732 void addInstance(const OSObject
* instance
, bool super
= false) const;
1733 void removeInstance(const OSObject
* instance
, bool super
= false) const;
1734 void applyToInstances(OSMetaClassInstanceApplierFunction applier
,
1735 void * context
) const;
1736 static void applyToInstancesOfClassName(
1737 const OSSymbol
* name
,
1738 OSMetaClassInstanceApplierFunction applier
,
1741 static void applyToInstances(OSOrderedSet
* set
,
1742 OSMetaClassInstanceApplierFunction applier
,
1745 #endif /* XNU_KERNEL_PRIVATE */
1747 /* Not to be included in headerdoc.
1749 * @define OSDeclareCommonStructors
1753 * Helper macro for for the standard metaclass-registration macros.
1756 * @param className The name of the C++ class, as a raw token,
1757 * <i>not</i> a string or macro.
1760 #define _OS_ADD_METAMETHODS(b) _OS_ADD_METAMETHODS_ ## b
1761 #define _OS_ADD_METAMETHODS_
1762 #define _OS_ADD_METAMETHODS_dispatch \
1763 virtual kern_return_t Dispatch(const IORPC rpc) APPLE_KEXT_OVERRIDE;
1765 #define _OS_ADD_METHODS(className, b) _OS_ADD_METHODS_ ## b(className)
1766 #define _OS_ADD_METHODS_(className)
1767 #define _OS_ADD_METHODS_dispatch(className) \
1768 className ## _Methods \
1769 className ## _KernelMethods
1771 #define SUPERDISPATCH ((OSDispatchMethod)&super::_Dispatch)
1773 #define OSDeclareCommonStructors(className, dispatch) \
1775 static const OSMetaClass * const superClass; \
1777 static const OSMetaClass * const metaClass; \
1778 static class MetaClass : public OSMetaClass { \
1781 virtual OSObject *alloc() const APPLE_KEXT_OVERRIDE;\
1782 _OS_ADD_METAMETHODS(dispatch); \
1784 friend class className ::MetaClass; \
1785 virtual const OSMetaClass * getMetaClass() const APPLE_KEXT_OVERRIDE; \
1787 className (const OSMetaClass *); \
1788 virtual ~ className () APPLE_KEXT_OVERRIDE; \
1789 _OS_ADD_METHODS(className, dispatch)
1793 * @define OSDeclareDefaultStructors
1797 * Declares run-time type information and functions
1798 * for a final (non-subclassable) Libkern C++ class.
1800 * @param className The name of the C++ class, as a raw token,
1801 * <i>not</i> a string or macro.
1804 * Concrete Libkern C++ classes should "call" this macro
1805 * immediately after the opening brace in a class declaration.
1806 * It leaves the current privacy state as <code>protected:</code>.
1808 #define _OSDeclareDefaultStructors(className, dispatch) \
1809 OSDeclareCommonStructors(className, dispatch); \
1815 #define OSDeclareDefaultStructors(className) \
1816 _OSDeclareDefaultStructors(className, )
1818 #define OSDeclareDefaultStructorsWithDispatch(className) \
1819 _OSDeclareDefaultStructors(className, dispatch)
1823 * @define OSDeclareAbstractStructors
1827 * Declares run-time type information and functions
1828 * for an abstract Libkern C++ class.
1830 * @param className The name of the C++ class, as a raw token,
1831 * <i>not</i> a string or macro.
1834 * Abstract Libkern C++ classes--those with at least one
1835 * pure virtual method--should "call" this macro
1836 * immediately after the opening brace in a class declaration.
1837 * It leaves the current privacy state as <code>protected:</code>.
1839 #define _OSDeclareAbstractStructors(className, dispatch) \
1840 OSDeclareCommonStructors(className, dispatch); \
1842 className (void); /* Make primary constructor private in abstract */ \
1845 #define OSDeclareAbstractStructors(className) \
1846 _OSDeclareAbstractStructors(className, )
1848 #define OSDeclareAbstractStructorsWithDispatch(className) \
1849 _OSDeclareAbstractStructors(className, dispatch)
1852 * @define OSDeclareFinalStructors
1856 * Declares run-time type information and functions
1857 * for a concrete Libkern C++ class.
1859 * @param className The name of the C++ class, as a raw token,
1860 * <i>not</i> a string or macro.
1863 * Final Libkern C++ classes--those that do not allow subclassing--should
1864 * "call" this macro immediately after the opening brace in a class declaration.
1865 * (Final classes in the kernel may actually have subclasses in the kernel,
1866 * but kexts cannot define any subclasses of a final class.)
1867 * It leaves the current privacy state as <code>protected:</code>.
1869 * <b>Note:</b> If the class is exported by a pseudokext (symbol set),
1870 * the final symbol generated by this macro must be exported
1871 * for the final-class attribute to be enforced.
1873 * <b>Warning:</b> Changing a class from "Default" to "Final" will break
1874 * binary compatibility.
1876 #define _OSDeclareFinalStructors(className, dispatch) \
1877 _OSDeclareDefaultStructors(className, dispatch) \
1879 void __OSFinalClass(void); \
1883 #define OSDeclareFinalStructors(className) \
1884 _OSDeclareFinalStructors(className, )
1886 #define OSDeclareFinalStructorsWithDispatch(className) \
1887 _OSDeclareFinalStructors(className, dispatch)
1890 /* Not to be included in headerdoc.
1892 * @define OSDefineMetaClassWithInit
1896 * Helper macro for for the standard metaclass-registration macros.
1899 * @param className The name of the C++ class, as a raw token,
1900 * <i>not</i> a string or macro.
1901 * @param superclassName The name of the superclass of the C++ class,
1903 * <i>not</i> a string or macro.
1904 * @param init A function to call in the constructor
1905 * of the class's OSMetaClass.
1907 #define OSDefineMetaClassWithInit(className, superclassName, init) \
1908 /* Class global data */ \
1909 className ::MetaClass className ::gMetaClass; \
1910 const OSMetaClass * const className ::metaClass = \
1911 & className ::gMetaClass; \
1912 const OSMetaClass * const className ::superClass = \
1913 & superclassName ::gMetaClass; \
1914 /* Class member functions */ \
1915 className :: className(const OSMetaClass *meta) \
1916 : superclassName (meta) { } \
1917 className ::~ className() { } \
1918 const OSMetaClass * className ::getMetaClass() const \
1919 { return &gMetaClass; } \
1920 /* The ::MetaClass constructor */ \
1921 className ::MetaClass::MetaClass() \
1922 : OSMetaClass(#className, className::superClass, sizeof(className)) \
1926 /* Not to be included in headerdoc.
1928 * @define OSDefineAbstractStructors
1932 * Helper macro for for the standard metaclass-registration macros.
1935 * @param className The name of the C++ class, as a raw token,
1936 * <i>not</i> a string or macro.
1937 * @param superclassName The name of the superclass of the C++ class,
1939 * <i>not</i> a string or macro.
1941 #define OSDefineAbstractStructors(className, superclassName) \
1942 OSObject * className ::MetaClass::alloc() const { return NULL; }
1945 /* Not to be included in headerdoc.
1947 * @define OSDefineDefaultStructors
1951 * Helper macro for for the standard metaclass-registration macros.
1954 * @param className The name of the C++ class, as a raw token,
1955 * <i>not</i> a string or macro.
1956 * @param superclassName The name of the superclass of the C++ class,
1958 * <i>not</i> a string or macro.
1960 #define OSDefineDefaultStructors(className, superclassName) \
1961 OSObject * className ::MetaClass::alloc() const \
1962 { return new className; } \
1963 className :: className () : superclassName (&gMetaClass) \
1964 { gMetaClass.instanceConstructed(); }
1966 /* Not to be included in headerdoc.
1968 * @define OSDefineDefaultStructors
1972 * Helper macro for for the standard metaclass-registration macros.
1975 * @param className The name of the C++ class, as a raw token,
1976 * <i>not</i> a string or macro.
1977 * @param superclassName The name of the superclass of the C++ class,
1979 * <i>not</i> a string or macro.
1981 #define OSDefineFinalStructors(className, superclassName) \
1982 OSDefineDefaultStructors(className, superclassName) \
1983 void className ::__OSFinalClass(void) { }
1986 /* Not to be included in headerdoc.
1988 * @define OSDefineMetaClassAndStructorsWithInit
1992 * Helper macro for for the standard metaclass-registration macros.
1995 * @param className The name of the C++ class, as a raw token,
1996 * <i>not</i> a string or macro.
1997 * @param superclassName The name of the superclass of the C++ class,
1999 * <i>not</i> a string or macro.
2000 * @param init A function to call in the constructor
2001 * of the class's OSMetaClass.
2003 #define OSDefineMetaClassAndStructorsWithInit(className, superclassName, init) \
2004 OSDefineMetaClassWithInit(className, superclassName, init) \
2005 OSDefineDefaultStructors(className, superclassName)
2008 /* Not to be included in headerdoc.
2010 * @define OSDefineMetaClassAndAbstractStructorsWithInit
2014 * Helper macro for for the standard metaclass-registration macros.
2017 * @param className The name of the C++ class, as a raw token,
2018 * <i>not</i> a string or macro.
2019 * @param superclassName The name of the superclass of the C++ class,
2021 * <i>not</i> a string or macro.
2022 * @param init A function to call in the constructor
2023 * of the class's OSMetaClass.
2025 #define OSDefineMetaClassAndAbstractStructorsWithInit(className, superclassName, init) \
2026 OSDefineMetaClassWithInit(className, superclassName, init) \
2027 OSDefineAbstractStructors(className, superclassName)
2030 /* Not to be included in headerdoc.
2032 * @define OSDefineMetaClassAndFinalStructorsWithInit
2036 * Helper macro for for the standard metaclass-registration macros.
2039 * @param className The name of the C++ class, as a raw token,
2040 * <i>not</i> a string or macro.
2041 * @param superclassName The name of the superclass of the C++ class,
2043 * <i>not</i> a string or macro.
2044 * @param init A function to call in the constructor
2045 * of the class's OSMetaClass.
2047 #define OSDefineMetaClassAndFinalStructorsWithInit(className, superclassName, init) \
2048 OSDefineMetaClassWithInit(className, superclassName, init) \
2049 OSDefineFinalStructors(className, superclassName)
2054 /* Not to be included in headerdoc.
2056 * @define OSDefineMetaClass
2060 * Helper macro for for the standard metaclass-registration macros.
2063 * @param className The name of the C++ class, as a raw token,
2064 * <i>not</i> a string or macro.
2065 * @param superclassName The name of the superclass of the C++ class,
2067 * <i>not</i> a string or macro.
2068 * @param init A function to call in the constructor
2069 * of the class's OSMetaClass.
2071 #define OSDefineMetaClass(className, superclassName) \
2072 OSDefineMetaClassWithInit(className, superclassName, )
2076 * @define OSDefineMetaClassAndStructors
2080 * Defines an OSMetaClass and associated routines
2081 * for a concrete Libkern C++ class.
2083 * @param className The name of the C++ class, as a raw token,
2084 * <i>not</i> a string or macro.
2085 * @param superclassName The name of the superclass of the C++ class,
2087 * <i>not</i> a string or macro.
2090 * Concrete Libkern C++ classes should "call" this macro
2091 * at the beginning of their implementation files,
2092 * before any function implementations for the class.
2094 #define OSDefineMetaClassAndStructors(className, superclassName) \
2095 OSDefineMetaClassAndStructorsWithInit(className, superclassName, )
2099 * @define OSDefineMetaClassAndAbstractStructors
2103 * Defines an OSMetaClass and associated routines
2104 * for an abstract 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 superclassName The name of the superclass of the C++ class,
2110 * <i>not</i> a string or macro.
2113 * Abstract Libkern C++ classes--those with at least one
2114 * pure virtual method--should "call" this macro
2115 * at the beginning of their implementation files,
2116 * before any function implementations for the class.
2118 #define OSDefineMetaClassAndAbstractStructors(className, superclassName) \
2119 OSDefineMetaClassAndAbstractStructorsWithInit (className, superclassName, )
2123 * @define OSDefineMetaClassAndFinalStructors
2127 * Defines an OSMetaClass and associated routines
2128 * for concrete Libkern C++ class.
2130 * @param className The name of the C++ class, as a raw token,
2131 * <i>not</i> a string or macro.
2132 * @param superclassName The name of the superclass of the C++ class,
2134 * <i>not</i> a string or macro.
2137 * Final Libkern C++ classes--those that do not allow
2138 * subclassing--should "call" this macro at the beginning
2139 * of their implementation files,
2140 * before any function implementations for the class.
2141 * (Final classes in the kernel may actually have subclasses in the kernel,
2142 * but kexts cannot define any subclasses of a final class.)
2144 * <b>Note:</b> If the class is exported by a pseudokext (symbol set),
2145 * the final symbol generated by this macro must be exported
2146 * for the final-class attribute to be enforced.
2148 * <b>Warning:</b> Changing a class from "Default" to "Final" will break
2149 * binary compatibility.
2151 #define OSDefineMetaClassAndFinalStructors(className, superclassName) \
2152 OSDefineMetaClassAndFinalStructorsWithInit(className, superclassName, )
2155 // Dynamic vtable patchup support routines and types
2156 void reservedCalled(int ind
) const;
2160 * @define OSMetaClassDeclareReservedUnused
2164 * Reserves vtable space for new virtual functions
2165 * in a Libkern C++ class.
2167 * @param className The name of the C++ class, as a raw token,
2168 * <i>not</i> a string or macro.
2169 * @param index The numeric index of the vtable slot,
2170 * as a raw constant, beginning from 0.
2173 * Libkern C++ classes in kernel extensions that can be used as libraries
2174 * can provide for backward compatibility by declaring a number
2175 * of reserved vtable slots
2176 * that can be replaced with new functions as they are added.
2177 * Each reserved declaration must be accompanied in the implementation
2178 * by a corresponding reference to
2179 * <code>@link OSMetaClassDefineReservedUnused
2180 * OSMetaClassDefineReservedUnused@/link</code>.
2182 * When replacing a reserved slot, change the macro from "Unused"
2183 * to "Used" to document the fact that the slot used to be reserved,
2184 * and declare the new function immediately after the "Used" macro
2185 * to preserve vtable ordering.
2187 * <code>@link OSMetaClassDeclareReservedUsed
2188 * OSMetaClassDeclareReservedUsed@/link</code>.
2190 #if APPLE_KEXT_VTABLE_PADDING
2191 #define OSMetaClassDeclareReservedUnused(className, index) \
2193 virtual void _RESERVED ## className ## index ()
2195 #define OSMetaClassDeclareReservedUnused(className, index)
2200 * @define OSMetaClassDeclareReservedUsed
2204 * Documents use of reserved vtable space for new virtual functions
2205 * in a Libkern C++ class.
2207 * @param className The name of the C++ class, as a raw token,
2208 * <i>not</i> a string or macro.
2209 * @param index The numeric index of the vtable slot,
2210 * as a raw constant, beginning from 0.
2213 * This macro evaluates to nothing, and is used to document reserved
2214 * vtable slots as they are filled.
2216 * <code>@link OSMetaClassDeclareReservedUnused
2217 * OSMetaClassDeclareReservedUnused@/link</code>.
2219 #define OSMetaClassDeclareReservedUsed(className, index)
2223 * @define OSMetaClassDefineReservedUnused
2227 * Defines a reserved vtable slot for a Libkern C++ class.
2229 * @param className The name of the C++ class, as a raw token,
2230 * <i>not</i> a string or macro.
2231 * @param index The numeric index of the vtable slot,
2232 * as a raw constant, beginning from 0.
2235 * Libkern C++ classes in kernel extensions that can be used as libraries
2236 * can provide for backward compatibility by declaring a number
2237 * of reserved vtable slots
2238 * that can be replaced with new functions as they are added.
2239 * Each reserved defintion accompanies
2240 * a corresponding declaration created with
2241 * <code>@link OSMetaClassDeclareReservedUnused
2242 * OSMetaClassDeclareReservedUnused@/link</code>.
2244 * This macro is used in the implementation file
2245 * to provide a placeholder definition for the reserved vtable slot,
2246 * as a function that calls <code>panic</code> with an error message.
2248 * When replacing a reserved slot, change the macro from "Unused"
2249 * to "Used" to document the fact that the slot used to be reserved,
2250 * and declare the new function immediately after the "Used" macro
2251 * to preserve vtable ordering.
2253 * <code>@link OSMetaClassDefineReservedUsed
2254 * OSMetaClassDefineReservedUsed@/link</code>.
2256 #if APPLE_KEXT_VTABLE_PADDING
2257 #define OSMetaClassDefineReservedUnused(className, index) \
2258 void className ::_RESERVED ## className ## index () \
2259 { gMetaClass.reservedCalled(index); }
2261 #define OSMetaClassDefineReservedUnused(className, index)
2266 * @define OSMetaClassDefineReservedUsed
2270 * Reserves vtable space for new virtual functions in a Libkern C++ class.
2272 * @param className The name of the C++ class, as a raw token,
2273 * <i>not</i> a string or macro.
2274 * @param index The numeric index of the vtable slot,
2275 * as a raw constant, beginning from 0.
2278 * This macro evaluates to nothing, and is used to document reserved
2279 * vtable slots as they are filled.
2281 * <code>@link OSMetaClassDefineReservedUnused
2282 * OSMetaClassDefineReservedUnused@/link</code>.
2284 #define OSMetaClassDefineReservedUsed(className, index)
2286 // I/O Kit debug internal routines.
2287 static void printInstanceCounts();
2288 static void serializeClassDictionary(OSDictionary
* dict
);
2289 #ifdef XNU_KERNEL_PRIVATE
2292 static void * trackedNew(size_t size
);
2293 static void trackedDelete(void * mem
, size_t size
);
2294 void trackedInstance(OSObject
* instance
) const;
2295 void trackedFree(OSObject
* instance
) const;
2296 void trackedAccumSize(OSObject
* instance
, size_t size
) const;
2297 struct IOTrackingQueue
* getTracking() const;
2298 #endif /* IOTRACKING */
2299 #endif /* XNU_KERNEL_PRIVATE */
2303 static OSDictionary
* getClassDictionary();
2304 virtual bool serialize(OSSerialize
* serializer
) const;
2306 // Virtual Padding functions for MetaClass's
2307 OSMetaClassDeclareReservedUnused(OSMetaClass
, 0);
2308 OSMetaClassDeclareReservedUnused(OSMetaClass
, 1);
2309 OSMetaClassDeclareReservedUnused(OSMetaClass
, 2);
2310 OSMetaClassDeclareReservedUnused(OSMetaClass
, 3);
2311 OSMetaClassDeclareReservedUnused(OSMetaClass
, 4);
2312 OSMetaClassDeclareReservedUnused(OSMetaClass
, 5);
2313 OSMetaClassDeclareReservedUnused(OSMetaClass
, 6);
2314 OSMetaClassDeclareReservedUnused(OSMetaClass
, 7);
2317 #endif /* !_LIBKERN_OSMETACLASS_H */