star-buffer

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

commit 7a2d89cb161bae7d05bc96139bc77f69a1aa77ec
parent 53e8129e6ae73e0041e6e6953f1255facf0b9847
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Wed, 30 Mar 2022 12:29:17 +0200

Refactoring and variable renaming

Diffstat:
Mdoc/sbuf.5.scd | 9++++-----
Msrc/sbuf.c | 48++++++++++++++++++++++++++++--------------------
Msrc/sbuf.h | 14+++++++-------
Msrc/sbuf_c.h | 8++++----
4 files changed, 43 insertions(+), 36 deletions(-)

diff --git a/doc/sbuf.5.scd b/doc/sbuf.5.scd @@ -25,15 +25,15 @@ Data are encoded with respect to the little endian bytes ordering, i.e. least significant bytes are stored first. ``` -<sbuf> ::= <pagesize> <count> <szelmt> <alelmt> +<sbuf> ::= <pagesize> <size> <szitem> <alitem> <padding> <list> <padding> <pagesize> ::= UINT64 -<count> ::= UINT64 -<szelmt> ::= UINT64 -<alelmt> ::= UINT64 # less than <pagesize> +<size> ::= UINT64 # Number of items stored +<szitem> ::= UINT64 # Size of an item in bytes +<alitem> ::= UINT64 # Alignment of an item (must be <= pagesize) --- @@ -44,4 +44,3 @@ significant bytes are stored first. <padding> ::= [ BYTE ... ] # Ensure alignement ``` - diff --git a/src/sbuf.c b/src/sbuf.c @@ -42,9 +42,9 @@ reset_sbuf(struct sbuf* sbuf) { ASSERT(sbuf); sbuf->pagesize = 0; - sbuf->count = 0; - sbuf->szelmt = 0; - sbuf->alelmt = 0; + sbuf->size = 0; + sbuf->szitem = 0; + sbuf->alitem = 0; if(sbuf->buffer && sbuf->buffer != MAP_FAILED) munmap(sbuf->buffer, sbuf->map_len); sbuf->buffer = NULL; @@ -108,9 +108,9 @@ load_stream(struct sbuf* sbuf, FILE* stream, const char* stream_name) } \ } (void)0 READ(&sbuf->pagesize, 1, "page size"); - READ(&sbuf->count, 1, "number of elements"); - READ(&sbuf->szelmt, 1, "size of an element"); - READ(&sbuf->alelmt, 1, "alignment of an element"); + READ(&sbuf->size, 1, "number of items"); + READ(&sbuf->szitem, 1, "size of an item"); + READ(&sbuf->alitem, 1, "alignment of an item"); #undef READ if(!IS_ALIGNED(sbuf->pagesize, sbuf->pagesize_os)) { @@ -121,34 +121,42 @@ load_stream(struct sbuf* sbuf, FILE* stream, const char* stream_name) res = RES_BAD_ARG; goto error; } - if(!sbuf->szelmt) { + if(!sbuf->size) { log_err(sbuf, - "%s: invalid element size `%lu'.\n", - stream_name, (unsigned long)sbuf->szelmt); + "%s: invalid buffer size %lu. " + "The number of items stored cannot be zero.\n", + stream_name, sbuf->size); res = RES_BAD_ARG; goto error; } - if(!IS_POW2(sbuf->alelmt)) { + if(!sbuf->szitem) { log_err(sbuf, - "%s: invalid element alignment `%lu'. It must be a power of 2.\n", - stream_name, (unsigned long)sbuf->alelmt); + "%s: invalid item size `%lu'.\n", + stream_name, (unsigned long)sbuf->szitem); res = RES_BAD_ARG; goto error; } - if(sbuf->alelmt > sbuf->pagesize) { + if(!IS_POW2(sbuf->alitem)) { log_err(sbuf, - "%s: invalid element alignment `%lu'. " + "%s: invalid item alignment `%lu'. It must be a power of 2.\n", + stream_name, (unsigned long)sbuf->alitem); + res = RES_BAD_ARG; + goto error; + } + if(sbuf->alitem > sbuf->pagesize) { + log_err(sbuf, + "%s: invalid item alignment `%lu'. " "It must be less than the provided pagesize `%lu'.\n", - stream_name, (unsigned long)sbuf->alelmt, (unsigned long)sbuf->pagesize); + stream_name, (unsigned long)sbuf->alitem, (unsigned long)sbuf->pagesize); res = RES_BAD_ARG; goto error; } - sbuf->pitch = ALIGN_SIZE(sbuf->szelmt, sbuf->alelmt); + sbuf->pitch = ALIGN_SIZE(sbuf->szitem, sbuf->alitem); /* Compute the length in bytes of the data to map */ - sbuf->map_len = sbuf->count * sbuf->pitch; + sbuf->map_len = sbuf->size * sbuf->pitch; sbuf->map_len = ALIGN_SIZE(sbuf->map_len, (size_t)sbuf->pagesize); /* Find the offsets of the data into the stream */ @@ -291,9 +299,9 @@ sbuf_get_desc(const struct sbuf* sbuf, struct sbuf_desc* desc) { if(!sbuf || !desc) return RES_BAD_ARG; desc->buffer = sbuf->buffer; - desc->count = sbuf->count; - desc->szelmt = (size_t)sbuf->szelmt; - desc->alelmt = (size_t)sbuf->alelmt; + desc->size = sbuf->size; + desc->szitem = (size_t)sbuf->szitem; + desc->alitem = (size_t)sbuf->alitem; desc->pitch = (size_t)sbuf->pitch; return RES_OK; } diff --git a/src/sbuf.h b/src/sbuf.h @@ -51,10 +51,10 @@ static const struct sbuf_create_args SBUF_CREATE_ARGS_DEFAULT = struct sbuf_desc { const void* buffer; - size_t count; /* #elements in the buffer */ - size_t szelmt; /* Size of a buffer element */ - size_t alelmt; /* Alignment of a buffer element */ - size_t pitch; /* #bytes between 2 consecutive elements */ + size_t size; /* #items in the buffer */ + size_t szitem; /* Size of a buffer item */ + size_t alitem; /* Alignment of a buffer item */ + size_t pitch; /* #bytes between 2 consecutive items */ }; #define SBUF_DESC_NULL__ {NULL, 0, 0, 0, 0} static const struct sbuf_desc SBUF_DESC_NULL = SBUF_DESC_NULL__; @@ -99,10 +99,10 @@ sbuf_get_desc static INLINE const void* sbuf_desc_at (const struct sbuf_desc* desc, - const size_t ielement) + const size_t iitem) { - ASSERT(desc && ielement < desc->count); - return (char*)desc->buffer + ielement*desc->pitch; + ASSERT(desc && iitem < desc->size); + return (char*)desc->buffer + iitem*desc->pitch; } END_DECLS diff --git a/src/sbuf_c.h b/src/sbuf_c.h @@ -23,10 +23,10 @@ struct mem_allocator; struct sbuf { uint64_t pagesize; - uint64_t count; - uint64_t szelmt; /* Size of an element */ - uint64_t alelmt; /* Alignment of an element */ - uint64_t pitch; /* #bytes between 2 consecutive elements */ + uint64_t size; /* #items */ + uint64_t szitem; /* Size of an item in bytes */ + uint64_t alitem; /* Alignment of an item in bytes */ + uint64_t pitch; /* #bytes between 2 consecutive items */ void* buffer; size_t map_len;