]> git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/c++/OSMetaClass.h
03da0e6c29a9975fc2fe06f23f753bcb34d70719
[apple/xnu.git] / libkern / libkern / c++ / OSMetaClass.h
1 /*
2 * Copyright (c) 2000-2019 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 #ifndef _LIBKERN_OSMETACLASS_H
29 #define _LIBKERN_OSMETACLASS_H
30
31 #include <sys/types.h>
32
33 #include <libkern/OSReturn.h>
34 #include <kern/debug.h>
35 #include <ptrauth.h>
36
37 /*
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.
43 */
44
45 /*
46 * By default, methods returning OSObjects are assumed to have the following
47 * owneship semantics:
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
51 * returned object.
52 *
53 * - All other methods are assumed to return at "+1", and the caller is
54 * responsible for releasing the returned object.
55 *
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".
61 *
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
69 * (kIOReturnSuccess).
70 * This can be customized using the attributes
71 * LIBKERN_RETURNS_RETAINED_ON_ZERO and LIBKERN_RETURNS_RETAINED_ON_NONZERO.
72 */
73 #if __has_attribute(os_returns_retained)
74 #define LIBKERN_RETURNS_RETAINED __attribute__((os_returns_retained))
75 #else
76 #define LIBKERN_RETURNS_RETAINED
77 #endif
78 #if __has_attribute(os_returns_not_retained)
79 #define LIBKERN_RETURNS_NOT_RETAINED __attribute__((os_returns_not_retained))
80 #else
81 #define LIBKERN_RETURNS_NOT_RETAINED
82 #endif
83
84 /*
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.
88 */
89 #if __has_attribute(os_consumed)
90 #define LIBKERN_CONSUMED __attribute__((os_consumed))
91 #else
92 #define LIBKERN_CONSUMED
93 #endif
94
95 /*
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).
99 */
100 #if __has_attribute(os_consumes_this)
101 #define LIBKERN_CONSUMES_THIS __attribute__((os_consumes_this))
102 #else
103 #define LIBKERN_CONSUMES_THIS
104 #endif
105
106 /*
107 * LIBKERN_RETURNS_RETAINED_ON_ZERO is an attribute applicable to out
108 * parameters.
109 * It specifies that an out parameter at +1 is written into an argument iff
110 * the function returns a zero return value.
111 */
112 #if __has_attribute(os_returns_retained_on_zero)
113 #define LIBKERN_RETURNS_RETAINED_ON_ZERO __attribute__((os_returns_retained_on_zero))
114 #else
115 #define LIBKERN_RETURNS_RETAINED_ON_ZERO
116 #endif
117
118 /*
119 * LIBKERN_RETURNS_RETAINED_ON_NON_ZERO is an attribute applicable to out
120 * parameters.
121 * It specifies that an out parameter at +1 is written into an argument iff
122 * the function returns a non-zero return value.
123 */
124 #if __has_attribute(os_returns_retained_on_non_zero)
125 #define LIBKERN_RETURNS_RETAINED_ON_NONZERO __attribute__((os_returns_retained_on_non_zero))
126 #else
127 #define LIBKERN_RETURNS_RETAINED_ON_NONZERO
128 #endif
129
130 class OSMetaClass;
131 class OSObject;
132 class OSString;
133 class OSSymbol;
134 class OSDictionary;
135 class OSSerialize;
136 #ifdef XNU_KERNEL_PRIVATE
137 class OSOrderedSet;
138 class OSCollection;
139 #endif /* XNU_KERNEL_PRIVATE */
140 struct IORPC;
141 class OSInterface
142 {
143 };
144
145 /*!
146 * @header
147 *
148 * @abstract
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.
152 */
153
154
155 /*! @parseOnly */
156 #define APPLE_KEXT_COMPATIBILITY
157
158 #ifdef XNU_KERNEL_PRIVATE
159
160 #ifdef CONFIG_EMBEDDED
161 #define APPLE_KEXT_VTABLE_PADDING 0
162 #else /* CONFIG_EMBEDDED */
163 /*! @parseOnly */
164 #define APPLE_KEXT_VTABLE_PADDING 1
165 #endif /* CONFIG_EMBEDDED */
166
167 #else /* XNU_KERNEL_PRIVATE */
168 #include <TargetConditionals.h>
169
170 #if (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)
171 #define APPLE_KEXT_VTABLE_PADDING 0
172 #else /* (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR) */
173 /*! @parseOnly */
174 #define APPLE_KEXT_VTABLE_PADDING 1
175 #endif /* (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR) */
176
177 #endif /* XNU_KERNEL_PRIVATE */
178
179 #define APPLE_KEXT_ALIGN_CONTAINERS (0 == APPLE_KEXT_VTABLE_PADDING)
180
181 #if defined(__LP64__)
182 /*! @parseOnly */
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
186 #else
187 #define APPLE_KEXT_LEGACY_ABI 1
188 #endif
189
190 #if defined(__LP64__)
191 /*! @parseOnly */
192 #define APPLE_KEXT_COMPATIBILITY_VIRTUAL
193 #else
194 // private method made virtual only for binary compatibility
195 #define APPLE_KEXT_COMPATIBILITY_VIRTUAL virtual
196 #endif
197
198 /*! @parseOnly */
199 #define APPLE_KEXT_DEPRECATED __attribute__((deprecated))
200
201
202 /*
203 * <rdar://problem/44872498> AppleUSBAudio builds xnu's libkern headers in user space
204 */
205 #if !defined(BUILD_FOR_USERSPACE) && (__cplusplus >= 201103L)
206 #define APPLE_KEXT_OVERRIDE override
207 #if defined(__LP64__)
208 #define APPLE_KEXT_COMPATIBILITY_OVERRIDE
209 #else
210 #define APPLE_KEXT_COMPATIBILITY_OVERRIDE APPLE_KEXT_OVERRIDE
211 #endif
212 #else
213 #define APPLE_KEXT_OVERRIDE
214 #define APPLE_KEXT_COMPATIBILITY_OVERRIDE
215 #endif
216
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\"")
220
221 #define APPLE_KEXT_WSHADOW_POP _Pragma("clang diagnostic pop")
222
223
224 /*!
225 * @class OSMetaClassBase
226 *
227 * @abstract
228 * OSMetaClassBase is the abstract bootstrap class
229 * for the Libkern and I/O Kit run-time type information system.
230 *
231 * @discussion
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.
242 *
243 * For more information, see
244 * <i>@link //apple_ref/doc/uid/TP40002799
245 * I/O Kit Device Driver Design Guidelines@/link</i>.
246 *
247 * <b>Use by Kernel Extensions</b>
248 *
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:
252 * <ul>
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>
260 * </ul>
261 *
262 * See @link //apple_ref/doc/class/OSMetaClass OSMetaClass@/link
263 * for more run-time type information interfaces.
264 *
265 * <b>Use Restrictions</b>
266 *
267 * OSMetaClassBase should not be subclassed by kernel extensions,
268 * nor should kernel extensions call its run-time type functions directly.
269 *
270 * The run-time type functions and macros are <b>not safe</b>
271 * to call in a primary interrupt context.
272 *
273 * <b>Concurrency Protection</b>
274 *
275 * The run-time type macros and functions of OSMetaClassBase are thread-safe.
276 */
277
278 class OSMetaClassBase
279 {
280 public:
281
282
283 /*!
284 * @define OSTypeAlloc
285 * @hidecontents
286 *
287 * @abstract
288 * Allocates an instance of the named object class.
289 *
290 * @param type The name of the desired class to be created,
291 * as a raw token, <i>not</i> a string or macro.
292 *
293 * @result
294 * A pointer to the new, uninitialized object on success;
295 * <code>NULL</code> on failure.
296 *
297 * @discussion
298 * See also
299 * <code>@link
300 * //apple_ref/cpp/clm/OSMetaClass/allocClassWithName/staticOSObject*\/(constchar*)
301 * OSMetaClass::allocClassWithName(const char *)@/link</code>
302 * and
303 * <code>@link
304 * //apple_ref/cpp/instm/OSMetaClass/alloc/virtualOSObject*\/()
305 * OSMetaClass::alloc@/link</code>.
306 *
307 * The OSTypeAlloc macro is used to avoid binary compatibility difficulties
308 * presented by the C++ <code>new</code> operator.
309 */
310 #define OSTypeAlloc(type) ((type *) ((type::metaClass)->alloc()))
311
312
313 /*!
314 * @define OSTypeID
315 * @hidecontents
316 *
317 * @abstract
318 * Returns the type ID (metaclass) of a class based on its name.
319 *
320 * @param type The name of the desired class, as a raw token,
321 * <i>not</i> a string or macro.
322 *
323 * @result
324 * The unique type ID (metaclass) for the class.
325 *
326 * @discussion
327 * It is typically more useful to determine whether a class is derived
328 * from another; see
329 * <code>@link //apple_ref/cpp/macro/OSDynamicCast OSDynamicCast@/link</code>
330 * and
331 * <code>@link //apple_ref/cpp/macro/OSCheckTypeInst OSCheckTypeInst@/link</code>.
332 */
333 #define OSTypeID(type) (type::metaClass)
334 #define OSMTypeID(type) ((OSMetaClass *) type::metaClass)
335
336
337 /*!
338 * @define OSTypeIDInst
339 * @hidecontents
340 *
341 * @abstract
342 * Returns the type ID (metaclass) for the class of an object instance.
343 *
344 * @param typeinst An instance of an OSObject subclass.
345 *
346 * @result
347 * The type ID of that object's class; that is, its metaclass.
348 *
349 * @discussion
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>
353 * and
354 * <code>@link //apple_ref/cpp/macro/OSCheckTypeInst OSCheckTypeInst@/link</code>.
355 */
356 #define OSTypeIDInst(typeinst) ((typeinst)->getMetaClass())
357
358
359 /*!
360 * @define OSDynamicCast
361 * @hidecontents
362 *
363 * @abstract
364 * Safe type-casting for Libkern C++ objects.
365 *
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>.
375 *
376 * @result
377 * <code>inst</code> if it is non-<code>NULL</code>
378 * and derived from <code>type</code>;
379 * otherwise <code>NULL</code>.
380 *
381 * @discussion
382 * <code>OSDynamicCast</code> is a rough equivalent
383 * to the standard C++ RTTI <code>dynamic_cast&lt;T&gt;</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.
391 */
392 #define OSDynamicCast(type, inst) \
393 ((type *) OSMetaClassBase::safeMetaCast((inst), OSTypeID(type)))
394
395 /*!
396 * @define OSRequiredCast
397 * @hidecontents
398 *
399 * @abstract
400 * Safe type-casting for Libkern C++ objects; panics on failure.
401 * The input parameters are the same as for the {@code OSDynamicCast} macro.
402 *
403 * @result {@code inst} if it is NULL or derived from {@code type};
404 * otherwise triggers a kernel panic.
405 *
406 * @discussion
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.
413 */
414 #define OSRequiredCast(type, inst) \
415 (type *) OSMetaClassBase::requiredMetaCast((inst), OSTypeID(type))
416
417 /*!
418 * @define OSCheckTypeInst
419 * @hidecontents
420 *
421 * @abstract
422 * Checks whether two objects are type-compatible.
423 *
424 * @param typeinst The reference object.
425 * @param inst The object to check for type compatibility.
426 *
427 * @result
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>.
432 */
433 #define OSCheckTypeInst(typeinst, inst) \
434 OSMetaClassBase::checkTypeInst(inst, typeinst)
435
436 #define OSSafeRelease(inst) \
437 do { int OSSafeRelease __attribute__ ((deprecated("Use OSSafeReleaseNULL"))); (OSSafeRelease); \
438 if (inst) (inst)->release(); } while (0)
439
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>.
443 */
444 #define OSSafeReleaseNULL(inst) do { if (inst != NULL) (inst)->release(); (inst) = NULL; } while (0)
445
446 typedef void (*_ptf_t)(void);
447
448 #if defined(__arm__) || defined(__arm64__)
449
450 static _ptf_t _ptmf2ptf(const OSMetaClassBase * self, void (OSMetaClassBase::*func)(void), uintptr_t typeDisc);
451
452 #elif defined(__i386__) || defined(__x86_64__)
453
454 // Slightly less arcane and slightly less evil code to do
455 // the same for kexts compiled with the standard Itanium C++
456 // ABI
457
458 static inline _ptf_t
459 _ptmf2ptf(const OSMetaClassBase *self, void (OSMetaClassBase::*func)(void),
460 uintptr_t typeDisc __attribute__((unused)))
461 {
462 union {
463 void (OSMetaClassBase::*fIn)(void);
464 uintptr_t fVTOffset;
465 _ptf_t fPFN;
466 } map;
467
468 map.fIn = func;
469
470 if (map.fVTOffset & 1) {
471 // virtual
472 union {
473 const OSMetaClassBase *fObj;
474 _ptf_t **vtablep;
475 } u;
476 u.fObj = self;
477
478 // Virtual member function so dereference vtable
479 return *(_ptf_t *)(((uintptr_t)*u.vtablep) + map.fVTOffset - 1);
480 } else {
481 // Not virtual, i.e. plain member func
482 return map.fPFN;
483 }
484 }
485
486 #else
487 #error Unknown architecture.
488 #endif /* __arm__ */
489
490
491 /*!
492 * @define OSMemberFunctionCast
493 * @hidecontents
494 *
495 * @abstract
496 * Converts a C++ member function pointer, relative to an instance,
497 * to a C-style pointer to function.
498 *
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
502 * you wish to cache.
503 * @param func The pointer to the member function itself,
504 * something like <code>&Class::function</code>.
505 *
506 * @result
507 * A pointer to a function of the given type referencing <code>self</code>.
508 *
509 * @discussion
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.
512 *
513 * No warnings are generated.
514 *
515 * This function will panic if an attempt is made to call it
516 * with a multiply-inheriting class.
517 */
518 #define OSMemberFunctionCast(cptrtype, self, func) \
519 (cptrtype) OSMetaClassBase:: \
520 _ptmf2ptf(self, (void (OSMetaClassBase::*)(void)) func, \
521 ptrauth_type_discriminator(__typeof__(func)))
522
523 protected:
524 OSMetaClassBase();
525 virtual
526 ~OSMetaClassBase();
527
528 private:
529 // Disable copy constructors of OSMetaClassBase based objects
530 /* Not to be included in headerdoc.
531 *
532 * @function operator =
533 *
534 * @abstract
535 * Disable implicit copy constructor by making private
536 *
537 * @param src Reference to source object that isn't allowed to be copied.
538 */
539 void operator =(OSMetaClassBase &src);
540
541 /* Not to be included in headerdoc.
542 *
543 * @function OSMetaClassBase
544 *
545 * @abstract
546 * Disable implicit copy constructor by making private
547 *
548 * @param src Reference to source object that isn't allowed to be copied.
549 */
550 OSMetaClassBase(OSMetaClassBase &src);
551
552 public:
553
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...?
556
557 /*!
558 * @function release
559 *
560 * @abstract
561 * Abstract declaration of
562 * <code>@link
563 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/(int)
564 * release(int freeWhen)@/link</code>.
565 *
566 * @discussion
567 * See
568 * <code>@link
569 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/(int)
570 * release(int freeWhen)@/link</code>.
571 */
572 virtual void release(int freeWhen) const = 0;
573
574
575 /*!
576 * @function getRetainCount
577 *
578 * @abstract
579 * Abstract declaration of
580 * <code>@link
581 * //apple_ref/cpp/instm/OSObject/getRetainCount/virtualint/()
582 * getRetainCount()@/link</code>.
583 *
584 * @discussion
585 * See
586 * <code>@link
587 * //apple_ref/cpp/instm/OSObject/getRetainCount/virtualint/()
588 * OSObject::getRetainCount()@/link</code>.
589 */
590 virtual int getRetainCount() const = 0;
591
592
593 /*!
594 * @function retain
595 *
596 * @abstract
597 * Abstract declaration of
598 * <code>@link
599 * //apple_ref/cpp/instm/OSObject/retain/virtualvoid/()
600 * retain()@/link</code>.
601 *
602 * @discussion
603 * See
604 * <code>@link
605 * //apple_ref/cpp/instm/OSObject/retain/virtualvoid/()
606 * OSObject::retain()@/link</code>.
607 */
608 virtual void retain() const = 0;
609
610
611 /*!
612 * @function release
613 *
614 * @abstract
615 * Abstract declaration of
616 * <code>@link
617 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/()
618 * release@/link</code>.
619 *
620 * @discussion
621 * See
622 * <code>@link
623 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/()
624 * OSObject::release@/link</code>.
625 */
626 virtual void release() const = 0;
627
628
629 /*!
630 * @function serialize
631 *
632 * @abstract
633 * Abstract declaration of
634 * <code>@link
635 * //apple_ref/cpp/instm/OSObject/serialize/virtualbool/(OSSerialize*)
636 * serialize@/link</code>.
637 *
638 * @discussion
639 * See
640 * <code>@link
641 * //apple_ref/cpp/instm/OSObject/serialize/virtualbool/(OSSerialize*)
642 * OSObject::serialize@/link</code>.
643 */
644 virtual bool serialize(OSSerialize * serializer) const = 0;
645
646
647 /*!
648 * @function getMetaClass
649 *
650 * @abstract
651 * Returns the OSMetaClass representing
652 * an OSMetaClassBase subclass.
653 *
654 * @discussion
655 * OSObject overrides this abstract member function
656 * to return the OSMetaClass object that represents
657 * each class for run-time typing.
658 */
659 virtual const OSMetaClass * getMetaClass() const = 0;
660
661
662 /*!
663 * @function isEqualTo
664 *
665 * @abstract
666 * Checks whether another object is equal to the receiver.
667 *
668 * @param anObject The object to copmare to the receiver.
669 *
670 * @result
671 * <code>true</code> if the objects are equal, <code>false</code> otherwise.
672 *
673 * @discussion
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.
681 */
682 virtual bool isEqualTo(const OSMetaClassBase * anObject) const;
683
684
685 /*!
686 * @function metaCast
687 *
688 * @abstract
689 * Casts this object is to the class managed by the given OSMetaClass.
690 *
691 * @param toMeta A pointer to a constant OSMetaClass
692 * for the desired target type.
693 *
694 * @result
695 * <code>this</code> if the object is derived
696 * from the class managed by <code>toMeta</code>,
697 * otherwise <code>NULL</code>.
698 *
699 * @discussion
700 * It is far more convenient to use
701 * <code>@link OSDynamicCast OSDynamicCast@/link</code>.
702 */
703 OSMetaClassBase * metaCast(const OSMetaClass * toMeta) const;
704
705
706 /*!
707 * @function metaCast
708 *
709 * @abstract
710 * Casts this object is to the class managed by the named OSMetaClass.
711 *
712 * @param toMeta An OSSymbol naming the desired target type.
713 *
714 * @result
715 * <code>this</code> if the object is derived
716 * from the class named by <code>toMeta</code>,
717 * otherwise <code>NULL</code>.
718 *
719 * @discussion
720 * It is far more convenient to use
721 * <code>@link OSDynamicCast OSDynamicCast@/link</code>.
722 */
723 OSMetaClassBase * metaCast(const OSSymbol * toMeta) const;
724
725
726 /*!
727 * @function metaCast
728 *
729 * @abstract
730 * Casts this object is to the class managed by the named OSMetaClass.
731 *
732 * @param toMeta An OSString naming the desired target type.
733 * @result
734 * <code>this</code> if the object is derived
735 * from the class named by <code>toMeta</code>,
736 * otherwise <code>NULL</code>.
737 *
738 * @discussion
739 * It is far more convenient to use
740 * <code>@link OSDynamicCast OSDynamicCast@/link</code>.
741 */
742 OSMetaClassBase * metaCast(const OSString * toMeta) const;
743
744
745 /*!
746 * @function metaCast
747 *
748 * @abstract
749 * Casts this object is to the class managed by the named OSMetaClass.
750 *
751 * @param toMeta A C string naming the desired target type.
752 * @result
753 * <code>this</code> if the object is derived
754 * from the class named by <code>toMeta</code>,
755 * otherwise <code>NULL</code>.
756 *
757 * @discussion
758 * It is far more convenient to use
759 * <code>@link OSDynamicCast OSDynamicCast@/link</code>.
760 */
761 OSMetaClassBase * metaCast(const char * toMeta) const;
762
763 // Helper inlines for run-time type preprocessor macros
764 /*!
765 * @function safeMetaCast
766 *
767 * @abstract
768 * Casts an object is to the class managed by the given OSMetaClass.
769 *
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.
773 *
774 * @result
775 * <code>anObject</code> if the object is derived
776 * from the class managed by <code>toMeta</code>,
777 * otherwise <code>NULL</code>.
778 *
779 * @discussion
780 * It is far more convenient to use
781 * <code>@link OSDynamicCast OSDynamicCast@/link</code>.
782 */
783 static OSMetaClassBase * safeMetaCast(
784 const OSMetaClassBase * anObject,
785 const OSMetaClass * toMeta);
786
787 /*!
788 * @function requiredMetaCast
789 *
790 * @abstract
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.
793 *
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.
797 *
798 * @result
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.
803 *
804 * @discussion
805 * It is far more convenient to use
806 * <code>@link OSRequiredCast OSRequiredCast@/link</code>.
807 */
808 static OSMetaClassBase *requiredMetaCast(
809 const OSMetaClassBase * anObject,
810 const OSMetaClass * toMeta);
811
812 /*!
813 * @function checkTypeInst
814 *
815 * @abstract
816 * Checks whether an object instance is of the same class
817 * as another object instance (or a subclass of that class).
818 *
819 * @param inst A pointer to the object to check.
820 * @param typeinst A pointer to an object of the class being checked.
821 *
822 * @result
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>.
827 *
828 * @discussion
829 * It is far more convenient to use
830 * <code>@link OSCheckTypeInst OSCheckTypeInst@/link</code>.
831 */
832 static bool checkTypeInst(
833 const OSMetaClassBase * inst,
834 const OSMetaClassBase * typeinst);
835
836 static void initialize(void);
837
838 public:
839
840 /*!
841 * @function taggedRetain
842 *
843 * @abstract
844 * Abstract declaration of
845 * <code>@link
846 * //apple_ref/cpp/instm/OSObject/taggedRetain/virtualvoid/(constvoid*)
847 * taggedRetain(const void *)@/link</code>.
848 *
849 * @discussion
850 * See
851 * <code>@link
852 * //apple_ref/cpp/instm/OSObject/taggedRetain/virtualvoid/(constvoid*)
853 * OSObject::taggedRetain(const void *)@/link</code>.
854 */
855 // WAS: virtual void _RESERVEDOSMetaClassBase0();
856 virtual void taggedRetain(const void * tag = NULL) const = 0;
857
858
859 /*!
860 * @function taggedRelease
861 *
862 * @abstract
863 * Abstract declaration of
864 * <code>@link
865 * //apple_ref/cpp/instm/OSObject/taggedRelease/virtualvoid/(constvoid*)
866 * taggedRelease(const void *)@/link</code>.
867 *
868 * @discussion
869 * See
870 * <code>@link
871 * //apple_ref/cpp/instm/OSObject/taggedRelease/virtualvoid/(constvoid*)
872 * OSObject::taggedRelease(const void *)@/link</code>.
873 */
874 // WAS: virtual void _RESERVEDOSMetaClassBase1();
875 virtual void taggedRelease(const void * tag = NULL) const = 0;
876
877 protected:
878 /*!
879 * @function taggedRelease
880 *
881 * @abstract
882 * Abstract declaration of
883 * <code>@link
884 * //apple_ref/cpp/instm/OSObject/taggedRelease/virtualvoid/(constvoid*,constint)
885 * taggedRelease(const void *, const int freeWhen)@/link</code>.
886 *
887 * @discussion
888 * See
889 * <code>@link
890 * //apple_ref/cpp/instm/OSObject/taggedRelease/virtualvoid/(constvoid*,constint)
891 * OSObject::taggedRelease(const void *, const int freeWhen)@/link</code>.
892 */
893 // WAS: virtual void _RESERVEDOSMetaClassBase2();
894 virtual void taggedRelease(
895 const void * tag,
896 const int freeWhen) const = 0;
897
898 public:
899 virtual kern_return_t
900 Dispatch(const IORPC rpc);
901
902 kern_return_t
903 Invoke(const IORPC rpc);
904
905 private:
906 #if APPLE_KEXT_VTABLE_PADDING
907 // Virtual Padding
908 virtual void _RESERVEDOSMetaClassBase4();
909 virtual void _RESERVEDOSMetaClassBase5();
910 virtual void _RESERVEDOSMetaClassBase6();
911 virtual void _RESERVEDOSMetaClassBase7();
912 #endif
913 } APPLE_KEXT_COMPATIBILITY;
914
915
916 #ifdef XNU_KERNEL_PRIVATE
917 typedef bool (*OSMetaClassInstanceApplierFunction)(const OSObject * instance,
918 void * context);
919 #endif /* XNU_KERNEL_PRIVATE */
920
921 /*!
922 * @class OSMetaClass
923 *
924 * @abstract
925 * OSMetaClass manages run-time type information
926 * for Libkern and I/O Kit C++ classes.
927 *
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.
936 *
937 * <b>Use by Kernel Extensions</b>
938 *
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:
944 * <ul>
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>
951 * </ul>
952 *
953 * The class implementation should then use one of these macros:
954 * <ul>
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>
964 * </ul>
965 *
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:
969 * <ul>
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>
982 * </ul>
983 *
984 * <b>Use Restrictions</b>
985 *
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.
989 *
990 * OSMetaClass functions should be considered
991 * <b>unsafe</b> to call in a primary interrupt context.
992 *
993 * <b>Concurrency Protection</b>
994 *
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.
1001 */
1002 class OSMetaClass : public OSMetaClassBase
1003 {
1004 friend class OSKext;
1005 #if IOKITSTATS
1006 friend class IOStatistics;
1007 #endif
1008
1009 private:
1010 // Can never be allocated must be created at compile time
1011 static void * operator new(size_t size);
1012
1013 /* Reserved for future use. (Internal use only) */
1014 struct ExpansionData *reserved;
1015
1016 /* superClass Handle to the superclass's meta class. */
1017 const OSMetaClass *superClassLink;
1018
1019 /* className OSSymbol of the class' name. */
1020 const OSSymbol *className;
1021
1022 /* classSize How big is a single instance of this class. */
1023 unsigned int classSize;
1024
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.
1028 */
1029 mutable unsigned int instanceCount;
1030
1031 /* Not to be included in headerdoc.
1032 *
1033 * @function OSMetaClass
1034 *
1035 * @abstract
1036 * The default private constructor.
1037 */
1038 OSMetaClass();
1039
1040 // Called by postModLoad
1041 /* Not to be included in headerdoc.
1042 *
1043 * @function logError
1044 *
1045 * @abstract
1046 * Logs an error string for an <code>OSReturn</code> value
1047 * using <code>printf</code>.
1048 *
1049 * @param result The <code>OSReturn</code> value for which to log a message.
1050 *
1051 * @discussion
1052 * This function is used to log errors loading kernel extensions.
1053 * Kernel extensions themselves should not call it.
1054 */
1055 static void logError(OSReturn result);
1056
1057 public:
1058
1059 /*!
1060 * @function getMetaClassWithName
1061 *
1062 * @abstract
1063 * Look up a metaclass in the run-time type information system.
1064 *
1065 * @param name The name of the desired class's metaclass.
1066 *
1067 * @result
1068 * A pointer to the metaclass object if found, <code>NULL</code> otherwise.
1069 */
1070 static const OSMetaClass * getMetaClassWithName(const OSSymbol * name);
1071
1072 #if XNU_KERNEL_PRIVATE
1073
1074 /*!
1075 * @function copyMetaClassWithName
1076 *
1077 * @abstract
1078 * Look up a metaclass in the run-time type information system.
1079 *
1080 * @param name The name of the desired class's metaclass.
1081 *
1082 * @result
1083 * A pointer to the metaclass object if found, <code>NULL</code> otherwise.
1084 * The metaclass will be protected from unloading until releaseMetaClass()
1085 * is called.
1086 */
1087 static const OSMetaClass * copyMetaClassWithName(const OSSymbol * name);
1088 /*!
1089 * @function releaseMetaClass
1090 *
1091 * @abstract
1092 * Releases reference obtained from copyMetaClassWithName().
1093 *
1094 * @discussion
1095 * The metaclass will be protected from unloading until releaseMetaClass()
1096 * is called.
1097 */
1098 void releaseMetaClass() const;
1099
1100 #endif /* XNU_KERNEL_PRIVATE */
1101
1102 protected:
1103 /*!
1104 * @function retain
1105 *
1106 * @abstract
1107 * Implements the abstract <code>retain</code> function to do nothing.
1108 *
1109 * @discussion
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.
1113 */
1114 virtual void retain() const;
1115
1116
1117 /*!
1118 * @function release
1119 *
1120 * @abstract
1121 * Implements the abstract <code>release</code> function to do nothing.
1122 *
1123 * @discussion
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.
1127 */
1128 virtual void release() const;
1129
1130
1131 /*!
1132 * @function release
1133 *
1134 * @abstract
1135 * Implements the abstract <code>release(int freeWhen)</code>
1136 * function to do nothing.
1137 *
1138 * @param freeWhen Unused.
1139 *
1140 * @discussion
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.
1144 */
1145 virtual void release(int freeWhen) const;
1146
1147
1148 /*!
1149 * @function taggedRetain
1150 *
1151 * @abstract
1152 * Implements the abstract <code>taggedRetain(const void *)</code>
1153 * function to do nothing.
1154 *
1155 * @param tag Unused.
1156 *
1157 * @discussion
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.
1161 */
1162 virtual void taggedRetain(const void * tag = NULL) const;
1163
1164
1165 /*!
1166 * @function taggedRelease
1167 *
1168 * @abstract
1169 * Implements the abstract <code>taggedRelease(const void *)</code>
1170 * function to do nothing.
1171 *
1172 * @param tag Unused.
1173 *
1174 * @discussion
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.
1178 */
1179 virtual void taggedRelease(const void * tag = NULL) const;
1180
1181
1182 /*!
1183 * @function taggedRelease
1184 *
1185 * @abstract
1186 * Implements the abstract <code>taggedRelease(const void *, cont int)</code>
1187 * function to do nothing.
1188 *
1189 * @param tag Unused.
1190 * @param freeWhen Unused.
1191 *
1192 * @discussion
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.
1196 */
1197 virtual void taggedRelease(
1198 const void * tag,
1199 const int freeWhen) const;
1200
1201
1202 /*!
1203 * @function getRetainCount
1204 *
1205 * @abstract
1206 * Implements the abstract <code>getRetainCount</code>
1207 * function to return 0.
1208 *
1209 * @result
1210 * Always returns 0.
1211 *
1212 * @discussion
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.
1216 */
1217 virtual int getRetainCount() const;
1218
1219
1220 /* Not to be included in headerdoc.
1221 *
1222 * @function getMetaClass
1223 *
1224 * @abstract
1225 * Returns the meta-metaclass.
1226 *
1227 * @result
1228 * The metaclass of the OSMetaClass object.
1229 */
1230 virtual const OSMetaClass * getMetaClass() const;
1231
1232
1233 /*!
1234 * @function OSMetaClass
1235 *
1236 * @abstract
1237 * Constructor for OSMetaClass objects.
1238 *
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.
1244 *
1245 * @discussion
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>.
1252 */
1253 OSMetaClass(const char * className,
1254 const OSMetaClass * superclass,
1255 unsigned int classSize);
1256
1257
1258 /*!
1259 * @function ~OSMetaClass
1260 *
1261 * @abstract
1262 * Destructor for OSMetaClass objects.
1263 *
1264 * @discussion
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.
1269 */
1270 virtual
1271 ~OSMetaClass();
1272
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.
1275 void
1276 operator delete(void *, size_t)
1277 {
1278 }
1279
1280 public:
1281 static const OSMetaClass * const metaClass;
1282
1283 /*!
1284 * @function preModLoad
1285 *
1286 * @abstract
1287 * Prepares the run-time type system
1288 * for the creation of new metaclasses
1289 * during loading of a kernel extension (module).
1290 *
1291 * @param kextID The bundle ID of the kext being loaded.
1292 *
1293 * @result
1294 * An opaque handle to the load context
1295 * for the kernel extension on success;
1296 * <code>NULL</code> on failure.
1297 *
1298 * @discussion
1299 * <i>Not for use by kernel extensions.</i>
1300 *
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>.
1309 */
1310 static void * preModLoad(const char * kextID);
1311
1312
1313 /*!
1314 * @function checkModLoad
1315 *
1316 * @abstract
1317 * Checks whether the current kext load operation can proceed.
1318 *
1319 * @param loadHandle The opaque handle returned
1320 * by <code>@link preModLoad preModLoad@/link</code>.
1321 * @result
1322 * <code>true</code> if no errors are outstanding
1323 * and the system is ready to process more metaclasses.
1324 *
1325 * @discussion
1326 * <i>Not for use by kernel extensions.</i>
1327 */
1328 static bool checkModLoad(void * loadHandle);
1329
1330
1331 /*!
1332 * @function postModLoad
1333 *
1334 * @abstract
1335 * Registers the metaclasses created during loading of a kernel extension.
1336 *
1337 * @param loadHandle The opaque handle returned
1338 * by <code>@link preModLoad preModLoad@/link</code>.
1339 * @result
1340 * The error code of the first error encountered,
1341 * or
1342 * <code>@link
1343 * //apple_ref/cpp/macro/kOSReturnSuccess
1344 * kOSReturnSuccess@/link</code>
1345 * if no error occurred.
1346 *
1347 * @discussion
1348 * <i>Not for use by kernel extensions.</i>
1349 *
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>.
1359 */
1360 static OSReturn postModLoad(void * loadHandle);
1361
1362 /*!
1363 * @function modHasInstance
1364 *
1365 * @abstract
1366 * Returns whether any classes defined by the named
1367 * kernel extension (or their subclasses) have existing instances.
1368 *
1369 * @param kextID The bundle ID of the kernel extension to check.
1370 *
1371 * @result
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.
1376 *
1377 * @discussion
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.
1384 */
1385 static bool modHasInstance(const char * kextID);
1386
1387
1388 /*!
1389 * @function reportModInstances
1390 *
1391 * @abstract
1392 * Logs the instance counts for classes
1393 * defined by a kernel extension.
1394 *
1395 * @param kextID The bundle ID of the kernel extension to report on.
1396 *
1397 * @discussion
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.
1403 */
1404 static void reportModInstances(const char * kextID);
1405
1406
1407 /*!
1408 * @function considerUnloads
1409 *
1410 * @abstract
1411 * Schedule automatic unloading of unused kernel extensions.
1412 *
1413 * @discussion
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.
1419 *
1420 * The I/O Kit calls this function when matching goes idle.
1421 *
1422 * Kernel extensions that define subclasses of
1423 * @link //apple_ref/doc/class/IOService IOService@/link
1424 * are eligible for automatic unloading.
1425 *
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.)
1433 */
1434 static void considerUnloads();
1435
1436 #if XNU_KERNEL_PRIVATE
1437 static bool removeClasses(OSCollection * metaClasses);
1438 #endif /* XNU_KERNEL_PRIVATE */
1439
1440 /*!
1441 * @function allocClassWithName
1442 *
1443 * @abstract
1444 * Allocates an instance of a named OSObject-derived class.
1445 *
1446 * @param name The name of the desired class.
1447 *
1448 * @result
1449 * A pointer to the newly-allocated, uninitialized object on success;
1450 * <code>NULL</code> on failure.
1451 *
1452 * @discussion
1453 * Kernel extensions should not need to use this function
1454 * directly, instead using static instance-creation functions
1455 * defined by classes.
1456 *
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.
1461 */
1462 static OSObject * allocClassWithName(const OSSymbol * name);
1463
1464
1465 /*!
1466 * function allocClassWithName
1467 *
1468 * @abstract
1469 * Allocates an instance of a named OSObject-derived class.
1470 *
1471 * @param name The name of the desired class.
1472 *
1473 * @result
1474 * A pointer to the newly-allocated, uninitialized object on success;
1475 * <code>NULL</code> on failure.
1476 *
1477 * @discussion
1478 * Kernel extensions should not need to use this function
1479 * directly, instead using static instance-creation functions
1480 * defined by classes.
1481 *
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.
1486 */
1487 static OSObject * allocClassWithName(const OSString * name);
1488
1489
1490 /*!
1491 * function allocClassWithName
1492 *
1493 * @abstract
1494 * Allocates an instance of a named OSObject-derived class.
1495 *
1496 * @param name The name of the desired class.
1497 *
1498 * @result
1499 * A pointer to the newly-allocated, uninitialized object on success;
1500 * <code>NULL</code> on failure.
1501 *
1502 * @discussion
1503 * Kernel extensions should not need to use this function
1504 * directly, instead using static instance-creation functions
1505 * defined by classes.
1506 *
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.
1511 */
1512 static OSObject * allocClassWithName(const char * name);
1513
1514
1515 /*!
1516 * @function checkMetaCastWithName
1517 *
1518 * @abstract
1519 * Search the metaclass inheritance hierarchy by name for an object instance.
1520 *
1521 * @param className The name of the desired class or superclass.
1522 * @param object The object whose metaclass begins the search.
1523 *
1524 * @result
1525 * <code>object</code> if it's derived from <code>className</code>;
1526 * <code>NULL</code> otherwise.
1527 *
1528 * @discussion
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>.
1533 */
1534 static OSMetaClassBase * checkMetaCastWithName(
1535 const OSSymbol * className,
1536 const OSMetaClassBase * object);
1537
1538 /*!
1539 * @function checkMetaCastWithName
1540 *
1541 * @abstract
1542 * Search the metaclass inheritance hierarchy by name for an object instance.
1543 *
1544 * @param className The name of the desired class or superclass.
1545 * @param object The object whose metaclass begins the search.
1546 *
1547 * @result
1548 * <code>object</code> if it's derived from <code>className</code>;
1549 * <code>NULL</code> otherwise.
1550 *
1551 * @discussion
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>.
1555 */
1556 static OSMetaClassBase * checkMetaCastWithName(
1557 const OSString * className,
1558 const OSMetaClassBase * object);
1559
1560 /*!
1561 * @function checkMetaCastWithName
1562 *
1563 * @abstract
1564 * Search the metaclass inheritance hierarchy by name for an object instance.
1565 *
1566 * @param className The name of the desired class or superclass.
1567 * @param object The object whose metaclass begins the search.
1568 *
1569 * @result
1570 * <code>object</code> if it's derived from <code>className</code>;
1571 * <code>NULL</code> otherwise.
1572 *
1573 * @discussion
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>.
1577 */
1578 static OSMetaClassBase * checkMetaCastWithName(
1579 const char * className,
1580 const OSMetaClassBase * object);
1581
1582
1583 /*!
1584 * @function instanceConstructed
1585 *
1586 * @abstract
1587 * Counts the instances of the class managed by this metaclass.
1588 *
1589 * @discussion
1590 * <i>Not for use by kernel extensions.</i>
1591 *
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
1596 * <code>@link
1597 * OSDefineMetaClassAndStructors
1598 * OSDefineMetaClassAndStructors@/link</code>
1599 * macro that all OSObject subclasses must use.
1600 *
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.
1606 */
1607 void instanceConstructed() const;
1608
1609
1610 /*!
1611 * @function instanceDestructed
1612 *
1613 * @abstract
1614 * Counts the instances of the class managed by this metaclass.
1615 *
1616 * @discussion
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.
1623 *
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.
1629 */
1630 void instanceDestructed() const;
1631
1632
1633 /*!
1634 * @function checkMetaCast
1635 *
1636 * @abstract
1637 * Check whether a given object is an instance of the receiving
1638 * metaclass's class or one derived from it.
1639 *
1640 * @param object The object to check for inheritance.
1641 *
1642 * @result
1643 * <code>object</code> if it is derived from the receiver's class,
1644 * <code>NULL</code> if not.
1645 */
1646 OSMetaClassBase * checkMetaCast(const OSMetaClassBase * object) const;
1647
1648
1649 /*!
1650 * @function getInstanceCount
1651 *
1652 * @abstract
1653 * Returns the number of existing instances of the metaclass's class.
1654 *
1655 * @result
1656 * The number of existing instances of the metaclass's class,
1657 * plus 1 for each subclass with any instance.
1658 */
1659 unsigned int getInstanceCount() const;
1660
1661
1662 /*!
1663 * @function getSuperClass
1664 *
1665 * @abstract
1666 * Returns the super-metaclass of the receiver.
1667 *
1668 * @result
1669 * Returns a pointer to the super-metaclass of the receiving
1670 * OSMetaClass, or <code>NULL</code> for OSObject's metaclass.
1671 */
1672 const OSMetaClass * getSuperClass() const;
1673
1674 /*!
1675 * @function getKmodName
1676 *
1677 * @abstract
1678 * Returns the bundle identifier of the kernel extension
1679 * that defines this metaclass.
1680 *
1681 * @result
1682 * The bundle identifier of the kernel extension that defines this metaclass.
1683 *
1684 * @discussion
1685 * "Kmod" is an older term for kernel extension.
1686 */
1687 const OSSymbol * getKmodName() const;
1688
1689
1690 /*!
1691 * @function getClassName
1692 *
1693 * @abstract
1694 * Returns the name of the C++ class managed by this metaclass.
1695 *
1696 * @result
1697 * Returns the name of the C++ class managed by this metaclass.
1698 */
1699 const char * getClassName() const;
1700 const OSSymbol * getClassNameSymbol() const;
1701
1702
1703 /*!
1704 * @function getClassSize
1705 *
1706 * @abstract
1707 * Returns the allocation size of the C++ class managed by this metaclass.
1708 *
1709 * @result
1710 * The allocation size of the C++ class managed by this metaclass.
1711 */
1712 unsigned int getClassSize() const;
1713
1714
1715 /*!
1716 * @function alloc
1717 *
1718 * @abstract
1719 * Allocates an instance of the C++ class managed by this metaclass.
1720 *
1721 * @result
1722 * A pointer to the newly allocated, uninitialized instance,
1723 * with a retain count of 1; <code>NULL</code> on allocation failure.
1724 *
1725 * @discussion
1726 * This function is automatically created by the metaclass-registration macros
1727 * to enable dynamic instance allocation.
1728 */
1729 virtual OSObject * alloc() const = 0;
1730
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,
1739 void * context);
1740 private:
1741 static void applyToInstances(OSOrderedSet * set,
1742 OSMetaClassInstanceApplierFunction applier,
1743 void * context);
1744 public:
1745 #endif /* XNU_KERNEL_PRIVATE */
1746
1747 /* Not to be included in headerdoc.
1748 *
1749 * @define OSDeclareCommonStructors
1750 * @hidecontents
1751 *
1752 * @abstract
1753 * Helper macro for for the standard metaclass-registration macros.
1754 * DO NOT USE.
1755 *
1756 * @param className The name of the C++ class, as a raw token,
1757 * <i>not</i> a string or macro.
1758 */
1759
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;
1764
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
1770
1771 #define SUPERDISPATCH ((OSDispatchMethod)&super::_Dispatch)
1772
1773 #define OSDeclareCommonStructors(className, dispatch) \
1774 private: \
1775 static const OSMetaClass * const superClass; \
1776 public: \
1777 static const OSMetaClass * const metaClass; \
1778 static class MetaClass : public OSMetaClass { \
1779 public: \
1780 MetaClass(); \
1781 virtual OSObject *alloc() const APPLE_KEXT_OVERRIDE;\
1782 _OS_ADD_METAMETHODS(dispatch); \
1783 } gMetaClass; \
1784 friend class className ::MetaClass; \
1785 virtual const OSMetaClass * getMetaClass() const APPLE_KEXT_OVERRIDE; \
1786 protected: \
1787 className (const OSMetaClass *); \
1788 virtual ~ className () APPLE_KEXT_OVERRIDE; \
1789 _OS_ADD_METHODS(className, dispatch)
1790
1791
1792 /*!
1793 * @define OSDeclareDefaultStructors
1794 * @hidecontents
1795 *
1796 * @abstract
1797 * Declares run-time type information and functions
1798 * for a final (non-subclassable) Libkern C++ class.
1799 *
1800 * @param className The name of the C++ class, as a raw token,
1801 * <i>not</i> a string or macro.
1802 *
1803 * @discussion
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>.
1807 */
1808 #define _OSDeclareDefaultStructors(className, dispatch) \
1809 OSDeclareCommonStructors(className, dispatch); \
1810 public: \
1811 className (void); \
1812 protected:
1813
1814
1815 #define OSDeclareDefaultStructors(className) \
1816 _OSDeclareDefaultStructors(className, )
1817
1818 #define OSDeclareDefaultStructorsWithDispatch(className) \
1819 _OSDeclareDefaultStructors(className, dispatch)
1820
1821
1822 /*!
1823 * @define OSDeclareAbstractStructors
1824 * @hidecontents
1825 *
1826 * @abstract
1827 * Declares run-time type information and functions
1828 * for an abstract Libkern C++ class.
1829 *
1830 * @param className The name of the C++ class, as a raw token,
1831 * <i>not</i> a string or macro.
1832 *
1833 * @discussion
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>.
1838 */
1839 #define _OSDeclareAbstractStructors(className, dispatch) \
1840 OSDeclareCommonStructors(className, dispatch); \
1841 private: \
1842 className (void); /* Make primary constructor private in abstract */ \
1843 protected:
1844
1845 #define OSDeclareAbstractStructors(className) \
1846 _OSDeclareAbstractStructors(className, )
1847
1848 #define OSDeclareAbstractStructorsWithDispatch(className) \
1849 _OSDeclareAbstractStructors(className, dispatch)
1850
1851 /*!
1852 * @define OSDeclareFinalStructors
1853 * @hidecontents
1854 *
1855 * @abstract
1856 * Declares run-time type information and functions
1857 * for a concrete Libkern C++ class.
1858 *
1859 * @param className The name of the C++ class, as a raw token,
1860 * <i>not</i> a string or macro.
1861 *
1862 * @discussion
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>.
1868 *
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.
1872 *
1873 * <b>Warning:</b> Changing a class from "Default" to "Final" will break
1874 * binary compatibility.
1875 */
1876 #define _OSDeclareFinalStructors(className, dispatch) \
1877 _OSDeclareDefaultStructors(className, dispatch) \
1878 private: \
1879 void __OSFinalClass(void); \
1880 protected:
1881
1882
1883 #define OSDeclareFinalStructors(className) \
1884 _OSDeclareFinalStructors(className, )
1885
1886 #define OSDeclareFinalStructorsWithDispatch(className) \
1887 _OSDeclareFinalStructors(className, dispatch)
1888
1889
1890 /* Not to be included in headerdoc.
1891 *
1892 * @define OSDefineMetaClassWithInit
1893 * @hidecontents
1894 *
1895 * @abstract
1896 * Helper macro for for the standard metaclass-registration macros.
1897 * DO NOT USE.
1898 *
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,
1902 * as a raw token,
1903 * <i>not</i> a string or macro.
1904 * @param init A function to call in the constructor
1905 * of the class's OSMetaClass.
1906 */
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)) \
1923 { init; }
1924
1925
1926 /* Not to be included in headerdoc.
1927 *
1928 * @define OSDefineAbstractStructors
1929 * @hidecontents
1930 *
1931 * @abstract
1932 * Helper macro for for the standard metaclass-registration macros.
1933 * DO NOT USE.
1934 *
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,
1938 * as a raw token,
1939 * <i>not</i> a string or macro.
1940 */
1941 #define OSDefineAbstractStructors(className, superclassName) \
1942 OSObject * className ::MetaClass::alloc() const { return NULL; }
1943
1944
1945 /* Not to be included in headerdoc.
1946 *
1947 * @define OSDefineDefaultStructors
1948 * @hidecontents
1949 *
1950 * @abstract
1951 * Helper macro for for the standard metaclass-registration macros.
1952 * DO NOT USE.
1953 *
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,
1957 * as a raw token,
1958 * <i>not</i> a string or macro.
1959 */
1960 #define OSDefineDefaultStructors(className, superclassName) \
1961 OSObject * className ::MetaClass::alloc() const \
1962 { return new className; } \
1963 className :: className () : superclassName (&gMetaClass) \
1964 { gMetaClass.instanceConstructed(); }
1965
1966 /* Not to be included in headerdoc.
1967 *
1968 * @define OSDefineDefaultStructors
1969 * @hidecontents
1970 *
1971 * @abstract
1972 * Helper macro for for the standard metaclass-registration macros.
1973 * DO NOT USE.
1974 *
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,
1978 * as a raw token,
1979 * <i>not</i> a string or macro.
1980 */
1981 #define OSDefineFinalStructors(className, superclassName) \
1982 OSDefineDefaultStructors(className, superclassName) \
1983 void className ::__OSFinalClass(void) { }
1984
1985
1986 /* Not to be included in headerdoc.
1987 *
1988 * @define OSDefineMetaClassAndStructorsWithInit
1989 * @hidecontents
1990 *
1991 * @abstract
1992 * Helper macro for for the standard metaclass-registration macros.
1993 * DO NOT USE.
1994 *
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,
1998 * as a raw token,
1999 * <i>not</i> a string or macro.
2000 * @param init A function to call in the constructor
2001 * of the class's OSMetaClass.
2002 */
2003 #define OSDefineMetaClassAndStructorsWithInit(className, superclassName, init) \
2004 OSDefineMetaClassWithInit(className, superclassName, init) \
2005 OSDefineDefaultStructors(className, superclassName)
2006
2007
2008 /* Not to be included in headerdoc.
2009 *
2010 * @define OSDefineMetaClassAndAbstractStructorsWithInit
2011 * @hidecontents
2012 *
2013 * @abstract
2014 * Helper macro for for the standard metaclass-registration macros.
2015 * DO NOT USE.
2016 *
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,
2020 * as a raw token,
2021 * <i>not</i> a string or macro.
2022 * @param init A function to call in the constructor
2023 * of the class's OSMetaClass.
2024 */
2025 #define OSDefineMetaClassAndAbstractStructorsWithInit(className, superclassName, init) \
2026 OSDefineMetaClassWithInit(className, superclassName, init) \
2027 OSDefineAbstractStructors(className, superclassName)
2028
2029
2030 /* Not to be included in headerdoc.
2031 *
2032 * @define OSDefineMetaClassAndFinalStructorsWithInit
2033 * @hidecontents
2034 *
2035 * @abstract
2036 * Helper macro for for the standard metaclass-registration macros.
2037 * DO NOT USE.
2038 *
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,
2042 * as a raw token,
2043 * <i>not</i> a string or macro.
2044 * @param init A function to call in the constructor
2045 * of the class's OSMetaClass.
2046 */
2047 #define OSDefineMetaClassAndFinalStructorsWithInit(className, superclassName, init) \
2048 OSDefineMetaClassWithInit(className, superclassName, init) \
2049 OSDefineFinalStructors(className, superclassName)
2050
2051
2052 /* Helpers */
2053
2054 /* Not to be included in headerdoc.
2055 *
2056 * @define OSDefineMetaClass
2057 * @hidecontents
2058 *
2059 * @abstract
2060 * Helper macro for for the standard metaclass-registration macros.
2061 * DO NOT USE.
2062 *
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,
2066 * as a raw token,
2067 * <i>not</i> a string or macro.
2068 * @param init A function to call in the constructor
2069 * of the class's OSMetaClass.
2070 */
2071 #define OSDefineMetaClass(className, superclassName) \
2072 OSDefineMetaClassWithInit(className, superclassName, )
2073
2074
2075 /*!
2076 * @define OSDefineMetaClassAndStructors
2077 * @hidecontents
2078 *
2079 * @abstract
2080 * Defines an OSMetaClass and associated routines
2081 * for a concrete Libkern C++ class.
2082 *
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,
2086 * as a raw token,
2087 * <i>not</i> a string or macro.
2088 *
2089 * @discussion
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.
2093 */
2094 #define OSDefineMetaClassAndStructors(className, superclassName) \
2095 OSDefineMetaClassAndStructorsWithInit(className, superclassName, )
2096
2097
2098 /*!
2099 * @define OSDefineMetaClassAndAbstractStructors
2100 * @hidecontents
2101 *
2102 * @abstract
2103 * Defines an OSMetaClass and associated routines
2104 * for an abstract Libkern C++ class.
2105 *
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,
2109 * as a raw token,
2110 * <i>not</i> a string or macro.
2111 *
2112 * @discussion
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.
2117 */
2118 #define OSDefineMetaClassAndAbstractStructors(className, superclassName) \
2119 OSDefineMetaClassAndAbstractStructorsWithInit (className, superclassName, )
2120
2121
2122 /*!
2123 * @define OSDefineMetaClassAndFinalStructors
2124 * @hidecontents
2125 *
2126 * @abstract
2127 * Defines an OSMetaClass and associated routines
2128 * for concrete Libkern C++ class.
2129 *
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,
2133 * as a raw token,
2134 * <i>not</i> a string or macro.
2135 *
2136 * @discussion
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.)
2143 *
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.
2147 *
2148 * <b>Warning:</b> Changing a class from "Default" to "Final" will break
2149 * binary compatibility.
2150 */
2151 #define OSDefineMetaClassAndFinalStructors(className, superclassName) \
2152 OSDefineMetaClassAndFinalStructorsWithInit(className, superclassName, )
2153
2154
2155 // Dynamic vtable patchup support routines and types
2156 void reservedCalled(int ind) const;
2157
2158
2159 /*!
2160 * @define OSMetaClassDeclareReservedUnused
2161 * @hidecontents
2162 *
2163 * @abstract
2164 * Reserves vtable space for new virtual functions
2165 * in a Libkern C++ class.
2166 *
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.
2171 *
2172 * @discussion
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>.
2181 *
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.
2186 * See
2187 * <code>@link OSMetaClassDeclareReservedUsed
2188 * OSMetaClassDeclareReservedUsed@/link</code>.
2189 */
2190 #if APPLE_KEXT_VTABLE_PADDING
2191 #define OSMetaClassDeclareReservedUnused(className, index) \
2192 private: \
2193 virtual void _RESERVED ## className ## index ()
2194 #else
2195 #define OSMetaClassDeclareReservedUnused(className, index)
2196 #endif
2197
2198
2199 /*!
2200 * @define OSMetaClassDeclareReservedUsed
2201 * @hidecontents
2202 *
2203 * @abstract
2204 * Documents use of reserved vtable space for new virtual functions
2205 * in a Libkern C++ class.
2206 *
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.
2211 *
2212 * @discussion
2213 * This macro evaluates to nothing, and is used to document reserved
2214 * vtable slots as they are filled.
2215 * See
2216 * <code>@link OSMetaClassDeclareReservedUnused
2217 * OSMetaClassDeclareReservedUnused@/link</code>.
2218 */
2219 #define OSMetaClassDeclareReservedUsed(className, index)
2220
2221
2222 /*!
2223 * @define OSMetaClassDefineReservedUnused
2224 * @hidecontents
2225 *
2226 * @abstract
2227 * Defines a reserved vtable slot for a Libkern C++ class.
2228 *
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.
2233 *
2234 * @discussion
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>.
2243 *
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.
2247 *
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.
2252 * See
2253 * <code>@link OSMetaClassDefineReservedUsed
2254 * OSMetaClassDefineReservedUsed@/link</code>.
2255 */
2256 #if APPLE_KEXT_VTABLE_PADDING
2257 #define OSMetaClassDefineReservedUnused(className, index) \
2258 void className ::_RESERVED ## className ## index () \
2259 { gMetaClass.reservedCalled(index); }
2260 #else
2261 #define OSMetaClassDefineReservedUnused(className, index)
2262 #endif
2263
2264
2265 /*!
2266 * @define OSMetaClassDefineReservedUsed
2267 * @hidecontents
2268 *
2269 * @abstract
2270 * Reserves vtable space for new virtual functions in a Libkern C++ class.
2271 *
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.
2276 *
2277 * @discussion
2278 * This macro evaluates to nothing, and is used to document reserved
2279 * vtable slots as they are filled.
2280 * See
2281 * <code>@link OSMetaClassDefineReservedUnused
2282 * OSMetaClassDefineReservedUnused@/link</code>.
2283 */
2284 #define OSMetaClassDefineReservedUsed(className, index)
2285
2286 // I/O Kit debug internal routines.
2287 static void printInstanceCounts();
2288 static void serializeClassDictionary(OSDictionary * dict);
2289 #ifdef XNU_KERNEL_PRIVATE
2290 #if IOTRACKING
2291 public:
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 */
2300
2301 private:
2302 // Obsolete APIs
2303 static OSDictionary * getClassDictionary();
2304 virtual bool serialize(OSSerialize * serializer) const;
2305
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);
2315 };
2316
2317 #endif /* !_LIBKERN_OSMETACLASS_H */