File: CFRuntime.c

package info (click to toggle)
gnustep-corebase 0.1.1%2B20230710-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,608 kB
  • sloc: ansic: 25,359; objc: 4,347; cpp: 75; xml: 49; makefile: 24; sh: 7
file content (558 lines) | stat: -rw-r--r-- 13,793 bytes parent folder | download
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/* CFRuntime.c
   
   Copyright (C) 2010-2011 Free Software Foundation, Inc.
   
   Written by: Stefan Bidigaray
   Date: January, 2010
   
   This file is part of GNUstep CoreBase Library.
   
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; see the file COPYING.LIB.
   If not, see <http://www.gnu.org/licenses/> or write to the 
   Free Software Foundation, 51 Franklin Street, Fifth Floor, 
   Boston, MA 02110-1301, USA.
*/

#include "CoreFoundation/CFRuntime.h"
#include "CoreFoundation/CFString.h"

#include "GSPrivate.h"
#include "GSObjCRuntime.h"

#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

#if HAVE_OBJC_RUNTIME_H
#include <objc/runtime.h>
#endif


/* GC stuff... */
Boolean kCFUseCollectableAllocator = false;
Boolean (*__CFObjCIsCollectable) (void *) = NULL;

/* CFRuntimeClass Table */
GS_PRIVATE CFRuntimeClass **__CFRuntimeClassTable = NULL;
GS_PRIVATE void **__CFRuntimeObjCClassTable = NULL;
GS_PRIVATE UInt32 __CFRuntimeClassTableCount = 0;
GS_PRIVATE UInt32 __CFRuntimeClassTableSize = 1024;        /* Initial size */

static GSMutex _kCFRuntimeTableLock;

GS_PRIVATE void *NSCFTypeClass = NULL;



/******************************/
/* Object header... lifted from base's NSObject.m */
#ifdef ALIGN
#undef ALIGN
#endif
#define ALIGN __alignof__(double)

/*
 *        Define a structure to hold information that is held locally
 *        (before the start) in each object.
 */
struct obj_layout_unpadded
{
  CFAllocatorRef allocator;
  CFIndex retained;
};
#define UNP sizeof(struct obj_layout_unpadded)

/*
 *        Now do the REAL version - using the other version to determine
 *        what padding (if any) is required to get the alignment of the
 *        structure correct.
 */
struct obj_layout
{
#if !defined(_MSC_VER)
  char padding[ALIGN - ((UNP % ALIGN) ? (UNP % ALIGN) : ALIGN)];
#endif
  CFAllocatorRef allocator;
  CFIndex retained;
};
typedef struct obj_layout *obj;
/******************************/

/* CFNotATypeClass declaration for index 0 of the class table. */
static CFRuntimeClass CFNotATypeClass = {
  0,                            /* Version */
  "CFNotATypeClass",            /* Class name */
  NULL,
  NULL,
  NULL,
  NULL,
  NULL,
  NULL,
  NULL
};

CFTypeID
_CFRuntimeRegisterClass (const CFRuntimeClass * const cls)
{
  CFTypeID ret;

  GSMutexLock (&_kCFRuntimeTableLock);
  if (__CFRuntimeClassTableCount >= __CFRuntimeClassTableSize)
    {
      GSMutexUnlock (&_kCFRuntimeTableLock);
      return _kCFRuntimeNotATypeID;
    }

  __CFRuntimeClassTable[__CFRuntimeClassTableCount] = (CFRuntimeClass *) cls;
  if (__CFRuntimeObjCClassTable)
    __CFRuntimeObjCClassTable[__CFRuntimeClassTableCount] = NSCFTypeClass;
  ret = __CFRuntimeClassTableCount++;
  GSMutexUnlock (&_kCFRuntimeTableLock);

  return ret;
}

const CFRuntimeClass *
_CFRuntimeGetClassWithTypeID (CFTypeID typeID)
{
  GSMutexLock (&_kCFRuntimeTableLock);
  if (typeID > __CFRuntimeClassTableCount)
    typeID = 0;
  GSMutexUnlock (&_kCFRuntimeTableLock);
  return __CFRuntimeClassTable[typeID];
}

void
_CFRuntimeUnregisterClassWithTypeID (CFTypeID typeID)
{
  GSMutexLock (&_kCFRuntimeTableLock);
  __CFRuntimeClassTable[typeID] = NULL;
  GSMutexUnlock (&_kCFRuntimeTableLock);
}



