]> git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/c++/OSMetaClass.h
2d2267ab1fb970fc90cda0298165bd904c4419b8
[apple/xnu.git] / libkern / libkern / c++ / OSMetaClass.h
1 /*
2 * Copyright (c) 2000 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
36 class OSMetaClass;
37 class OSObject;
38 class OSString;
39 class OSSymbol;
40 class OSDictionary;
41 class OSSerialize;
42 #ifdef XNU_KERNEL_PRIVATE
43 class OSOrderedSet;
44 #endif
45
46
47 /*!
48 * @header
49 *
50 * @abstract
51 * This header declares the OSMetaClassBase and OSMetaClass classes,
52 * which together form the basis of the Libkern and I/O Kit C++ class hierarchy
53 * and run-time type information facility.
54 */
55
56
57 /*! @parseOnly */
58 #define APPLE_KEXT_COMPATIBILITY
59
60 #ifdef XNU_KERNEL_PRIVATE
61
62 /*! @parseOnly */
63 #define APPLE_KEXT_VTABLE_PADDING 1
64
65 #else /* XNU_KERNEL_PRIVATE */
66 #include <TargetConditionals.h>
67
68 /*! @parseOnly */
69 #define APPLE_KEXT_VTABLE_PADDING 1
70
71 #endif /* XNU_KERNEL_PRIVATE */
72
73 #if defined(__LP64__)
74 /*! @parseOnly */
75 #define APPLE_KEXT_LEGACY_ABI 0
76 #else
77 #define APPLE_KEXT_LEGACY_ABI 1
78 #endif
79
80 #if defined(__LP64__)
81 /*! @parseOnly */
82 #define APPLE_KEXT_COMPATIBILITY_VIRTUAL
83 #else
84 // private method made virtual only for binary compatibility
85 #define APPLE_KEXT_COMPATIBILITY_VIRTUAL virtual
86 #endif
87
88 /*! @parseOnly */
89 #define APPLE_KEXT_DEPRECATED __attribute__((deprecated))
90
91
92 #if __cplusplus >= 201103L
93 #define APPLE_KEXT_OVERRIDE override
94 #if defined(__LP64__)
95 #define APPLE_KEXT_COMPATIBILITY_OVERRIDE
96 #else
97 #define APPLE_KEXT_COMPATIBILITY_OVERRIDE APPLE_KEXT_OVERRIDE
98 #endif
99 #else
100 #define APPLE_KEXT_OVERRIDE
101 #define APPLE_KEXT_COMPATIBILITY_OVERRIDE
102 #endif
103
104
105 /*!
106 * @class OSMetaClassBase
107 *
108 * @abstract
109 * OSMetaClassBase is the abstract bootstrap class
110 * for the Libkern and I/O Kit run-time type information system.
111 *
112 * @discussion
113 * OSMetaClassBase is the abstract C++ root class
114 * underlying the entire Libkern and I/O Kit class hierarchy.
115 * It defines the run-time type information system,
116 * including dynamic class allocation and safe type-casting,
117 * as well as the abstract interface for reference counting
118 * and a few other utility functions.
119 * OSMetaClassBase is the immediate superclass of
120 * @link //apple_ref/doc/class/OSObject OSObject@/link and
121 * @link //apple_ref/doc/class/OSMetaClass OSMetaClass@/link;
122 * no other class should derive from OSMetaClassBase.
123 *
124 * For more information, see
125 * <i>@link //apple_ref/doc/uid/TP40002799
126 * I/O Kit Device Driver Design Guidelines@/link</i>.
127 *
128 * <b>Use by Kernel Extensions</b>
129 *
130 * Kernel Extensions should never interact directly with OSMetaClassBase,
131 * but they will find useful several macros that tie in
132 * to the run-time type information system, specifically:
133 * <ul>
134 * <li><code>@link OSTypeAlloc OSTypeAlloc@/link</code> - allocation of new instances</li>
135 * <li><code>@link OSDynamicCast OSDynamicCast@/link</code> - safe type casting</li>
136 * <li><code>@link OSCheckTypeInst OSCheckTypeInst@/link</code> -
137 * checking for inheritance/derivation</li>
138 * <li><code>@link OSMemberFunctionCast OSMemberFunctionCast@/link</code> -
139 * casting C++ member functions to C function pointers
140 * for registration as callbacks</li>
141 * </ul>
142 *
143 * See @link //apple_ref/doc/class/OSMetaClass OSMetaClass@/link
144 * for more run-time type information interfaces.
145 *
146 * <b>Use Restrictions</b>
147 *
148 * OSMetaClassBase should not be subclassed by kernel extensions,
149 * nor should kernel extensions call its run-time type functions directly.
150 *
151 * The run-time type functions and macros are <b>not safe</b>
152 * to call in a primary interrupt context.
153 *
154 * <b>Concurrency Protection</b>
155 *
156 * The run-time type macros and functions of OSMetaClassBase are thread-safe.
157 */
158 class OSMetaClassBase
159 {
160 public:
161
162
163 /*!
164 * @define OSTypeAlloc
165 * @hidecontents
166 *
167 * @abstract
168 * Allocates an instance of the named object class.
169 *
170 * @param type The name of the desired class to be created,
171 * as a raw token, <i>not</i> a string or macro.
172 *
173 * @result
174 * A pointer to the new, uninitialized object on success;
175 * <code>NULL</code> on failure.
176 *
177 * @discussion
178 * See also
179 * <code>@link
180 * //apple_ref/cpp/clm/OSMetaClass/allocClassWithName/staticOSObject*\/(constchar*)
181 * OSMetaClass::allocClassWithName(const char *)@/link</code>
182 * and
183 * <code>@link
184 * //apple_ref/cpp/instm/OSMetaClass/alloc/virtualOSObject*\/()
185 * OSMetaClass::alloc@/link</code>.
186 *
187 * The OSTypeAlloc macro is used to avoid binary compatibility difficulties
188 * presented by the C++ <code>new</code> operator.
189 */
190 #define OSTypeAlloc(type) ((type *) ((type::metaClass)->alloc()))
191
192
193 /*!
194 * @define OSTypeID
195 * @hidecontents
196 *
197 * @abstract
198 * Returns the type ID (metaclass) of a class based on its name.
199 *
200 * @param type The name of the desired class, as a raw token,
201 * <i>not</i> a string or macro.
202 *
203 * @result
204 * The unique type ID (metaclass) for the class.
205 *
206 * @discussion
207 * It is typically more useful to determine whether a class is derived
208 * from another; see
209 * <code>@link //apple_ref/cpp/macro/OSDynamicCast OSDynamicCast@/link</code>
210 * and
211 * <code>@link //apple_ref/cpp/macro/OSCheckTypeInst OSCheckTypeInst@/link</code>.
212 */
213 #define OSTypeID(type) (type::metaClass)
214
215
216 /*!
217 * @define OSTypeIDInst
218 * @hidecontents
219 *
220 * @abstract
221 * Returns the type ID (metaclass) for the class of an object instance.
222 *
223 * @param typeinst An instance of an OSObject subclass.
224 *
225 * @result
226 * The type ID of that object's class; that is, its metaclass.
227 *
228 * @discussion
229 * It is typically more useful to determine whether an object is derived
230 * from a particular class; see
231 * <code>@link //apple_ref/cpp/macro/OSDynamicCast OSDynamicCast@/link</code>
232 * and
233 * <code>@link //apple_ref/cpp/macro/OSCheckTypeInst OSCheckTypeInst@/link</code>.
234 */
235 #define OSTypeIDInst(typeinst) ((typeinst)->getMetaClass())
236
237
238 /*!
239 * @define OSDynamicCast
240 * @hidecontents
241 *
242 * @abstract
243 * Safe type-casting for Libkern C++ objects.
244 *
245 * @param type The name of the desired class type, as a raw token,
246 * <i>not</i> a string or macro.
247 * It is assumed you intend to cast to a pointer
248 * to an object of this type.
249 * Type qualifiers, such as <code>const</code>,
250 * are not recognized and will cause
251 * a (usually obscure) compile error.
252 * @param inst A pointer to the object instance to be cast.
253 * May be <code>NULL</code>.
254 *
255 * @result
256 * <code>inst</code> if it is non-<code>NULL</code>
257 * and derived from <code>type</code>;
258 * otherwise <code>NULL</code>.
259 *
260 * @discussion
261 * <code>OSDynamicCast</code> is a rough equivalent
262 * to the standard C++ RTTI <code>dynamic_cast&lt;T&gt;</code> operator.
263 * Your code should use this instead of raw C type-casting,
264 * and check the resulting value.
265 * If the result is non-<code>NULL</code>,
266 * the object is safe to use as the type-cast class;
267 * if the result is <code>NULL</code>,
268 * the object does not derive from the type-cast class
269 * and your code should take appropriate steps to handle the error.
270 */
271 #define OSDynamicCast(type, inst) \
272 ((type *) OSMetaClassBase::safeMetaCast((inst), OSTypeID(type)))
273
274
275 /*!
276 * @define OSCheckTypeInst
277 * @hidecontents
278 *
279 * @abstract
280 * Checks whether two objects are type-compatible.
281 *
282 * @param typeinst The reference object.
283 * @param inst The object to check for type compatibility.
284 *
285 * @result
286 * <code>true</code> if both <code>inst</code> and
287 * <code>typeinst</code> are non-<code>NULL</code>
288 * and <code>inst</code> is derived from the class of <code>typeinst</code>;
289 * otherwise <code>false</code>.
290 */
291 #define OSCheckTypeInst(typeinst, inst) \
292 OSMetaClassBase::checkTypeInst(inst, typeinst)
293
294 /*! @function OSSafeRelease
295 * @abstract Release an object if not <code>NULL</code>.
296 * @param inst Instance of an OSObject, may be <code>NULL</code>.
297 */
298 #define OSSafeRelease(inst) do { if (inst) (inst)->release(); } while (0)
299
300 /*! @function OSSafeReleaseNULL
301 * @abstract Release an object if not <code>NULL</code>, then set it to <code>NULL</code>.
302 * @param inst Instance of an OSObject, may be <code>NULL</code>.
303 */
304 #define OSSafeReleaseNULL(inst) do { if (inst) (inst)->release(); (inst) = NULL; } while (0)
305
306 typedef void (*_ptf_t)(void);
307
308 #if APPLE_KEXT_LEGACY_ABI
309
310 // Arcane evil code interprets a C++ pointer to function as specified in the
311 // -fapple-kext ABI, i.e. the gcc-2.95 generated code. IT DOES NOT ALLOW
312 // the conversion of functions that are from MULTIPLY inherited classes.
313
314 static inline _ptf_t
315 _ptmf2ptf(const OSMetaClassBase *self, void (OSMetaClassBase::*func)(void))
316 {
317 union {
318 void (OSMetaClassBase::*fIn)(void);
319 struct { // Pointer to member function 2.95
320 unsigned short fToff;
321 short fVInd;
322 union {
323 _ptf_t fPFN;
324 short fVOff;
325 } u;
326 } fptmf2;
327 } map;
328
329 map.fIn = func;
330 if (map.fptmf2.fToff) {
331 panic("Multiple inheritance is not supported");
332 return 0;
333 } else if (map.fptmf2.fVInd < 0) {
334 // Not virtual, i.e. plain member func
335 return map.fptmf2.u.fPFN;
336 } else {
337 union {
338 const OSMetaClassBase *fObj;
339 _ptf_t **vtablep;
340 } u;
341 u.fObj = self;
342
343 // Virtual member function so dereference vtable
344 return (*u.vtablep)[map.fptmf2.fVInd - 1];
345 }
346 }
347
348 #else /* !APPLE_KEXT_LEGACY_ABI */
349 #if defined(__i386__) || defined(__x86_64__)
350
351 // Slightly less arcane and slightly less evil code to do
352 // the same for kexts compiled with the standard Itanium C++
353 // ABI
354
355 static inline _ptf_t
356 _ptmf2ptf(const OSMetaClassBase *self, void (OSMetaClassBase::*func)(void))
357 {
358 union {
359 void (OSMetaClassBase::*fIn)(void);
360 uintptr_t fVTOffset;
361 _ptf_t fPFN;
362 } map;
363
364 map.fIn = func;
365
366 if (map.fVTOffset & 1) {
367 // virtual
368 union {
369 const OSMetaClassBase *fObj;
370 _ptf_t **vtablep;
371 } u;
372 u.fObj = self;
373
374 // Virtual member function so dereference vtable
375 return *(_ptf_t *)(((uintptr_t)*u.vtablep) + map.fVTOffset - 1);
376 } else {
377 // Not virtual, i.e. plain member func
378 return map.fPFN;
379 }
380 }
381
382 #else
383 #error Unknown architecture.
384 #endif /* __arm__ */
385
386 #endif /* !APPLE_KEXT_LEGACY_ABI */
387
388 /*!
389 * @define OSMemberFunctionCast
390 * @hidecontents
391 *
392 * @abstract
393 * Converts a C++ member function pointer, relative to an instance,
394 * to a C-style pointer to function.
395 *
396 * @param cptrtype The function type declaration to cast to
397 * (typically provided as a <code>typedef</code> by I/O KitKit classes).
398 * @param self The <code>this</code> pointer of the object whose function
399 * you wish to cache.
400 * @param func The pointer to the member function itself,
401 * something like <code>&Class::function</code>.
402 *
403 * @result
404 * A pointer to a function of the given type referencing <code>self</code>.
405 *
406 * @discussion
407 * This function is used to generate pointers to C++ functions for instances,
408 * such that they can be registered as callbacks with I/O Kit objects.
409 *
410 * No warnings are generated.
411 *
412 * This function will panic if an attempt is made to call it
413 * with a multiply-inheriting class.
414 */
415 #define OSMemberFunctionCast(cptrtype, self, func) \
416 (cptrtype) OSMetaClassBase:: \
417 _ptmf2ptf(self, (void (OSMetaClassBase::*)(void)) func)
418
419 protected:
420 OSMetaClassBase();
421 virtual ~OSMetaClassBase();
422
423 private:
424 // Disable copy constructors of OSMetaClassBase based objects
425 /* Not to be included in headerdoc.
426 *
427 * @function operator =
428 *
429 * @abstract
430 * Disable implicit copy constructor by making private
431 *
432 * @param src Reference to source object that isn't allowed to be copied.
433 */
434 void operator =(OSMetaClassBase &src);
435
436 /* Not to be included in headerdoc.
437 *
438 * @function OSMetaClassBase
439 *
440 * @abstract
441 * Disable implicit copy constructor by making private
442 *
443 * @param src Reference to source object that isn't allowed to be copied.
444 */
445 OSMetaClassBase(OSMetaClassBase &src);
446
447 public:
448
449 // xx-review: the original comment for this makes it sound to me like we don't
450 // xx-review: catch over-releasing an object...?
451
452 /*!
453 * @function release
454 *
455 * @abstract
456 * Abstract declaration of
457 * <code>@link
458 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/(int)
459 * release(int freeWhen)@/link</code>.
460 *
461 * @discussion
462 * See
463 * <code>@link
464 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/(int)
465 * release(int freeWhen)@/link</code>.
466 */
467 virtual void release(int freeWhen) const = 0;
468
469
470 /*!
471 * @function getRetainCount
472 *
473 * @abstract
474 * Abstract declaration of
475 * <code>@link
476 * //apple_ref/cpp/instm/OSObject/getRetainCount/virtualint/()
477 * getRetainCount()@/link</code>.
478 *
479 * @discussion
480 * See
481 * <code>@link
482 * //apple_ref/cpp/instm/OSObject/getRetainCount/virtualint/()
483 * OSObject::getRetainCount()@/link</code>.
484 */
485 virtual int getRetainCount() const = 0;
486
487
488 /*!
489 * @function retain
490 *
491 * @abstract
492 * Abstract declaration of
493 * <code>@link
494 * //apple_ref/cpp/instm/OSObject/retain/virtualvoid/()
495 * retain()@/link</code>.
496 *
497 * @discussion
498 * See
499 * <code>@link
500 * //apple_ref/cpp/instm/OSObject/retain/virtualvoid/()
501 * OSObject::retain()@/link</code>.
502 */
503 virtual void retain() const = 0;
504
505
506 /*!
507 * @function release
508 *
509 * @abstract
510 * Abstract declaration of
511 * <code>@link
512 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/()
513 * release@/link</code>.
514 *
515 * @discussion
516 * See
517 * <code>@link
518 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/()
519 * OSObject::release@/link</code>.
520 */
521 virtual void release() const = 0;
522
523
524 /*!
525 * @function serialize
526 *
527 * @abstract
528 * Abstract declaration of
529 * <code>@link
530 * //apple_ref/cpp/instm/OSObject/serialize/virtualbool/(OSSerialize*)
531 * serialize@/link</code>.
532 *
533 * @discussion
534 * See
535 * <code>@link
536 * //apple_ref/cpp/instm/OSObject/serialize/virtualbool/(OSSerialize*)
537 * OSObject::serialize@/link</code>.
538 */
539 virtual bool serialize(OSSerialize * serializer) const = 0;
540
541
542 /*!
543 * @function getMetaClass
544 *
545 * @abstract
546 * Returns the OSMetaClass representing
547 * an OSMetaClassBase subclass.
548 *
549 * @discussion
550 * OSObject overrides this abstract member function
551 * to return the OSMetaClass object that represents
552 * each class for run-time typing.
553 */
554 virtual const OSMetaClass * getMetaClass() const = 0;
555
556
557 /*!
558 * @function isEqualTo
559 *
560 * @abstract
561 * Checks whether another object is equal to the receiver.
562 *
563 * @param anObject The object to copmare to the receiver.
564 *
565 * @result
566 * <code>true</code> if the objects are equal, <code>false</code> otherwise.
567 *
568 * @discussion
569 * OSMetaClassBase implements this as a direct pointer comparison,
570 * since it has no other information to judge equality by.
571 * Subclasses generally override this function
572 * to do a more meaningful comparison.
573 * For example, OSString implements it to return
574 * <code>true</code> if <code>anObject</code>
575 * is derived from OSString and represents the same C string.
576 */
577 virtual bool isEqualTo(const OSMetaClassBase * anObject) const;
578
579
580 /*!
581 * @function metaCast
582 *
583 * @abstract
584 * Casts this object is to the class managed by the given OSMetaClass.
585 *
586 * @param toMeta A pointer to a constant OSMetaClass
587 * for the desired target type.
588 *
589 * @result
590 * <code>this</code> if the object is derived
591 * from the class managed by <code>toMeta</code>,
592 * otherwise <code>NULL</code>.
593 *
594 * @discussion
595 * It is far more convenient to use
596 * <code>@link OSDynamicCast OSDynamicCast@/link</code>.
597 */
598 OSMetaClassBase * metaCast(const OSMetaClass * toMeta) const;
599
600
601 /*!
602 * @function metaCast
603 *
604 * @abstract
605 * Casts this object is to the class managed by the named OSMetaClass.
606 *
607 * @param toMeta An OSSymbol naming the desired target type.
608 *
609 * @result
610 * <code>this</code> if the object is derived
611 * from the class named by <code>toMeta</code>,
612 * otherwise <code>NULL</code>.
613 *
614 * @discussion
615 * It is far more convenient to use
616 * <code>@link OSDynamicCast OSDynamicCast@/link</code>.
617 */
618 OSMetaClassBase * metaCast(const OSSymbol * toMeta) const;
619
620
621 /*!
622 * @function metaCast
623 *
624 * @abstract
625 * Casts this object is to the class managed by the named OSMetaClass.
626 *
627 * @param toMeta An OSString naming the desired target type.
628 * @result
629 * <code>this</code> if the object is derived
630 * from the class named by <code>toMeta</code>,
631 * otherwise <code>NULL</code>.
632 *
633 * @discussion
634 * It is far more convenient to use
635 * <code>@link OSDynamicCast OSDynamicCast@/link</code>.
636 */
637 OSMetaClassBase * metaCast(const OSString * toMeta) const;
638
639
640 /*!
641 * @function metaCast
642 *
643 * @abstract
644 * Casts this object is to the class managed by the named OSMetaClass.
645 *
646 * @param toMeta A C string naming the desired target type.
647 * @result
648 * <code>this</code> if the object is derived
649 * from the class named by <code>toMeta</code>,
650 * otherwise <code>NULL</code>.
651 *
652 * @discussion
653 * It is far more convenient to use
654 * <code>@link OSDynamicCast OSDynamicCast@/link</code>.
655 */
656 OSMetaClassBase * metaCast(const char * toMeta) const;
657
658 // Helper inlines for run-time type preprocessor macros
659 /*!
660 * @function safeMetaCast
661 *
662 * @abstract
663 * Casts an object is to the class managed by the given OSMetaClass.
664 *
665 * @param anObject A pointer to the object to be cast.
666 * @param toMeta A pointer to a constant OSMetaClass
667 * for the desired target type.
668 *
669 * @result
670 * <code>anObject</code> if the object is derived
671 * from the class managed by <code>toMeta</code>,
672 * otherwise <code>NULL</code>.
673 *
674 * @discussion
675 * It is far more convenient to use
676 * <code>@link OSDynamicCast OSDynamicCast@/link</code>.
677 */
678 static OSMetaClassBase * safeMetaCast(
679 const OSMetaClassBase * anObject,
680 const OSMetaClass * toMeta);
681
682 /*!
683 * @function checkTypeInst
684 *
685 * @abstract
686 * Checks whether an object instance is of the same class
687 * as another object instance (or a subclass of that class).
688 *
689 * @param inst A pointer to the object to check.
690 * @param typeinst A pointer to an object of the class being checked.
691 *
692 * @result
693 * <code>true</code> if the object is derived
694 * from the class of <code>typeinst</code>
695 * or a subclass of that class,
696 * otherwise <code>false</code>.
697 *
698 * @discussion
699 * It is far more convenient to use
700 * <code>@link OSCheckTypeInst OSCheckTypeInst@/link</code>.
701 */
702 static bool checkTypeInst(
703 const OSMetaClassBase * inst,
704 const OSMetaClassBase * typeinst);
705
706 static void initialize(void);
707
708 public:
709
710 /*!
711 * @function taggedRetain
712 *
713 * @abstract
714 * Abstract declaration of
715 * <code>@link
716 * //apple_ref/cpp/instm/OSObject/taggedRetain/virtualvoid/(constvoid*)
717 * taggedRetain(const void *)@/link</code>.
718 *
719 * @discussion
720 * See
721 * <code>@link
722 * //apple_ref/cpp/instm/OSObject/taggedRetain/virtualvoid/(constvoid*)
723 * OSObject::taggedRetain(const void *)@/link</code>.
724 */
725 // WAS: virtual void _RESERVEDOSMetaClassBase0();
726 virtual void taggedRetain(const void * tag = 0) const = 0;
727
728
729 /*!
730 * @function taggedRelease
731 *
732 * @abstract
733 * Abstract declaration of
734 * <code>@link
735 * //apple_ref/cpp/instm/OSObject/taggedRelease/virtualvoid/(constvoid*)
736 * taggedRelease(const void *)@/link</code>.
737 *
738 * @discussion
739 * See
740 * <code>@link
741 * //apple_ref/cpp/instm/OSObject/taggedRelease/virtualvoid/(constvoid*)
742 * OSObject::taggedRelease(const void *)@/link</code>.
743 */
744 // WAS: virtual void _RESERVEDOSMetaClassBase1();
745 virtual void taggedRelease(const void * tag = 0) const = 0;
746
747 protected:
748 /*!
749 * @function taggedRelease
750 *
751 * @abstract
752 * Abstract declaration of
753 * <code>@link
754 * //apple_ref/cpp/instm/OSObject/taggedRelease/virtualvoid/(constvoid*,constint)
755 * taggedRelease(const void *, const int freeWhen)@/link</code>.
756 *
757 * @discussion
758 * See
759 * <code>@link
760 * //apple_ref/cpp/instm/OSObject/taggedRelease/virtualvoid/(constvoid*,constint)
761 * OSObject::taggedRelease(const void *, const int freeWhen)@/link</code>.
762 */
763 // WAS: virtual void _RESERVEDOSMetaClassBase2();
764 virtual void taggedRelease(
765 const void * tag,
766 const int freeWhen) const = 0;
767
768 private:
769 #if APPLE_KEXT_VTABLE_PADDING
770 // Virtual Padding
771 virtual void _RESERVEDOSMetaClassBase3();
772 virtual void _RESERVEDOSMetaClassBase4();
773 virtual void _RESERVEDOSMetaClassBase5();
774 virtual void _RESERVEDOSMetaClassBase6();
775 virtual void _RESERVEDOSMetaClassBase7();
776 #endif
777 } APPLE_KEXT_COMPATIBILITY;
778
779
780 #ifdef XNU_KERNEL_PRIVATE
781 typedef bool (*OSMetaClassInstanceApplierFunction)(const OSObject * instance,
782 void * context);
783 #endif /* XNU_KERNEL_PRIVATE */
784
785 /*!
786 * @class OSMetaClass
787 *
788 * @abstract
789 * OSMetaClass manages run-time type information
790 * for Libkern and I/O Kit C++ classes.
791 *
792 * @discussion
793 *
794 * OSMetaClass manages run-time type information
795 * for Libkern and I/O Kit C++ classes.
796 * An instance of OSMetaClass exists for (nearly) every such C++ class,
797 * keeping track of inheritance relationships, class lookup by name,
798 * instance counts, and more.
799 * OSMetaClass operates almost entirely behind the scenes,
800 * and kernel extensions should rarely, if ever,
801 * have to interact directly with OSMetaClass.
802 *
803 * <b>Use by Kernel Extensions</b>
804 *
805 * While kernel extensions rarey interact directly with OSMetaClass at run time,
806 * they must register their classes with the metaclass system
807 * using the macros declared here.
808 * The class declaration should use one of these two macros
809 * before its first member function declaration:
810 * <ul>
811 * <li><code>@link OSDeclareDefaultStructors OSDeclareDefaultStructors@/link</code> -
812 * for classes with no abstract member function declarations</li>
813 * <li><code>@link OSDeclareAbstractStructors OSDeclareAbstractStructors@/link</code> -
814 * for classes with at least one abstract member function declaration</li>
815 * <li><code>@link OSDeclareFinalStructors OSDeclareFinalStructors@/link</code> -
816 * for classes that should not be subclassable by another kext</li>
817 * </ul>
818 *
819 * The class implementation should then use one of these macros:
820 * <ul>
821 * <li><code>@link OSDefineMetaClassAndStructors
822 * OSDefineMetaClassAndStructors@/link</code> -
823 * for classes with no abstract member function declarations</li>
824 * <li><code>@link OSDefineMetaClassAndAbstractStructors
825 * OSDefineMetaClassAndAbstractStructors@/link</code> -
826 * for classes with at least one abstract member function declaration</li>
827 * <li><code>@link OSDefineMetaClassAndFinalStructors
828 * OSDefineMetaClassAndFinalStructors@/link</code> -
829 * for classes that should not be subclassable by another kext</li>
830 * </ul>
831 *
832 * Classes in kernel extensions that are intended for use as libraries
833 * may need to reserve vtable slots to preserve binary compatibility
834 * as new functions are added. They may do so with these macros:
835 * <ul>
836 * <li><code>@link OSMetaClassDeclareReservedUnused
837 * OSMetaClassDeclareReservedUnused@/link</code> -
838 * reserves a vtable slot</li>
839 * <li><code>@link OSMetaClassDefineReservedUnused
840 * OSMetaClassDefineReservedUnused@/link</code> -
841 * defines the reserved vtable slot as an unimplemented function</li>
842 * <li><code>@link OSMetaClassDeclareReservedUsed
843 * OSMetaClassDeclareReservedUsed@/link</code> -
844 * documents that a formerly reserved slot is now used</li>
845 * <li><code>@link OSMetaClassDefineReservedUsed
846 * OSMetaClassDefineReservedUsed@/link</code> -
847 * documents that a formerly reserved slot is now used</li>
848 * </ul>
849 *
850 * <b>Use Restrictions</b>
851 *
852 * OSMetaClass should not be explicitly subclassed by kernel extensions
853 * (the declare/define macros do that),
854 * nor should kernel extensions call its run-time type functions directly.
855 *
856 * OSMetaClass functions should be considered
857 * <b>unsafe</b> to call in a primary interrupt context.
858 *
859 * <b>Concurrency Protection</b>
860 *
861 * Kernel extensions should in general not interact
862 * with OSMetaClass objects directly,
863 * instead using the run-time type macros.
864 * Much of OSMetaClass's interface is intended for use
865 * by the run-time type information system,
866 * which handles concurrency and locking internally.
867 */
868 class OSMetaClass : private OSMetaClassBase
869 {
870 friend class OSKext;
871 #if IOKITSTATS
872 friend class IOStatistics;
873 #endif
874
875 private:
876 // Can never be allocated must be created at compile time
877 static void * operator new(size_t size);
878
879 /* Reserved for future use. (Internal use only) */
880 struct ExpansionData *reserved;
881
882 /* superClass Handle to the superclass's meta class. */
883 const OSMetaClass *superClassLink;
884
885 /* className OSSymbol of the class' name. */
886 const OSSymbol *className;
887
888 /* classSize How big is a single instance of this class. */
889 unsigned int classSize;
890
891 /* instanceCount Roughly number of instances of the object,
892 * +1 for each direct subclass with a nonzero refcount.
893 * Used primarily as a code-in-use flag.
894 */
895 mutable unsigned int instanceCount;
896
897 /* Not to be included in headerdoc.
898 *
899 * @function OSMetaClass
900 *
901 * @abstract
902 * The default private constructor.
903 */
904 OSMetaClass();
905
906 // Called by postModLoad
907 /* Not to be included in headerdoc.
908 *
909 * @function logError
910 *
911 * @abstract
912 * Logs an error string for an <code>OSReturn</code> value
913 * using <code>printf</code>.
914 *
915 * @param result The <code>OSReturn</code> value for which to log a message.
916 *
917 * @discussion
918 * This function is used to log errors loading kernel extensions.
919 * Kernel extensions themselves should not call it.
920 */
921 static void logError(OSReturn result);
922
923 public:
924
925 /*!
926 * @function getMetaClassWithName
927 *
928 * @abstract
929 * Look up a metaclass in the run-time type information system.
930 *
931 * @param name The name of the desired class's metaclass.
932 *
933 * @result
934 * A pointer to the metaclass object if found, <code>NULL</code> otherwise.
935 */
936 static const OSMetaClass * getMetaClassWithName(const OSSymbol * name);
937
938 protected:
939 /*!
940 * @function retain
941 *
942 * @abstract
943 * Implements the abstract <code>retain</code> function to do nothing.
944 *
945 * @discussion
946 * Since an OSMetaClass instance must remain in existence
947 * for as long as its kernel extension is loaded,
948 * OSMetaClass does not use reference-counting.
949 */
950 virtual void retain() const;
951
952
953 /*!
954 * @function release
955 *
956 * @abstract
957 * Implements the abstract <code>release</code> function to do nothing.
958 *
959 * @discussion
960 * Since an OSMetaClass instance must remain in existence
961 * for as long as its kernel extension is loaded,
962 * OSMetaClass does not use reference-counting.
963 */
964 virtual void release() const;
965
966
967 /*!
968 * @function release
969 *
970 * @abstract
971 * Implements the abstract <code>release(int freeWhen)</code>
972 * function to do nothing.
973 *
974 * @param freeWhen Unused.
975 *
976 * @discussion
977 * Since an OSMetaClass instance must remain in existence
978 * for as long as its kernel extension is loaded,
979 * OSMetaClass does not use reference-counting.
980 */
981 virtual void release(int freeWhen) const;
982
983
984 /*!
985 * @function taggedRetain
986 *
987 * @abstract
988 * Implements the abstract <code>taggedRetain(const void *)</code>
989 * function to do nothing.
990 *
991 * @param tag Unused.
992 *
993 * @discussion
994 * Since an OSMetaClass instance must remain in existence
995 * for as long as its kernel extension is loaded,
996 * OSMetaClass does not use reference-counting.
997 */
998 virtual void taggedRetain(const void * tag = 0) const;
999
1000
1001 /*!
1002 * @function taggedRelease
1003 *
1004 * @abstract
1005 * Implements the abstract <code>taggedRelease(const void *)</code>
1006 * function to do nothing.
1007 *
1008 * @param tag Unused.
1009 *
1010 * @discussion
1011 * Since an OSMetaClass instance must remain in existence
1012 * for as long as its kernel extension is loaded,
1013 * OSMetaClass does not use reference-counting.
1014 */
1015 virtual void taggedRelease(const void * tag = 0) const;
1016
1017
1018 /*!
1019 * @function taggedRelease
1020 *
1021 * @abstract
1022 * Implements the abstract <code>taggedRelease(const void *, cont int)</code>
1023 * function to do nothing.
1024 *
1025 * @param tag Unused.
1026 * @param freeWhen Unused.
1027 *
1028 * @discussion
1029 * Since an OSMetaClass instance must remain in existence
1030 * for as long as its kernel extension is loaded,
1031 * OSMetaClass does not use reference-counting.
1032 */
1033 virtual void taggedRelease(
1034 const void * tag,
1035 const int freeWhen) const;
1036
1037
1038 /*!
1039 * @function getRetainCount
1040 *
1041 * @abstract
1042 * Implements the abstract <code>getRetainCount</code>
1043 * function to return 0.
1044 *
1045 * @result
1046 * Always returns 0.
1047 *
1048 * @discussion
1049 * Since an OSMetaClass instance must remain in existence
1050 * for as long as its kernel extension is loaded,
1051 * OSMetaClass does not use reference-counting.
1052 */
1053 virtual int getRetainCount() const;
1054
1055
1056 /* Not to be included in headerdoc.
1057 *
1058 * @function getMetaClass
1059 *
1060 * @abstract
1061 * Returns the meta-metaclass.
1062 *
1063 * @result
1064 * The metaclass of the OSMetaClass object.
1065 */
1066 virtual const OSMetaClass * getMetaClass() const;
1067
1068
1069 /*!
1070 * @function OSMetaClass
1071 *
1072 * @abstract
1073 * Constructor for OSMetaClass objects.
1074 *
1075 * @param className A C string naming the C++ class
1076 * that this OSMetaClass represents.
1077 * @param superclass The OSMetaClass object representing the superclass
1078 * of this metaclass's class.
1079 * @param classSize The allocation size of the represented C++ class.
1080 *
1081 * @discussion
1082 * This constructor is protected and cannot be used
1083 * to instantiate OSMetaClass directly, as OSMetaClass is an abstract class.
1084 * This function is called during kext loading
1085 * to queue C++ classes for registration.
1086 * See <code>@link preModLoad preModLoad@/link</code> and
1087 * <code>@link postModLoad postModLoad@/link</code>.
1088 */
1089 OSMetaClass(const char * className,
1090 const OSMetaClass * superclass,
1091 unsigned int classSize);
1092
1093
1094 /*!
1095 * @function ~OSMetaClass
1096 *
1097 * @abstract
1098 * Destructor for OSMetaClass objects.
1099 *
1100 * @discussion
1101 * This function is called when the kernel extension that implements
1102 * the metaclass's class is unloaded.
1103 * The destructor removes all references to the class
1104 * from the run-time type information system.
1105 */
1106 virtual ~OSMetaClass();
1107
1108 // Needs to be overriden as NULL as all OSMetaClass objects are allocated
1109 // statically at compile time, don't accidently try to free them.
1110 void operator delete(void *, size_t) { };
1111
1112 public:
1113 static const OSMetaClass * const metaClass;
1114
1115 /*!
1116 * @function preModLoad
1117 *
1118 * @abstract
1119 * Prepares the run-time type system
1120 * for the creation of new metaclasses
1121 * during loading of a kernel extension (module).
1122 *
1123 * @param kextID The bundle ID of the kext being loaded.
1124 *
1125 * @result
1126 * An opaque handle to the load context
1127 * for the kernel extension on success;
1128 * <code>NULL</code> on failure.
1129 *
1130 * @discussion
1131 * <i>Not for use by kernel extensions.</i>
1132 *
1133 * Prepares the run-time type information system to record and register
1134 * metaclasses created by static constructors until a subsequent call to
1135 * <code>@link postModLoad postModLoad@/link</code>.
1136 * <code>preModLoad</code> takes a lock to ensure processing of a single
1137 * load operation at a time; the lock is released by
1138 * <code>@link postModLoad postModLoad@/link</code>.
1139 * Any OSMetaClass constructed between these two function calls
1140 * will be associated with <code>kextID</code>.
1141 */
1142 static void * preModLoad(const char * kextID);
1143
1144
1145 /*!
1146 * @function checkModLoad
1147 *
1148 * @abstract
1149 * Checks whether the current kext load operation can proceed.
1150 *
1151 * @param loadHandle The opaque handle returned
1152 * by <code>@link preModLoad preModLoad@/link</code>.
1153 * @result
1154 * <code>true</code> if no errors are outstanding
1155 * and the system is ready to process more metaclasses.
1156 *
1157 * @discussion
1158 * <i>Not for use by kernel extensions.</i>
1159 */
1160 static bool checkModLoad(void * loadHandle);
1161
1162
1163 /*!
1164 * @function postModLoad
1165 *
1166 * @abstract
1167 * Registers the metaclasses created during loading of a kernel extension.
1168 *
1169 * @param loadHandle The opaque handle returned
1170 * by <code>@link preModLoad preModLoad@/link</code>.
1171 * @result
1172 * The error code of the first error encountered,
1173 * or
1174 * <code>@link
1175 * //apple_ref/cpp/macro/kOSReturnSuccess
1176 * kOSReturnSuccess@/link</code>
1177 * if no error occurred.
1178 *
1179 * @discussion
1180 * <i>Not for use by kernel extensions.</i>
1181 *
1182 * Called after all static constructors in a kernel extension
1183 * have created metaclasses,
1184 * this function checks for duplicate class names,
1185 * then registers the new metaclasses under the kext ID
1186 * that @link preModLoad preModLoad@/link was called with,
1187 * so that they can be dynamically allocated
1188 * and have their instance counts tracked.
1189 * <code>postModLoad</code> releases the lock taken by
1190 * <code>@link preModLoad preModLoad@/link</code>.
1191 */
1192 static OSReturn postModLoad(void * loadHandle);
1193
1194 /*!
1195 * @function modHasInstance
1196 *
1197 * @abstract
1198 * Returns whether any classes defined by the named
1199 * kernel extension (or their subclasses) have existing instances.
1200 *
1201 * @param kextID The bundle ID of the kernel extension to check.
1202 *
1203 * @result
1204 * <code>true</code> if the kext is found and
1205 * if any class defined by that kext
1206 * has a nonzero instance count,
1207 * <code>false</code> otherwise.
1208 *
1209 * @discussion
1210 * This function is called before a kernel extension's static destructors
1211 * are invoked, prior to unloading the extension.
1212 * If any classes stil have instances or subclasses with instances,
1213 * those classes are logged
1214 * (using <code>@link reportModInstances reportModInstances@/link</code>) and
1215 * the kernel extension is not be unloaded.
1216 */
1217 static bool modHasInstance(const char * kextID);
1218
1219
1220 /*!
1221 * @function reportModInstances
1222 *
1223 * @abstract
1224 * Logs the instance counts for classes
1225 * defined by a kernel extension.
1226 *
1227 * @param kextID The bundle ID of the kernel extension to report on.
1228 *
1229 * @discussion
1230 * This function prints the names and instance counts
1231 * of any class defined by <code>kextID</code>
1232 * that has a nonzero instance count.
1233 * It's called by <code>@link modHasInstance modHasInstance@/link</code>
1234 * to help diagnose problems unloading kernel extensions.
1235 */
1236 static void reportModInstances(const char * kextID);
1237
1238
1239 /*!
1240 * @function considerUnloads
1241 *
1242 * @abstract
1243 * Schedule automatic unloading of unused kernel extensions.
1244 *
1245 * @discussion
1246 * This function schedules a check for kernel extensions
1247 * that can be automatically unloaded,
1248 * canceling any currently scheduled check.
1249 * At that time, any such kexts with no Libkern C++ instances
1250 * and no external references are unloaded.
1251 *
1252 * The I/O Kit calls this function when matching goes idle.
1253 *
1254 * Kernel extensions that define subclasses of
1255 * @link //apple_ref/doc/class/IOService IOService@/link
1256 * are eligible for automatic unloading.
1257 *
1258 * (On releases of Mac OS X prior to Snow Leopard (10.6),
1259 * any kernel extension defining any Libkern C++ class
1260 * was eligible for automatic unloading,
1261 * but that unload did not call the module stop routine.
1262 * Non-I/O Kit kernel extensions that define Libkern C++ subclasses
1263 * should be sure to have OSBundleLibraries declarations that ensure
1264 * they will not load on releases prior to Snow Leopard.)
1265 */
1266 static void considerUnloads();
1267
1268
1269 /*!
1270 * @function allocClassWithName
1271 *
1272 * @abstract
1273 * Allocates an instance of a named OSObject-derived class.
1274 *
1275 * @param name The name of the desired class.
1276 *
1277 * @result
1278 * A pointer to the newly-allocated, uninitialized object on success;
1279 * <code>NULL</code> on failure.
1280 *
1281 * @discussion
1282 * Kernel extensions should not need to use this function
1283 * directly, instead using static instance-creation functions
1284 * defined by classes.
1285 *
1286 * This function consults the run-time type information system
1287 * to find the metaclass for the named class.
1288 * If it exists, it calls the metaclass's <code>@link alloc alloc@/link</code>
1289 * function and returns the result.
1290 */
1291 static OSObject * allocClassWithName(const OSSymbol * name);
1292
1293
1294 /*!
1295 * function allocClassWithName
1296 *
1297 * @abstract
1298 * Allocates an instance of a named OSObject-derived class.
1299 *
1300 * @param name The name of the desired class.
1301 *
1302 * @result
1303 * A pointer to the newly-allocated, uninitialized object on success;
1304 * <code>NULL</code> on failure.
1305 *
1306 * @discussion
1307 * Kernel extensions should not need to use this function
1308 * directly, instead using static instance-creation functions
1309 * defined by classes.
1310 *
1311 * This function consults the run-time type information system
1312 * to find the metaclass for the named class.
1313 * If it exists, it calls the metaclass's <code>@link alloc alloc@/link</code>
1314 * function and returns the result.
1315 */
1316 static OSObject * allocClassWithName(const OSString * name);
1317
1318
1319 /*!
1320 * function allocClassWithName
1321 *
1322 * @abstract
1323 * Allocates an instance of a named OSObject-derived class.
1324 *
1325 * @param name The name of the desired class.
1326 *
1327 * @result
1328 * A pointer to the newly-allocated, uninitialized object on success;
1329 * <code>NULL</code> on failure.
1330 *
1331 * @discussion
1332 * Kernel extensions should not need to use this function
1333 * directly, instead using static instance-creation functions
1334 * defined by classes.
1335 *
1336 * This function consults the run-time type information system
1337 * to find the metaclass for the named class.
1338 * If it exists, it calls the metaclass's <code>@link alloc alloc@/link</code>
1339 * function and returns the result.
1340 */
1341 static OSObject * allocClassWithName(const char * name);
1342
1343
1344 /*!
1345 * @function checkMetaCastWithName
1346 *
1347 * @abstract
1348 * Search the metaclass inheritance hierarchy by name for an object instance.
1349 *
1350 * @param className The name of the desired class or superclass.
1351 * @param object The object whose metaclass begins the search.
1352 *
1353 * @result
1354 * <code>object</code> if it's derived from <code>className</code>;
1355 * <code>NULL</code> otherwise.
1356 *
1357 * @discussion
1358 * This function is the basis of the Libkern run-time type-checking system.
1359 * Kernel extensions should not use it directly,
1360 * instead using <code>@link OSDynamicCast OSDynamicCast@/link</code> or
1361 * <code>@link OSCheckTypeInst OSCheckTypeInst@/link</code>.
1362 */
1363 static OSMetaClassBase * checkMetaCastWithName(
1364 const OSSymbol * className,
1365 const OSMetaClassBase * object);
1366
1367 /*!
1368 * @function checkMetaCastWithName
1369 *
1370 * @abstract
1371 * Search the metaclass inheritance hierarchy by name for an object instance.
1372 *
1373 * @param className The name of the desired class or superclass.
1374 * @param object The object whose metaclass begins the search.
1375 *
1376 * @result
1377 * <code>object</code> if it's derived from <code>className</code>;
1378 * <code>NULL</code> otherwise.
1379 *
1380 * @discussion
1381 * Kernel extensions should not use this function directly,
1382 * instead using <code>@link OSDynamicCast OSDynamicCast@/link</code> or
1383 * <code>@link OSCheckTypeInst OSCheckTypeInst@/link</code>.
1384 */
1385 static OSMetaClassBase * checkMetaCastWithName(
1386 const OSString * className,
1387 const OSMetaClassBase * object);
1388
1389 /*!
1390 * @function checkMetaCastWithName
1391 *
1392 * @abstract
1393 * Search the metaclass inheritance hierarchy by name for an object instance.
1394 *
1395 * @param className The name of the desired class or superclass.
1396 * @param object The object whose metaclass begins the search.
1397 *
1398 * @result
1399 * <code>object</code> if it's derived from <code>className</code>;
1400 * <code>NULL</code> otherwise.
1401 *
1402 * @discussion
1403 * Kernel extensions should not use this function directly,
1404 * instead using <code>@link OSDynamicCast OSDynamicCast@/link</code> or
1405 * <code>@link OSCheckTypeInst OSCheckTypeInst@/link</code>.
1406 */
1407 static OSMetaClassBase * checkMetaCastWithName(
1408 const char * className,
1409 const OSMetaClassBase * object);
1410
1411
1412 /*!
1413 * @function instanceConstructed
1414 *
1415 * @abstract
1416 * Counts the instances of the class managed by this metaclass.
1417 *
1418 * @discussion
1419 * <i>Not for use by kernel extensions.</i>
1420 *
1421 * Every non-abstract class that inherits from OSObject
1422 * has a default constructor that calls it's own metaclass's
1423 * <code>instanceConstructed</code> function.
1424 * This constructor is defined by the
1425 * <code>@link
1426 * OSDefineMetaClassAndStructors
1427 * OSDefineMetaClassAndStructors@/link</code>
1428 * macro that all OSObject subclasses must use.
1429 *
1430 * If a class's instance count goes from 0 to 1--that is,
1431 * upon the creation of the first instance of that class--the
1432 * superclass's instance count is also incremented.
1433 * This propagates reference counts up the inheritance chain so that
1434 * superclasses are counted as "in use" when subclasses have instances.
1435 */
1436 void instanceConstructed() const;
1437
1438
1439 /*!
1440 * @function instanceDestructed
1441 *
1442 * @abstract
1443 * Counts the instances of the class managed by this metaclass.
1444 *
1445 * @discussion
1446 * Every non-abstract class that inherits from OSObject
1447 * has a default destructor that calls it's own metaclass's
1448 * <code>instanceDestructed</code> function.
1449 * This constructor is defined by the
1450 * @link OSDefineMetaClassAndStructors OSDefineMetaClassAndStructors@/link
1451 * macro that all OSObject subclasses must use.
1452 *
1453 * If a class's instance count goes from 1 to 0--that is,
1454 * upon the destruction of the last instance of that class--the
1455 * superclass's instance count is also decremented.
1456 * This reduces "in use" counts from superclasses when their subclasses
1457 * no longer have instances.
1458 */
1459 void instanceDestructed() const;
1460
1461
1462 /*!
1463 * @function checkMetaCast
1464 *
1465 * @abstract
1466 * Check whether a given object is an instance of the receiving
1467 * metaclass's class or one derived from it.
1468 *
1469 * @param object The object to check for inheritance.
1470 *
1471 * @result
1472 * <code>object</code> if it is derived from the receiver's class,
1473 * <code>NULL</code> if not.
1474 */
1475 OSMetaClassBase * checkMetaCast(const OSMetaClassBase * object) const;
1476
1477
1478 /*!
1479 * @function getInstanceCount
1480 *
1481 * @abstract
1482 * Returns the number of existing instances of the metaclass's class.
1483 *
1484 * @result
1485 * The number of existing instances of the metaclass's class,
1486 * plus 1 for each subclass with any instance.
1487 */
1488 unsigned int getInstanceCount() const;
1489
1490
1491 /*!
1492 * @function getSuperClass
1493 *
1494 * @abstract
1495 * Returns the super-metaclass of the receiver.
1496 *
1497 * @result
1498 * Returns a pointer to the super-metaclass of the receiving
1499 * OSMetaClass, or <code>NULL</code> for OSObject's metaclass.
1500 */
1501 const OSMetaClass * getSuperClass() const;
1502
1503 /*!
1504 * @function getKmodName
1505 *
1506 * @abstract
1507 * Returns the bundle identifier of the kernel extension
1508 * that defines this metaclass.
1509 *
1510 * @result
1511 * The bundle identifier of the kernel extension that defines this metaclass.
1512 *
1513 * @discussion
1514 * "Kmod" is an older term for kernel extension.
1515 */
1516 const OSSymbol * getKmodName() const;
1517
1518
1519 /*!
1520 * @function getClassName
1521 *
1522 * @abstract
1523 * Returns the name of the C++ class managed by this metaclass.
1524 *
1525 * @result
1526 * Returns the name of the C++ class managed by this metaclass.
1527 */
1528 const char * getClassName() const;
1529 const OSSymbol * getClassNameSymbol() const;
1530
1531
1532 /*!
1533 * @function getClassSize
1534 *
1535 * @abstract
1536 * Returns the allocation size of the C++ class managed by this metaclass.
1537 *
1538 * @result
1539 * The allocation size of the C++ class managed by this metaclass.
1540 */
1541 unsigned int getClassSize() const;
1542
1543
1544 /*!
1545 * @function alloc
1546 *
1547 * @abstract
1548 * Allocates an instance of the C++ class managed by this metaclass.
1549 *
1550 * @result
1551 * A pointer to the newly allocated, uninitialized instance,
1552 * with a retain count of 1; <code>NULL</code> on allocation failure.
1553 *
1554 * @discussion
1555 * This function is automatically created by the metaclass-registration macros
1556 * to enable dynamic instance allocation.
1557 */
1558 virtual OSObject * alloc() const = 0;
1559
1560 #ifdef XNU_KERNEL_PRIVATE
1561 void addInstance(const OSObject * instance, bool super = false) const;
1562 void removeInstance(const OSObject * instance, bool super = false) const;
1563 void applyToInstances(OSMetaClassInstanceApplierFunction applier,
1564 void * context) const;
1565 static void applyToInstancesOfClassName(
1566 const OSSymbol * name,
1567 OSMetaClassInstanceApplierFunction applier,
1568 void * context);
1569 private:
1570 static void applyToInstances(OSOrderedSet * set,
1571 OSMetaClassInstanceApplierFunction applier,
1572 void * context);
1573 public:
1574 #endif
1575
1576 /* Not to be included in headerdoc.
1577 *
1578 * @define OSDeclareCommonStructors
1579 * @hidecontents
1580 *
1581 * @abstract
1582 * Helper macro for for the standard metaclass-registration macros.
1583 * DO NOT USE.
1584 *
1585 * @param className The name of the C++ class, as a raw token,
1586 * <i>not</i> a string or macro.
1587 */
1588 #define OSDeclareCommonStructors(className) \
1589 private: \
1590 static const OSMetaClass * const superClass; \
1591 public: \
1592 static const OSMetaClass * const metaClass; \
1593 static class MetaClass : public OSMetaClass { \
1594 public: \
1595 MetaClass(); \
1596 virtual OSObject *alloc() const; \
1597 } gMetaClass; \
1598 friend class className ::MetaClass; \
1599 virtual const OSMetaClass * getMetaClass() const APPLE_KEXT_OVERRIDE; \
1600 protected: \
1601 className (const OSMetaClass *); \
1602 virtual ~ className ()
1603
1604
1605 /*!
1606 * @define OSDeclareDefaultStructors
1607 * @hidecontents
1608 *
1609 * @abstract
1610 * Declares run-time type information and functions
1611 * for a concrete Libkern C++ class.
1612 *
1613 * @param className The name of the C++ class, as a raw token,
1614 * <i>not</i> a string or macro.
1615 *
1616 * @discussion
1617 * Concrete Libkern C++ classes should "call" this macro
1618 * immediately after the opening brace in a class declaration.
1619 * It leaves the current privacy state as <code>protected:</code>.
1620 */
1621 #define OSDeclareDefaultStructors(className) \
1622 OSDeclareCommonStructors(className); \
1623 public: \
1624 className (); \
1625 protected:
1626
1627
1628 /*!
1629 * @define OSDeclareAbstractStructors
1630 * @hidecontents
1631 *
1632 * @abstract
1633 * Declares run-time type information and functions
1634 * for an abstract Libkern C++ class.
1635 *
1636 * @param className The name of the C++ class, as a raw token,
1637 * <i>not</i> a string or macro.
1638 *
1639 * @discussion
1640 * Abstract Libkern C++ classes--those with at least one
1641 * pure virtual method--should "call" this macro
1642 * immediately after the opening brace in a class declaration.
1643 * It leaves the current privacy state as <code>protected:</code>.
1644 */
1645 #define OSDeclareAbstractStructors(className) \
1646 OSDeclareCommonStructors(className); \
1647 private: \
1648 className (); /* Make primary constructor private in abstract */ \
1649 protected:
1650
1651 /*!
1652 * @define OSDeclareFinalStructors
1653 * @hidecontents
1654 *
1655 * @abstract
1656 * Declares run-time type information and functions
1657 * for a final (non-subclassable) Libkern C++ class.
1658 *
1659 * @param className The name of the C++ class, as a raw token,
1660 * <i>not</i> a string or macro.
1661 *
1662 * @discussion
1663 * Final Libkern C++ classes--those that do not allow subclassing--should
1664 * "call" this macro immediately after the opening brace in a class declaration.
1665 * (Final classes in the kernel may actually have subclasses in the kernel,
1666 * but kexts cannot define any subclasses of a final class.)
1667 * It leaves the current privacy state as <code>protected:</code>.
1668 *
1669 * <b>Note:</b> If the class is exported by a pseudokext (symbol set),
1670 * the final symbol generated by this macro must be exported
1671 * for the final-class attribute to be enforced.
1672 *
1673 * <b>Warning:</b> Changing a class from "Default" to "Final" will break
1674 * binary compatibility.
1675 */
1676 #define OSDeclareFinalStructors(className) \
1677 OSDeclareDefaultStructors(className) \
1678 private: \
1679 void __OSFinalClass(void); \
1680 protected:
1681
1682
1683 /* Not to be included in headerdoc.
1684 *
1685 * @define OSDefineMetaClassWithInit
1686 * @hidecontents
1687 *
1688 * @abstract
1689 * Helper macro for for the standard metaclass-registration macros.
1690 * DO NOT USE.
1691 *
1692 * @param className The name of the C++ class, as a raw token,
1693 * <i>not</i> a string or macro.
1694 * @param superclassName The name of the superclass of the C++ class,
1695 * as a raw token,
1696 * <i>not</i> a string or macro.
1697 * @param init A function to call in the constructor
1698 * of the class's OSMetaClass.
1699 */
1700 #define OSDefineMetaClassWithInit(className, superclassName, init) \
1701 /* Class global data */ \
1702 className ::MetaClass className ::gMetaClass; \
1703 const OSMetaClass * const className ::metaClass = \
1704 & className ::gMetaClass; \
1705 const OSMetaClass * const className ::superClass = \
1706 & superclassName ::gMetaClass; \
1707 /* Class member functions */ \
1708 className :: className(const OSMetaClass *meta) \
1709 : superclassName (meta) { } \
1710 className ::~ className() { } \
1711 const OSMetaClass * className ::getMetaClass() const \
1712 { return &gMetaClass; } \
1713 /* The ::MetaClass constructor */ \
1714 className ::MetaClass::MetaClass() \
1715 : OSMetaClass(#className, className::superClass, sizeof(className)) \
1716 { init; }
1717
1718
1719 /* Not to be included in headerdoc.
1720 *
1721 * @define OSDefineAbstractStructors
1722 * @hidecontents
1723 *
1724 * @abstract
1725 * Helper macro for for the standard metaclass-registration macros.
1726 * DO NOT USE.
1727 *
1728 * @param className The name of the C++ class, as a raw token,
1729 * <i>not</i> a string or macro.
1730 * @param superclassName The name of the superclass of the C++ class,
1731 * as a raw token,
1732 * <i>not</i> a string or macro.
1733 */
1734 #define OSDefineAbstractStructors(className, superclassName) \
1735 OSObject * className ::MetaClass::alloc() const { return 0; }
1736
1737
1738 /* Not to be included in headerdoc.
1739 *
1740 * @define OSDefineDefaultStructors
1741 * @hidecontents
1742 *
1743 * @abstract
1744 * Helper macro for for the standard metaclass-registration macros.
1745 * DO NOT USE.
1746 *
1747 * @param className The name of the C++ class, as a raw token,
1748 * <i>not</i> a string or macro.
1749 * @param superclassName The name of the superclass of the C++ class,
1750 * as a raw token,
1751 * <i>not</i> a string or macro.
1752 */
1753 #define OSDefineDefaultStructors(className, superclassName) \
1754 OSObject * className ::MetaClass::alloc() const \
1755 { return new className; } \
1756 className :: className () : superclassName (&gMetaClass) \
1757 { gMetaClass.instanceConstructed(); }
1758
1759 /* Not to be included in headerdoc.
1760 *
1761 * @define OSDefineDefaultStructors
1762 * @hidecontents
1763 *
1764 * @abstract
1765 * Helper macro for for the standard metaclass-registration macros.
1766 * DO NOT USE.
1767 *
1768 * @param className The name of the C++ class, as a raw token,
1769 * <i>not</i> a string or macro.
1770 * @param superclassName The name of the superclass of the C++ class,
1771 * as a raw token,
1772 * <i>not</i> a string or macro.
1773 */
1774 #define OSDefineFinalStructors(className, superclassName) \
1775 OSDefineDefaultStructors(className, superclassName) \
1776 void className ::__OSFinalClass(void) { }
1777
1778
1779 /* Not to be included in headerdoc.
1780 *
1781 * @define OSDefineMetaClassAndStructorsWithInit
1782 * @hidecontents
1783 *
1784 * @abstract
1785 * Helper macro for for the standard metaclass-registration macros.
1786 * DO NOT USE.
1787 *
1788 * @param className The name of the C++ class, as a raw token,
1789 * <i>not</i> a string or macro.
1790 * @param superclassName The name of the superclass of the C++ class,
1791 * as a raw token,
1792 * <i>not</i> a string or macro.
1793 * @param init A function to call in the constructor
1794 * of the class's OSMetaClass.
1795 */
1796 #define OSDefineMetaClassAndStructorsWithInit(className, superclassName, init) \
1797 OSDefineMetaClassWithInit(className, superclassName, init) \
1798 OSDefineDefaultStructors(className, superclassName)
1799
1800
1801 /* Not to be included in headerdoc.
1802 *
1803 * @define OSDefineMetaClassAndAbstractStructorsWithInit
1804 * @hidecontents
1805 *
1806 * @abstract
1807 * Helper macro for for the standard metaclass-registration macros.
1808 * DO NOT USE.
1809 *
1810 * @param className The name of the C++ class, as a raw token,
1811 * <i>not</i> a string or macro.
1812 * @param superclassName The name of the superclass of the C++ class,
1813 * as a raw token,
1814 * <i>not</i> a string or macro.
1815 * @param init A function to call in the constructor
1816 * of the class's OSMetaClass.
1817 */
1818 #define OSDefineMetaClassAndAbstractStructorsWithInit(className, superclassName, init) \
1819 OSDefineMetaClassWithInit(className, superclassName, init) \
1820 OSDefineAbstractStructors(className, superclassName)
1821
1822
1823 /* Not to be included in headerdoc.
1824 *
1825 * @define OSDefineMetaClassAndFinalStructorsWithInit
1826 * @hidecontents
1827 *
1828 * @abstract
1829 * Helper macro for for the standard metaclass-registration macros.
1830 * DO NOT USE.
1831 *
1832 * @param className The name of the C++ class, as a raw token,
1833 * <i>not</i> a string or macro.
1834 * @param superclassName The name of the superclass of the C++ class,
1835 * as a raw token,
1836 * <i>not</i> a string or macro.
1837 * @param init A function to call in the constructor
1838 * of the class's OSMetaClass.
1839 */
1840 #define OSDefineMetaClassAndFinalStructorsWithInit(className, superclassName, init) \
1841 OSDefineMetaClassWithInit(className, superclassName, init) \
1842 OSDefineFinalStructors(className, superclassName)
1843
1844
1845 /* Helpers */
1846
1847 /* Not to be included in headerdoc.
1848 *
1849 * @define OSDefineMetaClass
1850 * @hidecontents
1851 *
1852 * @abstract
1853 * Helper macro for for the standard metaclass-registration macros.
1854 * DO NOT USE.
1855 *
1856 * @param className The name of the C++ class, as a raw token,
1857 * <i>not</i> a string or macro.
1858 * @param superclassName The name of the superclass of the C++ class,
1859 * as a raw token,
1860 * <i>not</i> a string or macro.
1861 * @param init A function to call in the constructor
1862 * of the class's OSMetaClass.
1863 */
1864 #define OSDefineMetaClass(className, superclassName) \
1865 OSDefineMetaClassWithInit(className, superclassName, )
1866
1867
1868 /*!
1869 * @define OSDefineMetaClassAndStructors
1870 * @hidecontents
1871 *
1872 * @abstract
1873 * Defines an OSMetaClass and associated routines
1874 * for a concrete Libkern C++ class.
1875 *
1876 * @param className The name of the C++ class, as a raw token,
1877 * <i>not</i> a string or macro.
1878 * @param superclassName The name of the superclass of the C++ class,
1879 * as a raw token,
1880 * <i>not</i> a string or macro.
1881 *
1882 * @discussion
1883 * Concrete Libkern C++ classes should "call" this macro
1884 * at the beginning of their implementation files,
1885 * before any function implementations for the class.
1886 */
1887 #define OSDefineMetaClassAndStructors(className, superclassName) \
1888 OSDefineMetaClassAndStructorsWithInit(className, superclassName, )
1889
1890
1891 /*!
1892 * @define OSDefineMetaClassAndAbstractStructors
1893 * @hidecontents
1894 *
1895 * @abstract
1896 * Defines an OSMetaClass and associated routines
1897 * for an abstract Libkern C++ class.
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 *
1905 * @discussion
1906 * Abstract Libkern C++ classes--those with at least one
1907 * pure virtual method--should "call" this macro
1908 * at the beginning of their implementation files,
1909 * before any function implementations for the class.
1910 */
1911 #define OSDefineMetaClassAndAbstractStructors(className, superclassName) \
1912 OSDefineMetaClassAndAbstractStructorsWithInit (className, superclassName, )
1913
1914
1915 /*!
1916 * @define OSDefineMetaClassAndFinalStructors
1917 * @hidecontents
1918 *
1919 * @abstract
1920 * Defines an OSMetaClass and associated routines
1921 * for a final (non-subclassable) Libkern C++ class.
1922 *
1923 * @param className The name of the C++ class, as a raw token,
1924 * <i>not</i> a string or macro.
1925 * @param superclassName The name of the superclass of the C++ class,
1926 * as a raw token,
1927 * <i>not</i> a string or macro.
1928 *
1929 * @discussion
1930 * Final Libkern C++ classes--those that do not allow
1931 * subclassing--should "call" this macro at the beginning
1932 * of their implementation files,
1933 * before any function implementations for the class.
1934 * (Final classes in the kernel may actually have subclasses in the kernel,
1935 * but kexts cannot define any subclasses of a final class.)
1936 *
1937 * <b>Note:</b> If the class is exported by a pseudokext (symbol set),
1938 * the final symbol generated by this macro must be exported
1939 * for the final-class attribute to be enforced.
1940 *
1941 * <b>Warning:</b> Changing a class from "Default" to "Final" will break
1942 * binary compatibility.
1943 */
1944 #define OSDefineMetaClassAndFinalStructors(className, superclassName) \
1945 OSDefineMetaClassAndFinalStructorsWithInit(className, superclassName, )
1946
1947
1948 // Dynamic vtable patchup support routines and types
1949 void reservedCalled(int ind) const;
1950
1951
1952 /*!
1953 * @define OSMetaClassDeclareReservedUnused
1954 * @hidecontents
1955 *
1956 * @abstract
1957 * Reserves vtable space for new virtual functions
1958 * in a Libkern C++ class.
1959 *
1960 * @param className The name of the C++ class, as a raw token,
1961 * <i>not</i> a string or macro.
1962 * @param index The numeric index of the vtable slot,
1963 * as a raw constant, beginning from 0.
1964 *
1965 * @discussion
1966 * Libkern C++ classes in kernel extensions that can be used as libraries
1967 * can provide for backward compatibility by declaring a number
1968 * of reserved vtable slots
1969 * that can be replaced with new functions as they are added.
1970 * Each reserved declaration must be accompanied in the implementation
1971 * by a corresponding reference to
1972 * <code>@link OSMetaClassDefineReservedUnused
1973 * OSMetaClassDefineReservedUnused@/link</code>.
1974 *
1975 * When replacing a reserved slot, change the macro from "Unused"
1976 * to "Used" to document the fact that the slot used to be reserved,
1977 * and declare the new function immediately after the "Used" macro
1978 * to preserve vtable ordering.
1979 * See
1980 * <code>@link OSMetaClassDeclareReservedUsed
1981 * OSMetaClassDeclareReservedUsed@/link</code>.
1982 */
1983 #if APPLE_KEXT_VTABLE_PADDING
1984 #define OSMetaClassDeclareReservedUnused(className, index) \
1985 private: \
1986 virtual void _RESERVED ## className ## index ()
1987 #else
1988 #define OSMetaClassDeclareReservedUnused(className, index)
1989 #endif
1990
1991
1992 /*!
1993 * @define OSMetaClassDeclareReservedUsed
1994 * @hidecontents
1995 *
1996 * @abstract
1997 * Documents use of reserved vtable space for new virtual functions
1998 * in a Libkern C++ class.
1999 *
2000 * @param className The name of the C++ class, as a raw token,
2001 * <i>not</i> a string or macro.
2002 * @param index The numeric index of the vtable slot,
2003 * as a raw constant, beginning from 0.
2004 *
2005 * @discussion
2006 * This macro evaluates to nothing, and is used to document reserved
2007 * vtable slots as they are filled.
2008 * See
2009 * <code>@link OSMetaClassDeclareReservedUnused
2010 * OSMetaClassDeclareReservedUnused@/link</code>.
2011 */
2012 #define OSMetaClassDeclareReservedUsed(className, index)
2013
2014
2015 /*!
2016 * @define OSMetaClassDefineReservedUnused
2017 * @hidecontents
2018 *
2019 * @abstract
2020 * Defines a reserved vtable slot for a Libkern C++ class.
2021 *
2022 * @param className The name of the C++ class, as a raw token,
2023 * <i>not</i> a string or macro.
2024 * @param index The numeric index of the vtable slot,
2025 * as a raw constant, beginning from 0.
2026 *
2027 * @discussion
2028 * Libkern C++ classes in kernel extensions that can be used as libraries
2029 * can provide for backward compatibility by declaring a number
2030 * of reserved vtable slots
2031 * that can be replaced with new functions as they are added.
2032 * Each reserved defintion accompanies
2033 * a corresponding declaration created with
2034 * <code>@link OSMetaClassDeclareReservedUnused
2035 * OSMetaClassDeclareReservedUnused@/link</code>.
2036 *
2037 * This macro is used in the implementation file
2038 * to provide a placeholder definition for the reserved vtable slot,
2039 * as a function that calls <code>panic</code> with an error message.
2040 *
2041 * When replacing a reserved slot, change the macro from "Unused"
2042 * to "Used" to document the fact that the slot used to be reserved,
2043 * and declare the new function immediately after the "Used" macro
2044 * to preserve vtable ordering.
2045 * See
2046 * <code>@link OSMetaClassDefineReservedUsed
2047 * OSMetaClassDefineReservedUsed@/link</code>.
2048 */
2049 #if APPLE_KEXT_VTABLE_PADDING
2050 #define OSMetaClassDefineReservedUnused(className, index) \
2051 void className ::_RESERVED ## className ## index () \
2052 { gMetaClass.reservedCalled(index); }
2053 #else
2054 #define OSMetaClassDefineReservedUnused(className, index)
2055 #endif
2056
2057
2058 /*!
2059 * @define OSMetaClassDefineReservedUsed
2060 * @hidecontents
2061 *
2062 * @abstract
2063 * Reserves vtable space for new virtual functions in a Libkern C++ class.
2064 *
2065 * @param className The name of the C++ class, as a raw token,
2066 * <i>not</i> a string or macro.
2067 * @param index The numeric index of the vtable slot,
2068 * as a raw constant, beginning from 0.
2069 *
2070 * @discussion
2071 * This macro evaluates to nothing, and is used to document reserved
2072 * vtable slots as they are filled.
2073 * See
2074 * <code>@link OSMetaClassDefineReservedUnused
2075 * OSMetaClassDefineReservedUnused@/link</code>.
2076 */
2077 #define OSMetaClassDefineReservedUsed(className, index)
2078
2079 // I/O Kit debug internal routines.
2080 static void printInstanceCounts();
2081 static void serializeClassDictionary(OSDictionary * dict);
2082 #ifdef XNU_KERNEL_PRIVATE
2083 #if IOTRACKING
2084 public:
2085 static void * trackedNew(size_t size);
2086 static void trackedDelete(void * mem, size_t size);
2087 void trackedInstance(OSObject * instance) const;
2088 void trackedFree(OSObject * instance) const;
2089 void trackedAccumSize(OSObject * instance, size_t size) const;
2090 struct IOTrackingQueue * getTracking() const;
2091 #endif
2092 #endif
2093
2094 private:
2095 // Obsolete APIs
2096 static OSDictionary * getClassDictionary();
2097 virtual bool serialize(OSSerialize * serializer) const;
2098
2099 // Virtual Padding functions for MetaClass's
2100 OSMetaClassDeclareReservedUnused(OSMetaClass, 0);
2101 OSMetaClassDeclareReservedUnused(OSMetaClass, 1);
2102 OSMetaClassDeclareReservedUnused(OSMetaClass, 2);
2103 OSMetaClassDeclareReservedUnused(OSMetaClass, 3);
2104 OSMetaClassDeclareReservedUnused(OSMetaClass, 4);
2105 OSMetaClassDeclareReservedUnused(OSMetaClass, 5);
2106 OSMetaClassDeclareReservedUnused(OSMetaClass, 6);
2107 OSMetaClassDeclareReservedUnused(OSMetaClass, 7);
2108 };
2109
2110 #endif /* !_LIBKERN_OSMETACLASS_H */