star-buffer

Load 1D arrays in binary format
git clone git://git.meso-star.fr/star-buffer.git
Log | Files | Refs | README | LICENSE

sbuf.h (3133B)


      1 /* Copyright (C) 2022, 2023 |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 #ifndef SBUF_H
     17 #define SBUF_H
     18 
     19 #include <rsys/rsys.h>
     20 
     21 /* Library symbol management */
     22 #if defined(SBUF_SHARED_BUILD) /* Build shared library */
     23   #define SBUF_API extern EXPORT_SYM
     24 #elif defined(SBUF_STATIC) /* Use/build static library */
     25   #define SBUF_API extern LOCAL_SYM
     26 #else /* Use shared library */
     27   #define SBUF_API extern IMPORT_SYM
     28 #endif
     29 
     30 /* Helper macro that asserts if the invocation of the smsh function `Func'
     31  * returns an error. One should use this macro on smsh function calls for
     32  * which no explicit error checking is performed */
     33 #ifndef NDEBUG
     34   #define SBUF(Func) ASSERT(sbuf_ ## Func == RES_OK)
     35 #else
     36   #define SBUF(Func) sbuf_ ## Func
     37 #endif
     38 
     39 /* Forward declaration of external data types */
     40 struct logger;
     41 struct mem_allocator;
     42 
     43 struct sbuf_create_args {
     44   struct logger* logger; /* May be NULL <=> default logger */
     45   struct mem_allocator* allocator; /* NULL <=> use default allocator */
     46   int verbose; /* Verbosity level */
     47 };
     48 #define SBUF_CREATE_ARGS_DEFAULT__ {NULL, NULL, 0}
     49 static const struct sbuf_create_args SBUF_CREATE_ARGS_DEFAULT =
     50   SBUF_CREATE_ARGS_DEFAULT__;
     51 
     52 struct sbuf_desc {
     53   const void* buffer;
     54   size_t size; /* #items in the buffer */
     55   size_t szitem; /* Size of a buffer item */
     56   size_t alitem; /* Alignment of a buffer item */
     57   size_t pitch; /* #bytes between 2 consecutive items */
     58 };
     59 #define SBUF_DESC_NULL__ {NULL, 0, 0, 0, 0}
     60 static const struct sbuf_desc SBUF_DESC_NULL = SBUF_DESC_NULL__;
     61 
     62 /* Forward declaration of opaque data types */
     63 struct sbuf;
     64 
     65 BEGIN_DECLS
     66 
     67 /*******************************************************************************
     68  * Star-Buffer API
     69  ******************************************************************************/
     70 SBUF_API res_T
     71 sbuf_create
     72   (const struct sbuf_create_args* args,
     73    struct sbuf** sbuf);
     74 
     75 SBUF_API res_T
     76 sbuf_ref_get
     77   (struct sbuf* sbuf);
     78 
     79 SBUF_API res_T
     80 sbuf_ref_put
     81   (struct sbuf* sbuf);
     82 
     83 SBUF_API res_T
     84 sbuf_load
     85   (struct sbuf* sbuf,
     86    const char* path);
     87 
     88 SBUF_API res_T
     89 sbuf_load_stream
     90   (struct sbuf* sbuf,
     91    FILE* stream,
     92    const char* stream_name); /* NULL <=> use default stream name */
     93 
     94 SBUF_API res_T
     95 sbuf_get_desc
     96   (const struct sbuf* sbuf,
     97    struct sbuf_desc* desc);
     98 
     99 static INLINE const void*
    100 sbuf_desc_at
    101   (const struct sbuf_desc* desc,
    102    const size_t iitem)
    103 {
    104   ASSERT(desc && iitem < desc->size);
    105   return (char*)desc->buffer + iitem*desc->pitch;
    106 }
    107 
    108 END_DECLS
    109 
    110 #endif /* SBUF_H */