CFTypeRef
_CFRuntimeCreateInstance (CFAllocatorRef allocator, CFTypeID typeID,
                          CFIndex extraBytes, unsigned char *category)
{                               /* category is not used and should be NULL. */
  CFIndex instSize;
  CFRuntimeClass *cls;
  CFRuntimeBase *new;

  /* Return NULL if typeID is unknown. */
  if (_kCFRuntimeNotATypeID == typeID || typeID > __CFRuntimeClassTableCount)
    {
      return NULL;
    }
  if (NULL == allocator)
    allocator = CFAllocatorGetDefault ();

  instSize = sizeof (struct obj_layout) + sizeof (CFRuntimeBase) + extraBytes;
  new = (CFRuntimeBase *) CFAllocatorAllocate (allocator, instSize, 0);
  if (new)
    {
      new = memset (new, 0, instSize);
      ((obj) new)->allocator = allocator;
      new = (CFRuntimeBase *) & ((obj) new)[1];
      new->_isa =
        __CFRuntimeObjCClassTable ? __CFRuntimeObjCClassTable[typeID] : NULL;
      new->_typeID = typeID;

      cls = __CFRuntimeClassTable[typeID];
      if (NULL != cls->init)
        {
          /* Init instance... */
          cls->init (new);
        }
    }

  return new;
}

void
_CFRuntimeSetInstanceTypeID (CFTypeRef cf, CFTypeID typeID)
{
  ((CFRuntimeBase *) cf)->_typeID = typeID;
}

void
_CFRuntimeInitStaticInstance (void *memory, CFTypeID typeID)
{
  CFRuntimeClass *cls;
  CFRuntimeBase *obj = (CFRuntimeBase *) memory;

  if (_kCFRuntimeNotATypeID == typeID
      || typeID >= __CFRuntimeClassTableCount || NULL == memory)
    {
      return;
    }

  cls = __CFRuntimeClassTable[typeID];
  obj->_isa = __CFISAForTypeID (typeID);
  obj->_flags.ro = 1;
  obj->_flags.reserved = 0;
  obj->_flags.info = 0;
  obj->_typeID = typeID;
  if (cls->init != NULL)
    {
      /* Init instance... */
      cls->init (memory);
    }
}



CFStringRef
CFCopyDescription (CFTypeRef cf)
{
  CFRuntimeClass *cfclass;
  CFTypeID typeID;

  if (NULL == cf)
    return NULL;

  typeID = CFGetTypeID (cf);
  CF_OBJC_FUNCDISPATCHV (typeID, CFStringRef, cf, "description");

  if (_kCFRuntimeNotATypeID == typeID)
    return NULL;

  cfclass = __CFRuntimeClassTable[typeID];
  if (NULL != cfclass->copyFormattingDesc)
    {
      return cfclass->copyFormattingDesc (cf, NULL);
    }
  else
    {
      return CFStringCreateWithFormat (NULL, NULL, CFSTR ("<%s: %p>"),
                                       cfclass->className, cf);
    }

  return NULL;
}

CFStringRef
CFCopyTypeIDDescription (CFTypeID typeID)
{
  CFRuntimeClass *cfclass;

  if (_kCFRuntimeNotATypeID == typeID || typeID >= __CFRuntimeClassTableCount)
    return NULL;

  cfclass = __CFRuntimeClassTable[typeID];
  return __CFStringMakeConstantString (cfclass->className);
}

Boolean
CFEqual (CFTypeRef cf1, CFTypeRef cf2)
{
  CFRuntimeClass *cls;
  CFTypeID tID1, tID2;

  if (cf1 == cf2)
    return true;

  if (cf1 == NULL || cf2 == NULL)
    return false;

  /* Can't compare here if either objects are ObjC objects. */
  CF_OBJC_FUNCDISPATCHV (CFGetTypeID (cf1), Boolean, cf1, "isEqual:", cf2);
  CF_OBJC_FUNCDISPATCHV (CFGetTypeID (cf2), Boolean, cf2, "isEqual:", cf1);

  tID1 = CFGetTypeID (cf1);
  tID2 = CFGetTypeID (cf2);
  if (tID1 != tID2)
    return false;

  cls = __CFRuntimeClassTable[tID1];
  if (NULL != cls->equal)
    return cls->equal (cf1, cf2);

  return false;
}

CFAllocatorRef
CFGetAllocator (CFTypeRef cf)
{
  if (CF_IS_OBJC (CFGetTypeID (cf), cf) || ((CFRuntimeBase *) cf)->_flags.ro)
    return kCFAllocatorSystemDefault;

  return ((obj) cf)[-1].allocator;
}

