diff options
Diffstat (limited to 'src/stdlib/util.h')
| -rw-r--r-- | src/stdlib/util.h | 44 |
1 files changed, 31 insertions, 13 deletions
diff --git a/src/stdlib/util.h b/src/stdlib/util.h index 3b00e6e9..819cecd9 100644 --- a/src/stdlib/util.h +++ b/src/stdlib/util.h @@ -3,38 +3,56 @@ // Built-in utility functions #include <assert.h> +#include <err.h> #include <gc.h> #include <stdbool.h> #include <string.h> -#include <err.h> #define streq(a, b) (((a) == NULL && (b) == NULL) || (((a) == NULL) == ((b) == NULL) && strcmp(a, b) == 0)) #define starts_with(line, prefix) (strncmp(line, prefix, strlen(prefix)) == 0) -#define ends_with(line, suffix) (strlen(line) >= strlen(suffix) && strcmp(line + strlen(line) - strlen(suffix), suffix) == 0) -#define new(t, ...) ((t*)memcpy(GC_MALLOC(sizeof(t)), &(t){__VA_ARGS__}, sizeof(t))) -#define heap(x) (__typeof(x)*)memcpy(GC_MALLOC(sizeof(x)), (__typeof(x)[1]){x}, sizeof(x)) -#define stack(x) (__typeof(x)*)((__typeof(x)[1]){x}) -#define check_initialized(var, init_var, name) *({ if (!init_var) fail("The variable " name " is being accessed before it has been initialized!"); \ - &var; }) +#define ends_with(line, suffix) \ + (strlen(line) >= strlen(suffix) && strcmp(line + strlen(line) - strlen(suffix), suffix) == 0) +#define new(t, ...) ((t *)memcpy(GC_MALLOC(sizeof(t)), &(t){__VA_ARGS__}, sizeof(t))) +#define heap(x) (__typeof(x) *)memcpy(GC_MALLOC(sizeof(x)), (__typeof(x)[1]){x}, sizeof(x)) +#define stack(x) (__typeof(x) *)((__typeof(x)[1]){x}) +#define check_initialized(var, init_var, name) \ + *({ \ + if (!init_var) fail("The variable " name " is being accessed before it has been initialized!"); \ + &var; \ + }) -#define IF_DECLARE(decl, expr, block) if (({ decl; expr ? ({ block; 1; }) : 0; })) {} +#define IF_DECLARE(decl, expr, block) \ + if (({ \ + decl; \ + expr ? ({ \ + block; \ + 1; \ + }) \ + : 0; \ + })) { \ + } -#define WHEN(type, subj, var, body) { type var = subj; switch (var.$tag) body } +#define WHEN(type, subj, var, body) \ + { \ + type var = subj; \ + switch (var.$tag) \ + body \ + } #ifndef public -#define public __attribute__ ((visibility ("default"))) +#define public __attribute__((visibility("default"))) #endif #ifndef PUREFUNC -#define PUREFUNC __attribute__ ((pure)) +#define PUREFUNC __attribute__((pure)) #endif #ifndef CONSTFUNC -#define CONSTFUNC __attribute__ ((const)) +#define CONSTFUNC __attribute__((const)) #endif #ifndef INLINE -#define INLINE inline __attribute__ ((always_inline)) +#define INLINE inline __attribute__((always_inline)) #endif #ifndef likely |
