summaryrefslogtreecommitdiff
path: root/lua_benchmark/main.c
blob: ddcf2fe92a5dc8c2bdd9495e7c0b9713ed966ec0 (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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <assert.h>
#include <unistd.h>
#include <lua5.4/lua.h>
#include <lua5.4/lauxlib.h>
#include <lua5.4/lualib.h>

static void get_random(uint8_t *dst, size_t count) {
    int fd = open("/dev/urandom", O_RDONLY);
    assert(count == read(fd, dst, count));
    close(fd);
}

#if defined(ALLOC_MAGIC_BUDDY)

#include "magic_buddy/magic_buddy.h"
const size_t POOL_SIZE = 1024 * 1024 * 1024;
static struct buddy BUDDY;

static void init_allocator() {
    uint8_t magic[MAGIC_COOKIE_BYTES];
    get_random(magic, MAGIC_COOKIE_BYTES);

    uint8_t *pool = malloc(POOL_SIZE);
    init_buddy(pool, POOL_SIZE, magic, &BUDDY);
}

static void *allocator_fn(void *ud, void *ptr, size_t osize, size_t nsize) {
    // https://www.lua.org/manual/5.4/manual.html#4.6
    if (!ptr) return allocate(nsize, &BUDDY);

    if (nsize == 0) {
        liberate(ptr, osize, &BUDDY);
        return NULL;
    }

    return reallocate(ptr, nsize, osize, &BUDDY);
}

#elif defined(ALLOC_GROWING_MAGIC_BUDDY)

#include "magic_buddy/magic_buddy.h"
static size_t POOL_SIZE;
static void *POOL;
static void *END_POOL;
static struct buddy BUDDY;

static void init_allocator() {
    uint8_t magic[MAGIC_COOKIE_BYTES];
    get_random(magic, MAGIC_COOKIE_BYTES);

    POOL_SIZE = 1024;
    POOL = sbrk(POOL_SIZE);
    assert(POOL != (void *)-1);
    init_buddy(POOL, POOL_SIZE, magic, &BUDDY);
}

static int grow_pool() {
    if (sbrk(POOL_SIZE) != (void*)((uint8_t*)POOL + POOL_SIZE)) return 0;
    POOL_SIZE *= 2;
    grow_buddy(POOL, POOL_SIZE, &BUDDY);
    return 1;
}

static void *allocator_fn(void *ud, void *ptr, size_t osize, size_t nsize) {
    // https://www.lua.org/manual/5.4/manual.html#4.6
    void *result = 0;
    if (!ptr) {
        while (!(result = allocate(nsize, &BUDDY)))
            if (!grow_pool()) return 0;
        return result;
    }

    if (nsize == 0) {
        liberate(ptr, osize, &BUDDY);
        return NULL;
    }

    while (!(result = reallocate(ptr, nsize, osize, &BUDDY)))
        if (!grow_pool()) return 0;
    return result;
}

// custom malloc/free/calloc/realloc
void *malloc(size_t s) {
    if (!POOL) init_allocator();
    size_t *block = allocator_fn((void*)1, 0, 0, s + sizeof(size_t));
    *block = s + sizeof(size_t);
    return (void*)((uint8_t*)block + sizeof(size_t));
}
void free(void *ptr) {
    if (!ptr) return;
    if (!POOL) init_allocator();
    ptr = (uint8_t*)ptr - sizeof(size_t);
    size_t size = *(size_t*)ptr;
    allocator_fn((void*)1, ptr, size, 0);
}
void *realloc(void *ptr, size_t nsize) {
    if (!POOL) init_allocator();
    if (nsize == 0) {
        free(ptr);
        return 0;
    }
    ptr = (uint8_t*)ptr - sizeof(size_t);
    size_t osize = *(size_t*)ptr;
    void *res = allocator_fn((void*)1, ptr, osize, nsize + sizeof(size_t));
    *(size_t*)res = nsize + sizeof(size_t);
    return (void*)((uint8_t*)res + sizeof(size_t));
}
void *calloc(size_t cnt, size_t size) {
    if (!POOL) init_allocator();
    uint8_t *data = malloc(cnt * size);
    for (size_t i = 0; i < cnt * size; i++) data[i] = 0;
    return (void*)data;
}

#elif defined(ALLOC_SYSTEM)

static void init_allocator() { }

static void *allocator_fn(void *ud, void *ptr, size_t osize, size_t nsize) {
    // https://www.lua.org/manual/5.4/manual.html#4.6
    if (nsize == 0) {
        free(ptr);
        return NULL;
    }
    return realloc(ptr, nsize);
}

#else
#error "No allocator chosen!"
#endif

int main(int argc, char **argv) {
    init_allocator();

    // https://www.lua.org/pil/24.1.html
    char buff[256];
    int error;
    lua_State *L = lua_newstate(allocator_fn, 0);   /* opens Lua */
    luaL_openlibs(L);

    assert(argc == 2);
    luaL_dofile(L, argv[1]);
    lua_close(L);
    return 0;
}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback