1
2
3
4
5
6
7
8
9
|
1
2
3
4
5
6
7
8
9
|
-
+
|
/*
* Copyright (c) 2008-2022 Jonathan Schleifer <js@nil.im>
* Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
*
* All rights reserved.
*
* This file is part of ObjFW. It may be distributed under the terms of the
* Q Public License 1.0, which can be found in the file LICENSE.QPL included in
* the packaging of this file.
*
|
| ︙ | | |
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
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
|
+
+
+
+
-
+
|
# define OF_INLINE inline
# define OF_LIKELY(cond) (cond)
# define OF_UNLIKELY(cond) (cond)
# define OF_CONST_FUNC
# define OF_NO_RETURN_FUNC
# define OF_WEAK_REF(sym)
#endif
#ifdef __GNUC__
# define OF_ALIGN(alignment) __attribute__((__aligned__(alignment)))
#endif
#if __STDC_VERSION__ >= 201112L
# define OF_ALIGNOF(type) _Alignof(type)
# define OF_ALIGNAS(type) _Alignas(type)
#else
# define OF_ALIGNOF(type) __alignof__(type)
# define OF_ALIGNAS(type) __attribute__((__aligned__(__alignof__(type))))
# define OF_ALIGNAS(type) OF_ALIGN(OF_ALIGNOF(type))
#endif
#if __STDC_VERSION__ >= 201112L && defined(OF_HAVE_MAX_ALIGN_T)
# define OF_BIGGEST_ALIGNMENT _Alignof(max_align_t)
#else
# ifdef __BIGGEST_ALIGNMENT__
# define OF_BIGGEST_ALIGNMENT __BIGGEST_ALIGNMENT__
|
| ︙ | | |
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
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
|
+
+
+
+
+
+
+
+
+
+
+
|
#if __has_attribute(__objc_designated_initializer__)
# define OF_DESIGNATED_INITIALIZER \
__attribute__((__objc_designated_initializer__))
#else
# define OF_DESIGNATED_INITIALIZER
#endif
#if defined(__clang__) || OF_GCC_VERSION >= 405
# define OF_DEPRECATED(project, major, minor, msg) \
__attribute__((__deprecated__("Deprecated in " #project " " \
#major "." #minor ": " msg)))
#elif defined(__GNUC__)
# define OF_DEPRECATED(project, major, minor, msg) \
__attribute__((__deprecated__))
#else
# define OF_DEPRECATED(project, major, minor, msg)
#endif
#if __has_attribute(__objc_boxable__)
# define OF_BOXABLE __attribute__((__objc_boxable__))
#else
# define OF_BOXABLE
#endif
|
| ︙ | | |
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
|
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
|
-
+
-
+
-
+
-
+
+
+
+
+
+
+
+
+
-
-
+
+
-
+
+
+
+
+
+
|
#if __has_attribute(__objc_direct_members__) && defined(OF_APPLE_RUNTIME)
# define OF_DIRECT_MEMBERS __attribute__((__objc_direct_members__))
#else
# define OF_DIRECT_MEMBERS
#endif
#ifdef OF_APPLE_RUNTIME
# if defined(OF_X86_64) || defined(OF_X86) || defined(OF_ARM64) || \
# if defined(OF_AMD64) || defined(OF_X86) || defined(OF_ARM64) || \
defined(OF_ARM) || defined(OF_POWERPC)
# define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR
# define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR_STRET
# endif
#else
# if defined(OF_ELF)
# if defined(OF_X86_64) || defined(OF_X86) || \
# if defined(OF_AMD64) || defined(OF_X86) || \
defined(OF_ARM64) || defined(OF_ARM) || defined(OF_POWERPC) || \
defined(OF_MIPS) || defined(OF_SPARC64) || defined(OF_SPARC)
# define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR
# if __OBJFW_RUNTIME_ABI__ >= 800
# define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR_STRET
# endif
# endif
# elif defined(OF_MACH_O)
# if defined(OF_X86_64)
# if defined(OF_AMD64)
# define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR
# if __OBJFW_RUNTIME_ABI__ >= 800
# define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR_STRET
# endif
# endif
# elif defined(OF_WINDOWS)
# if defined(OF_X86_64) || defined(OF_X86)
# if defined(OF_AMD64) || defined(OF_X86)
# define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR
# if __OBJFW_RUNTIME_ABI__ >= 800
# define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR_STRET
# endif
# endif
# endif
#endif
#define OFMaxRetainCount UINT_MAX
#ifdef OBJC_COMPILING_RUNTIME
# define OFEnsure(cond) \
do { \
if OF_UNLIKELY (!(cond)) \
objc_error("ObjFWRT @ " __FILE__ ":" \
OF_STRINGIFY(__LINE__), \
"Failed to ensure condition:\n" #cond); \
} while(0)
#else
@class OFConstantString;
# ifdef __cplusplus
extern "C" {
# endif
extern void OFLog(OFConstantString *_Nonnull, ...);
# ifdef __cplusplus
}
# endif
# define OFEnsure(cond) \
do { \
if OF_UNLIKELY (!(cond)) { \
fprintf(stderr, "Failed to ensure condition " \
"in " __FILE__ ":%d:\n" #cond "\n", \
OFLog(@"Failed to ensure condition in " \
@__FILE__ ":%d: " @#cond, __LINE__); \
__LINE__); \
abort(); \
} \
} while (0)
#endif
#ifndef NDEBUG
# define OFAssert(...) OFEnsure(__VA_ARGS__)
#else
# define OFAssert(...)
#endif
#define OF_UNRECOGNIZED_SELECTOR OFMethodNotFound(self, _cmd);
#if __has_feature(objc_arc)
# define OF_INVALID_INIT_METHOD OFMethodNotFound(self, _cmd);
#else
# define OF_INVALID_INIT_METHOD \
@try { \
|
| ︙ | | |
398
399
400
401
402
403
404
405
406
407
408
409
410
411
|
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
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
# define OF_DEALLOC_UNSUPPORTED \
[self doesNotRecognizeSelector: _cmd]; \
\
abort(); \
\
[super dealloc]; /* Get rid of a stupid warning */
#endif
#define OF_SINGLETON_METHODS \
- (instancetype)autorelease \
{ \
return self; \
} \
\
- (instancetype)retain \
{ \
return self; \
} \
\
- (void)release \
{ \
} \
\
- (unsigned int)retainCount \
{ \
return OFMaxRetainCount; \
} \
\
- (void)dealloc \
{ \
OF_DEALLOC_UNSUPPORTED \
}
#define OF_CONSTRUCTOR(prio) \
static void __attribute__((__constructor__(prio))) \
OF_PREPROCESSOR_CONCAT(constructor, __LINE__)(void)
#define OF_DESTRUCTOR(prio) \
static void __attribute__((__destructor__(prio))) \
OF_PREPROCESSOR_CONCAT(destructor, __LINE__)(void)
|
| ︙ | | |
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
|
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
|
-
+
-
-
-
+
+
+
-
-
+
+
+
-
-
+
+
-
+
-
-
+
+
-
-
+
+
+
-
-
+
+
-
+
-
-
+
+
-
-
-
-
-
+
+
+
+
+
|
}
static OF_INLINE uint16_t OF_CONST_FUNC
OFByteSwap16NonConst(uint16_t i)
{
#if defined(OF_HAVE_BUILTIN_BSWAP16)
return __builtin_bswap16(i);
#elif (defined(OF_X86_64) || defined(OF_X86)) && defined(__GNUC__)
#elif (defined(OF_AMD64) || defined(OF_X86)) && defined(__GNUC__)
__asm__ (
"xchgb %h0, %b0"
: "=Q"(i)
: "0"(i)
"xchg{b} { %h0, %b0 | %b0, %h0 }"
: "=Q" (i)
: "0" (i)
);
#elif defined(OF_POWERPC) && defined(__GNUC__)
__asm__ (
"lhbrx %0, 0, %1"
: "=r"(i)
: "r"(&i), "m"(i)
: "=r" (i)
: "r" (&i),
"m" (i)
);
#elif defined(OF_ARMV6) && defined(__GNUC__)
__asm__ (
"rev16 %0, %0"
: "=r"(i)
: "0"(i)
: "=r" (i)
: "0" (i)
);
#else
i = (i & UINT16_C(0xFF00)) >> 8 |
(i & UINT16_C(0x00FF)) << 8;
#endif
return i;
}
static OF_INLINE uint32_t OF_CONST_FUNC
OFByteSwap32NonConst(uint32_t i)
{
#if defined(OF_HAVE_BUILTIN_BSWAP32)
return __builtin_bswap32(i);
#elif (defined(OF_X86_64) || defined(OF_X86)) && defined(__GNUC__)
#elif (defined(OF_AMD64) || defined(OF_X86)) && defined(__GNUC__)
__asm__ (
"bswap %0"
: "=q"(i)
: "0"(i)
: "=q" (i)
: "0" (i)
);
#elif defined(OF_POWERPC) && defined(__GNUC__)
__asm__ (
"lwbrx %0, 0, %1"
: "=r"(i)
: "r"(&i), "m"(i)
: "=r" (i)
: "r" (&i),
"m" (i)
);
#elif defined(OF_ARMV6) && defined(__GNUC__)
__asm__ (
"rev %0, %0"
: "=r"(i)
: "0"(i)
: "=r" (i)
: "0" (i)
);
#else
i = (i & UINT32_C(0xFF000000)) >> 24 |
(i & UINT32_C(0x00FF0000)) >> 8 |
(i & UINT32_C(0x0000FF00)) << 8 |
(i & UINT32_C(0x000000FF)) << 24;
#endif
return i;
}
static OF_INLINE uint64_t OF_CONST_FUNC
OFByteSwap64NonConst(uint64_t i)
{
#if defined(OF_HAVE_BUILTIN_BSWAP64)
return __builtin_bswap64(i);
#elif defined(OF_X86_64) && defined(__GNUC__)
#elif defined(OF_AMD64) && defined(__GNUC__)
__asm__ (
"bswap %0"
: "=r"(i)
: "0"(i)
: "=r" (i)
: "0" (i)
);
#elif defined(OF_X86) && defined(__GNUC__)
__asm__ (
"bswap %%eax\n\t"
"bswap %%edx\n\t"
"xchgl %%eax, %%edx"
: "=A"(i)
: "0"(i)
"bswap {%%}eax\n\t"
"bswap {%%}edx\n\t"
"xchg{l} { %%eax, %%edx | edx, eax }"
: "=A" (i)
: "0" (i)
);
#else
i = (uint64_t)OFByteSwap32NonConst(
(uint32_t)(i & UINT32_C(0xFFFFFFFF))) << 32 |
OFByteSwap32NonConst((uint32_t)(i >> 32));
#endif
return i;
|
| ︙ | | |