2024-08-14 00:33:52 +03:00
|
|
|
#ifndef DEFS_H_
|
|
|
|
#define DEFS_H_
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
|
2024-08-15 17:53:02 +03:00
|
|
|
#define lengthof(arr) (sizeof(arr) / sizeof(*arr))
|
2024-08-14 00:33:52 +03:00
|
|
|
#define containerof(ptr, contype, membpath) ((contype*)(0 ? (void)(((contype*)NULL)->membpath = *(ptr)) : (void)0, ((char *)(ptr)) - offsetof(contype, membpath)))
|
|
|
|
// Assuming child type has a field for the base type
|
|
|
|
// So for structs it is usually actual downcast, but for unions it is an upcast
|
|
|
|
#define DOWNCAST(contype, basename, ptr) containerof(ptr, contype, as_##basename)
|
2024-08-15 18:35:51 +03:00
|
|
|
// Expects ptr to be of type srctype* or void*, returns (dsttype*)ptr
|
|
|
|
#define IMPLICIT_CAST(dsttype, srctype, ptr) (((union { typeof(srctype) *src; typeof(dsttype) *dst; }){.src = ptr}).dst)
|
2024-08-15 17:53:02 +03:00
|
|
|
#define T_ALLOC(count, T) ((T*)calloc(count, sizeof(T)))
|
2024-08-14 00:33:52 +03:00
|
|
|
|
2024-08-15 21:57:17 +03:00
|
|
|
#define MODULE_CONSTRUCTOR(name) __attribute__((constructor)) static void name(void)
|
|
|
|
|
2024-08-14 00:33:52 +03:00
|
|
|
#define DEBUG_PRINT_VALUE(x, fmt) fprintf(stderr, #x " = " fmt "\n", x); fflush(stderr)
|
|
|
|
|
|
|
|
#endif /* end of include guard: DEFS_H_ */
|