2024-02-04 12:23:59 -08:00
|
|
|
#pragma once
|
|
|
|
|
2024-03-18 09:57:49 -07:00
|
|
|
// Built-in utility functions
|
|
|
|
|
2024-02-04 12:23:59 -08:00
|
|
|
#include <assert.h>
|
|
|
|
#include <gc.h>
|
2024-04-20 11:58:32 -07:00
|
|
|
#include <stdbool.h>
|
2024-02-04 12:23:59 -08:00
|
|
|
#include <string.h>
|
|
|
|
#include <err.h>
|
|
|
|
|
|
|
|
#define streq(a, b) (((a) == NULL && (b) == NULL) || (((a) == NULL) == ((b) == NULL) && strcmp(a, b) == 0))
|
2024-08-22 11:02:48 -07:00
|
|
|
#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)
|
2024-02-04 12:23:59 -08:00
|
|
|
#define new(t, ...) ((t*)memcpy(GC_MALLOC(sizeof(t)), &(t){__VA_ARGS__}, sizeof(t)))
|
2024-09-13 17:12:44 -07:00
|
|
|
#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})
|
2024-08-17 11:41:31 -07:00
|
|
|
#define check_initialized(var, name) *({ if (!var ## $initialized) fail("The variable " name " is being accessed before it has been initialized!"); \
|
|
|
|
&var; })
|
2024-02-04 12:23:59 -08:00
|
|
|
|
2024-11-11 21:04:56 -08:00
|
|
|
#define IF_DECLARE(decl, expr, block) if (({ decl; expr ? ({ block; 1; }) : 0; })) {}
|
|
|
|
|
2024-02-04 12:23:59 -08:00
|
|
|
#ifndef auto
|
|
|
|
#define auto __auto_type
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef public
|
|
|
|
#define public __attribute__ ((visibility ("default")))
|
|
|
|
#endif
|
|
|
|
|
2024-09-08 14:17:15 -07:00
|
|
|
#ifndef PUREFUNC
|
|
|
|
#define PUREFUNC __attribute__ ((pure))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONSTFUNC
|
|
|
|
#define CONSTFUNC __attribute__ ((const))
|
|
|
|
#endif
|
|
|
|
|
2024-10-29 20:14:31 -07:00
|
|
|
#ifndef INLINE
|
|
|
|
#define INLINE inline __attribute__ ((always_inline))
|
|
|
|
#endif
|
|
|
|
|
2024-12-07 12:59:37 -08:00
|
|
|
#ifndef likely
|
|
|
|
#define likely(x) (__builtin_expect(!!(x), 1))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef unlikely
|
|
|
|
#define unlikely(x) (__builtin_expect(!!(x), 0))
|
|
|
|
#endif
|
|
|
|
|
2024-10-30 10:59:15 -07:00
|
|
|
// GCC lets you define macro-like functions which are always inlined and never
|
|
|
|
// compiled using this combination of flags. See: https://gcc.gnu.org/onlinedocs/gcc/Inline.html
|
|
|
|
#ifndef MACROLIKE
|
|
|
|
#define MACROLIKE extern inline __attribute__((gnu_inline, always_inline))
|
|
|
|
#endif
|
|
|
|
|
2024-09-13 10:49:45 -07:00
|
|
|
__attribute__((format(printf, 1, 2)))
|
|
|
|
char *heap_strf(const char *fmt, ...);
|
2024-02-07 21:52:18 -08:00
|
|
|
|
2024-02-04 12:23:59 -08:00
|
|
|
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0
|