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