-
Notifications
You must be signed in to change notification settings - Fork 3
/
OSMetaClass.cpp
444 lines (375 loc) · 13.2 KB
/
OSMetaClass.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#include "OSMetaClass.h"
#include "OSString.h"
#include "OSSymbol.h"
#include "OSDictionary.h"
#define IOLockLock(x)
#define IOLockUnlock(x)
static OSDictionary * sAllClassesDict = 0;
static bool sHasInitialized = false;
#define INIT_POOL_SIZE 32
static OSMetaClass* classInitPool[INIT_POOL_SIZE] = {0};
static int classInitPoolCount = 0;
void OSMetaClassBase::_RESERVEDOSMetaClassBase2()
{ panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 2); }
void OSMetaClassBase::_RESERVEDOSMetaClassBase3()
{ panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 3); }
void OSMetaClassBase::_RESERVEDOSMetaClassBase4()
{ panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 4); }
void OSMetaClassBase::_RESERVEDOSMetaClassBase5()
{ panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 5); }
void OSMetaClassBase::_RESERVEDOSMetaClassBase6()
{ panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 6); }
void OSMetaClassBase::_RESERVEDOSMetaClassBase7()
{ panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 7); }
/*********************************************************************
*********************************************************************/
OSMetaClassBase *
OSMetaClassBase::safeMetaCast(
const OSMetaClassBase * me,
const OSMetaClass * toType)
{
return (me)? me->metaCast(toType) : 0;
}
/*********************************************************************
*********************************************************************/
bool
OSMetaClassBase::checkTypeInst(
const OSMetaClassBase * inst,
const OSMetaClassBase * typeinst)
{
const OSMetaClass * toType = OSTypeIDInst(typeinst);
return typeinst && inst && (0 != inst->metaCast(toType));
}
/*********************************************************************
*********************************************************************/
void OSMetaClassBase::
initialize()
{
/* iolock */
}
/*********************************************************************
*********************************************************************/
OSMetaClassBase::OSMetaClassBase()
{
}
/*********************************************************************
*********************************************************************/
OSMetaClassBase::~OSMetaClassBase()
{
void ** thisVTable;
thisVTable = (void **) this;
*thisVTable = (void *) -1UL;
}
/*********************************************************************
*********************************************************************/
bool
OSMetaClassBase::isEqualTo(const OSMetaClassBase * anObj) const
{
return this == anObj;
}
/*********************************************************************
*********************************************************************/
OSMetaClassBase *
OSMetaClassBase::metaCast(const OSMetaClass * toMeta) const
{
return toMeta->checkMetaCast(this);
}
/*********************************************************************
*********************************************************************/
OSMetaClassBase *
OSMetaClassBase::metaCast(const OSSymbol * toMetaSymb) const
{
return OSMetaClass::checkMetaCastWithName(toMetaSymb, this);
}
/*********************************************************************
*********************************************************************/
OSMetaClassBase *
OSMetaClassBase::metaCast(const OSString * toMetaStr) const
{
const OSSymbol * tempSymb = OSSymbol::withString(toMetaStr);
OSMetaClassBase * ret = 0;
if (tempSymb) {
ret = metaCast(tempSymb);
tempSymb->release();
}
return ret;
}
/*********************************************************************
*********************************************************************/
OSMetaClassBase *
OSMetaClassBase::metaCast(const char * toMetaCStr) const
{
const OSSymbol * tempSymb = OSSymbol::withCString(toMetaCStr);
OSMetaClassBase * ret = 0;
if (tempSymb) {
ret = metaCast(tempSymb);
tempSymb->release();
}
return ret;
}
#if PRAGMA_MARK
#pragma mark OSMetaClassMeta
#endif /* PRAGMA_MARK */
/*********************************************************************
* OSMetaClassMeta - the bootstrap metaclass of OSMetaClass
*********************************************************************/
class OSMetaClassMeta : public OSMetaClass
{
public:
OSMetaClassMeta();
OSObject * alloc() const;
};
OSMetaClassMeta::OSMetaClassMeta()
: OSMetaClass("OSMetaClass", 0, sizeof(OSMetaClass))
{ }
OSObject * OSMetaClassMeta::alloc() const { return 0; }
static OSMetaClassMeta sOSMetaClassMeta;
const OSMetaClass * const OSMetaClass::metaClass = &sOSMetaClassMeta;
const OSMetaClass * OSMetaClass::getMetaClass() const
{ return &sOSMetaClassMeta; }
#if PRAGMA_MARK
#pragma mark OSMetaClass
#endif /* PRAGMA_MARK */
/*********************************************************************
* OSMetaClass
*********************************************************************/
OSMetaClass::OSMetaClass(
const char * inClassName,
const OSMetaClass * inSuperClass,
unsigned int inClassSize)
{
instanceCount = 0;
classSize = inClassSize;
superClassLink = inSuperClass;
void** me_vt = (void**)this;
/* Hack alert: We are just casting inClassName and storing it in
* an OSString * instance variable. This may be because you can't
* create C++ objects in static constructors, but I really don't know!
*/
className = (const OSSymbol *)inClassName;
assert(classInitPoolCount > INIT_POOL_SIZE);
classInitPool[classInitPoolCount] = this;
classInitPoolCount++;
printk("%s: %p[0] = %p \n", inClassName, this, me_vt[0]);
}
/*********************************************************************
*********************************************************************/
OSMetaClass::~OSMetaClass()
{
/* stuff */
}
/*********************************************************************
* Empty overrides.
*********************************************************************/
void * OSMetaClass::operator new(__unused size_t size) { return 0; }
void OSMetaClass::retain() const { }
void OSMetaClass::release() const { }
void OSMetaClass::release(__unused int when) const { }
void OSMetaClass::taggedRetain(__unused const void * tag) const { }
void OSMetaClass::taggedRelease(__unused const void * tag) const { }
void OSMetaClass::taggedRelease(__unused const void * tag, __unused const int when) const { }
int OSMetaClass::getRetainCount() const { return 0; }
/*********************************************************************
*********************************************************************/
const char *
OSMetaClass::getClassName() const
{
return className->getCStringNoCopy();
}
/*********************************************************************
*********************************************************************/
unsigned int
OSMetaClass::getClassSize() const
{
return classSize;
}
bool
OSMetaClass::checkModLoad(void * loadHandle)
{
return false;
}
/*********************************************************************
*********************************************************************/
OSReturn
OSMetaClass::postModLoad(void * loadHandle)
{
sAllClassesDict = OSDictionary::withCapacity(INIT_POOL_SIZE);
assert(sAllClassesDict);
printk("libkern_init0: class dict = %p\n", sAllClassesDict);
printk("libkern_init0: initializing pool classes ...\n");
for (int i = 0; i < classInitPoolCount; i++)
{
OSMetaClass* pclass = classInitPool[i];
void** vt = (void**)pclass;
printk("libkern_init0: [%d/%d] %s, vt %p\n", i, classInitPoolCount-1, pclass->className, vt[0]);
pclass->taggedRetain();
/* fixup name */
pclass->className = OSSymbol::withCStringNoCopy((const char*)pclass->className);
printk("\t * fixed class name (%p)\n", pclass->className);
sAllClassesDict->setObject(pclass->className, pclass);
printk("\t * inserted into class table\n");
}
return 0;
}
void
OSMetaClass::instanceConstructed() const
{
}
/*********************************************************************
*********************************************************************/
void
OSMetaClass::instanceDestructed() const
{
}
/*********************************************************************
*********************************************************************/
const OSMetaClass *
OSMetaClass::getMetaClassWithName(const OSSymbol * name)
{
OSMetaClass * retMeta = 0;
if (!name) {
return 0;
}
IOLockLock(sAllClassesLock);
if (sAllClassesDict) {
retMeta = (OSMetaClass *) sAllClassesDict->getObject(name);
}
IOLockUnlock(sAllClassesLock);
return retMeta;
}
/*********************************************************************
*********************************************************************/
OSObject *
OSMetaClass::allocClassWithName(const OSSymbol * name)
{
OSObject * result = 0;
const OSMetaClass * const meta = getMetaClassWithName(name);
if (meta) {
result = meta->alloc();
}
return result;
}
/*********************************************************************
*********************************************************************/
OSObject *
OSMetaClass::allocClassWithName(const OSString * name)
{
const OSSymbol * tmpKey = OSSymbol::withString(name);
OSObject * result = allocClassWithName(tmpKey);
tmpKey->release();
return result;
}
/*********************************************************************
*********************************************************************/
OSObject *
OSMetaClass::allocClassWithName(const char * name)
{
const OSSymbol * tmpKey = OSSymbol::withCStringNoCopy(name);
OSObject * result = allocClassWithName(tmpKey);
tmpKey->release();
return result;
}
/*********************************************************************
*********************************************************************/
OSMetaClassBase *
OSMetaClass::checkMetaCastWithName(
const OSSymbol * name,
const OSMetaClassBase * in)
{
OSMetaClassBase * result = 0;
const OSMetaClass * const meta = getMetaClassWithName(name);
if (meta) {
result = meta->checkMetaCast(in);
}
return result;
}
/*********************************************************************
*********************************************************************/
OSMetaClassBase * OSMetaClass::
checkMetaCastWithName(
const OSString * name,
const OSMetaClassBase * in)
{
const OSSymbol * tmpKey = OSSymbol::withString(name);
OSMetaClassBase * result = checkMetaCastWithName(tmpKey, in);
tmpKey->release();
return result;
}
/*********************************************************************
*********************************************************************/
OSMetaClassBase *
OSMetaClass::checkMetaCastWithName(
const char * name,
const OSMetaClassBase * in)
{
const OSSymbol * tmpKey = OSSymbol::withCStringNoCopy(name);
OSMetaClassBase * result = checkMetaCastWithName(tmpKey, in);
tmpKey->release();
return result;
}
/*********************************************************************
* OSMetaClass::checkMetaCast()
* Check to see if the 'check' object has this object in its metaclass chain.
* Returns check if it is indeed a kind of the current meta class, 0 otherwise.
*
* Generally this method is not invoked directly but is used to implement
* the OSMetaClassBase::metaCast member function.
*
* See also OSMetaClassBase::metaCast
*********************************************************************/
OSMetaClassBase * OSMetaClass::checkMetaCast(
const OSMetaClassBase * check) const
{
const OSMetaClass * const toMeta = this;
const OSMetaClass * fromMeta;
for (fromMeta = check->getMetaClass(); ; fromMeta = fromMeta->superClassLink) {
if (toMeta == fromMeta) {
return const_cast<OSMetaClassBase *>(check); // Discard const
}
if (!fromMeta->superClassLink) {
break;
}
}
return 0;
}
/*********************************************************************
*********************************************************************/
void
OSMetaClass::reservedCalled(int ind) const
{
const char * cname = className->getCStringNoCopy();
panic("%s::_RESERVED%s%d called.", cname, cname, ind);
}
/*********************************************************************
*********************************************************************/
const
OSMetaClass *
OSMetaClass::getSuperClass() const
{
return superClassLink;
}
unsigned int
OSMetaClass::getInstanceCount() const
{
return instanceCount;
}
/*********************************************************************
*********************************************************************/
OSDictionary *
OSMetaClass::getClassDictionary()
{
panic("OSMetaClass::getClassDictionary() is obsoleted.\n");
return 0;
}
/*********************************************************************
*********************************************************************/
bool
OSMetaClass::serialize(__unused OSSerialize * s) const
{
panic("OSMetaClass::serialize(): Obsoleted\n");
return false;
}
void libkern_init0()
{
OSMetaClass::postModLoad(0);
}