stardis-solver

Solve coupled heat transfers
git clone git://git.meso-star.fr/stardis-solver.git
Log | Files | Refs | README | LICENSE

sdis_data.c (3270B)


      1 /* Copyright (C) 2016-2025 |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 "sdis.h"
     17 #include "sdis_device_c.h"
     18 #include "sdis_log.h"
     19 
     20 #include <rsys/math.h>
     21 #include <rsys/mem_allocator.h>
     22 
     23 struct sdis_data {
     24   void* mem; /* Raw memory where user data are stored */
     25   void (*release)(void*); /* Function to invoke priorly to `mem' destruction */
     26 
     27   ref_T ref;
     28   struct sdis_device* dev;
     29 };
     30 
     31 /*******************************************************************************
     32  * Helper functions
     33  ******************************************************************************/
     34 static void
     35 data_release(ref_T* ref)
     36 {
     37   struct sdis_data* data = NULL;
     38   struct sdis_device* dev = NULL;
     39   ASSERT(ref);
     40   data = CONTAINER_OF(ref, struct sdis_data, ref);
     41   dev = data->dev;
     42   if(data->release) data->release(data->mem);
     43   if(data->mem) MEM_RM(dev->allocator, data->mem);
     44   MEM_RM(dev->allocator, data);
     45   SDIS(device_ref_put(dev));
     46 }
     47 
     48 /*******************************************************************************
     49  * Exported functions
     50  ******************************************************************************/
     51 res_T
     52 sdis_data_create
     53   (struct sdis_device* dev,
     54    const size_t size,
     55    const size_t align,
     56    void (*release)(void*),
     57    struct sdis_data** out_data)
     58 {
     59   struct sdis_data* data = NULL;
     60   res_T res = RES_OK;
     61 
     62   if(!dev || !size || !IS_POW2(align) || !out_data) {
     63     res = RES_BAD_ARG;
     64     goto error;
     65   }
     66 
     67   data = MEM_CALLOC(dev->allocator, 1, sizeof(struct sdis_data));
     68   if(!data) {
     69     log_err(dev, "%s: could not allocate the Stardis data.\n", FUNC_NAME);
     70     res = RES_MEM_ERR;
     71     goto error;
     72   }
     73   ref_init(&data->ref);
     74   SDIS(device_ref_get(dev));
     75   data->dev = dev;
     76   data->release = release;
     77 
     78   data->mem = MEM_ALLOC_ALIGNED(dev->allocator, size, align);
     79   if(!data->mem) {
     80     log_err(dev, "%s: could not allocate the memory of the Stardis data. "
     81       "Size: %lu; alignment: %lu\n.", FUNC_NAME, size, align);
     82     res = RES_MEM_ERR;
     83     goto error;
     84   }
     85 
     86 exit:
     87   if(out_data) *out_data = data;
     88   return res;
     89 
     90 error:
     91   if(data) {
     92     SDIS(data_ref_put(data));
     93     data = NULL;
     94   }
     95   goto exit;
     96 }
     97 
     98 res_T
     99 sdis_data_ref_get(struct sdis_data* data)
    100 {
    101   if(!data) return RES_BAD_ARG;
    102   ref_get(&data->ref);
    103   return RES_OK;
    104 }
    105 
    106 res_T
    107 sdis_data_ref_put(struct sdis_data* data)
    108 {
    109   if(!data) return RES_BAD_ARG;
    110   ref_put(&data->ref, data_release);
    111   return RES_OK;
    112 }
    113 
    114 void*
    115 sdis_data_get(struct sdis_data* data)
    116 {
    117   if(!data) FATAL("invalid argument.\n");
    118   return data->mem;
    119 }
    120 
    121 const void*
    122 sdis_data_cget(const struct sdis_data* data)
    123 {
    124   if(!data) FATAL("invalid argument.\n");
    125   return data->mem;
    126 }
    127