star-geometry-3d

Clean and decorate 3D geometries
git clone git://git.meso-star.fr/star-geometry-3d.git
Log | Files | Refs | README | LICENSE

sg3d_device.c (3557B)


      1 /* Copyright (C) 2019, 2020, 2023, 2024 |Méso|Star> (contact@meso-star.com)
      2  *
      3  * This program is free software: you can redistribute it and/or modify
      4  * it under the terms of the GNU General Public License as published by
      5  * the Free Software Foundation, either version 3 of the License, or
      6  * (at your option) any later version.
      7  *
      8  * This program is distributed in the hope that it will be useful,
      9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     11  * GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License
     14  * along with this program. If not, see <http://www.gnu.org/licenses/>. */
     15 
     16 #include "sg3d.h"
     17 #include "sg3d_device.h"
     18 
     19 #include <rsys/logger.h>
     20 #include <rsys/mem_allocator.h>
     21 
     22 #include <stdarg.h>
     23 
     24 /******************************************************************************
     25  * Helper functions
     26  *****************************************************************************/
     27 static void
     28 log_msg
     29   (struct sg3d_device* dev,
     30    const enum log_type stream,
     31    const char* msg,
     32    va_list vargs)
     33 {
     34   ASSERT(dev && msg);
     35   if(dev->verbose) {
     36     res_T res; (void)res;
     37     res = logger_vprint(dev->logger, stream, msg, vargs);
     38     ASSERT(res == RES_OK);
     39   }
     40 }
     41 
     42 static void
     43 device_release(ref_T* ref)
     44 {
     45   struct sg3d_device* dev;
     46   ASSERT(ref);
     47   dev = CONTAINER_OF(ref, struct sg3d_device, ref);
     48   MEM_RM(dev->allocator, dev);
     49 }
     50 
     51 /******************************************************************************
     52  * Exported functions
     53  *****************************************************************************/
     54 res_T
     55 sg3d_device_create
     56   (struct logger* logger,
     57    struct mem_allocator* mem_allocator,
     58    const int verbose,
     59    struct sg3d_device** out_dev)
     60 {
     61   struct logger* log = NULL;
     62   struct sg3d_device* dev = NULL;
     63   struct mem_allocator* allocator = NULL;
     64   res_T res = RES_OK;
     65 
     66   if(!out_dev) {
     67     res = RES_BAD_ARG;
     68     goto error;
     69   }
     70 
     71   log = logger ? logger : LOGGER_DEFAULT;
     72   allocator = mem_allocator ? mem_allocator : &mem_default_allocator;
     73   dev = MEM_CALLOC(allocator, 1, sizeof(struct sg3d_device));
     74   if(!dev) {
     75     if(verbose) {
     76       /* Do not use helper log functions since dev is not initialised */
     77       CHK(logger_print(log, LOG_ERROR,
     78         "%s: could not allocate the star-geometry-3d device.\n", FUNC_NAME)
     79         == RES_OK);
     80     }
     81     res = RES_MEM_ERR;
     82     goto error;
     83   }
     84   dev->logger = log;
     85   dev->allocator = allocator;
     86   dev->verbose = verbose;
     87   ref_init(&dev->ref);
     88 
     89 exit:
     90   if(out_dev) *out_dev = dev;
     91   return res;
     92 error:
     93   if(dev) {
     94     SG3D(device_ref_put(dev));
     95     dev = NULL;
     96   }
     97   goto exit;
     98 }
     99 
    100 res_T
    101 sg3d_device_ref_get(struct sg3d_device* dev)
    102 {
    103   if(!dev) return RES_BAD_ARG;
    104   ref_get(&dev->ref);
    105   return RES_OK;
    106 }
    107 
    108 res_T
    109 sg3d_device_ref_put(struct sg3d_device* dev)
    110 {
    111   if(!dev) return RES_BAD_ARG;
    112   ref_put(&dev->ref, device_release);
    113   return RES_OK;
    114 }
    115 
    116 /******************************************************************************
    117  * Local functions
    118  *****************************************************************************/
    119 void
    120 log_err(struct sg3d_device* dev, const char* msg, ...)
    121 {
    122   va_list vargs_list;
    123   ASSERT(dev && msg);
    124 
    125   va_start(vargs_list, msg);
    126   log_msg(dev, LOG_ERROR, msg, vargs_list);
    127   va_end(vargs_list);
    128 }
    129 
    130 void
    131 log_warn(struct sg3d_device* dev, const char* msg, ...)
    132 {
    133   va_list vargs_list;
    134   ASSERT(dev && msg);
    135 
    136   va_start(vargs_list, msg);
    137   log_msg(dev, LOG_WARNING, msg, vargs_list);
    138   va_end(vargs_list);
    139 }