summaryrefslogtreecommitdiff
path: root/src/smrt_arena.c
blob: cf6418ec394406425ba6d5dfe2e1c223185aa780 (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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#if defined(__linux__)
    #ifndef _DEFAULT_SOURCE
        #define _DEFAULT_SOURCE
    #endif /* ifndef _DEFAULT_SOURCE */
#endif

#include "common.h"
#include "smrt_arena.h"

#include <string.h>

static __thread smrt_arena_t *_scratch_pool[SCRATCH_POOL_SIZE] = { 0 };

smrt_arena_t *smrt_arena_create(u64 reserve_size, u64 commit_size, b32 auto_decommit) {
    u32 pagesize = plat_get_pagesize();

    reserve_size = ALIGN_UP_POW2(reserve_size + sizeof(smrt_arena_t), pagesize);
     commit_size = ALIGN_UP_POW2( commit_size, pagesize);

    smrt_arena_t *arena = plat_mem_reserve(reserve_size);

    if (!plat_mem_commit(arena, commit_size)) {
        return NULL;
    }

    arena->reserve_size = reserve_size;
     arena->commit_size = commit_size;
      arena->commit_pos = commit_size;
             arena->pos = SMRT_ARENA_BASE_POS;
        arena->mark_pos = 0;
    arena->auto_decommit = auto_decommit;

    return arena;

}

static void smrt_arena__shrink_commit(smrt_arena_t *arena) {
    if (!arena->auto_decommit) { return; }

    u64 target_commit_pos = arena->pos + arena->commit_size - 1;
    target_commit_pos -= target_commit_pos % arena->commit_size;

    if (target_commit_pos >= arena->commit_pos) { return; }

    u64 decommit_size = arena->commit_pos - target_commit_pos;
    u8 *decommit_start = (u8*)arena + target_commit_pos;

    if (plat_mem_decommit(decommit_start, decommit_size)) {
        arena->commit_pos = target_commit_pos;

        if (arena->mark_pos > arena->commit_pos) {
            arena->mark_pos = 0;
        }
    }
}

void *smrt_arena_push(smrt_arena_t *arena, u64 alloc_amount, b32 zero_out) {
    u64 pos_aligned = ALIGN_UP_POW2(arena->pos, ARENA_ALIGN);
    u64     new_pos = pos_aligned + alloc_amount;

    if (new_pos > arena->reserve_size) { return NULL; }

    if (new_pos > arena->commit_pos) {
        u64 new_commit_pos = new_pos;
        new_commit_pos += arena->commit_size - 1;
        new_commit_pos -= new_commit_pos % arena->commit_size;
        new_commit_pos =  MIN(new_commit_pos, arena->reserve_size);

        u8 *mem = (u8*)arena + arena->commit_pos;
        u64 commit_size = new_commit_pos - arena->commit_pos;

        if (!plat_mem_commit(mem, commit_size)) {
            return NULL;
        }

        arena->commit_pos = new_commit_pos;
    }

    arena->pos = new_pos;

    u8 *out = (u8*)arena + pos_aligned;

    if (zero_out) {
        memset(out, 0, alloc_amount);
    }

    return out;
}

void *smrt_arena_mark_push(smrt_arena_t *arena, u64 alloc_amount, b32 zero_out) {
    u64 orig_pos = arena->pos;

    void *mem = smrt_arena_push(arena, alloc_amount, zero_out);

    if (mem) {
        arena->mark_pos = orig_pos;
    }

    return mem;
}

void smrt_arena_pop(smrt_arena_t *arena, u64 pop_amount) {
    pop_amount = MIN(pop_amount, arena->pos - SMRT_ARENA_BASE_POS);
    arena->pos -= pop_amount;

    smrt_arena__shrink_commit(arena);
}

void smrt_arena_pop_to(smrt_arena_t *arena, u64 pos) {
    u64 size = pos < arena->pos ? arena->pos - pos : 0;
    smrt_arena_pop(arena, size);
}

b32 smrt_arena_pop_to_mark(smrt_arena_t *arena) {
    if (arena->mark_pos == 0) {
        return false;
    }

    smrt_arena_pop_to(arena, arena->mark_pos);

    return true;
}

void smrt_arena_clear(smrt_arena_t *arena, b32 zero_out) {
    arena->pos = SMRT_ARENA_BASE_POS;

    if (zero_out) {
        memset((u8*)arena + SMRT_ARENA_BASE_POS, 0, arena->commit_size - SMRT_ARENA_BASE_POS);
    }

    smrt_arena__shrink_commit(arena);
}

void smrt_arena_mark(smrt_arena_t *arena) {
    arena->mark_pos = arena->pos;
}

void smrt_arena_destroy(smrt_arena_t *arena) {
    plat_mem_release(arena, arena->reserve_size);
}

smrta_temp_t smrta_temp_start(smrt_arena_t *arena) {
    return (smrta_temp_t){
            .arena=arena,
        .start_pos=arena->pos
    };
}

void smrta_temp_end(smrta_temp_t temp) {
    smrt_arena_pop_to(temp.arena, temp.start_pos);
}

smrta_temp_t smrta_scratch_start(smrt_arena_t **conflicts, u32 num_conflicts) {
    i32 candidate_idx = -1;

    for (i32 i = 0; i < SCRATCH_POOL_SIZE; i++) {
        b32 conflict_found = false;

        for (u32 j = 0; j < num_conflicts; j++) {
            if (_scratch_pool[i] == conflicts[j]) {
                conflict_found = true;
            }
        }

        if (!conflict_found) {
            candidate_idx = i;
            break;
        }
    }

    if (candidate_idx != -1) {
        smrt_arena_t **selected = &_scratch_pool[candidate_idx];

        if (!*selected) {
            *selected = smrt_arena_create(SMRTA_SCRATCH_RESERVE_SIZE, plat_get_pagesize(), true);
        }

        return smrta_temp_start(*selected);
    }

    return (smrta_temp_t){ 0 };
}

void smrta_scratch_end(smrta_temp_t scratch) {
    smrta_temp_end(scratch);
}

#if defined(_WIN32)

#include <windows.h>

u32 plat_get_pagesize(void) {
    SYSTEM_INFO sysinfo = { 0 };
    GetSystemInfo(&sysinfo);

    return sysinfo.dwPageSize;
}

void* plat_mem_reserve(u64 size) {
    return VirtualAlloc(NULL, size, MEM_RESERVE, PAGE_READWRITE);
}

b32 plat_mem_commit(void* ptr, u64 size) {
    void* ret = VirtualAlloc(ptr, size, MEM_COMMIT, PAGE_READWRITE);
    return ret != NULL;
}

b32 plat_mem_decommit(void* ptr, u64 size) {
    return VirtualFree(ptr, size, MEM_DECOMMIT);
}

b32 plat_mem_release(void* ptr, u64 size) {
    return VirtualFree(ptr, size, MEM_RELEASE);
}


#elif defined(__linux__)

#include <unistd.h>
#include <sys/mman.h>

u32 plat_get_pagesize(void) {
    return (u32)sysconf(_SC_PAGESIZE);
}

void* plat_mem_reserve(u64 size) {
    void* out = mmap(NULL, size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    if (out == MAP_FAILED) {
        return NULL;
    }
    return out;
}

b32 plat_mem_commit(void* ptr, u64 size) {
    i32 ret = mprotect(ptr, size, PROT_READ | PROT_WRITE);
    return ret == 0;
}

b32 plat_mem_decommit(void* ptr, u64 size) {
    i32 ret = mprotect(ptr, size, PROT_NONE);
    if (ret != 0) return false;
    ret = madvise(ptr, size, MADV_DONTNEED);
    return ret == 0;
}

b32 plat_mem_release(void* ptr, u64 size) {
    i32 ret = munmap(ptr, size);
    return ret == 0;
}

#endif