aboutsummaryrefslogtreecommitdiff
path: root/include/u.h
blob: c03d5450f66df5df8b79618e33d15fa0c808ac61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#pragma once

// ------------------------------------------------------------------------
// Standard library

#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <stdarg.h>
#include <float.h>
#include <limits.h>

#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>

// ------------------------------------------------------------------------
// 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;

typedef void* Iface;

#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);

// -----------------------------------------------------------------------------
// 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 "str.h"

// -----------------------------------------------------------------------------
// Maps or dictionaries

#include "map.h"

// ------------------------------------------------------------------
// Global macros

#ifdef RELEASE
#define Assert(x)
#else
#define Assert(x) 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))