1
0
Fork 0
mirror of https://github.com/tailix/libkernaux.git synced 2024-11-13 11:04:27 -05:00
libkernaux/include/kernaux/macro.h

102 lines
2.7 KiB
C
Raw Normal View History

#ifndef KERNAUX_INCLUDED_MACRO
#define KERNAUX_INCLUDED_MACRO
#ifdef __cplusplus
extern "C" {
#endif
#include <stddef.h>
#include <stdint.h>
2022-11-28 20:19:35 -05:00
/*********************
* Language features *
*********************/
2022-06-30 16:07:10 -04:00
2022-11-28 20:19:35 -05:00
#define KERNAUX_NORETURN __attribute__((noreturn))
#define KERNAUX_RETURNS_TWICE __attribute__((returns_twice))
2022-12-03 05:23:52 -05:00
#define KERNAUX_UNUSED __attribute__((unused))
#define KERNAUX_USED __attribute__((used))
#define KERNAUX_ALIGNED(num) __attribute__((aligned(num)))
#define KERNAUX_PRINTF(fmt, rest) __attribute__((format(printf, fmt, rest)))
#define KERNAUX_SECTION(name) __attribute__((section(name)))
2022-11-28 20:19:35 -05:00
#ifdef __TINYC__
# define KERNAUX_PACKED
#else
# define KERNAUX_PACKED __attribute__((packed))
#endif
2022-11-28 20:19:35 -05:00
#define KERNAUX_ASM(...) do { __asm__ __volatile__(__VA_ARGS__); } while (0)
/**************
* Visibility *
**************/
2022-06-20 17:28:35 -04:00
#ifdef KERNAUX_ACCESS_PRIVATE
# define KERNAUX_PRIVATE_FIELD(id) id
# define KERNAUX_PROTECTED_FIELD(id) id
#else
2022-06-20 17:28:35 -04:00
# define KERNAUX_PRIVATE_FIELD(id) _private_##id
# ifdef KERNAUX_ACCESS_PROTECTED
# define KERNAUX_PROTECTED_FIELD(id) id
# else
# define KERNAUX_PROTECTED_FIELD(id) _protected_##id
# endif
#endif // KERNAUX_ACCESS_PRIVATE
2022-11-28 20:19:35 -05:00
/*********************
* Static assertions *
*********************/
2022-12-11 08:40:49 -05:00
#define KERNAUX_STATIC_TEST(name, cond) \
KERNAUX_UNUSED \
static const int \
_kernaux_static_test_##name[(cond) ? 1 : -1]
#define KERNAUX_STATIC_TEST_STRUCT_SIZE(name, size) \
2022-12-11 08:40:49 -05:00
KERNAUX_STATIC_TEST(struct_size_##name, sizeof(struct name) == (size))
2022-12-07 18:13:27 -05:00
#define KERNAUX_STATIC_TEST_UNION_SIZE(name, size) \
2022-12-11 08:40:49 -05:00
KERNAUX_STATIC_TEST(union_size_##name, sizeof(union name) == (size))
2022-12-07 18:13:27 -05:00
2022-11-28 20:19:35 -05:00
/*****************
* Simple values *
*****************/
#define KERNAUX_EOF (-1)
/*********************
* Calculated values *
*********************/
#define KERNAUX_CONTAINER_OF(ptr, type, member) \
((type*)((uintptr_t)(ptr) - offsetof(type, member)))
#define KERNAUX_BITS(n) (1u << (n))
#define KERNAUX_BITS8(n) ((uint8_t )(((uint8_t )1) << (n)))
#define KERNAUX_BITS16(n) ((uint16_t)(((uint16_t)1) << (n)))
#define KERNAUX_BITS32(n) ((uint32_t)(((uint32_t)1) << (n)))
#define KERNAUX_BITS64(n) ((uint64_t)(((uint64_t)1) << (n)))
/*********************
* Safe type casting *
*********************/
#define KERNAUX_CAST_VAR(type, name, value) \
2022-12-11 08:40:49 -05:00
KERNAUX_STATIC_TEST(cast_pos_##name, sizeof(value) <= sizeof(type)); \
KERNAUX_STATIC_TEST(cast_neg_##name, sizeof(-(value)) <= sizeof(type)); \
type name = (type)(value); \
do {} while (0)
#define KERNAUX_CAST_CONST(type, name, value) \
KERNAUX_CAST_VAR(const type, name, value)
#ifdef __cplusplus
}
#endif
#endif