Skip to content

Commit

Permalink
Memory arena seems to be working. Testing it out with the window
Browse files Browse the repository at this point in the history
instance and a local arena for allocations generated from Lua code for C
structs.
  • Loading branch information
tanis2000 committed Mar 25, 2024
1 parent 2bf03d7 commit 8e0d6a0
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 68 deletions.
10 changes: 5 additions & 5 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 15 additions & 6 deletions example/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ typedef struct screen_shader_vs_params_t {

typedef struct game_state_t {
binocle_memory_arena *main_arena;
binocle_memory_arena *frame_arena;
} game_state_t;

binocle_window *window;
Expand Down Expand Up @@ -450,15 +451,23 @@ void main_loop() {

int main(int argc, char *argv[])
{
binocle_memory_init();
game_state = binocle_memory_bootstrap_push_struct(game_state_t, main_arena, binocle_memory_default_bootstrap_params(), binocle_memory_default_arena_params());
binocle_string s1 = binocle_memory_push_cstring(game_state->main_arena, "Something");
binocle_string s2 = binocle_memory_push_cstring(game_state->main_arena, "Different");
binocle_app_desc_t app_desc = {0};
app = binocle_app_new();
binocle_app_init(&app, &app_desc);
binocle_sdl_init();
window = binocle_window_new(DESIGN_WIDTH, DESIGN_HEIGHT, "Binocle Test Game");

game_state = binocle_memory_bootstrap_push_struct(
game_state_t, main_arena, binocle_memory_default_bootstrap_params(),
binocle_memory_default_arena_params());
game_state->frame_arena = binocle_memory_bootstrap_push_size(
BINOCLE_DEBUG_MEMORY_NAME("frame_arena") sizeof(binocle_memory_arena), 0,
binocle_memory_non_restored_arena_bootstrap_params(),
binocle_memory_default_arena_params());
binocle_string s1 = binocle_memory_push_cstring(game_state->main_arena, "Something");
binocle_string s2 = binocle_memory_push_cstring(game_state->main_arena, "Different");
binocle_log_info("%s", s1.data);
binocle_log_info("%s", s2.data);

window = binocle_window_new(game_state->main_arena, DESIGN_WIDTH, DESIGN_HEIGHT, "Binocle Test Game");
binocle_window_set_background_color(window, binocle_color_azure());
binocle_window_set_minimum_size(window, DESIGN_WIDTH, DESIGN_HEIGHT);
adapter = binocle_viewport_adapter_new(window, BINOCLE_VIEWPORT_ADAPTER_KIND_SCALING, BINOCLE_VIEWPORT_ADAPTER_SCALING_TYPE_PIXEL_PERFECT, window->original_width, window->original_height, window->original_width, window->original_height);
Expand Down
6 changes: 5 additions & 1 deletion src/binocle/core/binocle_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
// All rights reserved.
//

#include <stdlib.h>
#include "binocle_app.h"
#include "binocle_memory.h"
#include "binocle_sdl.h"
#include "sokol_gfx.h"
#include <stdlib.h>

#define SOKOL_IMPL
#include <sokol_time.h>
Expand All @@ -28,6 +29,9 @@ bool binocle_app_init(binocle_app *app, binocle_app_desc_t *desc) {
ShowWindow(windowHandle, SW_HIDE);
#endif

// Initialize the memory arena backend
binocle_memory_init();

// Initialize the filesystem
app->fs = binocle_fs_new();
binocle_fs_init(&app->fs);
Expand Down
7 changes: 7 additions & 0 deletions src/binocle/core/binocle_lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "binocle_log.h"
#include "binocle_log_wrap.h"
#include "binocle_material_wrap.h"
#include "binocle_memory.h"
#include "binocle_sdl_wrap.h"
#include "binocle_sprite_wrap.h"
#include "binocle_subtexture_wrap.h"
Expand All @@ -31,6 +32,8 @@
#include <sokol_time.h>
#include <stdlib.h>

binocle_lua g_binocle_lua;

binocle_lua binocle_lua_new() {
binocle_lua res = {0};
return res;
Expand All @@ -43,6 +46,10 @@ bool binocle_lua_init(binocle_lua *lua) {
return false;
}

// Initialize the dedicated memory arena
binocle_log_info("Initializing Lua memory arena");
lua->arena = binocle_memory_bootstrap_push_size(BINOCLE_DEBUG_MEMORY_NAME("lua") sizeof(binocle_memory_arena), 0, binocle_memory_default_bootstrap_params(), binocle_memory_default_arena_params());

// Load the Lua libraries
luaL_openlibs(lua->L);

Expand Down
2 changes: 2 additions & 0 deletions src/binocle/core/binocle_lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@
#endif

struct binocle_window;
struct binocle_memory_arena;

typedef struct binocle_lua {
lua_State *L;
time_t last_check_time;
char *last_script_run;
struct binocle_memory_arena *arena;
} binocle_lua;

typedef int (binocle_lua_fs_enumerate_pre_run_callback)();
Expand Down
88 changes: 41 additions & 47 deletions src/binocle/core/binocle_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
//

#include "binocle_memory.h"
#include "SDL_stdinc.h"
#include "binocle_math.h"
#include <assert.h>
#include <sys/mman.h>
Expand All @@ -30,6 +29,13 @@ binocle_memory_default_bootstrap_params(void) {
return params;
}

binocle_memory_arena_bootstrap_params
binocle_memory_non_restored_arena_bootstrap_params(void) {
binocle_memory_arena_bootstrap_params params = binocle_memory_default_bootstrap_params();
params.allocation_flags = BINOCLE_MEMORY_FLAG_NOT_RESTORED;
return params;
}

binocle_memory_index
binocle_memory_get_alignment_offset(binocle_memory_arena *Arena,
binocle_memory_index alignment) {
Expand Down Expand Up @@ -71,59 +77,47 @@ binocle_memory_block *binocle_memory_allocate(binocle_memory_index size,
// We require memory block headers not to change the cache
// line alignment of an allocation
assert(sizeof(binocle_memory_platform_block) == 56);

uintptr_t PageSize = 4096;
uintptr_t TotalSize = size + sizeof(binocle_memory_platform_block);
uintptr_t BaseOffset = sizeof(binocle_memory_platform_block);
uintptr_t ProtectOffset = 0;
if(flags & BINOCLE_MEMORY_FLAG_CHECK_UNDERFLOW)
{
TotalSize = size + 2*PageSize;
BaseOffset = 2*PageSize;
ProtectOffset = PageSize;
}
else if(flags & BINOCLE_MEMORY_FLAG_CHECK_OVERFLOW)
{
uintptr_t SizeRoundedUp = binocle_math_align_pow_2(size, PageSize);
TotalSize = SizeRoundedUp + 2*PageSize;
BaseOffset = PageSize + SizeRoundedUp - size;
ProtectOffset = PageSize + SizeRoundedUp;
}

binocle_memory_platform_block *Block = (binocle_memory_platform_block *)
mmap(0, TotalSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
assert(Block != MAP_FAILED);
Block->block.base = (uint8_t *)Block + BaseOffset;
assert(Block->block.used == 0);
assert(Block->block.prev_arena == 0);
uintptr_t page_size = 4096;
uintptr_t total_size = size + sizeof(binocle_memory_platform_block);
uintptr_t base_offset = sizeof(binocle_memory_platform_block);
uintptr_t protect_offset = 0;
if (flags & BINOCLE_MEMORY_FLAG_CHECK_UNDERFLOW) {
total_size = size + 2 * page_size;
base_offset = 2 * page_size;
protect_offset = page_size;
} else if (flags & BINOCLE_MEMORY_FLAG_CHECK_OVERFLOW) {
uintptr_t size_rounded_up = binocle_math_align_pow_2(size, page_size);
total_size = size_rounded_up + 2 * page_size;
base_offset = page_size + size_rounded_up - size;
protect_offset = page_size + size_rounded_up;
}

if(flags & (BINOCLE_MEMORY_FLAG_CHECK_UNDERFLOW|BINOCLE_MEMORY_FLAG_CHECK_OVERFLOW))
{
int Protected = mprotect((uint8_t *)Block + ProtectOffset, PageSize, PROT_NONE);
binocle_memory_platform_block *block = (binocle_memory_platform_block *)mmap(
0, total_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
assert(block != MAP_FAILED);
block->block.base = (uint8_t *)block + base_offset;
assert(block->block.used == 0);
assert(block->block.prev_arena == 0);

if (flags & (BINOCLE_MEMORY_FLAG_CHECK_UNDERFLOW |
BINOCLE_MEMORY_FLAG_CHECK_OVERFLOW)) {
int Protected =
mprotect((uint8_t *)block + protect_offset, page_size, PROT_NONE);
assert(Protected != -1);
}

binocle_memory_platform_block *Sentinel = &g_memory_state.memory_sentinel;
Block->next = Sentinel;
Block->block.size = size;
Block->block.flags = flags;
Block->prev = Sentinel->prev;
Block->prev->next = Block;
Block->next->prev = Block;

binocle_memory_block *PlatBlock = &Block->block;
return PlatBlock;
block->next = Sentinel;
block->block.size = size;
block->block.flags = flags;
block->prev = Sentinel->prev;
block->prev->next = block;
block->next->prev = block;

binocle_memory_block *plat_block = &block->block;
return plat_block;
}




// binocle_memory_block *block = SDL_calloc(1, size);
// block->flags = flags;
// block->size = size;
// block->base = (uint8_t *)block;
// return block;
//}

void *
binocle_memory_push_size_(BINOCLE_MEMORY_PARAM binocle_memory_arena *arena,
Expand Down
3 changes: 3 additions & 0 deletions src/binocle/core/binocle_memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ binocle_memory_arena_push_params binocle_memory_default_arena_params(void);
binocle_memory_arena_bootstrap_params
binocle_memory_default_bootstrap_params(void);

binocle_memory_arena_bootstrap_params
binocle_memory_non_restored_arena_bootstrap_params(void);

void *binocle_memory_bootstrap_push_size(
BINOCLE_MEMORY_PARAM uintptr_t struct_size, uintptr_t offset_to_arena,
binocle_memory_arena_bootstrap_params bootstrap_params,
Expand Down
15 changes: 8 additions & 7 deletions src/binocle/core/binocle_window.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
// All rights reserved.
//

#include <inttypes.h>
#include <stdlib.h>
#include "binocle_sdl.h"
#include "backend/binocle_color.h"
#include "binocle_window.h"
#include "backend/binocle_color.h"
#include "binocle_log.h"
#include "binocle_memory.h"
#include "binocle_sdl.h"
#include <inttypes.h>
#include <stdlib.h>

binocle_window *binocle_window_new(uint32_t width, uint32_t height, char *title) {
binocle_window *res = SDL_malloc(sizeof(binocle_window));
binocle_window *binocle_window_new(binocle_memory_arena *arena, uint32_t width, uint32_t height, char *title) {
binocle_window *res = binocle_memory_push_struct(arena, binocle_window, binocle_memory_default_arena_params());
SDL_memset(res, 0, sizeof(*res));

res->width = width;
Expand Down Expand Up @@ -64,7 +65,7 @@ void binocle_window_destroy(binocle_window *win) {
}
#endif

free(win);
win = NULL;
}

void binocle_window_refresh(binocle_window *win) {
Expand Down
4 changes: 3 additions & 1 deletion src/binocle/core/binocle_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "binocle_timer.h"
#include "backend/binocle_color.h"

struct binocle_memory_arena;

/**
* This is the representation of the app/window. Currently an app can only have one window
*/
Expand Down Expand Up @@ -53,7 +55,7 @@ typedef struct binocle_window {
* @param title the title of the window
* @return a window instance
*/
binocle_window *binocle_window_new(uint32_t width, uint32_t height, char *title);
binocle_window *binocle_window_new(struct binocle_memory_arena *arena, uint32_t width, uint32_t height, char *title);

/**
* \brief Destroys an initialized window
Expand Down
4 changes: 3 additions & 1 deletion src/binocle/core/binocle_window_wrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "binocle_window.h"
#include "binocle_color_wrap.h"

extern binocle_lua g_binocle_lua;

int l_binocle_window_set_background_color(lua_State *L) {
l_binocle_window_t *window = luaL_checkudata(L, 1, "binocle_window");
l_binocle_color_t *color = luaL_checkudata(L, 2, "binocle_color");
Expand All @@ -30,7 +32,7 @@ int l_binocle_window_new(lua_State *L) {
lua_getfield(L, LUA_REGISTRYINDEX, "binocle_window");
lua_setmetatable(L, -2);
SDL_memset(window, 0, sizeof(*window));
binocle_window *win = binocle_window_new(width, height, title);
binocle_window *win = binocle_window_new(g_binocle_lua.arena, width, height, title);
window->window = win;
return 1;
}
Expand Down

0 comments on commit 8e0d6a0

Please sign in to comment.