make memory allocation functions log the file they were called from
This commit is contained in:
parent
020797b9d8
commit
c1cc09ca07
4 changed files with 26 additions and 23 deletions
|
@ -5,12 +5,15 @@
|
|||
|
||||
#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
|
||||
void* g_mem_alloc_named(const char* resource, sz size);
|
||||
void* g_mem_calloc_named(const char* resource, sz size);
|
||||
void* g_mem_alloc(sz size);
|
||||
void* g_mem_calloc(sz size);
|
||||
void* __g_mem_alloc_named(const char* file, i32 line, const char* resource, sz size);
|
||||
void* __g_mem_calloc_named(const char* file, i32 line, const char* resource, sz size);
|
||||
void* __g_mem_alloc(const char* file, i32 line, sz size);
|
||||
void* __g_mem_calloc(const char* file, i32 line, sz size);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -135,8 +135,8 @@ sapp_desc sokol_main(int argc, char* argv[]) {
|
|||
.frame_cb = frame,
|
||||
.cleanup_cb = cleanup,
|
||||
.event_cb = input,
|
||||
.width = 640,
|
||||
.height = 480,
|
||||
.width = 1280,
|
||||
.height = 720,
|
||||
.window_title = "Gear",
|
||||
.icon.sokol_default = true,
|
||||
.logger.func = slog_func,
|
||||
|
|
|
@ -1,45 +1,45 @@
|
|||
#include <gear/memory.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);
|
||||
if(ptr != NULL) {
|
||||
if(resource != NULL) {
|
||||
log_info("%s allocated successfully (%zu bytes)", resource, size);
|
||||
log_debug("%s pointer: %p", resource, ptr);
|
||||
log_log(LOG_INFO, file, line, "%s allocated successfully (%zu bytes)", resource, size);
|
||||
log_log(LOG_DEBUG, file, line, "%s pointer: %p", resource, ptr);
|
||||
} else {
|
||||
log_info("%zu bytes allocated successfully", size);
|
||||
log_debug("Pointer: %p", ptr);
|
||||
log_log(LOG_INFO, file, line, "%zu bytes allocated successfully", size);
|
||||
log_log(LOG_DEBUG, file, line, "Pointer: %p", ptr);
|
||||
}
|
||||
|
||||
return ptr;
|
||||
} else {
|
||||
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 {
|
||||
log_fatal("Failed to allocate %zu bytes", size);
|
||||
log_log(LOG_FATAL, file, line, "Failed to allocate %zu bytes", size);
|
||||
}
|
||||
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
void* g_mem_calloc_named(const char* resource, sz size) {
|
||||
void* ptr = g_mem_alloc_named(resource, size);
|
||||
void* __g_mem_calloc_named(const char* file, i32 line, const char* resource, sz size) {
|
||||
void* ptr = __g_mem_alloc_named(file, line, resource, size);
|
||||
memset(ptr, 0, size);
|
||||
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 {
|
||||
log_info("Zeroid %zu bytes", size);
|
||||
log_log(LOG_INFO, file, line, "Zeroid %zu bytes", size);
|
||||
}
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void* g_mem_alloc(sz size) {
|
||||
return g_mem_alloc_named(NULL, size);
|
||||
void* __g_mem_alloc(const char* file, i32 line, sz size) {
|
||||
return __g_mem_alloc_named(file, line, NULL, size);
|
||||
}
|
||||
|
||||
void* g_mem_calloc(sz size) {
|
||||
return g_mem_calloc_named(NULL, size);
|
||||
void* __g_mem_calloc(const char* file, i32 line, sz size) {
|
||||
return __g_mem_calloc_named(file, line, NULL, size);
|
||||
}
|
||||
|
|
|
@ -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),
|
||||
NK_WINDOW_BORDER | NK_WINDOW_MOVABLE | NK_WINDOW_CLOSABLE | NK_WINDOW_SCALABLE)) {
|
||||
float frame_height = nk_window_get_content_region(ctx).h;
|
||||
|
||||
|
||||
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,
|
||||
console->output.buf, &console->output.len,
|
||||
|
|
Loading…
Add table
Reference in a new issue