replace mempool with custom cleanup functions default tip

Mon, 05 May 2025 19:47:25 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 05 May 2025 19:47:25 +0200
changeset 110
29f8d0d586f8
parent 109
08548799ae4a

replace mempool with custom cleanup functions

src/Makefile file | annotate | diff | comparison | revisions
src/ascension/glcontext.h file | annotate | diff | comparison | revisions
src/glcontext.c file | annotate | diff | comparison | revisions
test/snake/snake.c file | annotate | diff | comparison | revisions
--- a/src/Makefile	Sun May 04 21:50:13 2025 +0200
+++ b/src/Makefile	Mon May 05 19:47:25 2025 +0200
@@ -45,7 +45,9 @@
 
 FORCE:
 
-$(BUILD_DIR)/behavior.o: behavior.c ascension/behavior.h
+$(BUILD_DIR)/behavior.o: behavior.c ascension/behavior.h \
+ ascension/scene_node.h ascension/datatypes.h ascension/transform.h \
+ ascension/error.h
 	@echo "Compiling $<"
 	$(CC) -o $@ $(CFLAGS) -c $<
 
@@ -105,9 +107,9 @@
  ascension/primitives.h ascension/mesh.h ascension/shader.h \
  ascension/texture.h ascension/scene.h ascension/scene_node.h \
  ascension/transform.h ascension/camera.h ascension/input.h \
- ascension/ui/font.h ascension/scene.h ascension/shader.h ascension/2d.h \
- ascension/2d/sprite.h ascension/2d/../scene_node.h \
- ascension/2d/../texture.h
+ ascension/ui/font.h ascension/scene.h ascension/behavior.h \
+ ascension/shader.h ascension/2d.h ascension/2d/sprite.h \
+ ascension/2d/../scene_node.h ascension/2d/../texture.h
 	@echo "Compiling $<"
 	$(CC) -o $@ $(CFLAGS) -c $<
 
--- a/src/ascension/glcontext.h	Sun May 04 21:50:13 2025 +0200
+++ b/src/ascension/glcontext.h	Mon May 05 19:47:25 2025 +0200
@@ -30,12 +30,12 @@
 
 #include <SDL2/SDL.h>
 
+#include <cx/list.h>
+
 #include "primitives.h"
 #include "shader.h"
 #include "texture.h"
 
-#include <cx/mempool.h>
-
 typedef struct AscGLContextSettings {
     int gl_major_version;
     int gl_minor_version;
@@ -60,8 +60,8 @@
 
 typedef struct AscGLContext {
     SDL_Window *window;
-    CxMempool *mpool;
     SDL_GLContext glctx;
+    CxList *cleanup_funcs;
     AscMesh primitives[ASC_PRIMITIVE_COUNT];
     AscTexture textures_2d[ASC_TEXTURE_2D_COUNT];
     AscTexture textures_rect[ASC_TEXTURE_RECT_COUNT];
@@ -71,7 +71,6 @@
 } AscGLContext;
 
 #define asc_active_glctx (&asc_active_window->glctx)
-#define asc_active_glctx_mpool asc_active_window->glctx.mpool
 #define ASC_PRIMITIVE_PLANE (&asc_active_glctx->primitives[ASC_PRIMITIVE_PLANE_IDX])
 #define ASC_TEXTURE_2D_EMPTY_1X1 (&asc_active_glctx->textures_2d[ASC_TEXTURE_2D_EMPTY_1X1_IDX])
 #define ASC_TEXTURE_RECT_EMPTY_1X1 (&asc_active_glctx->textures_rect[ASC_TEXTURE_RECT_EMPTY_1X1_IDX])
@@ -92,4 +91,8 @@
     SDL_GL_MakeCurrent(ctx->window, ctx->glctx);
 }
 
+void asc_gl_context_add_cleanup_func(AscGLContext *ctx, void(*func)(void));
+void asc_gl_context_add_cleanup_func2(AscGLContext *ctx, void(*func)(void*), void *memory);
+void asc_gl_context_add_cleanup_func3(AscGLContext *ctx, void(*func)(void*,void*), void *memory, void *additional_data);
+
 #endif //ASCENSION_GLCONTEXT_H
