summaryrefslogtreecommitdiff
path: root/bootstrap/arena.c
blob: 0c735caaa1ae7eca10bd557140b9d7dcdd82bee6 (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

/*

  arena.c -- Arena allocator for LRL bootstrap transpiler

  Copyright © 2020 Samuel Lidén Borell <samuel@kodafritt.se>

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.

*/

#include "bootstrap.h"
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>

#define ARENA_SIZE 4096

struct ArenaBlock {
    unsigned char *data, *free, *end;
};

struct ArenaHolder {
    struct ArenaHolder *prev;
    unsigned capacity;
    struct ArenaBlock *blocks;
    struct ArenaBlock *last;
    struct ArenaBlock *last_capacity;
};

static void grow_arena_list(void);
static void *large_alloc(size_t size);

static struct ArenaHolder *holder;

/*
  As an optimization, we could keep buckets of arenas
  with <=8, <=16, <=32, ... <=4096 bytes free

  We could also track arena alignment, so we can allocate small-alignment
  structures in lesser aligned arenas.

  We should also add an environment variable for adding "bad" padding
  between allocations for Valgrind etc.
*/

void arena_new(void)
{
    struct ArenaHolder *hld = malloc(sizeof(struct ArenaHolder));
    if (!hld) out_of_memory();
    hld->capacity = 16;
    hld->blocks = malloc(hld->capacity * sizeof(struct ArenaBlock));
    if (!hld->blocks) out_of_memory();
    hld->last = hld->blocks; /* first block */
    hld->last_capacity = hld->blocks + hld->capacity*sizeof(struct ArenaBlock);
    hld->prev = holder;
    holder = hld;
}

void arena_free(void)
{
    struct ArenaHolder *hld = holder;
    struct ArenaBlock *block;
    assert(hld != NULL);
    holder = hld->prev;
    for (block = hld->blocks; block < hld->last; block++) {
        free(block->data);
    }
    free(hld->blocks);
    free(hld);
}

/* align must be a power of 2 */
#define ALIGN(ptr, align) ((void*)((((uintptr_t)ptr)+((align)-1)) & ~((uintptr_t)(align)-1)))

void *aalloc(size_t size, size_t align)
{
    struct ArenaHolder *hld = holder;
    struct ArenaBlock *block, *lastarena;
    void *p;
    if (!hld) {
        p = malloc(size);
        if (!p) out_of_memory();
        return p;
    }

    /* For large allocations, allocate its own arena */
    if (size > (ARENA_SIZE/4)*3) return large_alloc(size);

    /* Find an arena. This is O(n), so we may need to use bucket lists (see top
       of file) to improve performance if it turns out to be a problem. */
    for (block = hld->blocks; block < hld->last; block++) {
        unsigned char *aligned;
        /*if (block->end - block->free < size) continue;*/
        aligned = ALIGN(block->free, align);
        if (aligned+size > block->end) continue;
        /* Found space */
        block->free = aligned+size;
        return aligned;
    }

    /* No arena exists. Grab a blank one and allocate */
    if (hld->last == hld->last_capacity) {
        grow_arena_list();
        hld = holder;
    }
    p = malloc(ARENA_SIZE);
    if (!p) out_of_memory();
    lastarena = hld->last;
    lastarena->data = p;
    lastarena->end = lastarena->data + ARENA_SIZE;
    lastarena->free = lastarena->data + size;
    return lastarena->data;
}

static void *large_alloc(size_t size)
{
    struct ArenaBlock *lastarena;
    void *p;
    if (holder->last == holder->last_capacity) {
        grow_arena_list();
    }
    p = malloc(size);
    if (!p) out_of_memory();
    lastarena = holder->last;
    lastarena->data = p;
    lastarena->free = lastarena->end = lastarena->data + size;
    return lastarena->data;
}

/** Grows the list of arena blocks. */
static void grow_arena_list(void)
{
    size_t size = (size_t)(holder->last - holder->blocks);
    holder->capacity *= 2;
    holder->blocks = realloc(holder->blocks, holder->capacity*sizeof(struct ArenaBlock));
    if (!holder->blocks) out_of_memory();
    holder->last = holder->blocks + size;
    holder->last_capacity = holder->blocks + holder->capacity*sizeof(struct ArenaBlock);
}