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__))
341#if __has_attribute(__objc_direct_members__) && defined(OF_APPLE_RUNTIME)
342# define OF_DIRECT_MEMBERS __attribute__((__objc_direct_members__))
344# define OF_DIRECT_MEMBERS
347#ifdef OF_APPLE_RUNTIME
348# if defined(OF_AMD64) || defined(OF_X86) || defined(OF_ARM64) || \
349 defined(OF_ARM) || defined(OF_POWERPC)
350# define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR
351# define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR_STRET
355# if defined(OF_AMD64) || defined(OF_X86) || \
356 defined(OF_ARM64) || defined(OF_ARM) || \
357 defined(OF_POWERPC) || defined(OF_POWERPC64) || \
358 defined(OF_MIPS64_N64) || defined(OF_MIPS) || \
359 defined(OF_SPARC64) || defined(OF_SPARC) || \
360 defined(OF_RISCV64) || defined(OF_LOONGARCH64)
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_MACH_O)
367# if defined(OF_AMD64)
368# define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR
369# if __OBJFW_RUNTIME_ABI__ >= 800
370# define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR_STRET
373# elif defined(OF_WINDOWS)
374# if defined(OF_AMD64) || defined(OF_X86) || defined(OF_ARM64)
375# define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR
376# if __OBJFW_RUNTIME_ABI__ >= 800
377# define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR_STRET
383#define OFMaxRetainCount UINT_MAX
385#ifdef OBJC_COMPILING_RUNTIME
386# define OFEnsure(cond) \
388 if OF_UNLIKELY (!(cond)) \
389 objc_error("ObjFWRT @ " __FILE__ ":" \
390 OF_STRINGIFY(__LINE__), \
391 "Failed to ensure condition:\n" #cond); \
402# define OFEnsure(cond) \
404 if OF_UNLIKELY (!(cond)) { \
405 OFLog(@"Failed to ensure condition in " \
406 @__FILE__ ":%d: " @#cond, __LINE__); \
413# define OFAssert(...) OFEnsure(__VA_ARGS__)
415# define OFAssert(...)
418#define OF_UNRECOGNIZED_SELECTOR OFMethodNotFound(self, _cmd);
419#if __has_feature(objc_arc)
420# define OF_INVALID_INIT_METHOD OFMethodNotFound(self, _cmd);
422# define OF_INVALID_INIT_METHOD \
424 OFMethodNotFound(self, _cmd); \
433# define OF_DEALLOC_UNSUPPORTED \
434 [self doesNotRecognizeSelector: _cmd]; \
438 _Pragma("clang diagnostic push"); \
439 _Pragma("clang diagnostic ignored \"-Wunreachable-code\""); \
441 _Pragma("clang diagnostic pop");
443# define OF_DEALLOC_UNSUPPORTED \
444 [self doesNotRecognizeSelector: _cmd]; \
450#define OF_SINGLETON_METHODS \
451 - (instancetype)autorelease \
456 - (instancetype)retain \
465 - (unsigned int)retainCount \
467 return OFMaxRetainCount; \
472 OF_DEALLOC_UNSUPPORTED \
475#define OF_CONSTRUCTOR(prio) \
476 static void __attribute__((__constructor__(prio))) \
477 OF_PREPROCESSOR_CONCAT(constructor, __LINE__)(void)
478#define OF_DESTRUCTOR(prio) \
479 static void __attribute__((__destructor__(prio))) \
480 OF_PREPROCESSOR_CONCAT(destructor, __LINE__)(void)
482static OF_INLINE uint16_t OF_CONST_FUNC
483_OFByteSwap16Const(uint16_t i)
485 return (i & UINT16_C(0xFF00)) >> 8 | (i & UINT16_C(0x00FF)) << 8;
488static OF_INLINE uint32_t OF_CONST_FUNC
489_OFByteSwap32Const(uint32_t i)
491 return (i & UINT32_C(0xFF000000)) >> 24 |
492 (i & UINT32_C(0x00FF0000)) >> 8 |
493 (i & UINT32_C(0x0000FF00)) << 8 |
494 (i & UINT32_C(0x000000FF)) << 24;
497static OF_INLINE uint64_t OF_CONST_FUNC
498_OFByteSwap64Const(uint64_t i)
500 return (i & UINT64_C(0xFF00000000000000)) >> 56 |
501 (i & UINT64_C(0x00FF000000000000)) >> 40 |
502 (i & UINT64_C(0x0000FF0000000000)) >> 24 |
503 (i & UINT64_C(0x000000FF00000000)) >> 8 |
504 (i & UINT64_C(0x00000000FF000000)) << 8 |
505 (i & UINT64_C(0x0000000000FF0000)) << 24 |
506 (i & UINT64_C(0x000000000000FF00)) << 40 |
507 (i & UINT64_C(0x00000000000000FF)) << 56;
510static OF_INLINE uint16_t OF_CONST_FUNC
511_OFByteSwap16NonConst(uint16_t i)
513#if defined(OF_HAVE_BUILTIN_BSWAP16)
514 return __builtin_bswap16(i);
515#elif (defined(OF_AMD64) || defined(OF_X86)) && defined(__GNUC__)
517 "xchg{b} { %h0, %b0 | %b0, %h0 }"
521#elif defined(OF_POWERPC) && defined(__GNUC__)
528#elif defined(OF_ARMV6) && defined(__GNUC__)
535 i = (i & UINT16_C(0xFF00)) >> 8 |
536 (i & UINT16_C(0x00FF)) << 8;
541static OF_INLINE uint32_t OF_CONST_FUNC
542_OFByteSwap32NonConst(uint32_t i)
544#if defined(OF_HAVE_BUILTIN_BSWAP32)
545 return __builtin_bswap32(i);
546#elif (defined(OF_AMD64) || defined(OF_X86)) && defined(__GNUC__)
552#elif defined(OF_POWERPC) && defined(__GNUC__)
559#elif defined(OF_ARMV6) && defined(__GNUC__)
566 i = (i & UINT32_C(0xFF000000)) >> 24 |
567 (i & UINT32_C(0x00FF0000)) >> 8 |
568 (i & UINT32_C(0x0000FF00)) << 8 |
569 (i & UINT32_C(0x000000FF)) << 24;
574static OF_INLINE uint64_t OF_CONST_FUNC
575_OFByteSwap64NonConst(uint64_t i)
577#if defined(OF_HAVE_BUILTIN_BSWAP64)
578 return __builtin_bswap64(i);
579#elif defined(OF_AMD64) && defined(__GNUC__)
585#elif defined(OF_X86) && defined(__GNUC__)
589 "xchg{l} { %%eax, %%edx | edx, eax }"
594 i = (uint64_t)_OFByteSwap32NonConst(
595 (uint32_t)(i & UINT32_C(0xFFFFFFFF))) << 32 |
596 _OFByteSwap32NonConst((uint32_t)(i >> 32));
601#if defined(__GNUC__) || defined(DOXYGEN)
608# define OFByteSwap16(i) \
609 (__builtin_constant_p(i) ? _OFByteSwap16Const(i) : _OFByteSwap16NonConst(i))
617# define OFByteSwap32(i) \
618 (__builtin_constant_p(i) ? _OFByteSwap32Const(i) : _OFByteSwap32NonConst(i))
626# define OFByteSwap64(i) \
627 (__builtin_constant_p(i) ? _OFByteSwap64Const(i) : _OFByteSwap64NonConst(i))
629# define OFByteSwap16(i) _OFByteSwap16Const(i)
630# define OFByteSwap32(i) _OFByteSwap32Const(i)
631# define OFByteSwap64(i) _OFByteSwap64Const(i)
640static OF_INLINE uint32_t OF_CONST_FUNC
654static OF_INLINE
float OF_CONST_FUNC
658 memcpy(&ret, &uInt32, 4);
668static OF_INLINE uint64_t OF_CONST_FUNC
682static OF_INLINE
double OF_CONST_FUNC
686 memcpy(&ret, &uInt64, 8);
696static OF_INLINE
float OF_CONST_FUNC
709static OF_INLINE
double OF_CONST_FUNC
716#if defined(OF_BIG_ENDIAN) || defined(DOXYGEN)
724# define OFFromBigEndian16(i) (i)
733# define OFFromBigEndian32(i) (i)
742# define OFFromBigEndian64(i) (i)
751# define OFFromLittleEndian16(i) OFByteSwap16(i)
760# define OFFromLittleEndian32(i) OFByteSwap32(i)
769# define OFFromLittleEndian64(i) OFByteSwap64(i)
778# define OFToBigEndian16(i) (i)
787# define OFToBigEndian32(i) (i)
796# define OFToBigEndian64(i) (i)
805# define OFToLittleEndian16(i) OFByteSwap16(i)
814# define OFToLittleEndian32(i) OFByteSwap32(i)
823# define OFToLittleEndian64(i) OFByteSwap64(i)
825# define OFFromBigEndian16(i) OFByteSwap16(i)
826# define OFFromBigEndian32(i) OFByteSwap32(i)
827# define OFFromBigEndian64(i) OFByteSwap64(i)
828# define OFFromLittleEndian16(i) (i)
829# define OFFromLittleEndian32(i) (i)
830# define OFFromLittleEndian64(i) (i)
831# define OFToBigEndian16(i) OFByteSwap16(i)
832# define OFToBigEndian32(i) OFByteSwap32(i)
833# define OFToBigEndian64(i) OFByteSwap64(i)
834# define OFToLittleEndian16(i) (i)
835# define OFToLittleEndian32(i) (i)
836# define OFToLittleEndian64(i) (i)
839#if defined(OF_FLOAT_BIG_ENDIAN) || defined(DOXYGEN)
846# define OFFromBigEndianFloat(f) (f)
854# define OFFromBigEndianDouble(d) (d)
862# define OFFromLittleEndianFloat(f) OFByteSwapFloat(f)
870# define OFFromLittleEndianDouble(d) OFByteSwapDouble(d)
878# define OFToBigEndianFloat(f) (f)
886# define OFToBigEndianDouble(d) (d)
894# define OFToLittleEndianFloat(f) OFByteSwapFloat(f)
902# define OFToLittleEndianDouble(d) OFByteSwapDouble(d)
904# define OFFromBigEndianFloat(f) OFByteSwapFloat(f)
905# define OFFromBigEndianDouble(d) OFByteSwapDouble(d)
906# define OFFromLittleEndianFloat(f) (f)
907# define OFFromLittleEndianDouble(d) (d)
908# define OFToBigEndianFloat(f) OFByteSwapFloat(f)
909# define OFToBigEndianDouble(d) OFByteSwapDouble(d)
910# define OFToLittleEndianFloat(f) (f)
911# define OFToLittleEndianDouble(d) (d)
921#define OFRotateLeft(value, bits) \
922 (((bits) % (sizeof(value) * 8)) > 0 \
923 ? ((value) << ((bits) % (sizeof(value) * 8))) | \
924 ((value) >> (sizeof(value) * 8 - ((bits) % (sizeof(value) * 8)))) \
934#define OFRotateRight(value, bits) \
935 (((bits) % (sizeof(value) * 8)) > 0 \
936 ? ((value) >> ((bits) % (sizeof(value) * 8))) | \
937 ((value) << (sizeof(value) * 8 - ((bits) % (sizeof(value) * 8)))) \
947#define OFRoundUpToPowerOf2(pow2, value) \
948 (((value) + (pow2) - 1) & ~((pow2) - 1))
950#define OF_ULONG_BIT (sizeof(unsigned long) * CHAR_BIT)
953OFBitSetIsSet(
unsigned long *_Nonnull storage,
size_t idx)
955 return storage[idx / OF_ULONG_BIT] & (1ul << (idx % OF_ULONG_BIT));
959OFBitSetSet(
unsigned long *_Nonnull storage,
size_t idx)
961 storage[idx / OF_ULONG_BIT] |= (1ul << (idx % OF_ULONG_BIT));
965OFBitSetClear(
unsigned long *_Nonnull storage,
size_t idx)
967 storage[idx / OF_ULONG_BIT] &= ~(1ul << (idx % OF_ULONG_BIT));
971OFZeroMemory(
void *_Nonnull buffer_,
size_t length)
973 volatile unsigned char *buffer = (
volatile unsigned char *)buffer_;
975 while (buffer < (
unsigned char *)buffer_ + length)
980OFASCIIIsAlpha(
char c)
982 return ((c >=
'a' && c <=
'z') || (c >=
'A' && c <=
'Z'));
986OFASCIIIsDigit(
char c)
988 return (c >=
'0' && c <=
'9');
992OFASCIIIsAlnum(
char c)
994 return (OFASCIIIsAlpha(c) || OFASCIIIsDigit(c));
998OFASCIIIsSpace(
char c)
1000 return (c ==
' ' || c ==
'\t' || c ==
'\n' || c ==
'\r' || c ==
'\f' ||
1004static OF_INLINE
char
1005OFASCIIToUpper(
char c)
1007 return (c >=
'a' && c <=
'z' ?
'A' + (c -
'a') : c);
1010static OF_INLINE
char
1011OFASCIIToLower(
char c)
1013 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:655
#define OFByteSwap32(i)
Byte swaps the specified 32 bit integer.
Definition macros.h:617
static OF_INLINE double OF_CONST_FUNC OFBitConvertUInt64ToDouble(uint64_t uInt64)
Bit-converts the specified uint64_t to a double.
Definition macros.h:683
#define OFByteSwap64(i)
Byte swaps the specified 64 bit integer.
Definition macros.h:626
static OF_INLINE double OF_CONST_FUNC OFByteSwapDouble(double d)
Byte swaps the specified double.
Definition macros.h:710
static OF_INLINE float OF_CONST_FUNC OFByteSwapFloat(float f)
Byte swaps the specified float.
Definition macros.h:697
static OF_INLINE uint64_t OF_CONST_FUNC OFBitConvertDoubleToUInt64(double d)
Bit-converts the specified double to a uint64_t.
Definition macros.h:669
static OF_INLINE uint32_t OF_CONST_FUNC OFBitConvertFloatToUInt32(float f)
Bit-converts the specified float to a uint32_t.
Definition macros.h:641