--- a/src/glcontext.c	Sun May 04 21:50:13 2025 +0200
+++ b/src/glcontext.c	Mon May 05 19:47:25 2025 +0200
@@ -29,6 +29,7 @@
 #include "ascension/error.h"
 
 #include <GL/glew.h>
+#include <cx/array_list.h>
 
 static void asc_gl_debug_callback(
         GLenum source, GLenum type, GLuint id, GLenum severity,
@@ -96,6 +97,31 @@
     asc_texture_destroy(ctx->textures_2d, ASC_TEXTURE_2D_COUNT);
 }
 
+struct asc_gl_context_cleanup_data {
+    int type;
+    union {
+        void(*f1)(void);
+        void(*f2)(void*);
+        void(*f3)(void*, void*);
+    };
+    void *memory;
+    void *additional_data;
+};
+
+static void asc_gl_context_cleanup(struct asc_gl_context_cleanup_data *data) {
+    switch (data->type) {
+        case 1:
+            data->f1();
+            break;
+        case 2:
+            data->f2(data->memory);
+            break;
+        default:
+            data->f3(data->memory, data->additional_data);
+            break;
+    }
+}
+
 bool asc_gl_context_initialize(
         AscGLContext *ctx,
         SDL_Window *window,
@@ -136,7 +162,8 @@
             return false;
         }
 
-        ctx->mpool = cxMempoolCreateSimple(128);
+        ctx->cleanup_funcs = cxArrayListCreateSimple(sizeof(struct asc_gl_context_cleanup_data), 8);
+        cxDefineDestructor(ctx->cleanup_funcs, asc_gl_context_cleanup);
 
         return true;
     } else {
@@ -150,8 +177,7 @@
     if (ctx->glctx == NULL) return;
     SDL_GL_MakeCurrent(ctx->window, ctx->glctx);
 
-    cxMempoolFree(ctx->mpool);
-    ctx->mpool = NULL;
+    cxListFree(ctx->cleanup_funcs);
     asc_texture_destroy_predefined(ctx);
     asc_shader_destroy_predefined(ctx);
     asc_primitives_destroy(ctx);
@@ -160,3 +186,24 @@
     SDL_GL_DeleteContext(ctx->glctx);
     ctx->glctx = NULL;
 }
+
+void asc_gl_context_add_cleanup_func(AscGLContext *ctx, void(*func)(void)) {
+    struct asc_gl_context_cleanup_data *data = cxListEmplace(ctx->cleanup_funcs);
+    data->type = 1;
+    data->f1 = func;
+}
+
+void asc_gl_context_add_cleanup_func2(AscGLContext *ctx, void(*func)(void*), void *memory) {
+    struct asc_gl_context_cleanup_data *data = cxListEmplace(ctx->cleanup_funcs);
+    data->type = 2;
+    data->f2 = func;
+    data->memory = memory;
+}
+
+void asc_gl_context_add_cleanup_func3(AscGLContext *ctx, void(*func)(void*,void*), void *memory, void *additional_data) {
+    struct asc_gl_context_cleanup_data *data = cxListEmplace(ctx->cleanup_funcs);
+    data->type = 3;
+    data->f3 = func;
+    data->memory = memory;
+    data->additional_data = additional_data;
+}
--- a/test/snake/snake.c	Sun May 04 21:50:13 2025 +0200
+++ b/test/snake/snake.c	Mon May 05 19:47:25 2025 +0200
@@ -42,7 +42,7 @@
 #define BACKDROP_SCENE asc_window_scene(0)
 #define MAIN_SCENE asc_window_scene(1)
 
-static void destroy_textures(__attribute__((__unused__)) void *dummy) {
+static void destroy_textures(void) {
     asc_texture_destroy(tex2d, TEX2D_COUNT);
 }
 
@@ -50,7 +50,7 @@
     asc_texture_init_2d(tex2d, TEX2D_COUNT);
     asc_texture_from_file(TEXTURE_SHIP, "ship.png");
     asc_texture_from_file(TEXTURE_BACKDROP, "backdrop.png");
-    cxMempoolRegister(asc_active_glctx_mpool, tex2d, destroy_textures);
+    asc_gl_context_add_cleanup_func(asc_active_glctx, destroy_textures);
 }
 
 static void update_fps_counter(AscBehavior *behavior) {

mercurial