X-Git-Url: http://git.liburcu.org/?a=blobdiff_plain;f=src%2Fcommon%2Fdynamic-buffer.c;h=ec5d84bf9c9ea1a3955a04e2cf72ef9a5ce82189;hb=fd9b5d0f402623ffdc908a2fecc9d088f82110e9;hp=9ed16514fe64fa84dc735f15de862ca662a04fef;hpb=5b1dd5fb3a3ad3fa972bba3a1916012c018727ff;p=lttng-tools.git diff --git a/src/common/dynamic-buffer.c b/src/common/dynamic-buffer.c index 9ed16514f..ec5d84bf9 100644 --- a/src/common/dynamic-buffer.c +++ b/src/common/dynamic-buffer.c @@ -16,10 +16,13 @@ */ #include -#include #include #include +/* + * Round to (upper) power of two, val is returned if it already is a power of + * two. + */ static size_t round_to_power_of_2(size_t val) { @@ -34,12 +37,14 @@ size_t round_to_power_of_2(size_t val) return rounded; } +LTTNG_HIDDEN void lttng_dynamic_buffer_init(struct lttng_dynamic_buffer *buffer) { assert(buffer); memset(buffer, 0, sizeof(*buffer)); } +LTTNG_HIDDEN int lttng_dynamic_buffer_append(struct lttng_dynamic_buffer *buffer, const void *buf, size_t len) { @@ -55,11 +60,11 @@ int lttng_dynamic_buffer_append(struct lttng_dynamic_buffer *buffer, goto end; } - assert(buffer->capacity >= buffer->size); - if (buffer->capacity < (len + buffer->size)) { + assert(buffer->_capacity >= buffer->size); + if (buffer->_capacity < (len + buffer->size)) { ret = lttng_dynamic_buffer_set_capacity(buffer, - buffer->capacity + - (len - (buffer->capacity - buffer->size))); + buffer->_capacity + + (len - (buffer->_capacity - buffer->size))); if (ret) { goto end; } @@ -71,6 +76,7 @@ end: return ret; } +LTTNG_HIDDEN int lttng_dynamic_buffer_append_buffer(struct lttng_dynamic_buffer *dst_buffer, struct lttng_dynamic_buffer *src_buffer) { @@ -87,6 +93,7 @@ end: return ret; } +LTTNG_HIDDEN int lttng_dynamic_buffer_set_size(struct lttng_dynamic_buffer *buffer, size_t new_size) { @@ -100,23 +107,13 @@ int lttng_dynamic_buffer_set_size(struct lttng_dynamic_buffer *buffer, goto end; } - if (new_size > buffer->capacity) { - size_t original_size = buffer->size; - size_t original_capacity = buffer->capacity; - + if (new_size > buffer->_capacity) { ret = lttng_dynamic_buffer_set_capacity(buffer, new_size); if (ret) { goto end; } - /* - * Zero-initialize the space that was left in the buffer at the - * before we increased its capacity (original capacity - original size). - * The newly acquired capacity (new capacity - original capacity) - * is zeroed by lttng_dynamic_buffer_set_capacity(). - */ - memset(buffer->data + original_size, 0, - original_capacity - original_size); + memset(buffer->data + buffer->size, 0, new_size - buffer->size); } else if (new_size > buffer->size) { memset(buffer->data + buffer->size, 0, new_size - buffer->size); } else { @@ -135,12 +132,14 @@ end: return ret; } +LTTNG_HIDDEN int lttng_dynamic_buffer_set_capacity(struct lttng_dynamic_buffer *buffer, size_t demanded_capacity) { int ret = 0; void *new_buf; - size_t new_capacity = round_to_power_of_2(demanded_capacity); + size_t new_capacity = demanded_capacity ? + round_to_power_of_2(demanded_capacity) : 0; if (!buffer || demanded_capacity < buffer->size) { /* @@ -151,7 +150,7 @@ int lttng_dynamic_buffer_set_capacity(struct lttng_dynamic_buffer *buffer, goto end; } - if (new_capacity == buffer->capacity) { + if (new_capacity == buffer->_capacity) { goto end; } @@ -162,18 +161,19 @@ int lttng_dynamic_buffer_set_capacity(struct lttng_dynamic_buffer *buffer, goto end; } buffer->data = new_buf; - buffer->capacity = new_capacity; + buffer->_capacity = new_capacity; end: return ret; } /* Release any memory used by the dynamic buffer. */ +LTTNG_HIDDEN void lttng_dynamic_buffer_reset(struct lttng_dynamic_buffer *buffer) { if (!buffer) { return; } buffer->size = 0; - buffer->capacity = 0; + buffer->_capacity = 0; free(buffer->data); }