diff options
Diffstat (limited to 'include/libn.h')
-rw-r--r-- | include/libn.h | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/include/libn.h b/include/libn.h new file mode 100644 index 0000000..aad359e --- /dev/null +++ b/include/libn.h @@ -0,0 +1,74 @@ +#pragma once + +// ------------------------------------------------------------------------ +// Standard library + +#include <assert.h> +#include <stdlib.h> +#include <string.h> +#include <math.h> +#include <ctype.h> +#include <stdio.h> + +#include <unistd.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <fcntl.h> + +// ---------------------------------------------------------------------------- +// Dynamic array. + +typedef struct bufHdr +{ + vlong len; + vlong cap; + byte buf[]; +} bufHdr; + +#define _bufHdr(s) ((bufHdr*)((uint8*)(s)-offsetof(bufHdr, buf))) +#define buflen(s) ((s) ? (_bufHdr(s)->len) : 0) +#define bufcap(s) ((s) ? (_bufHdr(s)->cap) : 0) +#define bufend(s) ((s) + buflen(s)) +#define bufsize(s) ((s) ? (buflen(s) * sizeof((s)[0])) : 0) + +#define buffree(s) ((s) ? (free(_bufHdr(s)), (s) = nil) : 0) +#define buffit(s, n) ((n) <= bufcap(s) ? 0 : ((s) = bufgrow((s), (n), sizeof(*(s))))) + +#define bufresize(s, n) \ + do { \ + (buffit(s, n)); \ + ((_bufHdr(s)->len) = (n)); \ + } while (0) + +#define bufpush(s, ...) (buffit((s), 1 + buflen(s)), (s)[_bufHdr(s)->len++] = (__VA_ARGS__)) + +#define bufpop(s, i) (_bufpop((s), (i), sizeof(*(s))), (s)[_bufHdr(s)->len]) + +void* bufgrow(void*, vlong, vlong); +void _bufpop(void*, int, vlong); + +// ----------------------------------------------------------------------------- +// Co-routines + +typedef struct coro coro; + +coro* coro·new(uintptr stk, uintptr (*func)(coro*, uintptr)); +uintptr coro·yield(coro *c, uintptr arg); +error coro·free(coro *c); + +// ----------------------------------------------------------------------------- +// Strings + +#include ".include/str.h" + +// ----------------------------------------------------------------------------- +// Maps or dictionaries + +#include ".include/map.h" + +// ----------------------------------------------------------------------------- +// Error handling functions. + +void errorf(const byte* fmt, ...); + +#define panicf(...) (errorf(__VA_ARGS__), assert(0)) |