19#include "objfw-defs.h"
21#ifndef __STDC_LIMIT_MACROS
22# define __STDC_LIMIT_MACROS
24#ifndef __STDC_CONSTANT_MACROS
25# define __STDC_CONSTANT_MACROS
40#ifdef OF_OBJFW_RUNTIME
41# ifdef OF_COMPILING_OBJFW
44# include <ObjFWRT/ObjFWRT.h>
47#ifdef OF_APPLE_RUNTIME
48# include <objc/objc.h>
49# include <objc/runtime.h>
50# include <objc/message.h>
54# define restrict __restrict__
55#elif __STDC_VERSION__ < 199901L
59#if __STDC_VERSION__ >= 201112L && !defined(static_assert)
61# define static_assert _Static_assert
64#if defined(OF_HAVE__THREAD_LOCAL)
65# define OF_HAVE_COMPILER_TLS
66# ifdef OF_HAVE_THREADS_H
71# define thread_local _Thread_local
74# define thread_local _Thread_local
76#elif defined(OF_HAVE___THREAD)
77# define OF_HAVE_COMPILER_TLS
78# define thread_local __thread
85#if defined(OF_HAVE_COMPILER_TLS) && defined(OF_IOS) && defined(OF_X86)
86# undef OF_HAVE_COMPILER_TLS
90# define OF_INLINE inline __attribute__((__always_inline__))
91# define OF_LIKELY(cond) (__builtin_expect(!!(cond), 1))
92# define OF_UNLIKELY(cond) (__builtin_expect(!!(cond), 0))
93# define OF_CONST_FUNC __attribute__((__const__))
94# define OF_NO_RETURN_FUNC __attribute__((__noreturn__))
95# define OF_WEAK_REF(sym) __attribute__((__weakref__(sym)))
97# define OF_INLINE inline
98# define OF_LIKELY(cond) (cond)
99# define OF_UNLIKELY(cond) (cond)
100# define OF_CONST_FUNC
101# define OF_NO_RETURN_FUNC
102# define OF_WEAK_REF(sym)
105#if __STDC_VERSION__ >= 201112L
106# define OF_ALIGNOF(type) _Alignof(type)
107# define OF_ALIGNAS(type) _Alignas(type)
109# define OF_ALIGNOF(type) __alignof__(type)
110# define OF_ALIGNAS(type) __attribute__((__aligned__(__alignof__(type))))
113#if __STDC_VERSION__ >= 201112L && defined(OF_HAVE_MAX_ALIGN_T)
114# define OF_BIGGEST_ALIGNMENT _Alignof(max_align_t)
116# ifdef __BIGGEST_ALIGNMENT__
117# define OF_BIGGEST_ALIGNMENT __BIGGEST_ALIGNMENT__
120# define OF_BIGGEST_ALIGNMENT 16
124#define OF_PREPROCESSOR_CONCAT2(a, b) a##b
125#define OF_PREPROCESSOR_CONCAT(a, b) OF_PREPROCESSOR_CONCAT2(a, b)
127#if __OBJFW_RUNTIME_ABI__ || (defined(OF_APPLE_RUNTIME) && defined(__OBJC2__))
128# define OF_HAVE_NONFRAGILE_IVARS
131#ifdef OF_HAVE_NONFRAGILE_IVARS
132# define OF_RESERVE_IVARS(cls, num)
134# define OF_RESERVE_IVARS(cls, num) \
136 void *OF_PREPROCESSOR_CONCAT(_reserved_, cls)[num];
140# define OF_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
142# define OF_GCC_VERSION 0
145#define OF_STRINGIFY(s) OF_STRINGIFY2(s)
146#define OF_STRINGIFY2(s) #s
149# define __has_feature(x) 0
152#ifndef __has_attribute
153# define __has_attribute(x) 0
156#if __has_feature(objc_bool)
158# define YES __objc_yes
163# define true ((bool)1)
165# define false ((bool)0)
169#if !__has_feature(objc_instancetype)
170# define instancetype id
173#if __has_feature(blocks)
174# define OF_HAVE_BLOCKS
177#if __has_feature(objc_arc)
178# define OF_RETURNS_RETAINED __attribute__((__ns_returns_retained__))
179# define OF_RETURNS_NOT_RETAINED __attribute__((__ns_returns_not_retained__))
180# define OF_RETURNS_INNER_POINTER \
181 __attribute__((__objc_returns_inner_pointer__))
182# define OF_CONSUMED __attribute__((__ns_consumed__))
183# define OF_WEAK_UNAVAILABLE __attribute__((__objc_arc_weak_unavailable__))
185# define OF_RETURNS_RETAINED
186# define OF_RETURNS_NOT_RETAINED
187# define OF_RETURNS_INNER_POINTER
189# define OF_WEAK_UNAVAILABLE
194# undef __unsafe_unretained
196# undef __autoreleasing
197# define __unsafe_unretained
199# define __autoreleasing
202#if __has_feature(objc_generics)
203# define OF_HAVE_GENERICS
204# define OF_GENERIC(...) <__VA_ARGS__>
206# define OF_GENERIC(...)
209#if __has_feature(nullability)
210# define OF_ASSUME_NONNULL_BEGIN _Pragma("clang assume_nonnull begin")
211# define OF_ASSUME_NONNULL_END _Pragma("clang assume_nonnull end")
212# define OF_NULLABLE_PROPERTY(...) (__VA_ARGS__, nullable)
213# define OF_NULL_RESETTABLE_PROPERTY(...) (__VA_ARGS__, null_resettable)
215# define OF_ASSUME_NONNULL_BEGIN
216# define OF_ASSUME_NONNULL_END
219# define _Null_unspecified
220# define OF_NULLABLE_PROPERTY
221# define OF_NULL_RESETTABLE_PROPERTY
224# define null_unspecified
227#if __has_feature(objc_kindof)
228# define OF_KINDOF(class_) __kindof class_
230# define OF_KINDOF(class_) id
233#if __has_feature(objc_class_property)
234# define OF_HAVE_CLASS_PROPERTIES
237#if defined(__clang__) || OF_GCC_VERSION >= 405
238# define OF_UNREACHABLE __builtin_unreachable();
240# define OF_UNREACHABLE abort();
243#if defined(__clang__) || OF_GCC_VERSION >= 406
244# define OF_SENTINEL __attribute__((__sentinel__))
245# define OF_NO_RETURN __attribute__((__noreturn__))
252# define OF_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
254# define OF_WARN_UNUSED_RESULT
257#if __has_attribute(__unavailable__)
258# define OF_UNAVAILABLE __attribute__((__unavailable__))
260# define OF_UNAVAILABLE
263#if __has_attribute(__objc_requires_super__)
264# define OF_REQUIRES_SUPER __attribute__((__objc_requires_super__))
266# define OF_REQUIRES_SUPER
269#if __has_attribute(__objc_root_class__)
270# define OF_ROOT_CLASS __attribute__((__objc_root_class__))
272# define OF_ROOT_CLASS
275#if __has_attribute(__objc_subclassing_restricted__)
276# define OF_SUBCLASSING_RESTRICTED \
277 __attribute__((__objc_subclassing_restricted__))
279# define OF_SUBCLASSING_RESTRICTED
282#if __has_attribute(__objc_method_family__)
283# define OF_METHOD_FAMILY(f) __attribute__((__objc_method_family__(f)))
285# define OF_METHOD_FAMILY(f)
288#if __has_attribute(__objc_designated_initializer__)
289# define OF_DESIGNATED_INITIALIZER \
290 __attribute__((__objc_designated_initializer__))
292# define OF_DESIGNATED_INITIALIZER
295#if __has_attribute(__objc_boxable__)
296# define OF_BOXABLE __attribute__((__objc_boxable__))
301#if __has_attribute(__swift_name__)
302# define OF_SWIFT_NAME(name) __attribute__((__swift_name__(name)))
304# define OF_SWIFT_NAME(name)
307#if __has_attribute(__objc_direct__) && defined(OF_APPLE_RUNTIME)
308# define OF_DIRECT __attribute__((__objc_direct__))
312#if __has_attribute(__objc_direct_members__) && defined(OF_APPLE_RUNTIME)
313# define OF_DIRECT_MEMBERS __attribute__((__objc_direct_members__))
315# define OF_DIRECT_MEMBERS
318#ifdef OF_APPLE_RUNTIME
319# if defined(OF_AMD64) || defined(OF_X86) || defined(OF_ARM64) || \
320 defined(OF_ARM) || defined(OF_POWERPC)
321# define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR
322# define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR_STRET
326# if defined(OF_AMD64) || defined(OF_X86) || \
327 defined(OF_ARM64) || defined(OF_ARM) || defined(OF_POWERPC) || \
328 defined(OF_MIPS) || defined(OF_SPARC64) || defined(OF_SPARC)
329# define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR
330# if __OBJFW_RUNTIME_ABI__ >= 800
331# define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR_STRET
334# elif defined(OF_MACH_O)
335# if defined(OF_AMD64)
336# define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR
337# if __OBJFW_RUNTIME_ABI__ >= 800
338# define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR_STRET
341# elif defined(OF_WINDOWS)
342# if defined(OF_AMD64) || defined(OF_X86)
343# define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR
344# if __OBJFW_RUNTIME_ABI__ >= 800
345# define OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR_STRET
351#define OFMaxRetainCount UINT_MAX
353#ifdef OBJC_COMPILING_RUNTIME
354# define OFEnsure(cond) \
356 if OF_UNLIKELY (!(cond)) \
357 objc_error("ObjFWRT @ " __FILE__ ":" \
358 OF_STRINGIFY(__LINE__), \
359 "Failed to ensure condition:\n" #cond); \
364# define OFEnsure(cond) \
366 if OF_UNLIKELY (!(cond)) { \
367 OFLog(@"Failed to ensure condition in " \
368 @__FILE__ ":%d: " @#cond, __LINE__); \
375# define OFAssert(...) OFEnsure(__VA_ARGS__)
377# define OFAssert(...)
380#define OF_UNRECOGNIZED_SELECTOR OFMethodNotFound(self, _cmd);
381#if __has_feature(objc_arc)
382# define OF_INVALID_INIT_METHOD OFMethodNotFound(self, _cmd);
384# define OF_INVALID_INIT_METHOD \
386 OFMethodNotFound(self, _cmd); \
395# define OF_DEALLOC_UNSUPPORTED \
396 [self doesNotRecognizeSelector: _cmd]; \
400 _Pragma("clang diagnostic push"); \
401 _Pragma("clang diagnostic ignored \"-Wunreachable-code\""); \
403 _Pragma("clang diagnostic pop");
405# define OF_DEALLOC_UNSUPPORTED \
406 [self doesNotRecognizeSelector: _cmd]; \
412#define OF_SINGLETON_METHODS \
413 - (instancetype)autorelease \
418 - (instancetype)retain \
427 - (unsigned int)retainCount \
429 return OFMaxRetainCount; \
434 OF_DEALLOC_UNSUPPORTED \
437#define OF_CONSTRUCTOR(prio) \
438 static void __attribute__((__constructor__(prio))) \
439 OF_PREPROCESSOR_CONCAT(constructor, __LINE__)(void)
440#define OF_DESTRUCTOR(prio) \
441 static void __attribute__((__destructor__(prio))) \
442 OF_PREPROCESSOR_CONCAT(destructor, __LINE__)(void)
444static OF_INLINE uint16_t OF_CONST_FUNC
445OFByteSwap16Const(uint16_t i)
447 return (i & UINT16_C(0xFF00)) >> 8 | (i & UINT16_C(0x00FF)) << 8;
450static OF_INLINE uint32_t OF_CONST_FUNC
451OFByteSwap32Const(uint32_t i)
453 return (i & UINT32_C(0xFF000000)) >> 24 |
454 (i & UINT32_C(0x00FF0000)) >> 8 |
455 (i & UINT32_C(0x0000FF00)) << 8 |
456 (i & UINT32_C(0x000000FF)) << 24;
459static OF_INLINE uint64_t OF_CONST_FUNC
460OFByteSwap64Const(uint64_t i)
462 return (i & UINT64_C(0xFF00000000000000)) >> 56 |
463 (i & UINT64_C(0x00FF000000000000)) >> 40 |
464 (i & UINT64_C(0x0000FF0000000000)) >> 24 |
465 (i & UINT64_C(0x000000FF00000000)) >> 8 |
466 (i & UINT64_C(0x00000000FF000000)) << 8 |
467 (i & UINT64_C(0x0000000000FF0000)) << 24 |
468 (i & UINT64_C(0x000000000000FF00)) << 40 |
469 (i & UINT64_C(0x00000000000000FF)) << 56;
472static OF_INLINE uint16_t OF_CONST_FUNC
473OFByteSwap16NonConst(uint16_t i)
475#if defined(OF_HAVE_BUILTIN_BSWAP16)
476 return __builtin_bswap16(i);
477#elif (defined(OF_AMD64) || defined(OF_X86)) && defined(__GNUC__)
483#elif defined(OF_POWERPC) && defined(__GNUC__)
489#elif defined(OF_ARMV6) && defined(__GNUC__)
496 i = (i & UINT16_C(0xFF00)) >> 8 |
497 (i & UINT16_C(0x00FF)) << 8;
502static OF_INLINE uint32_t OF_CONST_FUNC
503OFByteSwap32NonConst(uint32_t i)
505#if defined(OF_HAVE_BUILTIN_BSWAP32)
506 return __builtin_bswap32(i);
507#elif (defined(OF_AMD64) || defined(OF_X86)) && defined(__GNUC__)
513#elif defined(OF_POWERPC) && defined(__GNUC__)
519#elif defined(OF_ARMV6) && defined(__GNUC__)
526 i = (i & UINT32_C(0xFF000000)) >> 24 |
527 (i & UINT32_C(0x00FF0000)) >> 8 |
528 (i & UINT32_C(0x0000FF00)) << 8 |
529 (i & UINT32_C(0x000000FF)) << 24;
534static OF_INLINE uint64_t OF_CONST_FUNC
535OFByteSwap64NonConst(uint64_t i)
537#if defined(OF_HAVE_BUILTIN_BSWAP64)
538 return __builtin_bswap64(i);
539#elif defined(OF_AMD64) && defined(__GNUC__)
545#elif defined(OF_X86) && defined(__GNUC__)
554 i = (uint64_t)OFByteSwap32NonConst(
555 (uint32_t)(i & UINT32_C(0xFFFFFFFF))) << 32 |
556 OFByteSwap32NonConst((uint32_t)(i >> 32));
562# define OFByteSwap16(i) \
563 (__builtin_constant_p(i) ? OFByteSwap16Const(i) : OFByteSwap16NonConst(i))
564# define OFByteSwap32(i) \
565 (__builtin_constant_p(i) ? OFByteSwap32Const(i) : OFByteSwap32NonConst(i))
566# define OFByteSwap64(i) \
567 (__builtin_constant_p(i) ? OFByteSwap64Const(i) : OFByteSwap64NonConst(i))
569# define OFByteSwap16(i) OFByteSwap16Const(i)
570# define OFByteSwap32(i) OFByteSwap32Const(i)
571# define OFByteSwap64(i) OFByteSwap64Const(i)
574static OF_INLINE uint32_t
575OFFloatToRawUInt32(
float f)
582static OF_INLINE
float
583OFRawUInt32ToFloat(uint32_t uInt32)
586 memcpy(&ret, &uInt32, 4);
590static OF_INLINE uint64_t
591OFDoubleToRawUInt64(
double d)
598static OF_INLINE
double
599OFRawUInt64ToDouble(uint64_t uInt64)
602 memcpy(&ret, &uInt64, 8);
606static OF_INLINE
float OF_CONST_FUNC
607OFByteSwapFloat(
float f)
609 return OFRawUInt32ToFloat(OFByteSwap32(OFFloatToRawUInt32(f)));
612static OF_INLINE
double OF_CONST_FUNC
613OFByteSwapDouble(
double d)
615 return OFRawUInt64ToDouble(OFByteSwap64(OFDoubleToRawUInt64(d)));
619# define OFFromBigEndian16(i) (i)
620# define OFFromBigEndian32(i) (i)
621# define OFFromBigEndian64(i) (i)
622# define OFFromLittleEndian16(i) OFByteSwap16(i)
623# define OFFromLittleEndian32(i) OFByteSwap32(i)
624# define OFFromLittleEndian64(i) OFByteSwap64(i)
625# define OFToBigEndian16(i) (i)
626# define OFToBigEndian32(i) (i)
627# define OFToBigEndian64(i) (i)
628# define OFToLittleEndian16(i) OFByteSwap16(i)
629# define OFToLittleEndian32(i) OFByteSwap32(i)
630# define OFToLittleEndian64(i) OFByteSwap64(i)
632# define OFFromBigEndian16(i) OFByteSwap16(i)
633# define OFFromBigEndian32(i) OFByteSwap32(i)
634# define OFFromBigEndian64(i) OFByteSwap64(i)
635# define OFFromLittleEndian16(i) (i)
636# define OFFromLittleEndian32(i) (i)
637# define OFFromLittleEndian64(i) (i)
638# define OFToBigEndian16(i) OFByteSwap16(i)
639# define OFToBigEndian32(i) OFByteSwap32(i)
640# define OFToBigEndian64(i) OFByteSwap64(i)
641# define OFToLittleEndian16(i) (i)
642# define OFToLittleEndian32(i) (i)
643# define OFToLittleEndian64(i) (i)
646#ifdef OF_FLOAT_BIG_ENDIAN
647# define OFFromBigEndianFloat(f) (f)
648# define OFFromBigEndianDouble(d) (d)
649# define OFFromLittleEndianFloat(f) OFByteSwapFloat(f)
650# define OFFromLittleEndianDouble(d) OFByteSwapDouble(d)
651# define OFToBigEndianFloat(f) (f)
652# define OFToBigEndianDouble(d) (d)
653# define OFToLittleEndianFloat(f) OFByteSwapFloat(f)
654# define OFToLittleEndianDouble(d) OFByteSwapDouble(d)
656# define OFFromBigEndianFloat(f) OFByteSwapFloat(f)
657# define OFFromBigEndianDouble(d) OFByteSwapDouble(d)
658# define OFFromLittleEndianFloat(f) (f)
659# define OFFromLittleEndianDouble(d) (d)
660# define OFToBigEndianFloat(f) OFByteSwapFloat(f)
661# define OFToBigEndianDouble(d) OFByteSwapDouble(d)
662# define OFToLittleEndianFloat(f) (f)
663# define OFToLittleEndianDouble(d) (d)
666#define OFRotateLeft(value, bits) \
667 (((bits) % (sizeof(value) * 8)) > 0 \
668 ? ((value) << ((bits) % (sizeof(value) * 8))) | \
669 ((value) >> (sizeof(value) * 8 - ((bits) % (sizeof(value) * 8)))) \
671#define OFRotateRight(value, bits) \
672 (((bits) % (sizeof(value) * 8)) > 0 \
673 ? ((value) >> ((bits) % (sizeof(value) * 8))) | \
674 ((value) << (sizeof(value) * 8 - ((bits) % (sizeof(value) * 8)))) \
677#define OFRoundUpToPowerOf2(pow2, value) \
678 (((value) + (pow2) - 1) & ~((pow2) - 1))
681OFBitsetIsSet(
unsigned char *_Nonnull storage,
size_t idx)
683 return storage[idx / CHAR_BIT] & (1u << (idx % CHAR_BIT));
687OFBitsetSet(
unsigned char *_Nonnull storage,
size_t idx)
689 storage[idx / CHAR_BIT] |= (1u << (idx % CHAR_BIT));
693OFBitsetClear(
unsigned char *_Nonnull storage,
size_t idx)
695 storage[idx / CHAR_BIT] &= ~(1u << (idx % CHAR_BIT));
699OFZeroMemory(
void *_Nonnull buffer_,
size_t length)
701 volatile unsigned char *buffer = (
volatile unsigned char *)buffer_;
703 while (buffer < (
unsigned char *)buffer_ + length)
708OFASCIIIsAlpha(
char c)
710 return ((c >=
'a' && c <=
'z') || (c >=
'A' && c <=
'Z'));
714OFASCIIIsDigit(
char c)
716 return (c >=
'0' && c <=
'9');
720OFASCIIIsAlnum(
char c)
722 return (OFASCIIIsAlpha(c) || OFASCIIIsDigit(c));
726OFASCIIIsSpace(
char c)
728 return (c ==
' ' || c ==
'\t' || c ==
'\n' || c ==
'\r' || c ==
'\f' ||
733OFASCIIToUpper(
char c)
735 return (c >=
'a' && c <=
'z' ?
'A' + (c -
'a') : c);
739OFASCIIToLower(
char c)
741 return (c >=
'A' && c <=
'Z' ?
'a' + (c -
'A') : c);
A class for storing constant strings using the @"" literal.
Definition OFConstantString.h:38