terrain

Real-time terrain generation using marching cubes
git clone git://git.christianermann.dev/terrain
Log | Files | Refs | README | LICENSE

commit 4e5cf13c287f2e9f607fcde9aeb97fed8ec922db
parent 279aa6c55d0f3c80a1dd706d317eb4170c993481
Author: Christian Ermann <christianermann@gmail.com>
Date:   Fri, 29 Jul 2022 16:47:23 -0700

Added memory management system

Diffstat:
Ainclude/memory.h | 18++++++++++++++++++
Msrc/chunk.c | 3++-
Asrc/memory.c | 48++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/shader.c | 11++++++-----
Msrc/threadpool.c | 8+++++---
5 files changed, 79 insertions(+), 9 deletions(-)

diff --git a/include/memory.h b/include/memory.h @@ -0,0 +1,18 @@ +#pragma once + +#include <stdlib.h> + +typedef enum memory_tag { + MEMORY_TAG_UNKNOWN, + MEMORY_TAG_RENDER, + MEMORY_TAG_STRING, + MEMORY_TAG_JOB, + MEMORY_TAG_MAX_TAGS +} memory_tag; + +void *s_alloc(size_t size, memory_tag tag); + +void s_free(void *mem, size_t size, memory_tag tag); + +void s_zeroMemory(void *mem, size_t size); + diff --git a/src/chunk.c b/src/chunk.c @@ -2,6 +2,7 @@ #include "marching_cubes.h" #include "threadpool.h" +#include "memory.h" #include <stdlib.h> @@ -13,7 +14,7 @@ struct UpdateArgs { static UpdateArgs *UpdateArgs_make(Chunk *c) { - UpdateArgs *args = malloc(sizeof *args); + UpdateArgs *args = s_alloc(sizeof *args, MEMORY_TAG_JOB); args->chunk = c; args->f = NULL; args->isolevel = 0.0f; diff --git a/src/memory.c b/src/memory.c @@ -0,0 +1,48 @@ +#include "memory.h" +#include "logger.h" + +#include <string.h> + +struct MemoryStats { + size_t allocated; + size_t allocated_tagged[MEMORY_TAG_MAX_TAGS]; +}; + +static struct MemoryStats stats; + +void *s_alloc(size_t size, memory_tag tag) +{ + if (tag == MEMORY_TAG_UNKNOWN) + { + LOGW("s_alloc MEMORY_TAG_UNKNOWN"); + } + + stats.allocated += size; + stats.allocated_tagged[tag] += size; + + void *mem = malloc(size); + + + s_zeroMemory(mem, size); + + return mem; +} + +void s_free(void *mem, size_t size, memory_tag tag) +{ + if (tag == MEMORY_TAG_UNKNOWN) + { + LOGW("s_free MEMORY_TAG_UNKNOWN"); + } + + stats.allocated -= size; + stats.allocated_tagged[tag] -= size; + + free(mem); +} + +void s_zeroMemory(void *mem, size_t size) +{ + memset(mem, 0, size); +} + diff --git a/src/shader.c b/src/shader.c @@ -1,8 +1,8 @@ #include "shader.h" #include "logger.h" +#include "memory.h" #include <stdio.h> -#include <stdlib.h> static char* stringFromFile(const char* filename) { @@ -48,7 +48,7 @@ static GLuint createAndCompileShader(const char* shader_string, { GLint size; glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &size); - char* string = (char*)malloc(size); + char *string = (char *)s_alloc(size, MEMORY_TAG_RENDER); glGetShaderInfoLog(shader, size, &size, string); LOGE("Failed to compile shader: %s", string); glDeleteShader(shader); @@ -73,7 +73,7 @@ static GLuint createAndLinkProgram(GLuint vertex_shader, { GLint size; glGetProgramiv(program, GL_INFO_LOG_LENGTH, &size); - char* string = (char*)malloc(size); + char *string = (char *)s_alloc(size, MEMORY_TAG_STRING); glGetProgramInfoLog(program, size, &size, string); LOGE("Failed to link program: %s", string); glDeleteProgram(program); @@ -119,7 +119,7 @@ Shader *Shader_make(const char* vert_filename, const char* frag_filename) GLuint program = createProgramFromFiles(vert_filename, frag_filename); - Shader *shader = malloc(sizeof *shader); + Shader *shader = s_alloc(sizeof *shader, MEMORY_TAG_RENDER); shader->program = program; shader->vert_filename = vert_filename; shader->frag_filename = frag_filename; @@ -131,7 +131,7 @@ void Shader_free(Shader *shader) { if (shader) { - free(shader); + s_free(shader, sizeof *shader, MEMORY_TAG_RENDER); } } @@ -175,3 +175,4 @@ void Shader_setMat4(const Shader *shader, const char *name, Mat4 m) GLint uniform_loc = glGetUniformLocation(shader->program, name); glUniformMatrix4fv(uniform_loc, 1, GL_FALSE, m[0]); } + diff --git a/src/threadpool.c b/src/threadpool.c @@ -1,5 +1,7 @@ #include "threadpool.h" +#include "memory.h" + #include <pthread.h> #include <stdbool.h> #include <stdlib.h> @@ -14,7 +16,7 @@ struct ThreadWork { static ThreadWork *ThreadWork_make(ThreadFunc func, void *arg) { - ThreadWork *work = malloc(sizeof *work); + ThreadWork *work = s_alloc(sizeof *work, MEMORY_TAG_JOB); work->func = func; work->arg = arg; work->next = NULL; @@ -26,7 +28,7 @@ static void ThreadWork_free(ThreadWork *work) { if (work) { - free(work); + s_free(work, sizeof *work, MEMORY_TAG_JOB); } } @@ -132,7 +134,7 @@ static void *ThreadPool_worker(void *arg) ThreadPool *ThreadPool_make(unsigned int thread_count) { - ThreadPool *pool = malloc(sizeof *pool); + ThreadPool *pool = s_alloc(sizeof *pool, MEMORY_TAG_JOB); pool->work_first = NULL; pool->work_last = NULL; pthread_mutex_init(&pool->work_mutex, NULL);