-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.h
More file actions
73 lines (63 loc) · 1.94 KB
/
util.h
File metadata and controls
73 lines (63 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// This is free and unencumbered software released into the public domain.
// For more information, see LICENSE.
#ifndef UTIL_H
#define UTIL_H
#include <stddef.h>
#include <err.h>
#include <stdio.h>
#include "ilog.h"
#define __pack __attribute__((packed))
#define __unused __attribute__((unused))
#define __align(n) __attribute__((aligned(n)))
#define __likey(x) __builtin_expect(!!(x), 1)
#define __unlikey(x) __builtin_expect(!!(x), 0)
#ifdef NDEBUG
#define xensure_f(f, cond)\
do {\
if (__unlikey((cond) == 0)) {\
f(1, "Invariant violated %s:%d\n %s\n" \
,__FILE__, __LINE__, #cond\
);\
}\
} while (0)
#else
#define xensure_f(f, cond)\
do{\
if (__unlikey((cond) == 0)) {\
fprintf(stderr, "Invariant violated %s:%d\n %s\n"\
,__FILE__, __LINE__, #cond\
);\
bug();\
}\
} while (0)
#endif
#define xensure(cond) xensure_f(errx, cond)
#define xensure_errno(cond) xensure_f(err, cond)
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define ptr_mask(p, m) ((void *)(((uintptr_t)(p)) & (m)))
#define ptr_align_bound(p, b) (ptr_mask(p, ~((b) - 1)))
#define ptr_align_bits(p, b) (ptr_align_bound(p, 1 << (b)))
// this will change to a dump and crash like behavior
#define ensure(cond)\
do {\
if (__unlikey((cond) == 0)) {\
ilog_dump();\
}\
xensure(cond);\
} while (0)
#ifndef NDEBUG
# define dbg(x) (x)
# define dbg_assert(cond) ensure(cond)
# define RUNTIME_INSTR (1)
#else
# define dbg(x)
# define dbg_assert(cond) ((void)(cond))
# define RUNTIME_INSTR (0)
#endif
void *reallocarr(void **, size_t, size_t);
void *reallocflexarr(void **, size_t, size_t, size_t );
void memzero(void *, size_t, size_t);
void rmemcpy(void *restrict, void *restrict, size_t);
void bug(void);
#endif // UTIL_H