summaryrefslogtreecommitdiff
path: root/src/util.h
blob: 88aee89337328be4e98427a4dae2535200bfc048 (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
#ifndef UTIL_H
#define UTIL_H
#include <stdlib.h>

/* This is commonly used, so put here for easy access. */
#define MYALLOC(TYPE, LEN) (TYPE*)malloc(sizeof(TYPE) * (LEN))


typedef long DATA;
typedef long NUM;
typedef unsigned long long UNUM; /* definitely bigger than size_t */

typedef unsigned char BOOL;


#define HC_ATTR __attribute__((__hot__, __const__))
#define H_ATTR __attribute__((__hot__))
#define P_ATTR __attribute__((__pure__))
#define C_ATTR __attribute__((__const__))
#define UNUSED __attribute__((__unused__))
#define U_ATTR UNUSED

#define D_ATTR(X) __attribute__((__unused__, __deprecated__("This is deprecated.\n" \
                                                            "Please use " X \
                                                            " instead")))

#define UD_ATTR __attribute__((__unused__, __deprecated__))
#define UC_ATTR __attribute__((__unused__, __const__))
#define UH_ATTR __attribute__((__unused__, __hot__))
#define UHP_ATTR __attribute__((__unused__, __hot__, __pure__))
#define UHC_ATTR __attribute__((__unused__, __hot__, __const__))
#define HP_ATTR __attribute__((__hot__, __pure__))
/* For convenient declarations */
#define CC_MOD(X) const X const
#define CCR_MOD(X) const X const restrict

#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define fleprintf0(M) eprintf("%s:%d, " M, __FILE__, __LINE__)
#define fleprintf(M, ...) eprintf("%s:%d, " M, __FILE__, __LINE__, __VA_ARGS__)

#define SAFE_MALLOC(TYPE, VAR, LEN, CLEAN) do { \
    VAR = malloc(sizeof (TYPE) * (LEN));        \
    if (VAR == NULL) {                          \
      fleprintf0("Fail to malloc\n");           \
      { CLEAN };                                \
    }                                           \
  } while (0)

#define SAFE_CALLOC(TYPE, VAR, LEN, CLEAN) do { \
    VAR = calloc((LEN), sizeof (TYPE));         \
    if (VAR == NULL) {                          \
      fleprintf0("Fail to calloc\n");           \
      { CLEAN };                                \
    }                                           \
  } while (0)

#define SAFE_REALLOC(TYPE, VAR, LEN, CLEAN) do {        \
    TYPE *macro_new_var =                               \
      realloc(VAR, sizeof (TYPE) * (LEN));              \
    if (macro_new_var == NULL) {                        \
      fleprintf0("Fail to realloc\n");                  \
      { CLEAN };                                        \
    } else {                                            \
      VAR = macro_new_var;                              \
    }                                                   \
  } while (0)

/* very convenient macro */
#define PASTER2(X, Y, Z) X ## Y ## Z
#define PASTER(X, Y, Z) PASTER2(X, Y, Z)

BOOL read_entire_file(const char *file_name, char **str, NUM *len);

/* The purpose of this macro is to be used at the end of a macro
   defining a function, so that we can add a semicolon when calling
   that macro.  I was using the keyword _Static_assert, but that
   requires C11, which sounds kind of absurd to me. */
#define MY_Static_MES(MES) struct STATIC_STRUCT_s

#endif