summaryrefslogtreecommitdiff
path: root/core/alloc.c
blob: bec36bc4f81c42aca3b656b82bb2c992e6d83a4b (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
#include <rune/core/alloc.h>
#include <rune/core/logging.h>
#include <stdlib.h>
#include <string.h>

// TODO: implement block coalescing so we can reuse freed blocks

#define DEADBLOCK       ((void*)0xDEADBEEF)

static struct mem_block first_block;

static struct mem_block* _find_free_block(size_t sz) {
        struct list_head *temp = &first_block.list;
        struct mem_block *block;
        while (temp != NULL) {
                block = (struct mem_block*)((void*)temp - offsetof(struct mem_block, list));
                if (block->sz == sz && block->free == 1)
                        return block;
                temp = temp->next;
        }
        return NULL;
}

static struct mem_block* _find_block(void *ptr) {
        struct list_head *temp = &first_block.list;
        struct mem_block *block;
        while (temp != NULL) {
                block = (struct mem_block*)((void*)temp - offsetof(struct mem_block, list));
                if (block->ptr == ptr)
                        return block;
                temp = temp->next;
        }
        return NULL;
}

static struct mem_block* _alloc_block(size_t sz) {
        if (first_block.ptr == NULL) {
                first_block.ptr == DEADBLOCK;
                first_block.sz = 0;
        }

        struct mem_block *ret = _find_free_block(sz);
        if (ret != NULL) {
                ret->free = 0;
                return ret->ptr;
        }

        ret = malloc(sizeof(struct mem_block));
        if (ret == NULL) {
                log_output(LOG_ERROR, "Cannot allocate block of size %d", sz);
                return NULL;
        }

        ret->ptr = malloc(sz);
        ret->sz = sz;
        ret->free = 0;
        list_add(&ret->list, &first_block.list);
        log_output(LOG_DEBUG, "Alloc'd block of size %d", sz);
        return ret;
}

static void _free_block(struct mem_block *block, int hard) {
        if (hard == 1) {
                list_del(&block->list);
                log_output(LOG_DEBUG, "Freed block of size %d", block->sz);
                free(block);
                return;
        }
        block->free = 1;
}

void* rune_alloc(size_t sz) {
        if (sz == 0)
                return NULL;

        struct mem_block *block = _find_free_block(sz);
        if (block != NULL) {
                block->free = 0;
                return block->ptr;
        }

        block = _alloc_block(sz);
        return block->ptr;
}

void* rune_calloc(size_t nmemb, size_t sz) {
        if (sz == 0)
                return NULL;

        struct mem_block *block = _find_free_block(sz);
        if (block != NULL) {
                block->free = 0;
                return block->ptr;
        }

        block = _alloc_block(sz);
        memset(block->ptr, 0, sz);
        return block->ptr;
}

void* rune_realloc(void *ptr, size_t sz) {
        if (ptr == NULL || sz == 0)
                return rune_alloc(sz);

        struct mem_block *old = _find_block(ptr);
        struct mem_block *new = _alloc_block(sz);
        memcpy(new->ptr, old->ptr, old->sz);
        old->free = 1;
        return new->ptr;
}

void rune_free(void *ptr) {
        if (ptr == NULL)
                return;

        struct mem_block *block = _find_block(ptr);
        if (block->free == 1)
                return;
        block->free = 1;
}

void rune_free_all(void) {
        struct list_head *temp = &first_block.list;
        struct mem_block *block;
        while (temp != NULL) {
                block = (struct mem_block*)((void*)temp - offsetof(struct mem_block, list));
                if (block->ptr == DEADBLOCK) {
                        temp = temp->next;
                        continue;
                }

                temp = temp->next;
                if (block->ptr != NULL)
                        _free_block(block, 1);
        }
}