star-3d

Surface structuring for efficient 3D geometric queries
git clone git://git.meso-star.com/star-3d.git
Log | Files | Refs | README | LICENSE

s3d_buffer.h (2898B)


      1 /* Copyright (C) 2015-2023, 2026 |Méso|Star> (contact@meso-star.com)
      2  *
      3  * This file is part of Star-3D.
      4  *
      5  * Star-3D is free software: you can redistribute it and/or modify
      6  * it under the terms of the GNU General Public License as published by
      7  * the Free Software Foundation, either version 3 of the License, or
      8  * (at your option) any later version.
      9  *
     10  * Star-3D is distributed in the hope that it will be useful,
     11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     13  * GNU General Public License for more details.
     14  *
     15  * You should have received a copy of the GNU General Public License
     16  * along with Star-3D. If not, see <http://www.gnu.org/licenses/>. */
     17 
     18 #if !defined(BUFFER_NAME) && !defined(BUFFER_DARRAY)
     19 
     20 #ifndef S3D_BUFFER_H
     21 #define S3D_BUFFER_H
     22 
     23 #include <rsys/mem_allocator.h>
     24 #include <rsys/ref_count.h>
     25 
     26 #endif /* S3D_BUFFER_H */
     27 #else
     28 /*
     29  * Generate the buffer type with respect to the following macros:
     30  *  - BUFFER_NAME: name of the structure and prefix of the functions;
     31  *  - BUFFER_DARRAY: type of the dynamic array of the buffer;
     32  */
     33 #if !defined(BUFFER_NAME) || !defined(BUFFER_DARRAY)
     34   #error "Missing macro definition"
     35 #endif
     36 
     37 #define BUFFER_FUNC__(Func) CONCAT(CONCAT(BUFFER_NAME, _), Func)
     38 #define BUFFER_DARRAY_FUNC__(Func) CONCAT(CONCAT(BUFFER_DARRAY, _), Func)
     39 
     40 struct BUFFER_NAME {
     41   struct BUFFER_DARRAY data;
     42   struct mem_allocator* allocator;
     43   ref_T ref;
     44 };
     45 
     46 /*******************************************************************************
     47  * Helper function
     48  ******************************************************************************/
     49 static INLINE void
     50 BUFFER_FUNC__(release__)(ref_T* ref)
     51 {
     52   struct BUFFER_NAME* buffer;
     53   ASSERT(ref);
     54   buffer = CONTAINER_OF(ref, struct BUFFER_NAME, ref);
     55   BUFFER_DARRAY_FUNC__(release)(&buffer->data);
     56   MEM_RM(buffer->allocator, buffer);
     57 }
     58 
     59 /*******************************************************************************
     60  * Buffer function
     61  ******************************************************************************/
     62 static INLINE res_T
     63 BUFFER_FUNC__(create)
     64   (struct mem_allocator* allocator,
     65    struct BUFFER_NAME** out_buffer)
     66 {
     67   struct BUFFER_NAME* buffer;
     68   ASSERT(allocator && out_buffer);
     69 
     70   buffer = (struct BUFFER_NAME*)MEM_CALLOC
     71     (allocator, 1, sizeof(struct BUFFER_NAME));
     72   if(!buffer) return RES_MEM_ERR;
     73   BUFFER_DARRAY_FUNC__(init)(allocator, &buffer->data);
     74   buffer->allocator = allocator;
     75   ref_init(&buffer->ref);
     76   *out_buffer = buffer;
     77   return RES_OK;
     78 }
     79 
     80 static INLINE void
     81 BUFFER_FUNC__(ref_get)(struct BUFFER_NAME* buffer)
     82 {
     83   ASSERT(buffer);
     84   ref_get(&buffer->ref);
     85 }
     86 
     87 static INLINE void
     88 BUFFER_FUNC__(ref_put)(struct BUFFER_NAME* buffer)
     89 {
     90   ASSERT(buffer);
     91   ref_put(&buffer->ref, BUFFER_FUNC__(release__));
     92 }
     93 
     94 #undef BUFFER_NAME
     95 #undef BUFFER_DARRAY
     96 
     97 #endif /* !BUFFER_NAME || !BUFFER_DARRAY */
     98