#ifndef __OS_ASSUMES_H__
#define __OS_ASSUMES_H__
#include <sys/cdefs.h>
__BEGIN_DECLS
#include <Availability.h>
#include <TargetConditionals.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <stdbool.h>
#include <_simple.h>
#include <errno.h>
#include <os/base_private.h>
#if __GNUC__
#define os_constant(x) __builtin_constant_p((x))
#define os_hardware_trap() __asm__ __volatile__ (""); __builtin_trap()
#define __OS_COMPILETIME_ASSERT__(e) __extension__({ \
char __compile_time_assert__[(e) ? 1 : -1]; \
(void)__compile_time_assert__; \
})
#else
#define os_constant(x) ((long)0)
#define os_hardware_trap() abort()
#define __OS_COMPILETIME_ASSERT__(e) (e)
#endif
typedef bool (*os_redirect_t)(const char *);
struct _os_redirect_assumes_s {
os_redirect_t redirect;
};
#define OS_ASSUMES_REDIRECT_SEG "__DATA"
#define OS_ASSUMES_REDIRECT_SECT "__os_assumes_log"
#define os_redirect_assumes(func) \
__attribute__((__used__)) \
__attribute__((__section__(OS_ASSUMES_REDIRECT_SEG "," OS_ASSUMES_REDIRECT_SECT))) \
static struct _os_redirect_assumes_s _os_redirect_##func = { \
.redirect = &func, \
};
typedef bool (*os_log_callout_t)(_SIMPLE_STRING asl_message, void *ctx, const char *);
#include <CrashReporterClient.h>
#define os_set_crash_message(arg) CRSetCrashLogMessage(arg)
#define os_assumes(e) __extension__({ \
__typeof__(e) _e = os_fastpath(e); \
if (!_e) { \
if (os_constant(e)) { \
__OS_COMPILETIME_ASSERT__(e); \
} \
_os_assumes_log((uint64_t)(uintptr_t)_e); \
_os_avoid_tail_call(); \
} \
_e; \
})
#define os_assumes_zero(e) __extension__({ \
__typeof__(e) _e = os_slowpath(e); \
if (_e) { \
if (os_constant(e)) { \
__OS_COMPILETIME_ASSERT__(!e); \
} \
_os_assumes_log((uint64_t)(uintptr_t)_e); \
_os_avoid_tail_call(); \
} \
_e; \
})
#define posix_assumes_zero(e) __extension__({ \
__typeof__(e) _e = os_slowpath(e); \
if (_e == (typeof(e))-1) { \
_os_assumes_log((uint64_t)(uintptr_t)errno); \
_os_avoid_tail_call(); \
} \
_e; \
})
#define os_assert(e) __extension__({ \
__typeof__(e) _e = os_fastpath(e); \
if (!_e) { \
if (os_constant(e)) { \
__OS_COMPILETIME_ASSERT__(e); \
} \
\
char *_fail_message = _os_assert_log((uint64_t)(uintptr_t)_e); \
os_set_crash_message(_fail_message); \
os_hardware_trap(); \
free(_fail_message); \
} \
})
#define os_assert_zero(e) __extension__({ \
__typeof__(e) _e = os_slowpath(e); \
if (_e) { \
if (os_constant(e)) { \
__OS_COMPILETIME_ASSERT__(!e); \
} \
\
char *_fail_message = _os_assert_log((uint64_t)(uintptr_t)_e); \
os_set_crash_message(_fail_message); \
os_hardware_trap(); \
free(_fail_message); \
} \
})
#define posix_assert_zero(e) __extension__({ \
__typeof__(e) _e = os_slowpath(e); \
if (_e == (__typeof__(e))-1) { \
char *_fail_message = _os_assert_log((uint64_t)(uintptr_t)errno); \
os_set_crash_message(_fail_message); \
os_hardware_trap(); \
free(_fail_message); \
} \
})
#define os_assumes_ctx(f, ctx, e) __extension__({ \
__typeof__(e) _e = os_fastpath(e); \
if (!_e) { \
if (os_constant(e)) { \
__OS_COMPILETIME_ASSERT__(e); \
} \
_os_assumes_log_ctx(f, ctx, (uintptr_t)_e); \
_os_avoid_tail_call(); \
} \
_e; \
})
#define os_assumes_zero_ctx(f, ctx, e) __extension__({ \
__typeof__(e) _e = os_slowpath(e); \
if (_e) { \
if (os_constant(e)) { \
__OS_COMPILETIME_ASSERT__(!e); \
} \
_os_assumes_log_ctx((f), (ctx), (uintptr_t)_e); \
_os_avoid_tail_call(); \
} \
_e; \
})
#define posix_assumes_zero_ctx(f, ctx, e) __extension__({ \
__typeof__(e) _e = os_slowpath(e); \
if (_e == (__typeof__(e))-1) { \
_os_assumes_log_ctx((f), (ctx), (uintptr_t)errno); \
_os_avoid_tail_call(); \
} \
_e; \
})
#define os_assert_ctx(f, ctx, e) __extension__({ \
__typeof__(e) _e = os_fastpath(e); \
if (!_e) { \
if (os_constant(e)) { \
__OS_COMPILETIME_ASSERT__(e); \
} \
\
char *_fail_message = _os_assert_log_ctx((f), (ctx), (uint64_t)(uintptr_t)_e); \
os_set_crash_message(_fail_message); \
os_hardware_trap(); \
free(_fail_message); \
} \
})
#define os_assert_zero_ctx(f, ctx, e) __extension__({ \
__typeof__(e) _e = os_slowpath(e); \
if (_e) { \
if (os_constant(e)) { \
__OS_COMPILETIME_ASSERT__(!e); \
} \
\
char *_fail_message = _os_assert_log_ctx((f), (ctx), (uint64_t)(uintptr_t)_e); \
os_set_crash_message(_fail_message); \
os_hardware_trap(); \
free(_fail_message); \
} \
})
#define posix_assert_zero_ctx(f, ctx, e) __extension__({ \
__typeof__(e) _e = os_slowpath(e); \
if (_e == (__typeof__(e))-1) { \
char *_fail_message = _os_assert_log_ctx((f), (ctx), (uint64_t)(uintptr_t)errno); \
os_set_crash_message(_fail_message); \
os_hardware_trap(); \
free(_fail_message); \
} \
})
__OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_6_0)
extern void
_os_assumes_log(uint64_t code);
__OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_6_0)
extern char *
_os_assert_log(uint64_t code);
__OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_6_0)
extern void
_os_assumes_log_ctx(os_log_callout_t callout, void *ctx, uint64_t code);
__OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_6_0)
extern char *
_os_assert_log_ctx(os_log_callout_t callout, void *ctx, uint64_t code);
__OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_6_0)
extern void
_os_avoid_tail_call(void);
__END_DECLS
#endif