Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory arena implementation #12

Merged
merged 6 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

2 changes: 1 addition & 1 deletion .idea/fileTemplates/includes/C File Header.h

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

4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ if(BINOCLE_SHOW_CONSOLE)
add_definitions(-DBINOCLE_SHOW_CONSOLE)
endif()

if(BINOCLE_LOG_MEMORY_ALLOCATIONS)
add_definitions(-DBINOCLE_LOG_MEMORY_ALLOCATIONS)
endif()

include(BinocleUtils)

SET(VERSION_MAJOR "0")
Expand Down
1 change: 1 addition & 0 deletions CMakeOptions.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
option(BINOCLE_LUAJIT "Enable LuaJIT on supported platforms (Windows, macOS)" OFF)
option(BINOCLE_SHOW_CONSOLE "Enable console output on Windows" OFF)
option(BINOCLE_HTTP "Enable HTTP support on supported platforms (Windows, macOS, web)" ON)
option(BINOCLE_LOG_MEMORY_ALLOCATIONS "Enable logging of memory allocations through the memory arena" OFF)
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# -- Project information -----------------------------------------------------

project = 'Binocle'
copyright = '2019-2021, Valerio Santinelli'
copyright = '2019-2023, Valerio Santinelli'
author = 'Valerio Santinelli'


Expand Down
21 changes: 20 additions & 1 deletion example/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "binocle_audio.h"
#include "sokol_gfx.h"
#include "binocle_http.h"
#include "binocle_memory.h"

//#define GAMELOOP 1
//#define DEMOLOOP
Expand Down Expand Up @@ -72,6 +73,11 @@ typedef struct screen_shader_vs_params_t {
float transform[16];
} 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;
binocle_input input;
binocle_viewport_adapter *adapter;
Expand All @@ -97,6 +103,7 @@ sg_image render_target;
sg_shader default_shader;
sg_shader screen_shader;
sg_image wabbit_image;
game_state_t *game_state;

#if (defined(__APPLE__) && !defined(__IPHONEOS__)) || defined(__EMSCRIPTEN__)
#define WITH_PHYSICS
Expand Down Expand Up @@ -447,7 +454,19 @@ int main(int argc, char *argv[])
binocle_app_desc_t app_desc = {0};
app = binocle_app_new();
binocle_app_init(&app, &app_desc);
binocle_sdl_init();

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(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);
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
5 changes: 5 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 @@ -43,6 +44,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
10 changes: 10 additions & 0 deletions src/binocle/core/binocle_math.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#ifndef BINOCLE_MATH_H
#define BINOCLE_MATH_H

#include <stdbool.h>
#include <stdint.h>
#include <kazmath/kazmath.h>

#ifndef MIN
Expand Down Expand Up @@ -65,4 +67,12 @@ static kmMat4 binocle_math_create_orthographic_matrix_off_center(
}
//#endif //BINOCLE_MATH_IMPL

static bool binocle_math_is_pow_2(uint32_t value)
{
bool result = ((value & ~(value - 1)) == value);
return result;
}

#define binocle_math_align_pow_2(value, alignment) (((value) + ((alignment) - 1)) & ~(((value) - (value)) + (alignment) - 1))

#endif //BINOCLE_MATH_H
Loading
Loading