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# if defined(OF_ELF) || defined(OF_MACHO)
103# define OF_VISIBILITY_HIDDEN __attribute__((__visibility__("hidden")))
105# define OF_VISIBILITY_HIDDEN
108# define OF_INLINE inline
109# define OF_LIKELY(cond) (cond)
110# define OF_UNLIKELY(cond) (cond)
111# define OF_CONST_FUNC
112# define OF_NO_RETURN_FUNC
113# define OF_WEAK_REF(sym)
114# define OF_VISIBILITY_HIDDEN
117#if __STDC_VERSION__ >= 201112L
118# define OF_ALIGN(size) _Alignas(size)
119# define OF_ALIGNOF(type) _Alignof(type)
120# define OF_ALIGNAS(type) _Alignas(type)
122# define OF_ALIGN(size) __attribute__((__aligned__(size)))
123# define OF_ALIGNOF(type) __alignof__(type)
124# define OF_ALIGNAS(type) OF_ALIGN(OF_ALIGNOF(type))
127#ifdef __BIGGEST_ALIGNMENT__
128# define OF_BIGGEST_ALIGNMENT __BIGGEST_ALIGNMENT__
131# define OF_BIGGEST_ALIGNMENT 16
137#if (defined(OF_AMD64) || defined(OF_X86)) && OF_BIGGEST_ALIGNMENT < 16
138# undef OF_BIGGEST_ALIGNMENT
139# define OF_BIGGEST_ALIGNMENT 16
142#define OF_PREPROCESSOR_CONCAT2(a, b) a##b
143#define OF_PREPROCESSOR_CONCAT(a, b) OF_PREPROCESSOR_CONCAT2(a, b)
145#if __OBJFW_RUNTIME_ABI__ || (defined(OF_APPLE_RUNTIME) && defined(__OBJC2__))
146# define OF_HAVE_NONFRAGILE_IVARS
149#ifdef OF_HAVE_NONFRAGILE_IVARS
150# define OF_RESERVE_IVARS(cls, num)
152# define OF_RESERVE_IVARS(cls, num) \
154 void *OF_PREPROCESSOR_CONCAT(_reserved_, cls)[num];
158# define OF_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
160# define OF_GCC_VERSION 0
163#define OF_STRINGIFY(s) OF_STRINGIFY2(s)
164#define OF_STRINGIFY2(s) #s
167# define __has_feature(x) 0
170#ifndef __has_attribute
171# define __has_attribute(x) 0
174#if __has_feature(objc_bool)
176# define YES __objc_yes
181# define true ((bool)1)
183# define false ((bool)0)
187#if !__has_feature(objc_instancetype)
188# define instancetype id
191#if __has_feature(blocks)
192# define OF_HAVE_BLOCKS
195#if __has_feature(objc_arc)
196# define OF_RETURNS_RETAINED __attribute__((__ns_returns_retained__))
197# define OF_RETURNS_NOT_RETAINED __attribute__((__ns_returns_not_retained__))
198# define OF_RETURNS_INNER_POINTER \
199 __attribute__((__objc_returns_inner_pointer__))
200# define OF_CONSUMED __attribute__((__ns_consumed__))
201# define OF_WEAK_UNAVAILABLE __attribute__((__objc_arc_weak_unavailable__))
203# define OF_RETURNS_RETAINED
204# define OF_RETURNS_NOT_RETAINED
205# define OF_RETURNS_INNER_POINTER
207# define OF_WEAK_UNAVAILABLE
212# undef __unsafe_unretained
214# undef __autoreleasing
215# define __unsafe_unretained
217# define __autoreleasing
220#if __has_feature(objc_generics)
221# define OF_HAVE_GENERICS
222# define OF_GENERIC(...) <__VA_ARGS__>
224# define OF_GENERIC(...)
227#if __has_feature(nullability)
228# define OF_ASSUME_NONNULL_BEGIN _Pragma("clang assume_nonnull begin")
229# define OF_ASSUME_NONNULL_END _Pragma("clang assume_nonnull end")
230# define OF_NULLABLE_PROPERTY(...) (__VA_ARGS__, nullable)
231# define OF_NULL_RESETTABLE_PROPERTY(...) (__VA_ARGS__, null_resettable)
233# define OF_ASSUME_NONNULL_BEGIN
234# define OF_ASSUME_NONNULL_END
237# define _Null_unspecified
238# define OF_NULLABLE_PROPERTY
239# define OF_NULL_RESETTABLE_PROPERTY
242# define null_unspecified
245#if __has_feature(objc_kindof)
246# define OF_KINDOF(class_) __kindof class_
248# define OF_KINDOF(class_) id
251#if __has_feature(objc_class_property)
252# define OF_HAVE_CLASS_PROPERTIES
255#if defined(__clang__) || OF_GCC_VERSION >= 405
256# define OF_UNREACHABLE __builtin_unreachable();
258# define OF_UNREACHABLE abort();
261#if defined(__clang__) || OF_GCC_VERSION >= 406
262# define OF_SENTINEL __attribute__((__sentinel__))
263# define OF_NO_RETURN __attribute__((__noreturn__))
270# define OF_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
272# define OF_WARN_UNUSED_RESULT
275#if __has_attribute(__unavailable__)
276# define OF_UNAVAILABLE __attribute__((__unavailable__))
278# define OF_UNAVAILABLE
281#if __has_attribute(__objc_requires_super__)
282# define OF_REQUIRES_SUPER __attribute__((__objc_requires_super__))
284# define OF_REQUIRES_SUPER
287#if __has_attribute(__objc_root_class__)
288# define OF_ROOT_CLASS __attribute__((__objc_root_class__))
290# define OF_ROOT_CLASS
293#if __has_attribute(__objc_subclassing_restricted__)
294# define OF_SUBCLASSING_RESTRICTED \
295 __attribute__((__objc_subclassing_restricted__))
297# define OF_SUBCLASSING_RESTRICTED
300#if __has_attribute(__objc_method_family__)
301# define OF_METHOD_FAMILY(f) __attribute__((__objc_method_family__(f)))
303# define OF_METHOD_FAMILY(f)
306#if __has_attribute(__objc_designated_initializer__)
307# define OF_DESIGNATED_INITIALIZER \
308 __attribute__((__objc_designated_initializer__))
310# define OF_DESIGNATED_INITIALIZER
313#if defined(__clang__) || OF_GCC_VERSION >= 405
314# define OF_DEPRECATED(project, major, minor, msg) \
315 __attribute__((__deprecated__("Deprecated in " #project " " \
316 #major "." #minor ": " msg)))
317#elif defined(__GNUC__)
318# define OF_DEPRECATED(project, major, minor, msg) \
319 __attribute__((__deprecated__))
321# define OF_DEPRECATED(project, major, minor, msg)
324#if __has_attribute(__objc_boxable__)
325# define OF_BOXABLE __attribute__((__objc_boxable__))
330#if __has_attribute(__swift_name__)
331# define OF_SWIFT_NAME(name) __attribute__((__swift_name__(name)))
333# define OF_SWIFT_NAME(name)
336#if __has_attribute(__objc_direct__) && defined(OF_APPLE_RUNTIME)
337# define OF_DIRECT __attribute__((__objc_direct__))
338# define OF_DIRECT_PROPERTY(...) (__VA_ARGS__, direct)
341# define OF_DIRECT_PROPERTY
343#if __has_attribute(__objc_direct_members__) && defined(OF_APPLE_RUNTIME)
344# define OF_DIRECT_MEMBERS __attribute__((__objc_direct_members__))
346# define OF_DIRECT_MEMBERS
349#ifdef OF_APPLE_RUNTIME
350# if defined(OF_AMD64) || defined(OF_X86) || defined(OF_ARM64) || \
351 defined(OF_ARM) || defined(OF_POWERPC)
352# define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR
353# define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR_STRET
357# if defined(OF_AMD64) || defined(OF_X86) || \
358 defined(OF_ARM64) || defined(OF_ARM) || \
359 defined(OF_POWERPC) || defined(OF_POWERPC64) || \
360 defined(OF_MIPS64_N64) || defined(OF_MIPS) || \
361 defined(OF_SPARC64) || defined(OF_SPARC) || \
362 defined(OF_RISCV64) || defined(OF_LOONGARCH64)
363# define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR
364# if __OBJFW_RUNTIME_ABI__ >= 800
365# define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR_STRET
368# elif defined(OF_MACH_O)
369# if defined(OF_AMD64)
370# define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR
371# if __OBJFW_RUNTIME_ABI__ >= 800
372# define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR_STRET
375# elif defined(OF_WINDOWS)
376# if defined(OF_AMD64) || defined(OF_X86) || defined(OF_ARM64)
377# define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR
378# if __OBJFW_RUNTIME_ABI__ >= 800
379# define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR_STRET
385#define OFMaxRetainCount UINT_MAX
387#ifdef OBJC_COMPILING_RUNTIME
388# define OFEnsure(cond) \
390 if OF_UNLIKELY (!(cond)) \
391 objc_error("ObjFWRT @ " __FILE__ ":" \
392 OF_STRINGIFY(__LINE__), \
393 "Failed to ensure condition:\n" #cond); \
404# define OFEnsure(cond) \
406 if OF_UNLIKELY (!(cond)) { \
407 OFLog(@"Failed to ensure condition in " \
408 @__FILE__ ":%d: " @#cond, __LINE__); \
415# define OFAssert(...) OFEnsure(__VA_ARGS__)
417# define OFAssert(...)
420#define OF_UNRECOGNIZED_SELECTOR OFMethodNotFound(self, _cmd);
421#if __has_feature(objc_arc)
422# define OF_INVALID_INIT_METHOD OFMethodNotFound(self, _cmd);
424# define OF_INVALID_INIT_METHOD \
426 OFMethodNotFound(self, _cmd); \
435# define OF_DEALLOC_UNSUPPORTED \
436 [self doesNotRecognizeSelector: _cmd]; \
440 _Pragma("clang diagnostic push"); \
441 _Pragma("clang diagnostic ignored \"-Wunreachable-code\""); \
443 _Pragma("clang diagnostic pop");
445# define OF_DEALLOC_UNSUPPORTED \
446 [self doesNotRecognizeSelector: _cmd]; \
452#define OF_SINGLETON_METHODS \
453 - (instancetype)autorelease \
458 - (instancetype)retain \
467 - (unsigned int)retainCount \
469 return OFMaxRetainCount; \
474 OF_DEALLOC_UNSUPPORTED \
477#define OF_CONSTRUCTOR(prio) \
478 static void __attribute__((__constructor__(prio))) \
479 OF_PREPROCESSOR_CONCAT(constructor, __LINE__)(void)
480#define OF_DESTRUCTOR(prio) \
481 static void __attribute__((__destructor__(prio))) \
482 OF_PREPROCESSOR_CONCAT(destructor, __LINE__)(void)
484static OF_INLINE uint16_t OF_CONST_FUNC
485_OFByteSwap16Const(uint16_t i)
487 return (i & UINT16_C(0xFF00)) >> 8 | (i & UINT16_C(0x00FF)) << 8;
490static OF_INLINE uint32_t OF_CONST_FUNC
491_OFByteSwap32Const(uint32_t i)
493 return (i & UINT32_C(0xFF000000)) >> 24 |
494 (i & UINT32_C(0x00FF0000)) >> 8 |
495 (i & UINT32_C(0x0000FF00)) << 8 |
496 (i & UINT32_C(0x000000FF)) << 24;
499static OF_INLINE uint64_t OF_CONST_FUNC
500_OFByteSwap64Const(uint64_t i)
502 return (i & UINT64_C(0xFF00000000000000)) >> 56 |
503 (i & UINT64_C(0x00FF000000000000)) >> 40 |
504 (i & UINT64_C(0x0000FF0000000000)) >> 24 |
505 (i & UINT64_C(0x000000FF00000000)) >> 8 |
506 (i & UINT64_C(0x00000000FF000000)) << 8 |
507 (i & UINT64_C(0x0000000000FF0000)) << 24 |
508 (i & UINT64_C(0x000000000000FF00)) << 40 |
509 (i & UINT64_C(0x00000000000000FF)) << 56;
512static OF_INLINE uint16_t OF_CONST_FUNC
513_OFByteSwap16NonConst(uint16_t i)
515#if defined(OF_HAVE_BUILTIN_BSWAP16)
516 return __builtin_bswap16(i);
517#elif (defined(OF_AMD64) || defined(OF_X86)) && defined(__GNUC__)
519 "xchg{b} { %h0, %b0 | %b0, %h0 }"
523#elif defined(OF_POWERPC) && defined(__GNUC__)
530#elif defined(OF_ARMV6) && defined(__GNUC__)
537 i = (i & UINT16_C(0xFF00)) >> 8 |
538 (i & UINT16_C(0x00FF)) << 8;
543static OF_INLINE uint32_t OF_CONST_FUNC
544_OFByteSwap32NonConst(uint32_t i)
546#if defined(OF_HAVE_BUILTIN_BSWAP32)
547 return __builtin_bswap32(i);
548#elif (defined(OF_AMD64) || defined(OF_X86)) && defined(__GNUC__)
554#elif defined(OF_POWERPC) && defined(__GNUC__)
561#elif defined(OF_ARMV6) && defined(__GNUC__)
568 i = (i & UINT32_C(0xFF000000)) >> 24 |
569 (i & UINT32_C(0x00FF0000)) >> 8 |
570 (i & UINT32_C(0x0000FF00)) << 8 |
571 (i & UINT32_C(0x000000FF)) << 24;
576static OF_INLINE uint64_t OF_CONST_FUNC
577_OFByteSwap64NonConst(uint64_t i)
579#if defined(OF_HAVE_BUILTIN_BSWAP64)
580 return __builtin_bswap64(i);
581#elif defined(OF_AMD64) && defined(__GNUC__)
587#elif defined(OF_X86) && defined(__GNUC__)
591 "xchg{l} { %%eax, %%edx | edx, eax }"
596 i = (uint64_t)_OFByteSwap32NonConst(
597 (uint32_t)(i & UINT32_C(0xFFFFFFFF))) << 32 |
598 _OFByteSwap32NonConst((uint32_t)(i >> 32));
603#if defined(__GNUC__) || defined(DOXYGEN)
610# define OFByteSwap16(i) \
611 (__builtin_constant_p(i) ? _OFByteSwap16Const(i) : _OFByteSwap16NonConst(i))
619# define OFByteSwap32(i) \
620 (__builtin_constant_p(i) ? _OFByteSwap32Const(i) : _OFByteSwap32NonConst(i))
628# define OFByteSwap64(i) \
629 (__builtin_constant_p(i) ? _OFByteSwap64Const(i) : _OFByteSwap64NonConst(i))
631# define OFByteSwap16(i) _OFByteSwap16Const(i)
632# define OFByteSwap32(i) _OFByteSwap32Const(i)
633# define OFByteSwap64(i) _OFByteSwap64Const(i)
642static OF_INLINE uint32_t OF_CONST_FUNC
656static OF_INLINE
float OF_CONST_FUNC
660 memcpy(&ret, &uInt32, 4);
670static OF_INLINE uint64_t OF_CONST_FUNC
684static OF_INLINE
double OF_CONST_FUNC
688 memcpy(&ret, &uInt64, 8);
698static OF_INLINE
float OF_CONST_FUNC
711static OF_INLINE
double OF_CONST_FUNC
718#if defined(OF_BIG_ENDIAN) || defined(DOXYGEN)
726# define OFFromBigEndian16(i) (i)
735# define OFFromBigEndian32(i) (i)
744# define OFFromBigEndian64(i) (i)
753# define OFFromLittleEndian16(i) OFByteSwap16(i)
762# define OFFromLittleEndian32(i) OFByteSwap32(i)
771# define OFFromLittleEndian64(i) OFByteSwap64(i)
780# define OFToBigEndian16(i) (i)
789# define OFToBigEndian32(i) (i)
798# define OFToBigEndian64(i) (i)
807# define OFToLittleEndian16(i) OFByteSwap16(i)
816# define OFToLittleEndian32(i) OFByteSwap32(i)
825# define OFToLittleEndian64(i) OFByteSwap64(i)
827# define OFFromBigEndian16(i) OFByteSwap16(i)
828# define OFFromBigEndian32(i) OFByteSwap32(i)
829# define OFFromBigEndian64(i) OFByteSwap64(i)
830# define OFFromLittleEndian16(i) (i)
831# define OFFromLittleEndian32(i) (i)
832# define OFFromLittleEndian64(i) (i)
833# define OFToBigEndian16(i) OFByteSwap16(i)
834# define OFToBigEndian32(i) OFByteSwap32(i)
835# define OFToBigEndian64(i) OFByteSwap64(i)
836# define OFToLittleEndian16(i) (i)
837# define OFToLittleEndian32(i) (i)
838# define OFToLittleEndian64(i) (i)
841#if defined(OF_FLOAT_BIG_ENDIAN) || defined(DOXYGEN)
848# define OFFromBigEndianFloat(f) (f)
856# define OFFromBigEndianDouble(d) (d)
864# define OFFromLittleEndianFloat(f) OFByteSwapFloat(f)
872# define OFFromLittleEndianDouble(d) OFByteSwapDouble(d)
880# define OFToBigEndianFloat(f) (f)
888# define OFToBigEndianDouble(d) (d)
896# define OFToLittleEndianFloat(f) OFByteSwapFloat(f)
904# define OFToLittleEndianDouble(d) OFByteSwapDouble(d)
906# define OFFromBigEndianFloat(f) OFByteSwapFloat(f)
907# define OFFromBigEndianDouble(d) OFByteSwapDouble(d)
908# define OFFromLittleEndianFloat(f) (f)
909# define OFFromLittleEndianDouble(d) (d)
910# define OFToBigEndianFloat(f) OFByteSwapFloat(f)
911# define OFToBigEndianDouble(d) OFByteSwapDouble(d)
912# define OFToLittleEndianFloat(f) (f)
913# define OFToLittleEndianDouble(d) (d)
923#define OFRotateLeft(value, bits) \
924 (((bits) % (sizeof(value) * 8)) > 0 \
925 ? ((value) << ((bits) % (sizeof(value) * 8))) | \
926 ((value) >> (sizeof(value) * 8 - ((bits) % (sizeof(value) * 8)))) \
936#define OFRotateRight(value, bits) \
937 (((bits) % (sizeof(value) * 8)) > 0 \
938 ? ((value) >> ((bits) % (sizeof(value) * 8))) | \
939 ((value) << (sizeof(value) * 8 - ((bits) % (sizeof(value) * 8)))) \
949#define OFRoundUpToPowerOf2(pow2, value) \
950 (((value) + (pow2) - 1) & ~((pow2) - 1))
952#define OF_ULONG_BIT (sizeof(unsigned long) * CHAR_BIT)
955OFBitSetIsSet(
unsigned long *_Nonnull storage,
size_t idx)
957 return storage[idx / OF_ULONG_BIT] & (1ul << (idx % OF_ULONG_BIT));
961OFBitSetSet(
unsigned long *_Nonnull storage,
size_t idx)
963 storage[idx / OF_ULONG_BIT] |= (1ul << (idx % OF_ULONG_BIT));
967OFBitSetClear(
unsigned long *_Nonnull storage,
size_t idx)
969 storage[idx / OF_ULONG_BIT] &= ~(1ul << (idx % OF_ULONG_BIT));
973OFZeroMemory(
void *_Nonnull buffer_,
size_t length)
975 volatile unsigned char *buffer = (
volatile unsigned char *)buffer_;
977 while (buffer < (
unsigned char *)buffer_ + length)
982OFASCIIIsAlpha(
char c)
984 return ((c >=
'a' && c <=
'z') || (c >=
'A' && c <=
'Z'));
988OFASCIIIsDigit(
char c)
990 return (c >=
'0' && c <=
'9');
994OFASCIIIsAlnum(
char c)
996 return (OFASCIIIsAlpha(c) || OFASCIIIsDigit(c));
1000OFASCIIIsSpace(
char c)
1002 return (c ==
' ' || c ==
'\t' || c ==
'\n' || c ==
'\r' || c ==
'\f' ||
1006static OF_INLINE
char
1007OFASCIIToUpper(
char c)
1009 return (c >=
'a' && c <=
'z' ?
'A' + (c -
'a') : c);
1012static OF_INLINE
char
1013OFASCIIToLower(
char c)
1015 return (c >=
'A' && c <=
'Z' ?
'a' + (c -
'A') : c);
A class for storing constant strings using the @"" literal.
Definition OFConstantString.h:42
static OF_INLINE float OF_CONST_FUNC OFBitConvertUInt32ToFloat(uint32_t uInt32)
Bit-converts the specified uint32_t to a float.
Definition macros.h:657
#define OFByteSwap32(i)
Byte swaps the specified 32 bit integer.
Definition macros.h:619
static OF_INLINE double OF_CONST_FUNC OFBitConvertUInt64ToDouble(uint64_t uInt64)
Bit-converts the specified uint64_t to a double.
Definition macros.h:685
#define OFByteSwap64(i)
Byte swaps the specified 64 bit integer.
Definition macros.h:628
static OF_INLINE double OF_CONST_FUNC OFByteSwapDouble(double d)
Byte swaps the specified double.
Definition macros.h:712
static OF_INLINE float OF_CONST_FUNC OFByteSwapFloat(float f)
Byte swaps the specified float.
Definition macros.h:699
static OF_INLINE uint64_t OF_CONST_FUNC OFBitConvertDoubleToUInt64(double d)
Bit-converts the specified double to a uint64_t.
Definition macros.h:671
static OF_INLINE uint32_t OF_CONST_FUNC OFBitConvertFloatToUInt32(float f)
Bit-converts the specified float to a uint32_t.
Definition macros.h:643