X-Git-Url: http://git.efficios.com/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fcommon%2Fdynamic-buffer.c;h=7005ef501776c0801f951e00b3a01aa17c699c85;hp=c295a5d2eb378f58b557fdfa6c841de6667db810;hb=38b6c4f5eb49d404ff3f2248b017cea415c49618;hpb=1c6def05e37f4a8a828a94aa0ca0e6236877f26f diff --git a/src/common/dynamic-buffer.c b/src/common/dynamic-buffer.c index c295a5d2e..7005ef501 100644 --- a/src/common/dynamic-buffer.c +++ b/src/common/dynamic-buffer.c @@ -1,25 +1,18 @@ /* - * Copyright (C) 2017 - Jérémie Galarneau + * Copyright (C) 2017 Jérémie Galarneau * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License, version 2.1 only, - * as published by the Free Software Foundation. + * SPDX-License-Identifier: LGPL-2.1-only * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - * for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #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 +27,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 +50,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,8 +66,9 @@ end: return ret; } +LTTNG_HIDDEN int lttng_dynamic_buffer_append_buffer(struct lttng_dynamic_buffer *dst_buffer, - struct lttng_dynamic_buffer *src_buffer) + const struct lttng_dynamic_buffer *src_buffer) { int ret; @@ -87,6 +83,7 @@ end: return ret; } +LTTNG_HIDDEN int lttng_dynamic_buffer_set_size(struct lttng_dynamic_buffer *buffer, size_t new_size) { @@ -100,23 +97,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,60 +122,58 @@ end: return ret; } +LTTNG_HIDDEN int lttng_dynamic_buffer_set_capacity(struct lttng_dynamic_buffer *buffer, - size_t new_capacity) + size_t demanded_capacity) { int ret = 0; - size_t rounded_capacity = round_to_power_of_2(new_capacity); + void *new_buf; + size_t new_capacity = demanded_capacity ? + round_to_power_of_2(demanded_capacity) : 0; - if (!buffer || new_capacity < buffer->size) { + if (!buffer || demanded_capacity < buffer->size) { + /* + * Shrinking a buffer's size by changing its capacity is + * unsupported. + */ ret = -1; goto end; } - if (rounded_capacity == buffer->capacity) { + if (new_capacity == buffer->_capacity) { goto end; } - if (!buffer->data) { - buffer->data = zmalloc(rounded_capacity); - if (!buffer->data) { - ret = -1; - goto end; - } - } else { - void *new_buf; - - new_buf = realloc(buffer->data, rounded_capacity); - if (new_buf) { - if (rounded_capacity > buffer->capacity) { - memset(new_buf + buffer->capacity, 0, - rounded_capacity - buffer->capacity); - } - } else { - /* Realloc failed, try to acquire a new block. */ - new_buf = zmalloc(rounded_capacity); - if (!new_buf) { - ret = -1; - goto end; - } - memcpy(new_buf, buffer->data, buffer->size); - free(buffer->data); - } - buffer->data = new_buf; + /* Memory is initialized by the size increases. */ + new_buf = realloc(buffer->data, new_capacity); + if (!new_buf) { + ret = -1; + goto end; } - buffer->capacity = rounded_capacity; + buffer->data = new_buf; + 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); } + +LTTNG_HIDDEN +size_t lttng_dynamic_buffer_get_capacity_left( + struct lttng_dynamic_buffer *buffer) +{ + if (!buffer) { + return 0; + } + return buffer->_capacity - buffer->size; +}