Browse Source

[core] inline buffer_reset()

buffer_reset() is used on pre-allocated buffers, so remove NULL check
master
Glenn Strauss 1 year ago
parent
commit
b600b75f20
  1. 6
      src/array.c
  2. 10
      src/buffer.c
  3. 24
      src/buffer.h

6
src/array.c

@ -73,10 +73,8 @@ void array_reset_data_strings(array * const a) {
for (uint32_t i = 0; i < used; ++i) {
data_string * const ds = data[i];
/*force_assert(ds->type == TYPE_STRING);*/
buffer * const k = &ds->key;
buffer * const v = &ds->value;
if (k->size > BUFFER_MAX_REUSE_SIZE) buffer_reset(k);
if (v->size > BUFFER_MAX_REUSE_SIZE) buffer_reset(v);
buffer_reset(&ds->key);
buffer_reset(&ds->value);
}
}

10
src/buffer.c

@ -46,21 +46,13 @@ void buffer_free(buffer *b) {
free(b);
}
__attribute_cold__
static void buffer_free_ptr(buffer *b) {
void buffer_free_ptr(buffer *b) {
free(b->ptr);
b->ptr = NULL;
b->used = 0;
b->size = 0;
}
void buffer_reset(buffer *b) {
force_assert(NULL != b);
b->used = 0;
/* release buffer larger than ... bytes */
if (b->size > BUFFER_MAX_REUSE_SIZE) buffer_free_ptr(b);
}
void buffer_move(buffer * restrict b, buffer * restrict src) {
buffer tmp;
force_assert(NULL != b);

24
src/buffer.h

@ -41,8 +41,6 @@ __attribute_returns_nonnull__
buffer* buffer_init_string(const char *str); /* str can be NULL */
void buffer_free(buffer *b); /* b can be NULL */
/* truncates to used == 0; frees large buffers, might keep smaller ones for reuse */
void buffer_reset(buffer *b); /* b can be NULL */
/* reset b. if NULL != b && NULL != src, move src content to b. reset src. */
void buffer_move(buffer * restrict b, buffer * restrict src);
@ -85,6 +83,21 @@ void buffer_string_set_length(buffer *b, uint32_t len);
*/
static inline void buffer_clear(buffer *b);
/* reset buffer
* - invalidate buffer contents
* - unsets used chars
* - keeps smaller buffer (unmodified) for reuse
* (b->ptr *is not* set to an empty, '\0'-terminated string "")
* - frees larger buffer (b->size > BUFFER_MAX_REUSE_SIZE)
*/
static inline void buffer_reset(buffer *b);
/* free buffer ptr
* - invalidate buffer contents; free ptr; reset ptr, used, size to 0
*/
__attribute_cold__
void buffer_free_ptr(buffer *b);
void buffer_copy_string(buffer * restrict b, const char * restrict s);
void buffer_copy_string_len(buffer * restrict b, const char * restrict s, size_t s_len);
static inline void buffer_copy_buffer(buffer * restrict b, const buffer * restrict src);
@ -265,4 +278,11 @@ static inline void buffer_clear(buffer *b) {
b->used = 0;
}
static inline void buffer_reset(buffer *b) {
b->used = 0;
/* release buffer larger than BUFFER_MAX_REUSE_SIZE bytes */
if (b->size > BUFFER_MAX_REUSE_SIZE) buffer_free_ptr(b);
}
#endif

Loading…
Cancel
Save