-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.h
88 lines (73 loc) · 2.32 KB
/
common.h
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
#ifndef _COMMON_H
# define _COMMON_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __GNUC__
# define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
#else
# define GCC_VERSION 0
#endif
#ifndef UNUSED
# if GCC_VERSION >= 20700
# define UNUSED __attribute__((unused))
# else
# define UNUSED
# endif
#endif
#if defined(TRUE) || defined(FALSE)
# undef TRUE
# undef FALSE
#endif
#define TRUE 1
#define FALSE !TRUE
#if GCC_VERSION >= 30400
# define __must_check __attribute__((warn_unused_result))
# define __malloc __attribute__((__malloc))
#else
# define __must_check
# define __malloc
#endif
#define __init __attribute__((constructor))
#define __exit __attribute__((destructor))
/*
* count number of elements of an array,
* it should be a compile error if passing a pointer as an argument
*/
#define ARRAY_COUNT(x) ((sizeof (x) / sizeof (0[x])) \
/ ((size_t) !(sizeof (x) % sizeof(0[x]))))
/* some useful string macros */
#define STRING_EQUAL(s1, s2) (strcmp(s1, s2) == 0)
#define STRING_CONTAIN(s, ss) (strstr(s, ss) != NULL)
#define STRING_EMPTY(s1) (strlen(s1) == 0)
#define RANDOM_BYTE ((uint8_t)(rand() % 256))
#ifdef CC_VERBOSE
#define CC_INFO(format, ...) \
fprintf(stdout, "INFO: %s:%i:%s(): " format, \
__FILE__, __LINE__, __func__, ##__VA_ARGS__);
#define CC_WARNING(format, ...) \
fprintf(stdout, "WARNING: %s:%i:%s(): " format, \
__FILE__, __LINE__, __func__, ##__VA_ARGS__);
#define CC_ERROR(format, ...) \
fprintf(stderr, "ERROR: %s:%i:%s(): " format, \
__FILE__, __LINE__, __func__, ##__VA_ARGS__);
#else
#define CC_INFO(format, ...)
#define CC_WARNING(format, ...)
#define CC_ERROR(format, ...)
#endif
#define RETURN_WITH_ERRMSG_IF(exp, ret, format, ...) \
do { \
if (exp) { \
CC_ERROR(format, ##__VA_ARGS__); \
return (ret); \
} \
} while (0)
#define ASSERT_WITH_ERRMSG_IF(exp, ret, format, ...) \
do { \
if (exp) { \
CC_ERROR(format, ##__VA_ARGS__); \
exit (ret); \
} \
} while (0)
#endif