commit 37ca5a5af6a6066ba12d98e9a8143cea10accd33
parent 73c95722e081d37cc85ac1bc065a7dffe5589951
Author: vaplv <vaplv@free.fr>
Date: Sat, 13 May 2017 14:59:03 +0200
Make dynamic the size of the big buffer buffered data
Diffstat:
2 files changed, 15 insertions(+), 20 deletions(-)
diff --git a/src/big_buffer.h b/src/big_buffer.h
@@ -40,7 +40,6 @@ enum bigbuf_flag {
* - BIGBUF_ALIGNMENT: optional alignment of the buffer that stores the in
* buffered data of the big buffer. By default use the maximum between the
* default alignment of the BIGBUF_DATA and 16 bytes;
- * - BIGBUIF_BUFSIZE: size in bytes of the buffered data. By default only one
* BIGBUF_DATA is buffered
*
* The name of the generated type is: struct bigbuf_<BIGBUF_NAME> and the
@@ -56,12 +55,6 @@ enum bigbuf_flag {
#define BIGBUF__ CONCAT(bigbuf_, BIGBUF_NAME)
#define BIGBUF_FUNC__(Func) CONCAT(CONCAT(CONCAT(bigbuf_, BIGBUF_NAME),_), Func)
-#ifndef BIGBUF_BUFSIZE
- #define BIGBUF_BUFSIZE 1
-#else
- STATIC_ASSERT(BIGBUF_BUFSIZE > 0, BIGBUF_BUFSIZE_must_be_greater_than_0);
-#endif
-
#ifndef BIGBUF_ALIGNMENT
#define BIGBUF_ALIGNMENT__ MMAX(ALIGNOF(BIGBUF_DATA), 16)
#else
@@ -183,6 +176,7 @@ BIGBUF_FUNC__(release)(struct BIGBUF__* bigbuf)
static INLINE res_T
BIGBUF_FUNC__(init)
(struct mem_allocator* allocator, /* May be NULL <=> use default allocator */
+ const size_t bufsz, /* size in bytes of the buffered data */
FILE* stream, /* May be NULL <=> internally manage the stream */
int mask, /* Combination of bigbuf_flag */
struct BIGBUF__* bigbuf)
@@ -196,9 +190,13 @@ BIGBUF_FUNC__(init)
res = RES_BAD_ARG;
goto error;
}
+ if(!bufsz) {
+ res = RES_BAD_ARG;
+ goto error;
+ }
bigbuf->allocator = allocator ? allocator : &mem_default_allocator;
- bigbuf->buf.capacity = BIGBUF_BUFSIZE / sizeof(BIGBUF_DATA);
+ bigbuf->buf.capacity = bufsz / sizeof(BIGBUF_DATA);
bigbuf->buf.capacity = MMAX(bigbuf->buf.capacity, 1);
bufsz_adjusted = bigbuf->buf.capacity * sizeof(BIGBUF_DATA);
@@ -347,7 +345,6 @@ error:
}
#undef BIGBUF_ALIGNMENT
-#undef BIGBUF_BUFSIZE
#undef BIGBUF_DATA
#undef BIGBUF_NAME
diff --git a/src/test_big_buffer.c b/src/test_big_buffer.c
@@ -20,18 +20,15 @@
#define BIGBUF_NAME byte
#define BIGBUF_DATA char
-#define BIGBUF_BUFSIZE 4
#include "big_buffer.h"
#define BIGBUF_NAME integer
#define BIGBUF_DATA int
#define BIGBUF_ALIGNMENT 64
-#define BIGBUF_BUFSIZE 1
#include "big_buffer.h"
#define BIGBUF_NAME real
#define BIGBUF_DATA double
-#define BIGBUF_BUFSIZE 45
#include "big_buffer.h"
static INLINE double
@@ -47,8 +44,9 @@ test_byte(struct mem_allocator* allocator)
size_t i;
char byte;
- CHECK(bigbuf_byte_init(allocator, NULL, BIGBUF_LOAD_STREAM, &bytes), RES_BAD_ARG);
- CHECK(bigbuf_byte_init(allocator, NULL, 0, &bytes), RES_OK);
+ CHECK(bigbuf_byte_init(allocator, 4, NULL, BIGBUF_LOAD_STREAM, &bytes), RES_BAD_ARG);
+ CHECK(bigbuf_byte_init(allocator, 0, NULL, 0, &bytes), RES_BAD_ARG);
+ CHECK(bigbuf_byte_init(allocator, 4, NULL, 0, &bytes), RES_OK);
CHECK(bigbuf_byte_size_get(&bytes), 0);
FOR_EACH(i, 0, 32) {
@@ -94,7 +92,7 @@ test_integer(struct mem_allocator* allocator)
NCHECK(stream = tmpfile(), NULL);
- CHECK(bigbuf_integer_init(NULL, stream, 0, &ints), RES_OK);
+ CHECK(bigbuf_integer_init(NULL, 1, stream, 0, &ints), RES_OK);
FOR_EACH(i, 0, 999) {
integer = (int)i;
@@ -121,7 +119,7 @@ test_integer(struct mem_allocator* allocator)
bigbuf_integer_release(&ints);
rewind(stream);
- CHECK(bigbuf_integer_init(allocator, stream, BIGBUF_LOAD_STREAM, &ints), RES_OK);
+ CHECK(bigbuf_integer_init(allocator, 1, stream, BIGBUF_LOAD_STREAM, &ints), RES_OK);
CHECK(bigbuf_integer_size_get(&ints), 1665);
FOR_EACH(i, 0, 666) {
@@ -138,7 +136,7 @@ test_integer(struct mem_allocator* allocator)
bigbuf_integer_release(&ints);
rewind(stream);
- CHECK(bigbuf_integer_init(NULL, stream, BIGBUF_LOAD_STREAM, &ints2), RES_OK);
+ CHECK(bigbuf_integer_init(NULL, 1, stream, BIGBUF_LOAD_STREAM, &ints2), RES_OK);
CHECK(bigbuf_integer_size_get(&ints2), bigbuf_integer_size_get(&ints));
CHECK(bigbuf_integer_size_get(&ints2), 2689);
@@ -161,8 +159,8 @@ test_real(struct mem_allocator* allocator)
size_t i;
NCHECK(stream = tmpfile(), NULL);
- CHECK(bigbuf_real_init(allocator, stream, BIGBUF_LOAD_STREAM, &reals), RES_IO_ERR);
- CHECK(bigbuf_real_init(allocator, stream, 0, &reals), RES_OK);
+ CHECK(bigbuf_real_init(allocator, 45, stream, BIGBUF_LOAD_STREAM, &reals), RES_IO_ERR);
+ CHECK(bigbuf_real_init(allocator, 45, stream, 0, &reals), RES_OK);
FOR_EACH(i, 0, 4000) {
real = (double)i / 100.0;
@@ -187,7 +185,7 @@ test_real(struct mem_allocator* allocator)
bigbuf_real_flush(&reals);
rewind(stream);
- bigbuf_real_init(allocator, stream, BIGBUF_LOAD_STREAM, &reals2);
+ bigbuf_real_init(allocator, 45, stream, BIGBUF_LOAD_STREAM, &reals2);
CHECK(bigbuf_real_size_get(&reals), 6000);
CHECK(bigbuf_real_size_get(&reals2), 6000);