Switch list to use memory arena

This commit is contained in:
2024-06-27 23:57:14 +01:00
parent 5636fa57fc
commit 52dbae227d
6 changed files with 151 additions and 65 deletions

View File

@@ -1,7 +1,8 @@
#ifndef TYPED_LIST_H
#define TYPED_LIST_H
#include "c_cpp_aliases/aliases.h"
#include "aliases.h"
#include "mem_arena.h"
#include <assert.h>
#include <string.h>
@@ -17,39 +18,27 @@
u64 count; \
} LIST_TYPE_NAME(T)
#define list_create(T) \
(LIST_TYPE_NAME(T) *)_create_list(sizeof(T), BASE_LIST_CAPACITY)
#define list_create_with_capacity(T, CAPACITY) \
(LIST_TYPE_NAME(T) *)_create_list(sizeof(T), CAPACITY)
#define list_create(T, ARENA) \
(LIST_TYPE_NAME(T) *)_create_list(ARENA, sizeof(T), BASE_LIST_CAPACITY)
#define list_create_with_capacity(T, ARENA, CAPACITY) \
(LIST_TYPE_NAME(T) *)_create_list(ARENA, sizeof(T), CAPACITY)
#define list_destroy(T, LP) \
do { \
munmap(LP->items, sizeof(T) * LP->capacity); \
LP->items = NULL; \
LP->count = 0; \
LP->capacity = 0; \
munmap(LP, sizeof(LIST_TYPE_NAME(T))); \
} while (0)
#define _increase_list_capacity(T, LP, CAP) \
#define _increase_list_capacity(T, ARENA, LP, CAP) \
do { \
u64 new_capacity = LP->capacity * 2; \
T *tmp = _alloc_for_list(sizeof(T) * new_capacity); \
T *tmp = _alloc_for_list(ARENA, sizeof(T) * new_capacity); \
assert(tmp != NULL && "Failed to increase capacity"); \
\
memcpy(tmp, LP->items, sizeof(T) * LP->count); \
\
i32 deallocated = munmap(LP->items, sizeof(T) * LP->capacity); \
assert(deallocated == 0 && "Failed to deallocate old memory"); \
\
LP->capacity = new_capacity; \
LP->items = tmp; \
} while (0)
#define list_append(T, LP, ITEM) \
#define list_append(T, ARENA, LP, ITEM) \
do { \
if (LP->count + 1 >= LP->capacity) { \
_increase_list_capacity(T, LP, LP->capacity * 2); \
_increase_list_capacity(T, ARENA, LP, LP->capacity * 2); \
} \
\
LP->items[(LP->count)++] = ITEM; \
@@ -59,13 +48,13 @@
#define list_get(LP, IDX) LP->items[IDX]
#define list_merge(T, DST, LP1, LP2) \
#define list_merge(T, ARENA, DST, LP1, LP2) \
do { \
u64 new_count = LP1->count + LP2->count; \
u64 capacity = \
new_count < BASE_LIST_CAPACITY ? BASE_LIST_CAPACITY : new_count * 2; \
\
DST = (LIST_TYPE_NAME(T) *)_create_list(new_count, capacity); \
DST = (LIST_TYPE_NAME(T) *)_create_list(ARENA, new_count, capacity); \
assert(DST != NULL && "Failed to allocate new list"); \
\
u64 lp1_copy_size = sizeof(T) * LP1->count; \
@@ -79,7 +68,7 @@
MAKE_LIST_TYPE(void);
list_void_t *_create_list(u64 size, u64 count);
void *_alloc_for_list(u64 size);
list_void_t *_create_list(Arena *arena, u64 size, u64 count);
void *_alloc_for_list(Arena *arena, u64 size);
#endif // !TYPED_LIST_H

View File

@@ -1,8 +1,9 @@
#ifndef RASTERISER_H
#define RASTERISER_H
#include "c_cpp_aliases/aliases.h"
#include "aliases.h"
#include "list/typed_list.h"
#include "mem_arena.h"
#include "vector/vec.h"
#include "window/window.h"
@@ -17,11 +18,17 @@ typedef struct {
vec2i_t p0;
vec2i_t p1;
vec2i_t p2;
f32 h0;
f32 h1;
f32 h2;
} triangle_t;
void draw_wireframe_triangle(window_t *wnd, triangle_t triangle,
void draw_wireframe_triangle(window_t *wnd, Arena *arena, triangle_t triangle,
colour_t colour);
void draw_filled_triangle(window_t *wnd, triangle_t triangle, colour_t colour);
void draw_line(window_t *wnd, line_t line, colour_t colour);
void draw_filled_triangle(window_t *wnd, Arena *arena, triangle_t triangle,
colour_t colour);
void draw_shaded_triangle(window_t *wnd, Arena *arena, triangle_t triangle,
colour_t colour);
void draw_line(window_t *wnd, Arena *arena, line_t line, colour_t colour);
#endif // !RASTERISER_H

View File

@@ -1,7 +1,7 @@
#ifndef WINDOW_H
#define WINDOW_H
#include "c_cpp_aliases/aliases.h"
#include "aliases.h"
#include "vector/vec.h"
#include <SDL2/SDL_video.h>
#include <stdbool.h>