commit 703d77180105ff8a837b82948b27514115c136eb
parent 21cb540d71091f27a04fe9ee56825766b20dbcf9
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Wed, 20 Jul 2022 11:50:57 +0200
Cleanup of material memory management
Diffstat:
3 files changed, 76 insertions(+), 19 deletions(-)
diff --git a/src/rngrd.c b/src/rngrd.c
@@ -22,6 +22,8 @@
#include "rngrd_c.h"
#include "rngrd_log.h"
+#include <modradurb/mrumtl.h>
+
#include <star/s3d.h>
#include <star/sbuf.h>
@@ -118,18 +120,12 @@ static void
release_rngrd(ref_T* ref)
{
struct rngrd* ground = CONTAINER_OF(ref, struct rngrd, ref);
- size_t i;
ASSERT(ref);
if(ground->logger == &ground->logger__) logger_release(&ground->logger__);
if(ground->s3d) S3D(device_ref_put(ground->s3d));
if(ground->s3d_view) S3D(scene_view_ref_put(ground->s3d_view));
if(ground->props) SBUF(ref_put(ground->props));
-
- FOR_EACH(i, 0, darray_mtl_size_get(&ground->mtls)) {
- struct mrumtl* mtl = darray_mtl_data_get(&ground->mtls)[i];
- if(mtl) MRUMTL(ref_put(mtl));
- }
darray_mtl_release(&ground->mtls);
str_release(&ground->name);
MEM_RM(ground->allocator, ground);
@@ -192,3 +188,40 @@ exit:
error:
goto exit;
}
+
+/*******************************************************************************
+ * Local functions
+ ******************************************************************************/
+res_T
+mtl_init(struct mem_allocator* allocator, struct mrumtl** mtl)
+{
+ (void)allocator;
+ *mtl = NULL;
+ return RES_OK;
+}
+
+void
+mtl_release(struct mrumtl** mtl)
+{
+ if(*mtl) MRUMTL(ref_put(*mtl));
+}
+
+res_T
+mtl_copy(struct mrumtl** dst, struct mrumtl* const* src)
+{
+ ASSERT(dst && src);
+ if(*dst) MRUMTL(ref_put(*dst));
+ *dst = *src;
+ if(*dst) MRUMTL(ref_get(*dst));
+ return RES_OK;
+}
+
+res_T
+mtl_copy_and_release(struct mrumtl** dst, struct mrumtl** src)
+{
+ ASSERT(dst && src);
+ if(*dst) MRUMTL(ref_put(*dst));
+ *dst = *src;
+ *src = NULL;
+ return RES_OK;
+}
diff --git a/src/rngrd_c.h b/src/rngrd_c.h
@@ -21,8 +21,6 @@
#ifndef RNGRD_C_H
#define RNGRD_C_H
-#include <modradurb/mrumtl.h>
-
#include <rsys/dynamic_array.h>
#include <rsys/logger.h>
#include <rsys/ref_count.h>
@@ -31,20 +29,40 @@
struct rngrd_create_args;
struct mrumtl;
-static INLINE res_T
-mtl_init(struct mem_allocator* allocator, struct mrumtl** mtl)
-{
- (void)allocator;
- *mtl = NULL;
- return RES_OK;
-}
+/*******************************************************************************
+ * Material
+ ******************************************************************************/
+extern LOCAL_SYM res_T
+mtl_init
+ (struct mem_allocator* allocator,
+ struct mrumtl** mtl);
+
+extern LOCAL_SYM void
+mtl_release
+ (struct mrumtl** mtl);
+
+extern LOCAL_SYM res_T
+mtl_copy
+ (struct mrumtl** dst,
+ struct mrumtl* const* src);
+
+extern LOCAL_SYM res_T
+mtl_copy_and_release
+ (struct mrumtl** dst,
+ struct mrumtl** src);
/* Generate the dynamic array of materials */
#define DARRAY_NAME mtl
#define DARRAY_DATA struct mrumtl*
#define DARRAY_FUNCTOR_INIT mtl_init
+#define DARRAY_FUNCTOR_RELEASE mtl_release
+#define DARRAY_FUNCTOR_COPY mtl_copy
+#define DARRAY_FUNCTOR_COPY_AND_RELEASE mtl_copy_and_release
#include <rsys/dynamic_array.h>
+/*******************************************************************************
+ * Ground
+ ******************************************************************************/
struct rngrd {
/* Geometry */
struct s3d_device* s3d;
diff --git a/src/rngrd_properties.c b/src/rngrd_properties.c
@@ -24,6 +24,8 @@
#include "rngrd_c.h"
#include "rngrd_log.h"
+#include <modradurb/mrumtl.h>
+
#include <star/sbuf.h>
#include <rsys/cstr.h>
@@ -45,14 +47,12 @@ check_sbuf_desc
if(desc->size != ground->ntriangles) {
log_err(ground,
- "%s: no sufficient surface properties regarding the mesh %s.\n",
+ "%s: no sufficient surface properties regarding the mesh %s\n",
args->props_filename, args->smsh_filename);
return RES_BAD_ARG;
}
- if(desc->szitem != 8
- || desc->alitem != 8
- || desc->pitch != 8) {
+ if(desc->szitem != 8 || desc->alitem != 8 || desc->pitch != 8) {
log_err(ground, "%s: unexpected layout of properties\n",
args->props_filename);
return RES_BAD_ARG;
@@ -109,6 +109,8 @@ parse_mtl(struct rngrd* ground, struct txtrdr* txtrdr, struct mrumtl** out_mtl)
ASSERT(wexp.we_wordc != 0);
args.verbose = ground->verbose;
+ args.logger = ground->logger;
+ args.allocator = ground->allocator;
res = mrumtl_create(&args, &mtl);
if(res != RES_OK) {
log_err(ground, "%s:%lu: could not create the MruMtl data structure\n",
@@ -130,6 +132,7 @@ exit:
*out_mtl = mtl;
return res;
error:
+ if(mtl) { MRUMTL(ref_put(mtl)); mtl = NULL; }
goto exit;
}
@@ -200,6 +203,7 @@ exit:
if(txtrdr) txtrdr_ref_put(txtrdr);
return res;
error:
+ darray_mtl_clear(&ground->mtls);
goto exit;
}
@@ -235,6 +239,8 @@ setup_properties(struct rngrd* ground, const struct rngrd_create_args* args)
exit:
return res;
error:
+ if(ground->props) { SBUF(ref_put(ground->props)); ground->props = NULL; }
+ darray_mtl_clear(&ground->mtls);
goto exit;
}