CFIndex
CFGetRetainCount (CFTypeRef cf)
{
  CF_OBJC_FUNCDISPATCHV (CFGetTypeID (cf), CFIndex, cf, "retainCount");

  if (!((CFRuntimeBase *) cf)->_flags.ro)
    return ((obj) cf)[-1].retained + 1;

  return UINT_MAX;
}

CFTypeID
CFGetTypeID (CFTypeRef cf)
{
  if (cf == NULL)
    return _kCFRuntimeNotATypeID;

#if defined(OBJC_SMALL_OBJECT_MASK)
  CFTypeID typeID = _kCFRuntimeNotATypeID;

  /* Small objects in ObjC are not valid pointers,
     hence we must avoid accessing them. */
  if (((uintptr_t) cf & OBJC_SMALL_OBJECT_MASK) == 0)
    typeID = ((CFRuntimeBase *) cf)->_typeID;

  CF_OBJC_FUNCDISPATCHV (typeID, CFTypeID, cf, "_cfTypeID");
#endif
  return ((CFRuntimeBase *) cf)->_typeID;
}

CFHashCode
CFHash (CFTypeRef cf)
{
  CFRuntimeClass *cls;

  CF_OBJC_FUNCDISPATCHV (CFGetTypeID (cf), CFHashCode, cf, "hash");

  cls = __CFRuntimeClassTable[CFGetTypeID (cf)];
  if (cls->hash)
    return cls->hash (cf);

  return (CFHashCode) ((uintptr_t) cf >> 3);
}

CFTypeRef
CFMakeCollectable (CFTypeRef cf)
{
/* FIXME */
  return cf;
}

void
CFRelease (CFTypeRef cf)
{
#if defined (OBJC_SMALL_OBJECT_MASK)
  if (((unsigned long)cf & OBJC_SMALL_OBJECT_MASK) == 0)
#endif
    {
      CF_OBJC_FUNCDISPATCHV (CFGetTypeID (cf), void, cf, "release");

      if (!((CFRuntimeBase *) cf)->_flags.ro)
        {
          CFIndex result = GSAtomicDecrementCFIndex (&(((obj) cf)[-1].retained));
          if (result < 0)
            {
              assert (result == -1);
              GSRuntimeDeallocateInstance (cf);
            }
        }
    }
}

CFTypeRef
CFRetain (CFTypeRef cf)
{
#if defined (OBJC_SMALL_OBJECT_MASK)
  if (((unsigned long)cf & OBJC_SMALL_OBJECT_MASK) == 0)
#endif
    {
      CF_OBJC_FUNCDISPATCHV (CFGetTypeID (cf), CFTypeRef, cf, "retain");

      if (!((CFRuntimeBase *) cf)->_flags.ro)
        {
          CFIndex result = GSAtomicIncrementCFIndex (&(((obj) cf)[-1].retained));
          assert (result < INT_MAX);
        }
    }
  return cf;
}

CFTypeRef
CFAutorelease (CFTypeRef cf)
{
#if __has_feature(objc_arc)
  return objc_autoreleaseReturnValue(cf);
#else
  CF_OBJC_CALLV(CFTypeRef, cf, cf, "autorelease");
  return cf;
#endif
}

void *
_CFBridgingRelease (CFTypeRef cf)
{
  CF_OBJC_CALLV(CFTypeRef, cf, cf, "autorelease");
  return (void *)cf;
}

CFTypeRef
_CFBridgingRetain (void *obj)
{
  CF_OBJC_CALLV(void *, obj, obj, "retain");
  return (CFTypeRef)obj;
}

const void *
CFTypeRetainCallBack (CFAllocatorRef allocator, const void *value)
{
  return CFRetain (value);
}

void
CFTypeReleaseCallBack (CFAllocatorRef alloc, const void *value)
{
  CFRelease (value);
}



void
GSRuntimeDeallocateInstance (CFTypeRef cf)
{
  CFRuntimeClass *cls;
  cls = __CFRuntimeClassTable[CFGetTypeID (cf)];

  if (cls->finalize)
    cls->finalize (cf);
  CFAllocatorDeallocate (CFGetAllocator (cf), (void *) &((obj) cf)[-1]);
}

static CFTypeRef GSRuntimeConstantTable[512];
static CFIndex GSRuntimeConstantTableSize = 0;

void
GSRuntimeConstantInit (CFTypeRef cf, CFTypeID typeID)
{
  ((CFRuntimeBase *) cf)->_typeID = typeID;
  GSRuntimeConstantTable[GSRuntimeConstantTableSize++] = cf;
}

