20 #ifndef OBJFW_MACROS_H 21 #define OBJFW_MACROS_H 23 #include "objfw-defs.h" 25 #ifndef __STDC_LIMIT_MACROS 26 # define __STDC_LIMIT_MACROS 28 #ifndef __STDC_CONSTANT_MACROS 29 # define __STDC_CONSTANT_MACROS 46 #ifdef OF_OBJFW_RUNTIME 47 # ifdef OF_COMPILING_OBJFW 50 # include <ObjFWRT/ObjFWRT.h> 53 #ifdef OF_APPLE_RUNTIME 54 # include <objc/objc.h> 55 # include <objc/runtime.h> 56 # include <objc/message.h> 60 # define restrict __restrict__ 61 #elif __STDC_VERSION__ < 199901L 65 #if __STDC_VERSION__ >= 201112L && !defined(static_assert) 67 # define static_assert _Static_assert 70 #if defined(OF_HAVE__THREAD_LOCAL) 71 # define OF_HAVE_COMPILER_TLS 72 # ifdef OF_HAVE_THREADS_H 77 # define thread_local _Thread_local 80 # define thread_local _Thread_local 82 #elif defined(OF_HAVE___THREAD) 83 # define OF_HAVE_COMPILER_TLS 84 # define thread_local __thread 91 #if defined(OF_HAVE_COMPILER_TLS) && defined(OF_IOS) && defined(OF_X86) 92 # undef OF_HAVE_COMPILER_TLS 96 # define OF_INLINE inline __attribute__((__always_inline__)) 97 # define OF_LIKELY(cond) (__builtin_expect(!!(cond), 1)) 98 # define OF_UNLIKELY(cond) (__builtin_expect(!!(cond), 0)) 99 # define OF_CONST_FUNC __attribute__((__const__)) 100 # define OF_NO_RETURN_FUNC __attribute__((__noreturn__)) 101 # define OF_WEAK_REF(sym) __attribute__((__weakref__(sym))) 102 # define OF_VISIBILITY_HIDDEN __attribute__((__visibility__("hidden"))) 104 # define OF_INLINE inline 105 # define OF_LIKELY(cond) (cond) 106 # define OF_UNLIKELY(cond) (cond) 107 # define OF_CONST_FUNC 108 # define OF_NO_RETURN_FUNC 109 # define OF_WEAK_REF(sym) 110 # define OF_VISIBILITY_HIDDEN 113 #if __STDC_VERSION__ >= 201112L 114 # define OF_ALIGN(size) _Alignas(size) 115 # define OF_ALIGNOF(type) _Alignof(type) 116 # define OF_ALIGNAS(type) _Alignas(type) 118 # define OF_ALIGN(size) __attribute__((__aligned__(size))) 119 # define OF_ALIGNOF(type) __alignof__(type) 120 # define OF_ALIGNAS(type) OF_ALIGN(OF_ALIGNOF(type)) 123 #ifdef __BIGGEST_ALIGNMENT__ 124 # define OF_BIGGEST_ALIGNMENT __BIGGEST_ALIGNMENT__ 127 # define OF_BIGGEST_ALIGNMENT 16 133 #if (defined(OF_AMD64) || defined(OF_X86)) && OF_BIGGEST_ALIGNMENT < 16 134 # undef OF_BIGGEST_ALIGNMENT 135 # define OF_BIGGEST_ALIGNMENT 16 138 #define OF_PREPROCESSOR_CONCAT2(a, b) a##b 139 #define OF_PREPROCESSOR_CONCAT(a, b) OF_PREPROCESSOR_CONCAT2(a, b) 141 #if __OBJFW_RUNTIME_ABI__ || (defined(OF_APPLE_RUNTIME) && defined(__OBJC2__)) 142 # define OF_HAVE_NONFRAGILE_IVARS 145 #ifdef OF_HAVE_NONFRAGILE_IVARS 146 # define OF_RESERVE_IVARS(cls, num) 148 # define OF_RESERVE_IVARS(cls, num) \ 150 void *OF_PREPROCESSOR_CONCAT(_reserved_, cls)[num]; 154 # define OF_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) 156 # define OF_GCC_VERSION 0 159 #define OF_STRINGIFY(s) OF_STRINGIFY2(s) 160 #define OF_STRINGIFY2(s) #s 162 #ifndef __has_feature 163 # define __has_feature(x) 0 166 #ifndef __has_attribute 167 # define __has_attribute(x) 0 170 #if __has_feature(objc_bool) 172 # define YES __objc_yes 174 # define NO __objc_no 177 # define true ((bool)1) 179 # define false ((bool)0) 183 #if !__has_feature(objc_instancetype) 184 # define instancetype id 187 #if __has_feature(blocks) 188 # define OF_HAVE_BLOCKS 191 #if __has_feature(objc_arc) 192 # define OF_RETURNS_RETAINED __attribute__((__ns_returns_retained__)) 193 # define OF_RETURNS_NOT_RETAINED __attribute__((__ns_returns_not_retained__)) 194 # define OF_RETURNS_INNER_POINTER \ 195 __attribute__((__objc_returns_inner_pointer__)) 196 # define OF_CONSUMED __attribute__((__ns_consumed__)) 197 # define OF_WEAK_UNAVAILABLE __attribute__((__objc_arc_weak_unavailable__)) 199 # define OF_RETURNS_RETAINED 200 # define OF_RETURNS_NOT_RETAINED 201 # define OF_RETURNS_INNER_POINTER 203 # define OF_WEAK_UNAVAILABLE 208 # undef __unsafe_unretained 210 # undef __autoreleasing 211 # define __unsafe_unretained 213 # define __autoreleasing 216 #if __has_feature(objc_generics) 217 # define OF_HAVE_GENERICS 218 # define OF_GENERIC(...) <__VA_ARGS__> 220 # define OF_GENERIC(...) 223 #if __has_feature(nullability) 224 # define OF_ASSUME_NONNULL_BEGIN _Pragma("clang assume_nonnull begin") 225 # define OF_ASSUME_NONNULL_END _Pragma("clang assume_nonnull end") 226 # define OF_NULLABLE_PROPERTY(...) (__VA_ARGS__, nullable) 227 # define OF_NULL_RESETTABLE_PROPERTY(...) (__VA_ARGS__, null_resettable) 229 # define OF_ASSUME_NONNULL_BEGIN 230 # define OF_ASSUME_NONNULL_END 233 # define _Null_unspecified 234 # define OF_NULLABLE_PROPERTY 235 # define OF_NULL_RESETTABLE_PROPERTY 238 # define null_unspecified 241 #if __has_feature(objc_kindof) 242 # define OF_KINDOF(class_) __kindof class_ 244 # define OF_KINDOF(class_) id 247 #if __has_feature(objc_class_property) 248 # define OF_HAVE_CLASS_PROPERTIES 251 #if defined(__clang__) || OF_GCC_VERSION >= 405 252 # define OF_UNREACHABLE __builtin_unreachable(); 254 # define OF_UNREACHABLE abort(); 257 #if defined(__clang__) || OF_GCC_VERSION >= 406 258 # define OF_SENTINEL __attribute__((__sentinel__)) 259 # define OF_NO_RETURN __attribute__((__noreturn__)) 262 # define OF_NO_RETURN 266 # define OF_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__)) 268 # define OF_WARN_UNUSED_RESULT 271 #if __has_attribute(__unavailable__) 272 # define OF_UNAVAILABLE __attribute__((__unavailable__)) 274 # define OF_UNAVAILABLE 277 #if __has_attribute(__objc_requires_super__) 278 # define OF_REQUIRES_SUPER __attribute__((__objc_requires_super__)) 280 # define OF_REQUIRES_SUPER 283 #if __has_attribute(__objc_root_class__) 284 # define OF_ROOT_CLASS __attribute__((__objc_root_class__)) 286 # define OF_ROOT_CLASS 289 #if __has_attribute(__objc_subclassing_restricted__) 290 # define OF_SUBCLASSING_RESTRICTED \ 291 __attribute__((__objc_subclassing_restricted__)) 293 # define OF_SUBCLASSING_RESTRICTED 296 #if __has_attribute(__objc_method_family__) 297 # define OF_METHOD_FAMILY(f) __attribute__((__objc_method_family__(f))) 299 # define OF_METHOD_FAMILY(f) 302 #if __has_attribute(__objc_designated_initializer__) 303 # define OF_DESIGNATED_INITIALIZER \ 304 __attribute__((__objc_designated_initializer__)) 306 # define OF_DESIGNATED_INITIALIZER 309 #if defined(__clang__) || OF_GCC_VERSION >= 405 310 # define OF_DEPRECATED(project, major, minor, msg) \ 311 __attribute__((__deprecated__("Deprecated in " #project " " \ 312 #major "." #minor ": " msg))) 313 #elif defined(__GNUC__) 314 # define OF_DEPRECATED(project, major, minor, msg) \ 315 __attribute__((__deprecated__)) 317 # define OF_DEPRECATED(project, major, minor, msg) 320 #if __has_attribute(__objc_boxable__) 321 # define OF_BOXABLE __attribute__((__objc_boxable__)) 326 #if __has_attribute(__swift_name__) 327 # define OF_SWIFT_NAME(name) __attribute__((__swift_name__(name))) 329 # define OF_SWIFT_NAME(name) 332 #if __has_attribute(__objc_direct__) && defined(OF_APPLE_RUNTIME) 333 # define OF_DIRECT __attribute__((__objc_direct__)) 337 #if __has_attribute(__objc_direct_members__) && defined(OF_APPLE_RUNTIME) 338 # define OF_DIRECT_MEMBERS __attribute__((__objc_direct_members__)) 340 # define OF_DIRECT_MEMBERS 343 #ifdef OF_APPLE_RUNTIME 344 # if defined(OF_AMD64) || defined(OF_X86) || defined(OF_ARM64) || \ 345 defined(OF_ARM) || defined(OF_POWERPC) 346 # define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR 347 # define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR_STRET 351 # if defined(OF_AMD64) || defined(OF_X86) || \ 352 defined(OF_ARM64) || defined(OF_ARM) || defined(OF_POWERPC) || \ 353 defined(OF_MIPS) || defined(OF_SPARC64) || defined(OF_SPARC) 354 # define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR 355 # if __OBJFW_RUNTIME_ABI__ >= 800 356 # define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR_STRET 359 # elif defined(OF_MACH_O) 360 # if defined(OF_AMD64) 361 # define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR 362 # if __OBJFW_RUNTIME_ABI__ >= 800 363 # define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR_STRET 366 # elif defined(OF_WINDOWS) 367 # if defined(OF_AMD64) || defined(OF_X86) 368 # define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR 369 # if __OBJFW_RUNTIME_ABI__ >= 800 370 # define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR_STRET 376 #define OFMaxRetainCount UINT_MAX 378 #ifdef OBJC_COMPILING_RUNTIME 379 # define OFEnsure(cond) \ 381 if OF_UNLIKELY (!(cond)) \ 382 objc_error("ObjFWRT @ " __FILE__ ":" \ 383 OF_STRINGIFY(__LINE__), \ 384 "Failed to ensure condition:\n" #cond); \ 395 # define OFEnsure(cond) \ 397 if OF_UNLIKELY (!(cond)) { \ 398 OFLog(@"Failed to ensure condition in " \ 399 @__FILE__ ":%d: " @#cond, __LINE__); \ 406 # define OFAssert(...) OFEnsure(__VA_ARGS__) 408 # define OFAssert(...) 411 #define OF_UNRECOGNIZED_SELECTOR OFMethodNotFound(self, _cmd); 412 #if __has_feature(objc_arc) 413 # define OF_INVALID_INIT_METHOD OFMethodNotFound(self, _cmd); 415 # define OF_INVALID_INIT_METHOD \ 417 OFMethodNotFound(self, _cmd); \ 426 # define OF_DEALLOC_UNSUPPORTED \ 427 [self doesNotRecognizeSelector: _cmd]; \ 431 _Pragma("clang diagnostic push"); \ 432 _Pragma("clang diagnostic ignored \"-Wunreachable-code\""); \ 434 _Pragma("clang diagnostic pop"); 436 # define OF_DEALLOC_UNSUPPORTED \ 437 [self doesNotRecognizeSelector: _cmd]; \ 443 #define OF_SINGLETON_METHODS \ 444 - (instancetype)autorelease \ 449 - (instancetype)retain \ 458 - (unsigned int)retainCount \ 460 return OFMaxRetainCount; \ 465 OF_DEALLOC_UNSUPPORTED \ 468 #define OF_CONSTRUCTOR(prio) \ 469 static void __attribute__((__constructor__(prio))) \ 470 OF_PREPROCESSOR_CONCAT(constructor, __LINE__)(void) 471 #define OF_DESTRUCTOR(prio) \ 472 static void __attribute__((__destructor__(prio))) \ 473 OF_PREPROCESSOR_CONCAT(destructor, __LINE__)(void) 475 static OF_INLINE uint16_t OF_CONST_FUNC
476 _OFByteSwap16Const(uint16_t i)
478 return (i & UINT16_C(0xFF00)) >> 8 | (i & UINT16_C(0x00FF)) << 8;
481 static OF_INLINE uint32_t OF_CONST_FUNC
482 _OFByteSwap32Const(uint32_t i)
484 return (i & UINT32_C(0xFF000000)) >> 24 |
485 (i & UINT32_C(0x00FF0000)) >> 8 |
486 (i & UINT32_C(0x0000FF00)) << 8 |
487 (i & UINT32_C(0x000000FF)) << 24;
490 static OF_INLINE uint64_t OF_CONST_FUNC
491 _OFByteSwap64Const(uint64_t i)
493 return (i & UINT64_C(0xFF00000000000000)) >> 56 |
494 (i & UINT64_C(0x00FF000000000000)) >> 40 |
495 (i & UINT64_C(0x0000FF0000000000)) >> 24 |
496 (i & UINT64_C(0x000000FF00000000)) >> 8 |
497 (i & UINT64_C(0x00000000FF000000)) << 8 |
498 (i & UINT64_C(0x0000000000FF0000)) << 24 |
499 (i & UINT64_C(0x000000000000FF00)) << 40 |
500 (i & UINT64_C(0x00000000000000FF)) << 56;
503 static OF_INLINE uint16_t OF_CONST_FUNC
504 _OFByteSwap16NonConst(uint16_t i)
506 #if defined(OF_HAVE_BUILTIN_BSWAP16) 507 return __builtin_bswap16(i);
508 #elif (defined(OF_AMD64) || defined(OF_X86)) && defined(__GNUC__) 510 "xchg{b} { %h0, %b0 | %b0, %h0 }" 514 #elif defined(OF_POWERPC) && defined(__GNUC__) 521 #elif defined(OF_ARMV6) && defined(__GNUC__) 528 i = (i & UINT16_C(0xFF00)) >> 8 |
529 (i & UINT16_C(0x00FF)) << 8;
534 static OF_INLINE uint32_t OF_CONST_FUNC
535 _OFByteSwap32NonConst(uint32_t i)
537 #if defined(OF_HAVE_BUILTIN_BSWAP32) 538 return __builtin_bswap32(i);
539 #elif (defined(OF_AMD64) || defined(OF_X86)) && defined(__GNUC__) 545 #elif defined(OF_POWERPC) && defined(__GNUC__) 552 #elif defined(OF_ARMV6) && defined(__GNUC__) 559 i = (i & UINT32_C(0xFF000000)) >> 24 |
560 (i & UINT32_C(0x00FF0000)) >> 8 |
561 (i & UINT32_C(0x0000FF00)) << 8 |
562 (i & UINT32_C(0x000000FF)) << 24;
567 static OF_INLINE uint64_t OF_CONST_FUNC
568 _OFByteSwap64NonConst(uint64_t i)
570 #if defined(OF_HAVE_BUILTIN_BSWAP64) 571 return __builtin_bswap64(i);
572 #elif defined(OF_AMD64) && defined(__GNUC__) 578 #elif defined(OF_X86) && defined(__GNUC__) 582 "xchg{l} { %%eax, %%edx | edx, eax }" 587 i = (uint64_t)_OFByteSwap32NonConst(
588 (uint32_t)(i & UINT32_C(0xFFFFFFFF))) << 32 |
589 _OFByteSwap32NonConst((uint32_t)(i >> 32));
594 #if defined(__GNUC__) || defined(DOXYGEN) 601 # define OFByteSwap16(i) \ 602 (__builtin_constant_p(i) ? _OFByteSwap16Const(i) : _OFByteSwap16NonConst(i)) 610 # define OFByteSwap32(i) \ 611 (__builtin_constant_p(i) ? _OFByteSwap32Const(i) : _OFByteSwap32NonConst(i)) 619 # define OFByteSwap64(i) \ 620 (__builtin_constant_p(i) ? _OFByteSwap64Const(i) : _OFByteSwap64NonConst(i)) 622 # define OFByteSwap16(i) _OFByteSwap16Const(i) 623 # define OFByteSwap32(i) _OFByteSwap32Const(i) 624 # define OFByteSwap64(i) _OFByteSwap64Const(i) 633 static OF_INLINE uint32_t OF_CONST_FUNC
647 static OF_INLINE
float OF_CONST_FUNC
651 memcpy(&ret, &uInt32, 4);
661 static OF_INLINE uint64_t OF_CONST_FUNC
675 static OF_INLINE
double OF_CONST_FUNC
679 memcpy(&ret, &uInt64, 8);
689 static OF_INLINE
float OF_CONST_FUNC
702 static OF_INLINE
double OF_CONST_FUNC
709 #if defined(OF_BIG_ENDIAN) || defined(DOXYGEN) 717 # define OFFromBigEndian16(i) (i) 726 # define OFFromBigEndian32(i) (i) 735 # define OFFromBigEndian64(i) (i) 744 # define OFFromLittleEndian16(i) OFByteSwap16(i) 753 # define OFFromLittleEndian32(i) OFByteSwap32(i) 762 # define OFFromLittleEndian64(i) OFByteSwap64(i) 771 # define OFToBigEndian16(i) (i) 780 # define OFToBigEndian32(i) (i) 789 # define OFToBigEndian64(i) (i) 798 # define OFToLittleEndian16(i) OFByteSwap16(i) 807 # define OFToLittleEndian32(i) OFByteSwap32(i) 816 # define OFToLittleEndian64(i) OFByteSwap64(i) 818 # define OFFromBigEndian16(i) OFByteSwap16(i) 819 # define OFFromBigEndian32(i) OFByteSwap32(i) 820 # define OFFromBigEndian64(i) OFByteSwap64(i) 821 # define OFFromLittleEndian16(i) (i) 822 # define OFFromLittleEndian32(i) (i) 823 # define OFFromLittleEndian64(i) (i) 824 # define OFToBigEndian16(i) OFByteSwap16(i) 825 # define OFToBigEndian32(i) OFByteSwap32(i) 826 # define OFToBigEndian64(i) OFByteSwap64(i) 827 # define OFToLittleEndian16(i) (i) 828 # define OFToLittleEndian32(i) (i) 829 # define OFToLittleEndian64(i) (i) 832 #if defined(OF_FLOAT_BIG_ENDIAN) || defined(DOXYGEN) 839 # define OFFromBigEndianFloat(f) (f) 847 # define OFFromBigEndianDouble(d) (d) 855 # define OFFromLittleEndianFloat(f) OFByteSwapFloat(f) 863 # define OFFromLittleEndianDouble(d) OFByteSwapDouble(d) 871 # define OFToBigEndianFloat(f) (f) 879 # define OFToBigEndianDouble(d) (d) 887 # define OFToLittleEndianFloat(f) OFByteSwapFloat(f) 895 # define OFToLittleEndianDouble(d) OFByteSwapDouble(d) 897 # define OFFromBigEndianFloat(f) OFByteSwapFloat(f) 898 # define OFFromBigEndianDouble(d) OFByteSwapDouble(d) 899 # define OFFromLittleEndianFloat(f) (f) 900 # define OFFromLittleEndianDouble(d) (d) 901 # define OFToBigEndianFloat(f) OFByteSwapFloat(f) 902 # define OFToBigEndianDouble(d) OFByteSwapDouble(d) 903 # define OFToLittleEndianFloat(f) (f) 904 # define OFToLittleEndianDouble(d) (d) 914 #define OFRotateLeft(value, bits) \ 915 (((bits) % (sizeof(value) * 8)) > 0 \ 916 ? ((value) << ((bits) % (sizeof(value) * 8))) | \ 917 ((value) >> (sizeof(value) * 8 - ((bits) % (sizeof(value) * 8)))) \ 927 #define OFRotateRight(value, bits) \ 928 (((bits) % (sizeof(value) * 8)) > 0 \ 929 ? ((value) >> ((bits) % (sizeof(value) * 8))) | \ 930 ((value) << (sizeof(value) * 8 - ((bits) % (sizeof(value) * 8)))) \ 940 #define OFRoundUpToPowerOf2(pow2, value) \ 941 (((value) + (pow2) - 1) & ~((pow2) - 1)) 943 static OF_INLINE
bool 944 OFBitsetIsSet(
unsigned char *_Nonnull storage,
size_t idx)
946 return storage[idx / CHAR_BIT] & (1u << (idx % CHAR_BIT));
949 static OF_INLINE
void 950 OFBitsetSet(
unsigned char *_Nonnull storage,
size_t idx)
952 storage[idx / CHAR_BIT] |= (1u << (idx % CHAR_BIT));
955 static OF_INLINE
void 956 OFBitsetClear(
unsigned char *_Nonnull storage,
size_t idx)
958 storage[idx / CHAR_BIT] &= ~(1u << (idx % CHAR_BIT));
961 static OF_INLINE
void 962 OFZeroMemory(
void *_Nonnull buffer_,
size_t length)
964 volatile unsigned char *buffer = (
volatile unsigned char *)buffer_;
966 while (buffer < (
unsigned char *)buffer_ + length)
970 static OF_INLINE
bool 971 OFASCIIIsAlpha(
char c)
973 return ((c >=
'a' && c <=
'z') || (c >=
'A' && c <=
'Z'));
976 static OF_INLINE
bool 977 OFASCIIIsDigit(
char c)
979 return (c >=
'0' && c <=
'9');
982 static OF_INLINE
bool 983 OFASCIIIsAlnum(
char c)
985 return (OFASCIIIsAlpha(c) || OFASCIIIsDigit(c));
988 static OF_INLINE
bool 989 OFASCIIIsSpace(
char c)
991 return (c ==
' ' || c ==
'\t' || c ==
'\n' || c ==
'\r' || c ==
'\f' ||
995 static OF_INLINE
char 996 OFASCIIToUpper(
char c)
998 return (c >=
'a' && c <=
'z' ?
'A' + (c -
'a') : c);
1001 static OF_INLINE
char 1002 OFASCIIToLower(
char c)
1004 return (c >=
'A' && c <=
'Z' ?
'a' + (c -
'A') : c);
static OF_INLINE double OF_CONST_FUNC OFBitConvertUInt64ToDouble(uint64_t uInt64)
Bit-converts the specified uint64_t to a double.
Definition: macros.h:676
static OF_INLINE double OF_CONST_FUNC OFByteSwapDouble(double d)
Byte swaps the specified double.
Definition: macros.h:703
A class for storing constant strings using the @"" literal.
Definition: OFConstantString.h:41
static OF_INLINE float OF_CONST_FUNC OFBitConvertUInt32ToFloat(uint32_t uInt32)
Bit-converts the specified uint32_t to a float.
Definition: macros.h:648
#define OFByteSwap64(i)
Byte swaps the specified 64 bit integer.
Definition: macros.h:619
static OF_INLINE uint64_t OF_CONST_FUNC OFBitConvertDoubleToUInt64(double d)
Bit-converts the specified double to a uint64_t.
Definition: macros.h:662
static OF_INLINE uint32_t OF_CONST_FUNC OFBitConvertFloatToUInt32(float f)
Bit-converts the specified float to a uint32_t.
Definition: macros.h:634
static OF_INLINE float OF_CONST_FUNC OFByteSwapFloat(float f)
Byte swaps the specified float.
Definition: macros.h:690
void OFLog(OFConstantString *format,...)
Logs the specified printf-style format to OFStdErr.
Definition: OFStdIOStream.m:94
#define OFByteSwap32(i)
Byte swaps the specified 32 bit integer.
Definition: macros.h:610