code / tomo

Lines41.3K C23.7K Markdown9.7K YAML5.0K Tomo2.3K
7 others 763
Python231 Shell230 make212 INI47 Text21 SVG16 Lua6
(63 lines)
1 // Built-in utility functions
3 #pragma once
5 #include <assert.h>
6 #include <err.h>
7 #include <gc.h>
8 #include <stdbool.h>
9 #include <string.h>
11 #define streq(a, b) (((a) == NULL && (b) == NULL) || (((a) == NULL) == ((b) == NULL) && strcmp(a, b) == 0))
12 #define starts_with(line, prefix) (strncmp(line, prefix, strlen(prefix)) == 0)
13 #define ends_with(line, suffix) \
14 (strlen(line) >= strlen(suffix) && strcmp(line + strlen(line) - strlen(suffix), suffix) == 0)
15 #define new(t, ...) ((t *)memcpy(GC_MALLOC(sizeof(t)), &(t){__VA_ARGS__}, sizeof(t)))
16 #define heap(x) (__typeof(x) *)memcpy(GC_MALLOC(sizeof(x)), (__typeof(x)[1]){x}, sizeof(x))
17 #define stack(x) (__typeof(x) *)((__typeof(x)[1]){x})
18 #define check_initialized(var, init_var, name) \
19 *({ \
20 if (!init_var) fail("The variable " name " is being accessed before it has been initialized!"); \
21 &var; \
22 })
24 #define WHEN(type, subj, var, body) \
25 { \
26 type var = subj; \
27 switch (var.$tag) \
28 body \
31 #ifndef public
32 #define public __attribute__((visibility("default")))
33 #endif
35 #ifndef PUREFUNC
36 #define PUREFUNC __attribute__((pure))
37 #endif
39 #ifndef CONSTFUNC
40 #define CONSTFUNC __attribute__((const))
41 #endif
43 #ifndef INLINE
44 #define INLINE inline __attribute__((always_inline))
45 #endif
47 #ifndef likely
48 #define likely(x) (__builtin_expect(!!(x), 1))
49 #endif
51 #ifndef unlikely
52 #define unlikely(x) (__builtin_expect(!!(x), 0))
53 #endif
55 // GCC lets you define macro-like functions which are always inlined and never
56 // compiled using this combination of flags. See: https://gcc.gnu.org/onlinedocs/gcc/Inline.html
57 #ifndef MACROLIKE
58 #ifdef __TINYC__
59 #define MACROLIKE static inline __attribute__((gnu_inline, always_inline))
60 #else
61 #define MACROLIKE extern inline __attribute__((gnu_inline, always_inline))
62 #endif
63 #endif