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")))
104# define OF_VISIBILITY_INTERNAL __attribute__((__visibility__("internal")))
106# define OF_VISIBILITY_HIDDEN
107# define OF_VISIBILITY_INTERNAL
109# define OF_MALLOC_FUNC __attribute__((__malloc__))
111# define OF_INLINE inline
112# define OF_LIKELY(cond) (cond)
113# define OF_UNLIKELY(cond) (cond)
114# define OF_CONST_FUNC
115# define OF_NO_RETURN_FUNC
116# define OF_WEAK_REF(sym)
117# define OF_VISIBILITY_HIDDEN
118# define OF_VISIBILITY_INTERNAL
119# define OF_MALLOC_FUNC
122#if __STDC_VERSION__ >= 201112L
123# define OF_ALIGN(size) _Alignas(size)
124# define OF_ALIGNOF(type) _Alignof(type)
125# define OF_ALIGNAS(type) _Alignas(type)
127# define OF_ALIGN(size) __attribute__((__aligned__(size)))
128# define OF_ALIGNOF(type) __alignof__(type)
129# define OF_ALIGNAS(type) OF_ALIGN(OF_ALIGNOF(type))
132#ifdef __BIGGEST_ALIGNMENT__
133# define OF_BIGGEST_ALIGNMENT __BIGGEST_ALIGNMENT__
136# define OF_BIGGEST_ALIGNMENT 16
142#if (defined(OF_AMD64) || defined(OF_X86)) && OF_BIGGEST_ALIGNMENT < 16
143# undef OF_BIGGEST_ALIGNMENT
144# define OF_BIGGEST_ALIGNMENT 16
147#define OF_PREPROCESSOR_CONCAT2(a, b) a##b
148#define OF_PREPROCESSOR_CONCAT(a, b) OF_PREPROCESSOR_CONCAT2(a, b)
149#define OF_PREPROCESSOR_STRINGIFY2(x) #x
150#define OF_PREPROCESSOR_STRINGIFY(x) OF_PREPROCESSOR_STRINGIFY2(x)
152#if __OBJFW_RUNTIME_ABI__ || (defined(OF_APPLE_RUNTIME) && defined(__OBJC2__))
153# define OF_HAVE_NONFRAGILE_IVARS
156#ifdef OF_HAVE_NONFRAGILE_IVARS
157# define OF_RESERVE_IVARS(cls, num)
159# define OF_RESERVE_IVARS(cls, num) \
161 void *OF_PREPROCESSOR_CONCAT(_reserved_, cls)[num];
165# define OF_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
167# define OF_GCC_VERSION 0
170#define OF_STRINGIFY(s) OF_STRINGIFY2(s)
171#define OF_STRINGIFY2(s) #s
174# define __has_feature(x) 0
177#ifndef __has_attribute
178# define __has_attribute(x) 0
181#if __has_feature(objc_bool)
183# define YES __objc_yes
188# define true ((bool)1)
190# define false ((bool)0)
194#if !__has_feature(objc_instancetype)
195# define instancetype id
198#if __has_feature(blocks)
199# define OF_HAVE_BLOCKS
202#if __has_feature(objc_arc)
203# define OF_RETURNS_RETAINED __attribute__((__ns_returns_retained__))
204# define OF_RETURNS_NOT_RETAINED __attribute__((__ns_returns_not_retained__))
205# define OF_RETURNS_INNER_POINTER \
206 __attribute__((__objc_returns_inner_pointer__))
207# define OF_CONSUMED __attribute__((__ns_consumed__))
208# define OF_WEAK_UNAVAILABLE __attribute__((__objc_arc_weak_unavailable__))
210# define OF_RETURNS_RETAINED
211# define OF_RETURNS_NOT_RETAINED
212# define OF_RETURNS_INNER_POINTER
214# define OF_WEAK_UNAVAILABLE
219# undef __unsafe_unretained
221# undef __autoreleasing
222# define __unsafe_unretained
224# define __autoreleasing
227#if __has_feature(objc_generics)
228# define OF_HAVE_GENERICS
229# define OF_GENERIC(...) <__VA_ARGS__>
231# define OF_GENERIC(...)
234#if __has_feature(nullability)
235# define OF_ASSUME_NONNULL_BEGIN _Pragma("clang assume_nonnull begin")
236# define OF_ASSUME_NONNULL_END _Pragma("clang assume_nonnull end")
237# define OF_NULLABLE_PROPERTY(...) (__VA_ARGS__, nullable)
238# define OF_NULL_RESETTABLE_PROPERTY(...) (__VA_ARGS__, null_resettable)
240# define OF_ASSUME_NONNULL_BEGIN
241# define OF_ASSUME_NONNULL_END
244# define _Null_unspecified
245# define OF_NULLABLE_PROPERTY
246# define OF_NULL_RESETTABLE_PROPERTY
249# define null_unspecified
252#if __has_feature(objc_kindof)
253# define OF_KINDOF(class_) __kindof class_
255# define OF_KINDOF(class_) id
258#if __has_feature(objc_class_property)
259# define OF_HAVE_CLASS_PROPERTIES
262#if defined(__clang__) || OF_GCC_VERSION >= 405
263# define OF_UNREACHABLE __builtin_unreachable();
265# define OF_UNREACHABLE abort();
268#if defined(__clang__) || OF_GCC_VERSION >= 406
269# define OF_SENTINEL __attribute__((__sentinel__))
270# define OF_NO_RETURN __attribute__((__noreturn__))
277# define OF_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
279# define OF_WARN_UNUSED_RESULT
282#if __has_attribute(__unavailable__)
283# define OF_UNAVAILABLE __attribute__((__unavailable__))
285# define OF_UNAVAILABLE
288#if __has_attribute(__objc_requires_super__)
289# define OF_REQUIRES_SUPER __attribute__((__objc_requires_super__))
291# define OF_REQUIRES_SUPER
294#if __has_attribute(__objc_root_class__)
295# define OF_ROOT_CLASS __attribute__((__objc_root_class__))
297# define OF_ROOT_CLASS
300#if __has_attribute(__objc_subclassing_restricted__)
301# define OF_SUBCLASSING_RESTRICTED \
302 __attribute__((__objc_subclassing_restricted__))
304# define OF_SUBCLASSING_RESTRICTED
307#if __has_attribute(__objc_method_family__)
308# define OF_METHOD_FAMILY(f) __attribute__((__objc_method_family__(f)))
310# define OF_METHOD_FAMILY(f)
313#if __has_attribute(__objc_designated_initializer__)
314# define OF_DESIGNATED_INITIALIZER \
315 __attribute__((__objc_designated_initializer__))
317# define OF_DESIGNATED_INITIALIZER
320#if defined(__clang__) || OF_GCC_VERSION >= 405
321# define OF_DEPRECATED(project, major, minor, msg) \
322 __attribute__((__deprecated__("Deprecated in " #project " " \
323 #major "." #minor ": " msg)))
324#elif defined(__GNUC__)
325# define OF_DEPRECATED(project, major, minor, msg) \
326 __attribute__((__deprecated__))
328# define OF_DEPRECATED(project, major, minor, msg)
331#if __has_attribute(__objc_boxable__)
332# define OF_BOXABLE __attribute__((__objc_boxable__))
337#if __has_attribute(__swift_name__)
338# define OF_SWIFT_NAME(name) __attribute__((__swift_name__(name)))
340# define OF_SWIFT_NAME(name)
343#if defined(OF_APPLE_RUNTIME) || (defined(OF_OBJFW_RUNTIME) && \
344 defined(__clang_major__) && __clang_major__ >= 21)
345# if __has_attribute(__objc_direct__)
346# define OF_DIRECT __attribute__((__objc_direct__))
347# define OF_DIRECT_PROPERTY(...) (__VA_ARGS__, direct)
349# if __has_attribute(__objc_direct_members__)
350# define OF_DIRECT_MEMBERS __attribute__((__objc_direct_members__))
356#ifndef OF_DIRECT_PROPERTY
357# define OF_DIRECT_PROPERTY
359#ifndef OF_DIRECT_MEMBERS
360# define OF_DIRECT_MEMBERS
363#ifdef OF_APPLE_RUNTIME
364# if defined(OF_AMD64) || defined(OF_X86) || defined(OF_ARM64) || \
365 defined(OF_ARM) || defined(OF_POWERPC)
366# define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR
367# define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR_STRET
371# if defined(OF_AMD64) || defined(OF_X86) || \
372 defined(OF_ARM64) || defined(OF_ARM) || \
373 defined(OF_POWERPC) || defined(OF_POWERPC64) || \
374 defined(OF_MIPS64_N64) || defined(OF_MIPS) || \
375 defined(OF_SPARC64) || defined(OF_SPARC) || \
376 defined(OF_RISCV64) || defined(OF_LOONGARCH64)
377# define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR
378# if __OBJFW_RUNTIME_ABI__ >= 800
379# define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR_STRET
382# elif defined(OF_MACH_O)
383# if defined(OF_AMD64)
384# define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR
385# if __OBJFW_RUNTIME_ABI__ >= 800
386# define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR_STRET
389# elif defined(OF_WINDOWS)
390# if defined(OF_AMD64) || defined(OF_X86) || defined(OF_ARM64)
391# define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR
392# if __OBJFW_RUNTIME_ABI__ >= 800
393# define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR_STRET
399#define OFMaxRetainCount UINT_MAX
401#ifdef OBJC_COMPILING_RUNTIME
402# define OFEnsure(cond) \
404 if OF_UNLIKELY (!(cond)) \
405 objc_error("ObjFWRT @ " __FILE__ ":" \
406 OF_STRINGIFY(__LINE__), \
407 "Failed to ensure condition:\n" #cond); \
418# define OFEnsure(cond) \
420 if OF_UNLIKELY (!(cond)) { \
421 OFLog(@"Failed to ensure condition in " \
422 @__FILE__ ":%d: " @#cond, __LINE__); \
429# define OFAssert(...) OFEnsure(__VA_ARGS__)
431# define OFAssert(...)
434#define OF_UNRECOGNIZED_SELECTOR OFMethodNotFound(self, _cmd);
435#if __has_feature(objc_arc)
436# define OF_INVALID_INIT_METHOD OFMethodNotFound(self, _cmd);
438# define OF_INVALID_INIT_METHOD \
440 OFMethodNotFound(self, _cmd); \
442 objc_release(self); \
449# define OF_DEALLOC_UNSUPPORTED \
450 [self doesNotRecognizeSelector: _cmd]; \
454 _Pragma("clang diagnostic push"); \
455 _Pragma("clang diagnostic ignored \"-Wunreachable-code\""); \
457 _Pragma("clang diagnostic pop");
459# define OF_DEALLOC_UNSUPPORTED \
460 [self doesNotRecognizeSelector: _cmd]; \
466#define OF_SINGLETON_METHODS \
467 - (instancetype)autorelease \
472 - (instancetype)retain \
481 - (unsigned int)retainCount \
483 return OFMaxRetainCount; \
488 OF_DEALLOC_UNSUPPORTED \
491#define OF_CONSTRUCTOR(prio) \
492 static void __attribute__((__constructor__(prio))) \
493 OF_PREPROCESSOR_CONCAT(constructor, __LINE__)(void)
494#define OF_DESTRUCTOR(prio) \
495 static void __attribute__((__destructor__(prio))) \
496 OF_PREPROCESSOR_CONCAT(destructor, __LINE__)(void)
498static OF_INLINE uint16_t OF_CONST_FUNC
499_OFByteSwap16Const(uint16_t i)
501 return (i & UINT16_C(0xFF00)) >> 8 | (i & UINT16_C(0x00FF)) << 8;
504static OF_INLINE uint32_t OF_CONST_FUNC
505_OFByteSwap32Const(uint32_t i)
507 return (i & UINT32_C(0xFF000000)) >> 24 |
508 (i & UINT32_C(0x00FF0000)) >> 8 |
509 (i & UINT32_C(0x0000FF00)) << 8 |
510 (i & UINT32_C(0x000000FF)) << 24;
513static OF_INLINE uint64_t OF_CONST_FUNC
514_OFByteSwap64Const(uint64_t i)
516 return (i & UINT64_C(0xFF00000000000000)) >> 56 |
517 (i & UINT64_C(0x00FF000000000000)) >> 40 |
518 (i & UINT64_C(0x0000FF0000000000)) >> 24 |
519 (i & UINT64_C(0x000000FF00000000)) >> 8 |
520 (i & UINT64_C(0x00000000FF000000)) << 8 |
521 (i & UINT64_C(0x0000000000FF0000)) << 24 |
522 (i & UINT64_C(0x000000000000FF00)) << 40 |
523 (i & UINT64_C(0x00000000000000FF)) << 56;
526static OF_INLINE uint16_t OF_CONST_FUNC
527_OFByteSwap16NonConst(uint16_t i)
529#if defined(OF_HAVE_BUILTIN_BSWAP16)
530 return __builtin_bswap16(i);
531#elif (defined(OF_AMD64) || defined(OF_X86)) && defined(__GNUC__)
533 "xchg{b} { %h0, %b0 | %b0, %h0 }"
537#elif defined(OF_POWERPC) && defined(__GNUC__)
544#elif defined(OF_ARMV6) && defined(__GNUC__)
551 i = (i & UINT16_C(0xFF00)) >> 8 |
552 (i & UINT16_C(0x00FF)) << 8;
557static OF_INLINE uint32_t OF_CONST_FUNC
558_OFByteSwap32NonConst(uint32_t i)
560#if defined(OF_HAVE_BUILTIN_BSWAP32)
561 return __builtin_bswap32(i);
562#elif (defined(OF_AMD64) || defined(OF_X86)) && defined(__GNUC__)
568#elif defined(OF_POWERPC) && defined(__GNUC__)
575#elif defined(OF_ARMV6) && defined(__GNUC__)
582 i = (i & UINT32_C(0xFF000000)) >> 24 |
583 (i & UINT32_C(0x00FF0000)) >> 8 |
584 (i & UINT32_C(0x0000FF00)) << 8 |
585 (i & UINT32_C(0x000000FF)) << 24;
590static OF_INLINE uint64_t OF_CONST_FUNC
591_OFByteSwap64NonConst(uint64_t i)
593#if defined(OF_HAVE_BUILTIN_BSWAP64)
594 return __builtin_bswap64(i);
595#elif defined(OF_AMD64) && defined(__GNUC__)
601#elif defined(OF_X86) && defined(__GNUC__)
605 "xchg{l} { %%eax, %%edx | edx, eax }"
610 i = (uint64_t)_OFByteSwap32NonConst(
611 (uint32_t)(i & UINT32_C(0xFFFFFFFF))) << 32 |
612 _OFByteSwap32NonConst((uint32_t)(i >> 32));
617#if defined(__GNUC__) || defined(DOXYGEN)
624# define OFByteSwap16(i) \
625 (__builtin_constant_p(i) ? _OFByteSwap16Const(i) : _OFByteSwap16NonConst(i))
633# define OFByteSwap32(i) \
634 (__builtin_constant_p(i) ? _OFByteSwap32Const(i) : _OFByteSwap32NonConst(i))
642# define OFByteSwap64(i) \
643 (__builtin_constant_p(i) ? _OFByteSwap64Const(i) : _OFByteSwap64NonConst(i))
645# define OFByteSwap16(i) _OFByteSwap16Const(i)
646# define OFByteSwap32(i) _OFByteSwap32Const(i)
647# define OFByteSwap64(i) _OFByteSwap64Const(i)
656static OF_INLINE uint32_t OF_CONST_FUNC
670static OF_INLINE
float OF_CONST_FUNC
674 memcpy(&ret, &uInt32, 4);
684static OF_INLINE uint64_t OF_CONST_FUNC
698static OF_INLINE
double OF_CONST_FUNC
702 memcpy(&ret, &uInt64, 8);
712static OF_INLINE
float OF_CONST_FUNC
725static OF_INLINE
double OF_CONST_FUNC
732#if defined(OF_BIG_ENDIAN) || defined(DOXYGEN)
740# define OFFromBigEndian16(i) (i)
749# define OFFromBigEndian32(i) (i)
758# define OFFromBigEndian64(i) (i)
767# define OFFromLittleEndian16(i) OFByteSwap16(i)
776# define OFFromLittleEndian32(i) OFByteSwap32(i)
785# define OFFromLittleEndian64(i) OFByteSwap64(i)
794# define OFToBigEndian16(i) (i)
803# define OFToBigEndian32(i) (i)
812# define OFToBigEndian64(i) (i)
821# define OFToLittleEndian16(i) OFByteSwap16(i)
830# define OFToLittleEndian32(i) OFByteSwap32(i)
839# define OFToLittleEndian64(i) OFByteSwap64(i)
841# define OFFromBigEndian16(i) OFByteSwap16(i)
842# define OFFromBigEndian32(i) OFByteSwap32(i)
843# define OFFromBigEndian64(i) OFByteSwap64(i)
844# define OFFromLittleEndian16(i) (i)
845# define OFFromLittleEndian32(i) (i)
846# define OFFromLittleEndian64(i) (i)
847# define OFToBigEndian16(i) OFByteSwap16(i)
848# define OFToBigEndian32(i) OFByteSwap32(i)
849# define OFToBigEndian64(i) OFByteSwap64(i)
850# define OFToLittleEndian16(i) (i)
851# define OFToLittleEndian32(i) (i)
852# define OFToLittleEndian64(i) (i)
855#if defined(OF_FLOAT_BIG_ENDIAN) || defined(DOXYGEN)
862# define OFFromBigEndianFloat(f) (f)
870# define OFFromBigEndianDouble(d) (d)
878# define OFFromLittleEndianFloat(f) OFByteSwapFloat(f)
886# define OFFromLittleEndianDouble(d) OFByteSwapDouble(d)
894# define OFToBigEndianFloat(f) (f)
902# define OFToBigEndianDouble(d) (d)
910# define OFToLittleEndianFloat(f) OFByteSwapFloat(f)
918# define OFToLittleEndianDouble(d) OFByteSwapDouble(d)
920# define OFFromBigEndianFloat(f) OFByteSwapFloat(f)
921# define OFFromBigEndianDouble(d) OFByteSwapDouble(d)
922# define OFFromLittleEndianFloat(f) (f)
923# define OFFromLittleEndianDouble(d) (d)
924# define OFToBigEndianFloat(f) OFByteSwapFloat(f)
925# define OFToBigEndianDouble(d) OFByteSwapDouble(d)
926# define OFToLittleEndianFloat(f) (f)
927# define OFToLittleEndianDouble(d) (d)
937#define OFRotateLeft(value, bits) \
938 (((bits) % (sizeof(value) * 8)) > 0 \
939 ? ((value) << ((bits) % (sizeof(value) * 8))) | \
940 ((value) >> (sizeof(value) * 8 - ((bits) % (sizeof(value) * 8)))) \
950#define OFRotateRight(value, bits) \
951 (((bits) % (sizeof(value) * 8)) > 0 \
952 ? ((value) >> ((bits) % (sizeof(value) * 8))) | \
953 ((value) << (sizeof(value) * 8 - ((bits) % (sizeof(value) * 8)))) \
963#define OFRoundUpToPowerOf2(pow2, value) \
964 (((value) + (pow2) - 1) & ~((pow2) - 1))
966#define OF_ULONG_BIT (sizeof(unsigned long) * CHAR_BIT)
969OFBitSetIsSet(
unsigned long *_Nonnull storage,
size_t idx)
971 return storage[idx / OF_ULONG_BIT] & (1ul << (idx % OF_ULONG_BIT));
975OFBitSetSet(
unsigned long *_Nonnull storage,
size_t idx)
977 storage[idx / OF_ULONG_BIT] |= (1ul << (idx % OF_ULONG_BIT));
981OFBitSetClear(
unsigned long *_Nonnull storage,
size_t idx)
983 storage[idx / OF_ULONG_BIT] &= ~(1ul << (idx % OF_ULONG_BIT));
987OFZeroMemory(
void *_Nonnull buffer_,
size_t length)
989 volatile unsigned char *buffer = (
volatile unsigned char *)buffer_;
991 while (buffer < (
unsigned char *)buffer_ + length)
996OFASCIIIsAlpha(
char c)
998 return ((c >=
'a' && c <=
'z') || (c >=
'A' && c <=
'Z'));
1001static OF_INLINE
bool
1002OFASCIIIsDigit(
char c)
1004 return (c >=
'0' && c <=
'9');
1007static OF_INLINE
bool
1008OFASCIIIsAlnum(
char c)
1010 return (OFASCIIIsAlpha(c) || OFASCIIIsDigit(c));
1013static OF_INLINE
bool
1014OFASCIIIsSpace(
char c)
1016 return (c ==
' ' || c ==
'\t' || c ==
'\n' || c ==
'\r' || c ==
'\f' ||
1020static OF_INLINE
char
1021OFASCIIToUpper(
char c)
1023 return (c >=
'a' && c <=
'z' ?
'A' + (c -
'a') : c);
1026static OF_INLINE
char
1027OFASCIIToLower(
char c)
1029 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:671
#define OFByteSwap32(i)
Byte swaps the specified 32 bit integer.
Definition macros.h:633
static OF_INLINE double OF_CONST_FUNC OFBitConvertUInt64ToDouble(uint64_t uInt64)
Bit-converts the specified uint64_t to a double.
Definition macros.h:699
#define OFByteSwap64(i)
Byte swaps the specified 64 bit integer.
Definition macros.h:642
static OF_INLINE double OF_CONST_FUNC OFByteSwapDouble(double d)
Byte swaps the specified double.
Definition macros.h:726
static OF_INLINE float OF_CONST_FUNC OFByteSwapFloat(float f)
Byte swaps the specified float.
Definition macros.h:713
static OF_INLINE uint64_t OF_CONST_FUNC OFBitConvertDoubleToUInt64(double d)
Bit-converts the specified double to a uint64_t.
Definition macros.h:685
static OF_INLINE uint32_t OF_CONST_FUNC OFBitConvertFloatToUInt32(float f)
Bit-converts the specified float to a uint32_t.
Definition macros.h:657