sgf_device.c (3867B)
1 /* Copyright (C) 2021, 2024 |Meso|Star> (contact@meso-star.com) 2 * Copyright (C) 2015-2018 EDF S.A., France (syrthes-support@edf.fr) 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 16 17 #include "sgf.h" 18 #include "sgf_device_c.h" 19 20 #include <rsys/logger.h> 21 #include <rsys/mem_allocator.h> 22 23 #include <star/s2d.h> 24 #include <star/s3d.h> 25 26 /******************************************************************************* 27 * Helper function 28 ******************************************************************************/ 29 static void 30 device_release(ref_T* ref) 31 { 32 struct sgf_device* dev; 33 ASSERT(ref); 34 dev = CONTAINER_OF(ref, struct sgf_device, ref); 35 if(dev->s2d) S2D(device_ref_put(dev->s2d)); 36 if(dev->s3d) S3D(device_ref_put(dev->s3d)); 37 MEM_RM(dev->allocator, dev); 38 } 39 40 /******************************************************************************* 41 * API functions 42 ******************************************************************************/ 43 res_T 44 sgf_device_create 45 (struct logger* log, 46 struct mem_allocator* allocator, 47 const int verbose, 48 struct sgf_device** out_dev) 49 { 50 struct mem_allocator* mem_allocator; 51 struct sgf_device* dev = NULL; 52 struct logger* logger = NULL; 53 res_T res = RES_OK; 54 55 if(!out_dev) { 56 res = RES_BAD_ARG; 57 goto error; 58 } 59 60 mem_allocator = allocator ? allocator : &mem_default_allocator; 61 logger = log ? log : LOGGER_DEFAULT; 62 63 dev = MEM_CALLOC(mem_allocator, 1, sizeof(struct sgf_device)); 64 if(!dev) { 65 if(verbose) { 66 logger_print(logger, LOG_ERROR, 67 "%s: couldn't allocate the Star-Gebhart-Factor device.\n", FUNC_NAME); 68 } 69 res = RES_MEM_ERR; 70 goto error; 71 } 72 ref_init(&dev->ref); 73 dev->allocator = mem_allocator; 74 dev->logger = logger; 75 dev->verbose = verbose; 76 77 res = s3d_device_create(NULL, dev->allocator, 0, &dev->s3d); 78 if(res != RES_OK) { 79 log_error(dev, "%s: couldn't create the Star-3D device.\n", FUNC_NAME); 80 goto error; 81 } 82 res = s2d_device_create(NULL, dev->allocator, 0, &dev->s2d); 83 if(res != RES_OK) { 84 log_error(dev, "%s: couldn't create the Star-2D device.\n", FUNC_NAME); 85 goto error; 86 } 87 88 exit: 89 if(out_dev) *out_dev = dev; 90 return res; 91 error: 92 if(dev) { 93 SGF(device_ref_put(dev)); 94 dev = NULL; 95 } 96 goto exit; 97 } 98 99 res_T 100 sgf_device_ref_get(struct sgf_device* dev) 101 { 102 if(!dev) return RES_BAD_ARG; 103 ref_get(&dev->ref); 104 return RES_OK; 105 } 106 107 res_T 108 sgf_device_ref_put(struct sgf_device* dev) 109 { 110 if(!dev) return RES_BAD_ARG; 111 ref_put(&dev->ref, device_release); 112 return RES_OK; 113 } 114 115 /******************************************************************************* 116 * Local function 117 ******************************************************************************/ 118 void 119 log_error(struct sgf_device* dev, const char* msg, ...) 120 { 121 va_list vargs; 122 res_T res; (void)res; 123 ASSERT(dev && msg); 124 125 if(!dev->verbose) return; 126 127 va_start(vargs, msg); 128 res = logger_vprint(dev->logger, LOG_ERROR, msg, vargs); 129 ASSERT(res == RES_OK); 130 va_end(vargs); 131 } 132 133 134 void 135 log_warning(struct sgf_device* dev, const char* msg, ...) 136 { 137 va_list vargs; 138 res_T res; (void)res; 139 ASSERT(dev && msg); 140 141 if(!dev->verbose) return; 142 143 va_start(vargs, msg); 144 res = logger_vprint(dev->logger, LOG_WARNING, msg, vargs); 145 ASSERT(res == RES_OK); 146 va_end(vargs); 147 }