make memory allocation functions log the file they were called from

This commit is contained in:
sam 2024-09-07 17:11:03 +12:00
parent 020797b9d8
commit c1cc09ca07
4 changed files with 26 additions and 23 deletions

View file

@ -5,12 +5,15 @@
#define MEM_ALLOC_FUNC malloc #define MEM_ALLOC_FUNC malloc
// TODO: use macros in order to get location that function was called and log with that info instead #define g_mem_alloc_named(resource, size) __g_mem_alloc_named(__FILE__, __LINE__, resource, size);
#define g_mem_calloc_named(resource, size) __g_mem_calloc_named(__FILE__, __LINE__, resource, size);
#define g_mem_alloc(size) __g_mem_alloc(__FILE__, __LINE__, size);
#define g_mem_calloc(resource, size) __g_mem_calloc(__FILE__, __LINE__, size);
// src/memory/alloc.c // src/memory/alloc.c
void* g_mem_alloc_named(const char* resource, sz size); void* __g_mem_alloc_named(const char* file, i32 line, const char* resource, sz size);
void* g_mem_calloc_named(const char* resource, sz size); void* __g_mem_calloc_named(const char* file, i32 line, const char* resource, sz size);
void* g_mem_alloc(sz size); void* __g_mem_alloc(const char* file, i32 line, sz size);
void* g_mem_calloc(sz size); void* __g_mem_calloc(const char* file, i32 line, sz size);
#endif #endif

View file

@ -135,8 +135,8 @@ sapp_desc sokol_main(int argc, char* argv[]) {
.frame_cb = frame, .frame_cb = frame,
.cleanup_cb = cleanup, .cleanup_cb = cleanup,
.event_cb = input, .event_cb = input,
.width = 640, .width = 1280,
.height = 480, .height = 720,
.window_title = "Gear", .window_title = "Gear",
.icon.sokol_default = true, .icon.sokol_default = true,
.logger.func = slog_func, .logger.func = slog_func,

View file

@ -1,45 +1,45 @@
#include <gear/memory.h> #include <gear/memory.h>
#include <log.c/log.h> #include <log.c/log.h>
void* g_mem_alloc_named(const char* resource, sz size) { void* __g_mem_alloc_named(const char* file, i32 line, const char* resource, sz size) {
void* ptr = MEM_ALLOC_FUNC(size); void* ptr = MEM_ALLOC_FUNC(size);
if(ptr != NULL) { if(ptr != NULL) {
if(resource != NULL) { if(resource != NULL) {
log_info("%s allocated successfully (%zu bytes)", resource, size); log_log(LOG_INFO, file, line, "%s allocated successfully (%zu bytes)", resource, size);
log_debug("%s pointer: %p", resource, ptr); log_log(LOG_DEBUG, file, line, "%s pointer: %p", resource, ptr);
} else { } else {
log_info("%zu bytes allocated successfully", size); log_log(LOG_INFO, file, line, "%zu bytes allocated successfully", size);
log_debug("Pointer: %p", ptr); log_log(LOG_DEBUG, file, line, "Pointer: %p", ptr);
} }
return ptr; return ptr;
} else { } else {
if(resource != NULL) { if(resource != NULL) {
log_fatal("Failed to allocate %s (%zu bytes)", resource, size); log_log(LOG_FATAL, file, line, "Failed to allocate %s (%zu bytes)", resource, size);
} else { } else {
log_fatal("Failed to allocate %zu bytes", size); log_log(LOG_FATAL, file, line, "Failed to allocate %zu bytes", size);
} }
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
void* g_mem_calloc_named(const char* resource, sz size) { void* __g_mem_calloc_named(const char* file, i32 line, const char* resource, sz size) {
void* ptr = g_mem_alloc_named(resource, size); void* ptr = __g_mem_alloc_named(file, line, resource, size);
memset(ptr, 0, size); memset(ptr, 0, size);
if(resource != NULL) { if(resource != NULL) {
log_info("Zeroed %zu bytes for %s", size, resource); log_log(LOG_INFO, file, line, "Zeroed %zu bytes for %s", size, resource);
} else { } else {
log_info("Zeroid %zu bytes", size); log_log(LOG_INFO, file, line, "Zeroid %zu bytes", size);
} }
return ptr; return ptr;
} }
void* g_mem_alloc(sz size) { void* __g_mem_alloc(const char* file, i32 line, sz size) {
return g_mem_alloc_named(NULL, size); return __g_mem_alloc_named(file, line, NULL, size);
} }
void* g_mem_calloc(sz size) { void* __g_mem_calloc(const char* file, i32 line, sz size) {
return g_mem_calloc_named(NULL, size); return __g_mem_calloc_named(file, line, NULL, size);
} }

View file

@ -7,7 +7,7 @@ void g_console_draw(g_gui_context* ctx, g_gui_layer* layer) {
if (nk_begin(ctx, "Console", nk_rect(50, 50, 500, 500), if (nk_begin(ctx, "Console", nk_rect(50, 50, 500, 500),
NK_WINDOW_BORDER | NK_WINDOW_MOVABLE | NK_WINDOW_CLOSABLE | NK_WINDOW_SCALABLE)) { NK_WINDOW_BORDER | NK_WINDOW_MOVABLE | NK_WINDOW_CLOSABLE | NK_WINDOW_SCALABLE)) {
float frame_height = nk_window_get_content_region(ctx).h; float frame_height = nk_window_get_content_region(ctx).h;
nk_layout_row_dynamic(ctx, frame_height - 42.5f, 1); nk_layout_row_dynamic(ctx, frame_height - 42.5f, 1);
snk_edit_string(ctx, NK_EDIT_CLIPBOARD | NK_EDIT_SELECTABLE | NK_EDIT_MULTILINE | NK_EDIT_GOTO_END_ON_ACTIVATE, snk_edit_string(ctx, NK_EDIT_CLIPBOARD | NK_EDIT_SELECTABLE | NK_EDIT_MULTILINE | NK_EDIT_GOTO_END_ON_ACTIVATE,
console->output.buf, &console->output.len, console->output.buf, &console->output.len,