summaryrefslogtreecommitdiffstats
path: root/src/macro.h
blob: d664c3ef97a65fa6718598e007caf32cbcc5f25a (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#ifndef foomacrohfoo
#define foomacrohfoo

#include <stdio.h>
#include <assert.h>

#ifdef __GNUC__
#define SA_PRETTY_FUNCTION __PRETTY_FUNCTION__
#else
#define SA_PRETTY_FUNCTION ""
#endif

#define sa_return_if_fail(expr) \
    do { \
        if (!(expr)) { \
             fprintf(stderr, "%s: Assertion <%s> failed.\n", SA_PRETTY_FUNCTION, #expr ); \
            return; \
        } \
    } while(0)

#define sa_return_val_if_fail(expr, val) \
    do { \
        if (!(expr)) { \
            fprintf(stderr, "%s: Assertion <%s> failed.\n", SA_PRETTY_FUNCTION, #expr ); \
            return (val); \
        } \
    } while(0)

#define sa_return_if_fail_mutex(m, expr)         \
    do { \
        if (!(expr)) { \
            fprintf(stderr, "%s: Assertion <%s> failed.\n", SA_PRETTY_FUNCTION, #expr ); \
            sa_mutex_unlock(m);                                                       \
            return; \
        } \
    } while(0)

#define sa_return_val_if_fail_mutex(m, expr, val)        \
    do { \
        if (!(expr)) { \
                                                                        \
            fprintf(stderr, "%s: Assertion <%s> failed.\n", SA_PRETTY_FUNCTION, #expr ); \
            sa_mutex_unlock(m);                                                       \
            return (val); \
        } \
    } while(0)

#define sa_assert assert

/* An assert which guarantees side effects of x */
#ifdef NDEBUG
#define sa_assert_se(x) x
#else
#define sa_assert_se(x) sa_assert(x)
#endif

#define sa_assert_not_reached() sa_assert(!"Should not be reached.")

#define sa_assert_success(x) do {               \
        int _r = (x);                           \
        sa_assert(_r == 0);                     \
    } while(0)

#define SA_ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0]))

#ifndef MAX
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#endif

#ifndef MIN
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif

#ifndef CLAMP
#define CLAMP(x, low, high)  (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
#endif

#ifndef FALSE
#define FALSE (0)
#endif

#ifndef TRUE
#define TRUE (!FALSE)
#endif

#define SA_PTR_TO_UINT(p) ((unsigned int) (unsigned long) (p))
#define SA_UINT_TO_PTR(u) ((void*) (unsigned long) (u))

#define SA_PTR_TO_UINT32(p) ((uint32_t) SA_PTR_TO_UINT(p))
#define SA_UINT32_TO_PTR(u) SA_UINT_TO_PTR((uint32_t) u)

#define SA_PTR_TO_INT(p) ((int) SA_PTR_TO_UINT(p))
#define SA_INT_TO_PTR(u) SA_UINT_TO_PTR((int) u)

#define SA_PTR_TO_INT32(p) ((int32_t) SA_PTR_TO_UINT(p))
#define SA_INT32_TO_PTR(u) SA_UINT_TO_PTR((int32_t) u)

static inline size_t sa_align(size_t l) {
    return (((l + sizeof(void*) - 1) / sizeof(void*)) * sizeof(void*));
}

#define SA_ALIGN(x) (sa_align(x))

#endif