sphin.c (3559B)
1 /* Copyright (C) 2024-2026 Centre National de la Recherche Scientifique 2 * Copyright (C) 2024-2026 Clermont Auvergne INP 3 * Copyright (C) 2024-2026 INSA Lyon 4 * Copyright (C) 2024-2026 Institut Mines Télécom Albi-Carmaux 5 * Copyright (C) 2024-2026 Institut National Polytechnique de Toulouse 6 * Copyright (C) 2024-2026 |Méso|Star> (contact@meso-star.com) 7 * Copyright (C) 2024-2026 PhotonLyX (info@photonlyx.com) 8 * Copyright (C) 2024-2026 Université de Lorraine 9 * Copyright (C) 2024-2026 Université Paul Sabatier 10 * Copyright (C) 2024-2026 Université Toulouse - Jean Jaurès 11 * 12 * This program is free software: you can redistribute it and/or modify 13 * it under the terms of the GNU General Public License as published by 14 * the Free Software Foundation, either version 3 of the License, or 15 * (at your option) any later version. 16 * 17 * This program is distributed in the hope that it will be useful, 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 * GNU General Public License for more details. 21 * 22 * You should have received a copy of the GNU General Public License 23 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 24 25 #include "sphin.h" /* !! Public declarations */ 26 #include "sphin_c.h" /* Private declarations */ 27 28 #include <rsys/logger.h> 29 #include <rsys/mem_allocator.h> 30 31 /******************************************************************************* 32 * Helper functions 33 ******************************************************************************/ 34 static res_T 35 check_sphin_create_args 36 (const struct sphin_create_args* args) 37 { 38 if (NULL == args) { 39 return RES_BAD_ARG; 40 } 41 return RES_OK; 42 } 43 44 static void 45 release_sphin 46 (ref_T* address) 47 { 48 /* sphin = 314 49 address = 334 50 0--------8--------12----------20-----24 51 | logger | verbose | allocator | ref | 52 +--------+---------+-----------+-----+ */ 53 struct sphin* sphin = NULL; 54 ASSERT(NULL != address); 55 56 sphin = CONTAINER_OF(address, struct sphin, ref); 57 MEM_RM(sphin->allocator, sphin); 58 } 59 60 /******************************************************************************* 61 * Exported functions 62 ******************************************************************************/ 63 res_T 64 sphin_create 65 (const struct sphin_create_args* args, 66 struct sphin** out_sphin) 67 { 68 struct mem_allocator* allocator = NULL; 69 struct sphin* sphin = NULL; 70 res_T res = RES_OK; 71 72 if (NULL == out_sphin) { 73 res = RES_BAD_ARG; 74 goto error; 75 } 76 res = check_sphin_create_args(args); 77 if (RES_OK != res) goto error; 78 79 if (NULL == args->allocator) { 80 allocator = &mem_default_allocator; 81 } else { 82 allocator = args->allocator; 83 } 84 sphin = MEM_CALLOC(allocator, 1, sizeof(struct sphin)); 85 if (NULL == sphin) { 86 res = RES_MEM_ERR; 87 goto error; 88 } 89 ref_init(&sphin->ref); 90 sphin->verbose = args->verbose; 91 sphin->allocator = allocator; 92 if (NULL == args->logger) { 93 sphin->logger = LOGGER_DEFAULT; 94 } else { 95 sphin->logger = args->logger; 96 } 97 98 exit: 99 if (NULL != out_sphin) *out_sphin = sphin; 100 return res; 101 102 error: 103 if (NULL != sphin) { 104 SPHIN(ref_put(sphin)); /* <=> ASSERT(sphin_ref_put(sphin) == RES_OK) */ 105 sphin = NULL; 106 } 107 goto exit; 108 } 109 110 res_T 111 sphin_ref_get 112 (struct sphin* sphin) 113 { 114 if (NULL == sphin) { 115 return RES_BAD_ARG; 116 } 117 ref_get(&sphin->ref); 118 return RES_OK; 119 } 120 121 res_T 122 sphin_ref_put 123 (struct sphin* sphin) 124 { 125 if (NULL == sphin) { 126 return RES_BAD_ARG; 127 } 128 ref_put(&sphin->ref, release_sphin); 129 return RES_OK; 130 }