summaryrefslogtreecommitdiff
path: root/src/common.h
blob: eaa07431a253eff13dbc209c2f9c7e928c0e1162 (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
#ifndef LOG_H
#define LOG_H

#include <stdio.h>
#include <stdlib.h>

#define ARR_SIZE(a) (sizeof(a)/sizeof(*(a)))

#define __RED__    "\033[0;31m"
#define __GREEN__  "\033[0;32m"
#define __YELLOW__ "\033[0;33m"
#define __RESET__  "\033[0m"

#define STR(x) #x
#define XSTR(x) STR(x)

#define info(...) do { fprintf(stdout, __GREEN__"[INFO]"__RESET__"  "__VA_ARGS__); fprintf(stdout, "\n"); } while(0)
#define err(...)  do { fprintf(stderr, __RED__"[ERROR]"__RESET__" "__FILE__":"XSTR(__LINE__)": "__VA_ARGS__); fprintf(stderr, "\n"); } while(0)
#define warn(...) do { fprintf(stderr, __YELLOW__"[WARN]"__RESET__"  "__FILE__":"XSTR(__LINE__)": "__VA_ARGS__); fprintf(stderr, "\n"); } while(0)

#define die(...) do {     \
        err(__VA_ARGS__); \
        abort();          \
    } while(0)

void *xmalloc(size_t size);
void *xcalloc(size_t nmemb, size_t size);
void *xrealloc(void *ptr, size_t size);

#define F_LOAD_FILE_ALIGNED(T)                              \
    static int load_file_##T##_aligned(                     \
        char *path, size_t *size, u32 *buf)                 \
    {                                                       \
        int ret = 1;                                        \
                                                            \
        FILE *fp = fopen(path, "rb");                       \
        if(!fp) {                                           \
            err("fopen: %s: %s", path, strerror(errno));    \
            return 1;                                       \
        }                                                   \
                                                            \
        fseek(fp, 0, SEEK_END);                             \
        *size = (size_t)ftell(fp);                          \
        *size += (*size % sizeof(u32));                     \
                                                            \
        if(buf == NULL) {                                   \
            ret = 0;                                        \
            goto exit;                                      \
        }                                                   \
                                                            \
        fseek(fp, 0, SEEK_SET);                             \
        fread(buf, sizeof(u32), *size/sizeof(u32), fp);     \
                                                            \
        ret =  0;                                           \
    exit:                                                   \
        fclose(fp);                                         \
        return ret;                                         \
    }

#endif