aboutsummaryrefslogtreecommitdiff
path: root/sys/base/memory.c
blob: a1a47c50010b727f950f995e281708e185cbc437 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <u.h>
#include <base.h>

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

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

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

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

mem·Allocator sys·Memory = {
    .alloc   = ·calloc,
    .free    = ·free
};


// -------------------------------------------------------------------------
// Dynamic buffer.

/* Grow to particular size */
void*
·bufgrow(void* buf, vlong newLen, vlong eltsize)
{
    assert(bufcap(buf) <= (SIZE_MAX - 1) / 2);

    vlong newCap = MAX(16, MAX(1 + 2 * bufcap(buf), newLen));

    assert(newLen <= newCap);
    assert(newCap <= (SIZE_MAX - offsetof(BufHdr, buf)) / eltsize);

    vlong newSize = offsetof(BufHdr, buf) + newCap * eltsize;

    BufHdr* newHdr;
    if (buf) {
        newHdr = bufhdr(buf);
        newHdr = (BufHdr*)realloc((void*)newHdr, newSize);
    } else {
        newHdr      = (BufHdr*)malloc(newSize);
        newHdr->len = 0;
    }

    newHdr->cap = newCap;
    return (void*)newHdr->buf;
}

/* Pop out a value */
void
·bufdel(void *buf, int i, vlong eltsize)
{
    int n;
    byte *b;
    byte stk[1024];
    assert(eltsize < sizeof(stk));

    b = (byte*)buf;
    if(n = buflen(buf), i < n) {
        memcpy(stk, b+eltsize*i, eltsize);
        memcpy(b+eltsize*i, b+eltsize*(i+1), eltsize*(n-i-1));
        memcpy(b+eltsize*(n-1), stk, eltsize);
    } 
    bufhdr(buf)->len--;
}

// -------------------------------------------------------------------------
// Arena allocator

#define ARENA_ALIGN 8
#define ARENA_BLOCK_SIZE 1024 * 1024

#define ALIGN_DOWN(n, a) ((n) & ~((a)-1))
#define ALIGN_UP(n, a) ALIGN_DOWN((n) + (a)-1, (a))
#define ALIGN_DOWN_PTR(p, a) ((void*)ALIGN_DOWN((uintptr)(p), (a)))
#define ALIGN_UP_PTR(p, a) ((void*)ALIGN_UP((uintptr)(p), (a)))

struct Block
{
    struct Block *next;
    byte   buf[];
};

struct mem·Arena
{
    void  *heap;
    mem·Allocator mem;

    byte  *off;
    byte  *end;
    struct Block *curr;
    struct Block first;
}; 

mem·Arena*
mem·makearena(mem·Allocator from, void *impl)
{
    mem·Arena *a  = from.alloc(impl, 1, sizeof(*a) + ARENA_BLOCK_SIZE);
    a->mem        = from;
    a->heap       = impl;
    a->off        = a->first.buf;
    a->end        = a->first.buf + ARENA_BLOCK_SIZE;
    a->curr       = &a->first;
    a->first.next = nil;

    return a;
}

static
void
grow(mem·Arena *a, vlong min)
{
    uintptr size;
    struct Block *blk;

    size   = ALIGN_UP(MAX(min, ARENA_BLOCK_SIZE), ARENA_ALIGN);
    blk    = a->mem.alloc(a->heap, 1, sizeof(*blk) + size);
    a->off = blk->buf;
    a->end = a->off + size;

    assert(a->curr->next == nil);
    assert(a->off == ALIGN_DOWN_PTR(a->off, ARENA_ALIGN));

    a->curr->next = blk;
    a->curr       = blk;
}

void*
mem·arenaalloc(mem·Arena *a, uint n, ulong size)
{
    if(!n) {
        return nil;
    }

    void *ptr;
    // TODO(nnoll): check for overflow
    size = n * size;

    if (size > (ulong)(a->end - a->off)) {
        grow(a, size);
        assert(size <= (uintptr)(a->end - a->off));
    }

    ptr    = a->off;
    a->off = ALIGN_UP_PTR(a->off + size, ARENA_ALIGN);

    assert(a->off <= a->end);
    assert(ptr == ALIGN_DOWN_PTR(ptr, ARENA_ALIGN));

    return ptr;
}

void
mem·freearena(mem·Arena *a)
{
    struct Block *it, *next;

    it = a->first.next;
    while (it != nil) {
        next = it->next;
        a->mem.free(a->heap, it);
        it   = next;
    }

    a->mem.free(a->heap, a);
}

static
void*
·arenaalloc(void *heap, uint n, ulong size)
{
    return mem·arenaalloc(heap, n, size);
}

static
void
·arenafree(void *heap, void *ptr)
{
    /* no-op */
}

mem·Allocator mem·ArenaAllocator = {
    .alloc = ·arenaalloc,
    .free  = ·arenafree,
};

// -------------------------------------------------------------------------
// Generalized memory helpers

void
memset64(void *dst, uint64 val, uintptr size)
{
    intptr i;

    for (i = 0; i < (size & (~7)); i += 8) {
        memcpy((byte*)dst + i, &val, 8);
    }

    for (; i < size; i++) {
        ((byte*)dst)[i] = ((byte*)&val)[i&7];
    }
}