add custom memory functions

This commit is contained in:
sam 2024-09-05 22:39:43 +12:00
parent 9504e5a3db
commit 4683e3b4ad
11 changed files with 157 additions and 159 deletions

View file

@ -2,7 +2,7 @@ BINARY=gear-bin
SHADERC=sokol-shdc
CFLAGS=-I. -Iinclude -Wall -Wextra -Werror -Wno-header-guard -Wno-comment -g
CFLAGS=-I. -Iinclude -Wall -Wextra -Werror -Wno-header-guard -Wno-comment -Ofast
LDFLAGS=-g
SHADER_FLAGS=--slang glsl430:hlsl5:metal_macos

16
include/gear/memory.h Normal file
View file

@ -0,0 +1,16 @@
#ifndef __G_MEMORY_H__
#define __G_MEMORY_H__
#include <gear/base.h>
#define MEM_ALLOC_FUNC malloc
// TODO: use macros in order to get location that function was called and log with that info instead
// 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);
#endif

View file

@ -1,21 +1,14 @@
#include <gear/camera.h>
#include <gear/memory.h>
#include <log.c/log.h>
g_camera* g_camera_create(vec3 position, vec3 rotation, float fov) {
g_camera* camera = calloc(1, sizeof(g_camera));
if(camera != NULL) {
log_info("Camera allocated successfully");
log_debug("Camera pointer: %p", camera);
camera->position = position;
camera->rotation = rotation;
camera->fov = fov;
camera->near = 0.1f;
camera->far = 1000.0f;
} else {
log_fatal("Failed to allocate camera");
exit(EXIT_FAILURE);
}
g_camera* camera = g_mem_calloc_named("Camera", sizeof(g_camera));
camera->position = position;
camera->rotation = rotation;
camera->fov = fov;
camera->near = 0.1f;
camera->far = 1000.0f;
return camera;
}

View file

