commit 54694d97b87baf7639d805f438d717055e828a7b
parent ec7420667890b3bbd03666f207564c513c3d1a8a
Author: Eduardo Fontana Lazzari <edufonlaz@gmail.com>
Date: Wed, 4 Dec 2024 10:52:27 +0100
Add memory management functions
Get, put and release functions for the sphin structure
Diffstat:
| M | src/sphin.c | | | 52 | +++++++++++++++++++++++++++++++++++++++++++++------- |
1 file changed, 45 insertions(+), 7 deletions(-)
diff --git a/src/sphin.c b/src/sphin.c
@@ -25,12 +25,12 @@
#include "sphin.h"
#include <rsys/logger.h>
#include <rsys/mem_allocator.h>
-#include <rsys/ref_count.h>
+#include <rsys/ref_count.h> /* ref_get/ref_put */
struct sphin {
/* Log */
struct logger* logger;
- int verbose;
+ int verbose;
/* Memory management */
struct mem_allocator* allocator;
@@ -50,14 +50,30 @@ check_sphin_create_args
return RES_OK;
}
+static void
+release_sphin
+ (ref_T* address)
+{
+ /* sphin = 314
+ address = 334
+ 0--------8--------12----------20-----24
+ | logger | verbose | allocator | ref |
+ +--------+---------+-----------+-----+ */
+ struct sphin* sphin = NULL;
+ ASSERT(address != NULL);
+
+ sphin = CONTAINER_OF(address, struct sphin, ref);
+ MEM_RM(sphin->allocator, sphin);
+}
+
/*******************************************************************************
* Exported functions
******************************************************************************/
res_T
-sphin_create
+sphin_create
(const struct sphin_create_args* args,
struct sphin** out_sphin)
-{
+{
struct mem_allocator* allocator = NULL;
struct sphin* sphin = NULL;
res_T res = RES_OK;
@@ -67,7 +83,7 @@ sphin_create
goto error;
}
res = check_sphin_create_args(args);
- if (res != RES_OK) goto error;
+ if (res != RES_OK) goto error;
if (args->allocator == NULL) {
allocator = &mem_default_allocator;
@@ -80,7 +96,7 @@ sphin_create
goto error;
}
ref_init(&sphin->ref);
- sphin->verbose = args->verbose;
+ sphin->verbose = args->verbose;
sphin->allocator = allocator;
if (args->logger == NULL) {
sphin->logger = LOGGER_DEFAULT;
@@ -94,8 +110,30 @@ exit:
error:
if (sphin != NULL) {
- SPHIN(ref_put(sphin));
+ SPHIN(ref_put(sphin)); /* <=> ASSERT(sphin_ref_put(sphin) == RES_OK) */
sphin = NULL;
}
goto exit;
}
+
+res_T
+sphin_ref_get
+ (struct sphin* sphin)
+{
+ if (sphin == NULL) {
+ return RES_BAD_ARG;
+ }
+ ref_get(&sphin->ref);
+ return RES_OK;
+}
+
+res_T
+sphin_ref_put
+ (struct sphin* sphin)
+{
+ if (sphin == NULL) {
+ return RES_BAD_ARG;
+ }
+ ref_put(&sphin->ref, release_sphin);
+ return RES_OK;
+}