commit ec7420667890b3bbd03666f207564c513c3d1a8a
parent 682f6666f149870494bb17ff008624e307ebb6c1
Author: Eduardo Fontana Lazzari <edufonlaz@gmail.com>
Date: Tue, 3 Dec 2024 13:45:34 +0100
Implementation of the sphin_create function
This function is responsible to build the sphin structure based on input
args. This structure contains information such as the logger, the
verbosity level and the memory allocator.
Diffstat:
| A | src/sphin.c | | | 101 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 101 insertions(+), 0 deletions(-)
diff --git a/src/sphin.c b/src/sphin.c
@@ -0,0 +1,101 @@
+/* Copyright (C) 2024 Centre National de la Recherche Scientifique
+ * Copyright (C) 2024 Clermont Auvergne INP
+ * Copyright (C) 2024 INSA Lyon
+ * Copyright (C) 2024 Institut Mines Télécom Albi-Carmaux
+ * Copyright (C) 2024 Institut National Polytechnique de Toulouse
+ * Copyright (C) 2024 |Méso|Star> (contact@meso-star.com)
+ * Copyright (C) 2024 PhotonLyX (info@photonlyx.com)
+ * Copyright (C) 2024 Université de Lorraine
+ * Copyright (C) 2024 Université Paul Sabatier
+ * Copyright (C) 2024 Université Toulouse - Jean Jaurès
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include "sphin.h"
+#include <rsys/logger.h>
+#include <rsys/mem_allocator.h>
+#include <rsys/ref_count.h>
+
+struct sphin {
+ /* Log */
+ struct logger* logger;
+ int verbose;
+
+ /* Memory management */
+ struct mem_allocator* allocator;
+ ref_T ref;
+};
+
+/*******************************************************************************
+ * Helper functions
+ ******************************************************************************/
+static res_T
+check_sphin_create_args
+ (const struct sphin_create_args* args)
+{
+ if (args == NULL) {
+ return RES_BAD_ARG;
+ }
+ return RES_OK;
+}
+
+/*******************************************************************************
+ * Exported functions
+ ******************************************************************************/
+res_T
+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;
+
+ if (out_sphin == NULL) {
+ res = RES_BAD_ARG;
+ goto error;
+ }
+ res = check_sphin_create_args(args);
+ if (res != RES_OK) goto error;
+
+ if (args->allocator == NULL) {
+ allocator = &mem_default_allocator;
+ } else {
+ allocator = args->allocator;
+ }
+ sphin = MEM_CALLOC(allocator, 1, sizeof(struct sphin));
+ if (sphin == NULL) {
+ res = RES_MEM_ERR;
+ goto error;
+ }
+ ref_init(&sphin->ref);
+ sphin->verbose = args->verbose;
+ sphin->allocator = allocator;
+ if (args->logger == NULL) {
+ sphin->logger = LOGGER_DEFAULT;
+ } else {
+ sphin->logger = args->logger;
+ }
+
+exit:
+ if (out_sphin != NULL) *out_sphin = sphin;
+ return res;
+
+error:
+ if (sphin != NULL) {
+ SPHIN(ref_put(sphin));
+ sphin = NULL;
+ }
+ goto exit;
+}