aboutsummaryrefslogtreecommitdiff
path: root/src/std/scoped_allocator.c
blob: 3104fd310e5f24a0655c10db49211922754fd02a (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
#include "../../include/std/scoped_allocator.h"
#include "../../include/std/alloc.h"
#include "../../include/std/thread.h"
#include "../../include/std/log.h"
#include <assert.h>

#define ALLOC_NODE_SIZE 4096

int scoped_allocator_node_init(ScopedAllocatorNode *self) {
    self->data = NULL;
    self->size = 0;
    self->next = NULL;
    return am_malloc(ALLOC_NODE_SIZE, (void**)&self->data);
}

void scoped_allocator_node_deinit(ScopedAllocatorNode *self) {
    am_free(self->data);
    self->data = NULL;
    self->size = 0;
    if(self->next) {
        scoped_allocator_node_deinit(self->next);
        am_free(self->next);
        self->next = NULL;
    }
}

int scoped_allocator_init(ScopedAllocator *self) {
    return_if_error(scoped_allocator_node_init(&self->head));
    self->current = &self->head;
    return_if_error(buffer_init(&self->buffers, NULL));
    return buffer_init(&self->mutexes, NULL);
}

static void buffer_deinit(Buffer *self) {
    am_free(self->data);
    self->data = NULL;
    self->size = 0;
    self->capacity = 0;
}

static void scoped_allocator_deinit_buffers(ScopedAllocator *self) {
    Buffer **buffer;
    Buffer **buffers_end;
    buffer = buffer_start(&self->buffers);
    buffers_end = buffer_end(&self->buffers);
    while(buffer != buffers_end) {
        buffer_deinit(*buffer);
        ++buffer;
    }
    buffer_deinit(&self->buffers);
}

static void scoped_allocator_deinit_mutexes(ScopedAllocator *self) {
    amal_mutex **mutex;
    amal_mutex **mutexes_end;
    mutex = buffer_start(&self->mutexes);
    mutexes_end = buffer_end(&self->mutexes);
    while(mutex != mutexes_end) {
        amal_mutex_deinit(*mutex);
        ++mutex;
    }
    buffer_deinit(&self->mutexes);
}

void scoped_allocator_deinit(ScopedAllocator *self) {
    self->current = NULL;
    scoped_allocator_deinit_buffers(self);
    scoped_allocator_deinit_mutexes(self);
    scoped_allocator_node_deinit(&self->head);
}

static CHECK_RESULT int scoped_allocator_ensure_capacity_for(ScopedAllocator *self, usize size) {
    void *new_node;
    new_node = NULL;
    
    if(self->current->size + size > ALLOC_NODE_SIZE) {
        return_if_error(am_malloc(sizeof(ScopedAllocatorNode), &new_node));
        cleanup_if_error(scoped_allocator_node_init(new_node));
        self->current->next = new_node;
        self->current = new_node;
    }
    return ALLOC_OK;

    cleanup:
    if(new_node)
        am_free(new_node);
    return ALLOC_FAIL;
}

static void* align_ptr_ceil(void *ptr, uintptr_t alignment) {
    uintptr_t ptrval;
    ptrval = (uintptr_t)ptr;
    return (void*)((ptrval + alignment + 1) & ~(alignment - 1));
}

static usize align_ptr_ceil_offset(void *ptr, uintptr_t alignment) {
    return (uintptr_t)align_ptr_ceil(ptr, alignment) - (uintptr_t)ptr;
}

#define SCOPED_ALLOC_ALIGNMENT 8

int scoped_allocator_alloc(ScopedAllocator *self, usize size, void **mem) {
    ScopedAllocatorNode *current;
    usize alloc_size;
    assert(self->current);
    current = self->current;

    if(size >= ALLOC_NODE_SIZE) {
        amal_log_error("scoped_allocator_alloc: tried to alloc memory of size %lu. Max allowed alloc size is %lu", size, ALLOC_NODE_SIZE);
        return -1;
    }

    alloc_size = size + align_ptr_ceil_offset(self->current->data + self->current->size, SCOPED_ALLOC_ALIGNMENT);
    return_if_error(scoped_allocator_ensure_capacity_for(self, alloc_size));
    /* Reallocated (new node created) */
    if(self->current != current) {
        *mem = self->current->data;
        self->current->size += size;
    } else {
        *mem = align_ptr_ceil(self->current->data + self->current->size, SCOPED_ALLOC_ALIGNMENT);
        self->current->size += alloc_size;
    }
    return 0;
}

int scoped_allocator_add_buffer(ScopedAllocator *self, Buffer *buffer) {
    return buffer_append(&self->buffers, &buffer, sizeof(Buffer*));
}

int scoped_allocator_create_mutex(ScopedAllocator *self, amal_mutex **mutex) {
    *mutex = NULL;
    return_if_error(scoped_allocator_alloc(self, sizeof(amal_mutex), (void**)mutex));
    amal_mutex_init(*mutex);
    return buffer_append(&self->mutexes, mutex, sizeof(amal_mutex*));
}