star-vx

Structuring voxels for ray-tracing
git clone git://git.meso-star.fr/star-vx.git
Log | Files | Refs | README | LICENSE

svx_device.c (3143B)


      1 /* Copyright (C) 2018, 2020-2025 |Méso|Star> (contact@meso-star.com)
      2  * Copyright (C) 2018 Université Paul Sabatier
      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 "svx.h"
     18 #include "svx_device.h"
     19 
     20 #include <rsys/logger.h>
     21 #include <rsys/mem_allocator.h>
     22 
     23 /*******************************************************************************
     24  * Helper functions
     25  ******************************************************************************/
     26 static void
     27 log_msg
     28   (const struct svx_device* dev,
     29    const enum log_type stream,
     30    const char* msg,
     31    va_list vargs)
     32 {
     33   ASSERT(dev && msg);
     34   if(dev->verbose) {
     35     res_T res; (void)res;
     36     res = logger_vprint(dev->logger, stream, msg, vargs);
     37     ASSERT(res == RES_OK);
     38   }
     39 }
     40 
     41 static void
     42 device_release(ref_T* ref)
     43 {
     44   struct svx_device* dev;
     45   ASSERT(ref);
     46   dev = CONTAINER_OF(ref, struct svx_device, ref);
     47   MEM_RM(dev->allocator, dev);
     48 }
     49 
     50 /*******************************************************************************
     51  * Exported functions
     52  ******************************************************************************/
     53 res_T
     54 svx_device_create
     55   (struct logger* log,
     56    struct mem_allocator* mem_allocator,
     57    const int verbose,
     58    struct svx_device** out_dev)
     59 {
     60   struct svx_device* dev = NULL;
     61   struct mem_allocator* allocator = NULL;
     62   struct logger* logger = NULL;
     63   res_T res = RES_OK;
     64 
     65   if(!out_dev) {
     66     res = RES_BAD_ARG;
     67     goto error;
     68   }
     69 
     70   allocator = mem_allocator ? mem_allocator : &mem_default_allocator;
     71   logger = log ? log : LOGGER_DEFAULT;
     72 
     73   dev = MEM_CALLOC(allocator, 1, sizeof(struct svx_device));
     74   if(!dev) {
     75     res = RES_MEM_ERR;
     76     goto error;
     77   }
     78 
     79 
     80   ref_init(&dev->ref);
     81   dev->allocator = allocator;
     82   dev->logger = logger;
     83   dev->verbose = verbose;
     84 
     85 exit:
     86   if(out_dev) *out_dev = dev;
     87   return res;
     88 error:
     89   if(dev) {
     90     SVX(device_ref_put(dev));
     91     dev = NULL;
     92   }
     93   goto exit;
     94 }
     95 
     96 res_T
     97 svx_device_ref_get(struct svx_device* dev)
     98 {
     99   if(!dev) return RES_BAD_ARG;
    100   ref_get(&dev->ref);
    101   return RES_OK;
    102 }
    103 
    104 res_T
    105 svx_device_ref_put(struct svx_device* dev)
    106 {
    107   if(!dev) return RES_BAD_ARG;
    108   ref_put(&dev->ref, device_release);
    109   return RES_OK;
    110 }
    111 
    112 /*******************************************************************************
    113  * Local function
    114  ******************************************************************************/
    115 void
    116 log_err(const struct svx_device* dev, const char* msg, ...)
    117 {
    118   va_list vargs_list;
    119   ASSERT(dev && msg);
    120 
    121   va_start(vargs_list, msg);
    122   log_msg(dev, LOG_ERROR, msg, vargs_list);
    123   va_end(vargs_list);
    124 }
    125