libcortex
Event loop library following a "batteries included" approach
Loading...
Searching...
No Matches
intern.h
1#ifndef INTERN_H
2#define INTERN_H
3
4#ifdef __cplusplus
5extern "C" {
6#endif
7
8/*
9 * Mario Kicherer (dev@kicherer.org) 2016
10 *
11 */
12
13#define CRTX_STRINGIFYB(x) #x
14#define CRTX_STRINGIFY(x) CRTX_STRINGIFYB(x)
15
16#define CRTX_VLEVEL_SILENT 0
17#define CRTX_VLEVEL_ERR 1
18#define CRTX_VLEVEL_INFO 2
19#define CRTX_VLEVEL_DBG 3
20#define CRTX_VLEVEL_VDBG 4
21
22#define CRTX_SUCCESS 0
23// #define CRTX_ERROR -1
24
25#define CRTX_INFO(fmt, ...) do { crtx_printf(CRTX_VLEVEL_INFO, fmt, ##__VA_ARGS__); } while (0)
26
27#ifndef CRTX_VDEBUG
28 #define CRTX_DBG(fmt, ...) do { crtx_printf(CRTX_VLEVEL_DBG, fmt, ##__VA_ARGS__); } while (0)
29 #define CRTX_VDBG(fmt, ...) do { crtx_printf(CRTX_VLEVEL_VDBG, fmt, ##__VA_ARGS__); } while (0)
30#else
31 #define CRTX_DBG(fmt, ...) do { crtx_printf(CRTX_VLEVEL_DBG, "%s:%d:%s(): " fmt, __FILE__, __LINE__, __func__, ##__VA_ARGS__); } while (0)
32 #define CRTX_VDBG(fmt, ...) do { crtx_printf(CRTX_VLEVEL_VDBG, "%s:%d:%s(): " fmt, __FILE__, __LINE__, __func__, ##__VA_ARGS__); } while (0)
33#endif
34
35#ifndef SEGV_ON_ERROR
36#define CRTX_ERROR(fmt, ...) do { crtx_printf(CRTX_VLEVEL_ERR, "\x1b[31m"); crtx_printf(CRTX_VLEVEL_ERR, fmt, ##__VA_ARGS__); crtx_printf(CRTX_VLEVEL_ERR, "\x1b[0m"); } while (0)
37#else
38#define CRTX_ERROR(fmt, ...) do { crtx_printf(CRTX_VLEVEL_ERR, "\x1b[31m"); crtx_printf(CRTX_VLEVEL_ERR, fmt, ##__VA_ARGS__); crtx_printf(CRTX_VLEVEL_ERR, "\x1b[0m"); char *d = 0; printf("%c", *d); } while (0)
39#endif
40
41#define CRTX_ASSERT(x) do { if (!(x)) { CRTX_ERROR(__FILE__ ":" CRTX_STRINGIFY(__LINE__) " assertion failed: " #x "\n"); exit(1); } } while (0)
42
43#define CRTX_ASSERT_STR(x, msg, ...) do { if (!(x)) { CRTX_ERROR(__FILE__ ":" CRTX_STRINGIFY(__LINE__) " " msg " " #x "failed\n", __VA_ARGS__); exit(1); } } while (0)
44
45#if __STDC_VERSION__ >= 201112L
46#define CRTX_STATIC_ASSERT(X, msg) _Static_assert(X, msg)
47#else
48#define CRTX_STATIC_ASSERT(X, msg) ({ extern int __attribute__((error("assertion failed: '" #X "' - " #msg))) crtx_static_assert(); ((X)?0:crtx_static_assert()),0; })
49#endif
50
51#define CRTX_POPCOUNT32(x) __builtin_popcount(x)
52
53#define CRTX_DEFINE_ALLOC_FUNCTION(lstnr) \
54 struct crtx_ ## lstnr ## _listener * crtx_alloc_ ## lstnr ## _listener() { \
55 return calloc(1, sizeof(struct crtx_ ## lstnr ## _listener)); \
56 }
57#define CRTX_DEFINE_CALLOC_FUNCTION(lstnr) \
58 struct crtx_ ## lstnr ## _listener * crtx_ ## lstnr ## _calloc_listener() { \
59 struct crtx_ ## lstnr ## _listener *tmp; \
60 tmp = calloc(1, sizeof(struct crtx_ ## lstnr ## _listener)); \
61 crtx_ ## lstnr ## _clear_listener(tmp); \
62 return tmp; \
63 }
64
65#ifdef __cplusplus
66}
67#endif
68
69#endif