summaryrefslogtreecommitdiff
path: root/vendors/_.h
blob: ef1092767ad4adaa52e639805845eb3fd99a4ffa (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
#pragma once
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "TZR.h"

void wdeinit(void);
[[nodiscard]] void *alloc(size_t size);
void _free(void *ptr);
void *_realloc(void *ptr, size_t size);

#define free _free
#define malloc alloc
#define calloc(X, Y) alloc((X) * (Y))
#define realloc _realloc

// math
#define clamp(X, MIN, MAX) ({ \
	const auto ___x = (X); \
	const auto ___min = (MIN); \
	const auto ___max = (MAX); \
	(___x > ___max) ? ___max : ((___x < ___min) ? ___min : ___x); \
})
#define max(X, Y) ({ \
	const auto ___x = (X); \
	const auto ___y = (Y); \
	(___x > ___y) ? ___x : ___y; \
})
#define min(X, Y) ({ \
	const auto ___x = (X); \
	const auto ___y = (Y); \
	(___x < ___y) ? ___x : ___y; \
})

#define STR(X) #X
#define defer(X) if (atexit(X)) { X(); panic("defer"); }
#define auto __auto_type
#define foreach(E, L) for (__auto_type E = (L); E != NULL; E = E->next)
#define rfor(I, F, T) for (__auto_type I = F; I < T; I++)
#define repeat(I, T) for (__auto_type I = 0u; I < T; I++)
#define with(I, T) auto I = (T); if (I)

// logging
#define pgeneric(PREFIX, ...) fprintf(stderr, \
                                      "\x1b["PREFIX" %s:%s:%d\x1b[0m \t", \
                                      __FILE_NAME__, __FUNCTION__, __LINE__), \
                              fprintf(stderr, __VA_ARGS__), fputc('\n', stderr)
#define plog(...) pgeneric("94mLOG", __VA_ARGS__)
#define perr(...) pgeneric("91mERR", __VA_ARGS__)
#define pwrn(...) pgeneric("93mWRN", __VA_ARGS__)
#define panic(...) perr(__VA_ARGS__), exit(EXIT_FAILURE)
#define assert(X) if (!(X)) panic("assert failed: "STR(X))