@ -1,36 +1,31 @@
#include <gear/graphics/models.h>
#include <gear/memory.h>
#include <log.c/log.h>
g_model* g_model_create(g_vertex_vec* vertices, g_index_vec* indices) {
g_model* model = calloc(1, sizeof(g_model));
if(model != NULL) {
log_info("Model allocated successfully");
log_debug("Model pointer: %p", model);
g_model* model = g_mem_calloc_named("Model", sizeof(g_model));
if(vertices != NULL) {
for(sz i = 0; i < vertices->size; i++) {
sl_vec_push(model->vertices, vertices->data[i]);
}
model->vertex_buffer = sg_make_buffer(&(sg_buffer_desc){
.type = SG_BUFFERTYPE_VERTEXBUFFER,
.data = SL_SG_RANGE(model->vertices)
});
if(vertices != NULL) {
for(sz i = 0; i < vertices->size; i++) {
sl_vec_push(model->vertices, vertices->data[i]);
}
if(indices != NULL) {
for(sz i = 0; i < indices->size; i++) {
sl_vec_push(model->indices, indices->data[i]);
}
model->vertex_buffer = sg_make_buffer(&(sg_buffer_desc){
.type = SG_BUFFERTYPE_VERTEXBUFFER,
.data = SL_SG_RANGE(model->vertices)
});
}
model->index_buffer = sg_make_buffer(&(sg_buffer_desc){
.type = SG_BUFFERTYPE_INDEXBUFFER,
.data = SL_SG_RANGE(model->indices)
});
if(indices != NULL) {
for(sz i = 0; i < indices->size; i++) {
sl_vec_push(model->indices, indices->data[i]);
}
} else {
log_fatal("Failed to allocate model");
exit(EXIT_FAILURE);
model->index_buffer = sg_make_buffer(&(sg_buffer_desc){
.type = SG_BUFFERTYPE_INDEXBUFFER,
.data = SL_SG_RANGE(model->indices)
});
}
return model;

View file

@ -1,4 +1,6 @@
#include <gear/graphics/renderer.h>
#include <gear/memory.h>
#include <log.c/log.h>
g_renderer* g_renderer_create(g_color clear_color) {
@ -7,19 +9,11 @@ g_renderer* g_renderer_create(g_color clear_color) {
.clear_value = G_COLOR_TO_FLOAT_ARR(clear_color),
};
g_renderer* renderer = calloc(1, sizeof(g_renderer));
if(renderer != NULL) {
log_info("Renderer allocated successfully");
log_debug("Renderer pointer: %p", renderer);
renderer->pass_action = (sg_pass_action){
.colors = { color_action, color_action, color_action, color_action },
.depth = { .load_action = SG_LOADACTION_CLEAR, .clear_value = 1.0f },
};
} else {
log_fatal("Failed to allocate renderer");
exit(EXIT_FAILURE);
}
g_renderer* renderer = g_mem_calloc_named("Renderer", sizeof(g_renderer));
renderer->pass_action = (sg_pass_action){
.colors = { color_action, color_action, color_action, color_action },
.depth = { .load_action = SG_LOADACTION_CLEAR, .clear_value = 1.0f },
};
return renderer;
}

View file

@ -1,5 +1,7 @@
#include <gear/graphics/textures.h>
#include <gear/images.h>
#include <gear/memory.h>
#include <log.c/log.h>
g_texture* g_texture_create(g_image* image) {
@ -22,19 +24,11 @@ g_texture* g_texture_create_filtered(g_image* image, sg_filter min_filter, sg_fi
.mag_filter = mag_filter
});
g_texture* texture = calloc(1, sizeof(g_texture));
if(texture != NULL) {
log_info("Texture allocated successfully");
log_debug("Texture pointer: %p", texture);
texture->width = image->width;
texture->height = image->height;
texture->image = sg_img;
texture->sampler = sg_samp;
} else {
log_fatal("Failed to allocate texture");
exit(EXIT_FAILURE);
}
g_texture* texture = g_mem_calloc_named("Texture", sizeof(g_texture));
texture->width = image->width;
texture->height = image->height;
texture->image = sg_img;
texture->sampler = sg_samp;
g_image_destroy(image);

View file

@ -1,34 +1,26 @@
#include <gear/images.h>
#include <gear/memory.h>
#include <string.h>
#include <log.c/log.h>
g_image* g_image_create(u8* pixels, i32 width, i32 height) {
g_image* p_image = calloc(1, sizeof(g_image));
if (p_image != NULL) {
log_info("Image allocated successfully");
log_debug("Image pointer: %p", p_image);
g_image* image = g_mem_calloc_named("Image", sizeof(g_image));
image->width = width;
image->height = height;
image->channels = G_IMAGE_CHANNELS;
image->pixels = pixels;
p_image->width = width;
p_image->height = height;
p_image->channels = G_IMAGE_CHANNELS;
p_image->pixels = pixels;
} else {
log_fatal("Failed to allocate image");
exit(EXIT_FAILURE);
}
return p_image;
return image;
}
g_image* g_image_default() {
g_image* p_image = calloc(1, sizeof(g_image));
if(p_image != NULL) {
p_image->width = G_IMAGE_MISSING_DIMENSIONS,
p_image->height = G_IMAGE_MISSING_DIMENSIONS,
p_image->channels = G_IMAGE_CHANNELS,
p_image->pixels = calloc(1, G_IMAGE_MISSING_BYTESIZE);
memcpy(p_image->pixels, G_IMAGE_MISSING_PIXELS, G_IMAGE_MISSING_BYTESIZE);
}
g_image* image = g_mem_calloc_named("Image", sizeof(g_image));
image->width = G_IMAGE_MISSING_DIMENSIONS,
image->height = G_IMAGE_MISSING_DIMENSIONS,
image->channels = G_IMAGE_CHANNELS,
image->pixels = g_mem_alloc(G_IMAGE_MISSING_BYTESIZE);
memcpy(image->pixels, G_IMAGE_MISSING_PIXELS, G_IMAGE_MISSING_BYTESIZE);
return p_image;
return image;
}

View file

@ -9,6 +9,7 @@
#include <gear/ui/gui.h>
#include <gear/ui/console.h>
#include <gear/resources.h>
#include <gear/memory.h>
#include <gear/graphics/models.h>
#include <shaders/transform.glsl.h>
@ -65,7 +66,7 @@ void init() {
model = g_model_create(&vertices_vec, &indices_vec);
bind = calloc(1, sizeof(sg_bindings));
bind = g_mem_calloc_named("Bindings", sizeof(sg_bindings));
bind->vertex_buffers[0] = model->vertex_buffer;
bind->index_buffer = model->index_buffer;

45
src/memory/alloc.c Normal file
View file

@ -0,0 +1,45 @@
#include <gear/memory.h>
#include <log.c/log.h>
void* g_mem_alloc_named(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);
} else {
log_info("%zu bytes allocated successfully", size);
log_debug("Pointer: %p", ptr);
}
return ptr;
} else {
if(resource != NULL) {
log_fatal("Failed to allocate %s (%zu bytes)", resource, size);
} else {
log_fatal("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);
memset(ptr, 0, size);
if(resource != NULL) {
log_info("Zeroed %zu bytes for %s", size, resource);
} else {
log_info("Zeroid %zu bytes", size);
}
return ptr;
}
void* g_mem_alloc(sz size) {
return g_mem_alloc_named(NULL, size);
}
void* g_mem_calloc(sz size) {
return g_mem_calloc_named(NULL, size);
}

View file

@ -1,17 +1,8 @@
#include "gear/memory.h"
#include <gear/ui/gui.h>
#include <gear/ui/console.h>
#include <log.c/log.h>
/*
sz combined_len = console->output.len + len;
if(combined_len > G_CONSOLE_MAX_LEN) {
sz required_trim = combined_len - G_CONSOLE_MAX_LEN;
if(required_trim < G_CONSOLE_MAX_LEN / 10) required_trim = G_CONSOLE_MAX_LEN / 5;
memcpy(console->output.buf, console->output.buf + required_trim, G_CONSOLE_MAX_LEN - required_trim);
}*/
char format_input_buf[G_CONSOLE_MAX_LEN] = { 0 };
char fmt[G_CONSOLE_MAX_LEN];
@ -39,21 +30,13 @@ void log_callback(log_Event* event) {
}
g_gui_layer* g_console_create() {
g_console* console = calloc(1, sizeof(g_console));
if(console != NULL) {
log_info("Console allocated successfully");
log_debug("Console pointer: %p", console);
memset(&console->input, 0, G_CONSOLE_BUFFER_SIZE);
memset(&console->output, 0, G_CONSOLE_BUFFER_SIZE);
} else {
log_fatal("Failed to allocate console");
exit(EXIT_FAILURE);
}
g_console* console = g_mem_calloc_named("Console", sizeof(g_console));
memset(&console->input, 0, G_CONSOLE_BUFFER_SIZE);
memset(&console->output, 0, G_CONSOLE_BUFFER_SIZE);
log_add_callback(log_callback, console, LOG_INFO);
g_gui_layer* layer = calloc(1, sizeof(g_gui_layer));
g_gui_layer* layer = g_mem_calloc_named("Console GUI Layer", sizeof(g_gui_layer));
layer->name = "Console";
layer->draw = g_console_draw;
layer->data = console;

View file

@ -1,4 +1,5 @@
#include <gear/ui/gui.h>
#include <gear/memory.h>
#include <gear/ui/console.h>
#include <gear/resources.h>
#include <sokol/sokol_log.h>
@ -13,62 +14,46 @@ g_gui* g_gui_create() {
});
log_info("Sokol nuklear initialized successfully");
g_gui* gui = calloc(1, sizeof(g_gui));
if(gui != NULL) {
log_info("GUI allocated successfully");
log_debug("GUI pointer: %p", gui);
g_gui* gui = g_mem_calloc_named("GUI", sizeof(g_gui));
g_gui_add_layer(&gui->layers, g_console_create, g_console_destroy);
g_gui_add_layer(&gui->layers, g_console_create, g_console_destroy);
} else {
log_fatal("Failed to allocate GUI");
exit(EXIT_FAILURE);
}
struct nk_font_atlas* atlas = g_mem_calloc_named("Font atlas", sizeof(struct nk_font_atlas));
nk_font_atlas_init_default(atlas);
nk_font_atlas_begin(atlas);
struct nk_font_atlas* atlas = calloc(1, sizeof(struct nk_font_atlas));
if(atlas != NULL) {
log_info("Font atlas allocated successfully");
log_debug("Font atlas pointer: %p", atlas);
f32 font_size = 22.05f;
struct nk_font_config config = nk_font_config(font_size);
config.pixel_snap = true;
nk_font_atlas_init_default(atlas);
nk_font_atlas_begin(atlas);
g_gui_font = nk_font_atlas_add_from_memory(atlas,
res_OpenSans_Regular_ttf,
res_OpenSans_Regular_ttf_len,
font_size, &config);
f32 font_size = 22.05f;
struct nk_font_config config = nk_font_config(font_size);
config.pixel_snap = true;
i32 atlas_width, atlas_height;
const u8* pixels = nk_font_atlas_bake(atlas, &atlas_width, &atlas_height, NK_FONT_ATLAS_RGBA32);
g_gui_font = nk_font_atlas_add_from_memory(atlas,
res_OpenSans_Regular_ttf,
res_OpenSans_Regular_ttf_len,
font_size, &config);
sg_image font_image = sg_make_image(&(sg_image_desc){
.label = "Font image",
.width = atlas_width,
.height = atlas_height,
.data.subimage[0][0] = (sg_range){
.ptr = pixels,
.size = atlas_width * atlas_height * 4
},
.pixel_format = SG_PIXELFORMAT_RGBA8
});
i32 atlas_width, atlas_height;
const u8* pixels = nk_font_atlas_bake(atlas, &atlas_width, &atlas_height, NK_FONT_ATLAS_RGBA32);
sg_sampler font_sampler = sg_make_sampler(&(sg_sampler_desc){
.label = "Font sampler"
});
sg_image font_image = sg_make_image(&(sg_image_desc){
.label = "Font image",
.width = atlas_width,
.height = atlas_height,
.data.subimage[0][0] = (sg_range){
.ptr = pixels,
.size = atlas_width * atlas_height * 4
},
.pixel_format = SG_PIXELFORMAT_RGBA8
});
snk_image_t snk_img = snk_make_image(&(snk_image_desc_t){
.image = font_image,
.sampler = font_sampler
});
sg_sampler font_sampler = sg_make_sampler(&(sg_sampler_desc){
.label = "Font sampler"
});
snk_image_t snk_img = snk_make_image(&(snk_image_desc_t){
.image = font_image,
.sampler = font_sampler
});
nk_font_atlas_end(atlas, snk_nkhandle(snk_img), NULL);
} else {
log_fatal("Failed to allocate font atlas");
exit(EXIT_FAILURE);
}
nk_font_atlas_end(atlas, snk_nkhandle(snk_img), NULL);
return gui;
}