sln_device.c (2938B)
1 /* Copyright (C) 2022, 2026 |Méso|Star> (contact@meso-star.com) 2 * Copyright (C) 2026 Université de Lorraine 3 * Copyright (C) 2022 Centre National de la Recherche Scientifique 4 * Copyright (C) 2022 Université Paul Sabatier 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 18 19 #include "sln.h" 20 #include "sln_device_c.h" 21 22 /******************************************************************************* 23 * Helper functions 24 ******************************************************************************/ 25 static INLINE res_T 26 check_sln_device_create_args(const struct sln_device_create_args* args) 27 { 28 return args ? RES_OK : RES_BAD_ARG; 29 } 30 31 static void 32 release_sln_device(ref_T* ref) 33 { 34 struct sln_device* sln = CONTAINER_OF(ref, struct sln_device, ref); 35 ASSERT(ref); 36 if(sln->logger == &sln->logger__) logger_release(&sln->logger__); 37 MEM_RM(sln->allocator, sln); 38 } 39 40 /******************************************************************************* 41 * Exported functions 42 ******************************************************************************/ 43 res_T 44 sln_device_create 45 (const struct sln_device_create_args* args, 46 struct sln_device** out_sln) 47 { 48 struct mem_allocator* allocator = NULL; 49 struct sln_device* sln = NULL; 50 res_T res = RES_OK; 51 52 if(!out_sln) { res = RES_BAD_ARG; goto error; } 53 res = check_sln_device_create_args(args); 54 if(res != RES_OK) goto error; 55 56 allocator = args->allocator ? args->allocator : &mem_default_allocator; 57 sln = MEM_CALLOC(allocator, 1, sizeof(*sln)); 58 if(!sln) { 59 #define ERR_STR "Could not allocate the Star-Line data structure.\n" 60 if(args->logger) { 61 logger_print(args->logger, LOG_ERROR, ERR_STR); 62 } else { 63 fprintf(stderr, MSG_ERROR_PREFIX ERR_STR); 64 } 65 #undef ERR_STR 66 res = RES_MEM_ERR; 67 goto error; 68 } 69 ref_init(&sln->ref); 70 sln->allocator = allocator; 71 sln->verbose = args->verbose; 72 sln->logger = args->logger ? args->logger : LOGGER_DEFAULT; 73 74 exit: 75 if(out_sln) *out_sln = sln; 76 return res; 77 error: 78 if(sln) { SLN(device_ref_put(sln)); sln = NULL; } 79 goto exit; 80 } 81 82 res_T 83 sln_device_ref_get(struct sln_device* sln) 84 { 85 if(!sln) return RES_BAD_ARG; 86 ref_get(&sln->ref); 87 return RES_OK; 88 } 89 90 res_T 91 sln_device_ref_put(struct sln_device* sln) 92 { 93 if(!sln) return RES_BAD_ARG; 94 ref_put(&sln->ref, release_sln_device); 95 return RES_OK; 96 }