star-line

Structure for accelerating line importance sampling
git clone git://git.meso-star.com/star-line.git
Log | Files | Refs | README | LICENSE

sln_device.c (2976B)


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