GS_PRIVATE void
GSRuntimeInitializeConstants (void)
{
  CFIndex i;
  CFTypeID tid;

  for (i = 0; i < GSRuntimeConstantTableSize; ++i)
    {
      tid = ((CFRuntimeBase *) GSRuntimeConstantTable[i])->_typeID;
      ((CFRuntimeBase *) GSRuntimeConstantTable[i])->_isa =
        __CFRuntimeObjCClassTable[tid];
    }
}

GS_PRIVATE void CFAllocatorInitialize (void);
GS_PRIVATE void CFArrayInitialize (void);
GS_PRIVATE void CFAttributedStringInitialize (void);
GS_PRIVATE void CFBagInitialize (void);
GS_PRIVATE void CFBinaryHeapInitialize (void);
GS_PRIVATE void CFBitVectorInitialize (void);
GS_PRIVATE void CFBooleanInitialize (void);
GS_PRIVATE void CFCalendarInitialize (void);
GS_PRIVATE void CFCharacterSetInitialize (void);
GS_PRIVATE void CFDataInitialize (void);
GS_PRIVATE void CFBundleInitialize (void);
GS_PRIVATE void CFDateInitialize (void);
GS_PRIVATE void CFDateFormatterInitialize (void);
GS_PRIVATE void CFDictionaryInitialize (void);
GS_PRIVATE void CFErrorInitialize (void);
GS_PRIVATE void CFLocaleInitialize (void);
GS_PRIVATE void CFNullInitialize (void);
GS_PRIVATE void CFNumberInitialize (void);
GS_PRIVATE void CFNumberFormatterInitialize (void);
GS_PRIVATE void CFRunLoopInitialize (void);
GS_PRIVATE void CFSetInitialize (void);
GS_PRIVATE void CFStreamInitialize (void);
GS_PRIVATE void CFStringInitialize (void);
GS_PRIVATE void CFConstantStringInitialize (void);
GS_PRIVATE void CFStringEncodingInitialize (void);
GS_PRIVATE void CFTimeZoneInitialize (void);
GS_PRIVATE void CFTreeInitialize (void);
GS_PRIVATE void CFURLInitialize (void);
GS_PRIVATE void CFUUIDInitialize (void);
GS_PRIVATE void CFXMLNodeInitialize (void);

#if !defined(_MSC_VER)
GS_PRIVATE void CFInitialize (void) __attribute__ ((constructor));
#endif

static CFIndex CFInitialized = 0;
void
CFInitialize (void)
{
  /* Only initialize once. */
  if (GSAtomicCompareAndSwapCFIndex (&CFInitialized, 0, 1) == 1)
    return;

  /* Initialize CFRuntimeClassTable */
  __CFRuntimeClassTable = (CFRuntimeClass **) calloc (__CFRuntimeClassTableSize,
                                                      sizeof (CFRuntimeClass
                                                              *));

  GSMutexInitialize (&_kCFRuntimeTableLock);

  /* CFNotATypeClass should be at index = 0 */
  _CFRuntimeRegisterClass (&CFNotATypeClass);

  CFAllocatorInitialize ();
  CFArrayInitialize ();
  CFAttributedStringInitialize ();
  CFBagInitialize ();
  CFBinaryHeapInitialize ();
  CFBitVectorInitialize ();
  CFBooleanInitialize ();
  CFCalendarInitialize ();
  CFCharacterSetInitialize ();
  CFDataInitialize ();
  CFBundleInitialize ();
  CFDateInitialize ();
  CFDateFormatterInitialize ();
  CFDictionaryInitialize ();
  CFErrorInitialize ();
  CFLocaleInitialize ();
  CFNullInitialize ();
  CFNumberInitialize ();
  CFNumberFormatterInitialize ();
  CFSetInitialize ();
  CFStreamInitialize ();
  CFStringInitialize ();
  CFConstantStringInitialize (); /* must be after CFStringIntialize () */
  CFStringEncodingInitialize ();
  CFTimeZoneInitialize ();
  CFTreeInitialize ();
  CFURLInitialize ();
  CFUUIDInitialize ();
  CFXMLNodeInitialize ();
  CFRunLoopInitialize ();
}

#if defined(_MSC_VER)
#include <windows.h>

WinBOOL
DllMain (HINSTANCE hinst, DWORD fdwReason, LPVOID ldvReserved)
{
  if (fdwReason == DLL_PROCESS_ATTACH)
    {
      CFInitialize ();
    }

  return TRUE;
}
#endif