aboutsummaryrefslogtreecommitdiff
path: root/include/libn.h
blob: 654771311bbbbac72375aa18dfbe2358b28ec4fa (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#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 <fcntl.h>
#include <sys/stat.h>
#include <sys/types.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);

// -----------------------------------------------------------------------------
// interfaces
// TODO(nnoll): Think about this idea
/*
typedef struct Iface {
    void* impl;
    byte  fcn[];
} Iface;
*/
// -----------------------------------------------------------------------------
// memory allocation

/* allocator interface */
typedef struct mem·Allocator {
    void *(*alloc)(void *iface, uint n, ulong size);
    void  (*free)(void *iface, void *ptr);
} mem·Allocator;

/* system implementation */
static
void ·free(void* _, void* ptr) {
    return free(ptr);
}

static
void *·alloc(void* _, uint n, ulong size) {
    return malloc(n*size);
}

// TODO(nnoll): Allow for nil iterfaces?
static
mem·Allocator mem·sys = {
    .alloc   = ·alloc, 
    .free    = ·free
};

typedef struct mem·Arena mem·Arena;

mem·Arena *mem·newarena(mem·Allocator from, void*);
void      *mem·arenaalloc(mem·Arena *A, uint n, ulong size); 
void       mem·freearena(mem·Arena *A);

// -----------------------------------------------------------------------------
// coroutines

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

// TODO(nnoll): Move here?
#include ".include/str.h"

// -----------------------------------------------------------------------------
// maps

#include ".include/map.h"

// -----------------------------------------------------------------------------
// I/O

typedef FILE Stream;
enum SeekPos
{
    seek·CUR = SEEK_CUR,
    seek·SET = SEEK_SET,
    seek·END = SEEK_END
};

Stream *io·open(byte *name, byte *mode);
error   io·close(Stream *s);
byte    io·getbyte(Stream *s);
error   io·ungetbyte(Stream *s, byte c);
int     io·read(Stream *s, int sz, int n, void *buf);
int     io·readln(Stream *s, int n, byte* buf);
error   io·putbyte(Stream *s, byte c);
int     io·putstring(Stream *s, string str);
int     io·write(Stream *s, int sz, int n, void *buf);
int     io·flush(Stream *s);
int     io·seek(Stream *s, long off, enum SeekPos origin);

/* generic I/O interfaces */

typedef struct io·Reader
{
    int (*read)(void*, int sz, int n, void *buf);
} io·Reader;

typedef struct io·Peeker
{
    byte  (*get)(void*);
    error (*unget)(void*, byte);
} io·Peeker;

typedef struct io·FullReader
{
    io·Reader;
    io·Peeker;
} io·FullReader;

typedef struct io·Writer
{
    int (*write)(void*, int sz, int n, void *buf);
} io·Writer;

typedef struct io·Putter
{
    error (*put)(void*, byte);
    int   (*putstr)(void*, string);
} io·Putter;

typedef struct io·FullWriter
{
    io·Writer;
    io·Putter;
} io·FullWriter;

typedef struct io·ReadWriter
{
    io·Reader;
    io·Writer;
} io·ReadWriter;

// -----------------------------------------------------------------------------
// libflate

typedef struct gz·Reader gz·Reader;
typedef struct gz·Writer gz·Writer;

gz·Reader *gz·newreader(io·Reader rdr, void* r, mem·Allocator mem, void* m);
int        gz·read(gz·Reader *rdr, int sz, int n, void *buf);
error      gz·freereader(gz·Reader *rdr);

gz·Writer *gz·newwriter(io·Writer wtr, void* w, mem·Allocator mem, void* m);
error      gz·freewriter(gz·Writer *wtr);
int        gz·write(gz·Writer *wtr, int sz, int n, void *buf);

// -----------------------------------------------------------------------------
// Error handling functions.

void errorf(const byte* fmt, ...);

#define panicf(...)  (errorf(__VA_ARGS__), assert(0))