1 #ifndef _OS_BASE_H
2 #define _OS_BASE_H
3 
4 #define OS_CONSUME __attribute__((os_consumed))
5 #define OS_RETURNS_RETAINED __attribute__((os_returns_retained))
6 #define OS_RETURNS_RETAINED_ON_ZERO __attribute__((os_returns_retained_on_zero))
7 #define OS_RETURNS_RETAINED_ON_NONZERO __attribute__((os_returns_retained_on_non_zero))
8 #define OS_RETURNS_NOT_RETAINED __attribute__((os_returns_not_retained))
9 #define OS_CONSUMES_THIS __attribute__((os_consumes_this))
10 
11 #define OSTypeID(type)   (type::metaClass)
12 
13 #define OSDynamicCast(type, inst)   \
14     ((type *) OSMetaClassBase::safeMetaCast((inst), OSTypeID(type)))
15 #define OSRequiredCast(type, inst)   \
16     ((type *) OSMetaClassBase::requiredMetaCast((inst), OSTypeID(type)))
17 
18 #define OSTypeAlloc(type)   ((type *) ((type::metaClass)->alloc()))
19 
20 using size_t = decltype(sizeof(int));
21 
22 typedef int kern_return_t;
23 struct IORPC {};
24 
25 struct OSMetaClass;
26 
27 struct OSMetaClassBase {
28   static OSMetaClassBase *safeMetaCast(const OSMetaClassBase *inst,
29                                        const OSMetaClass *meta);
30   static OSMetaClassBase *requiredMetaCast(const OSMetaClassBase *inst,
31                                            const OSMetaClass *meta);
32 
33   OSMetaClassBase *metaCast(const char *toMeta);
34 
35   virtual void retain() const;
36   virtual void release() const;
37 
38   virtual void taggedRetain(const void * tag = nullptr) const;
39   virtual void taggedRelease(const void * tag = nullptr) const;
40 
41   virtual void free();
~OSMetaClassBaseOSMetaClassBase42   virtual ~OSMetaClassBase(){};
43 
44   kern_return_t Invoke(IORPC invoke);
45 };
46 
47 typedef kern_return_t (*OSDispatchMethod)(OSMetaClassBase *self,
48                                           const IORPC rpc);
49 
50 struct OSObject : public OSMetaClassBase {
~OSObjectOSObject51   virtual ~OSObject(){}
52 
fooOSObject53   unsigned int foo() { return 42; }
54 
55   virtual OS_RETURNS_NOT_RETAINED OSObject *identity();
56 
57   static OSObject *generateObject(int);
58 
59   static OSObject *getObject();
60   static OSObject *GetObject();
61 
62   static void * operator new(size_t size);
63 
64   static const OSMetaClass * const metaClass;
65 };
66 
67 struct OSMetaClass : public OSMetaClassBase {
68   virtual OSObject * alloc() const;
~OSMetaClassOSMetaClass69   virtual ~OSMetaClass(){}
70 };
71 
72 #endif /* _OS_BASE_H */
73