From 65264ba6d1f7e7402009f33b60e3263bf1f02498 Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Fri, 17 Apr 2020 18:04:04 -0700 Subject: init: prototype of code skeleton --- include/u.h | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 include/u.h (limited to 'include/u.h') diff --git a/include/u.h b/include/u.h new file mode 100644 index 0000000..349a236 --- /dev/null +++ b/include/u.h @@ -0,0 +1,117 @@ +#pragma once + +// ------------------------------------------------------------------------ +// Standard library + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +// ------------------------------------------------------------------------ +// Modern type aliases + +typedef char byte; +typedef unsigned char ubyte; +typedef signed char sbyte; + +typedef long long vlong; +typedef unsigned long long uvlong; + +typedef unsigned int uint; +typedef uint8_t uint8; +typedef uint16_t uint16; +typedef uint32_t uint32; +typedef uint64_t uint64; + +typedef int8_t int8; +typedef int16_t int16; +typedef int32_t int32; +typedef int64_t int64; + +typedef float float32; +typedef double float64; + +typedef uintptr_t uintptr; +typedef intptr_t intptr; + +typedef int error; + +#define nil NULL + +// ---------------------------------------------------------------------------- +// 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); + +// ----------------------------------------------------------------------------- +// Strings + +#include "str.h" + +// ----------------------------------------------------------------------------- +// Maps or dictionaries + +#include "map.h" + +// ------------------------------------------------------------------ +// Global macros + +#ifndef RELEASE +#define Assert(x) assert(x) +#else +#define Assert(x) +#endif + +#define arrlen(Array) (sizeof(Array) / sizeof((Array)[0])) + +#define MAX(x, y) ((x) >= (y) ? (x) : (y)) +#define MIN(x, y) ((x) < (y) ? (x) : (y)) + +// ----------------------------------------------------------------------------- +// Error handling functions. + +void errorf(const byte* fmt, ...); + +#define panicf(...) (errorf(__VA_ARGS__), assert(0)) -- cgit v1.2.1