commit 05ab9a77eb38b07d196f7c934df023d4c03d0e6e
parent 3b840d9b76a52ba95719d1feddb7eeec5ba95191
Author: Eduardo Fontana Lazzari <edufonlaz@gmail.com>
Date: Tue, 14 Jan 2025 10:59:43 +0100
Implement creation and destruction of sphin_config struct
Diffstat:
2 files changed, 64 insertions(+), 2 deletions(-)
diff --git a/src/sphin.h b/src/sphin.h
@@ -38,9 +38,9 @@
* returns an error. One should use this macro on sphin function calls for which
* no explicit error checking is performed */
#ifndef NDEBUG
- #define SPHIN(Func) ASSERT(sphin_ ## Func == RES_OK)
+ #define SPHIN(Func) ASSERT(sphin_##Func == RES_OK)
#else
- #define SPHIN(Func) sphin_ ## Func
+ #define SPHIN(Func) sphin_##Func
#endif
struct sphin_create_args {
@@ -87,6 +87,14 @@ sphin_load_stream
const char* stream_name,
struct sphin_config** config);
+SPHIN_API res_T
+sphin_config_ref_get
+ (struct sphin_config* config);
+
+SPHIN_API res_T
+sphin_config_ref_put
+ (struct sphin_config* config);
+
END_DECLS
#endif /* SPHIN_H */
diff --git a/src/sphin_config.c b/src/sphin_config.c
@@ -46,10 +46,44 @@ config_create
(struct sphin* sphin,
struct sphin_config** out_config)
{
+ struct sphin_config* config = NULL;
res_T res = RES_OK;
ASSERT(NULL != sphin);
ASSERT(NULL != out_config);
+
+ config = MEM_CALLOC(sphin->allocator, 1, sizeof(struct sphin_config));
+ if (NULL == config) {
+ res = RES_MEM_ERR;
+ goto error;
+ }
+ ref_init(&config->ref);
+ SPHIN(ref_get(sphin));
+ config->sphin = sphin;
+
+exit:
+ *out_config = config;
+ return res;
+error:
+ if (NULL != config) {
+ SPHIN(config_ref_put(config)); /* <=> ASSERT(sphin_config_ref_put(config) == RES_OK) */
+ config = NULL;
+ }
+ goto exit;
+}
+
+static void
+release_config(ref_T* address)
+{
+ struct sphin_config* config = NULL;
+ struct sphin* sphin = NULL;
+
+ ASSERT(NULL != address);
+
+ config = CONTAINER_OF(address, struct sphin_config, ref);
+ sphin = config->sphin;
+ MEM_RM(sphin->allocator, config);
+ SPHIN(ref_put(sphin));
}
static res_T
@@ -132,3 +166,23 @@ sphin_load_stream
}
return load_stream(sphin, stream, streamname, config);
}
+
+res_T
+sphin_config_ref_get(struct sphin_config* config)
+{
+ if(NULL == config) {
+ return RES_BAD_ARG;
+ }
+ ref_get(&config->ref);
+ return RES_OK;
+}
+
+res_T
+sphin_config_ref_put(struct sphin_config* config)
+{
+ if(NULL == config) {
+ return RES_BAD_ARG;
+ }
+ ref_put(&config->ref, release_config);
+ return RES_OK;
+}