X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fcommon%2Fdynamic-buffer.c;h=d6ad6740fc62434ffc09f06dfc8689f955111a23;hp=fd39f813b3c2568beec2dff1be91343161ec8517;hb=f895927f329180ca1e6f4426e9d3c6250ae698ba;hpb=c90647fe68da8a767a2c03dc0135d4a09992af16 diff --git a/src/common/dynamic-buffer.c b/src/common/dynamic-buffer.c index fd39f813b..d6ad6740f 100644 --- a/src/common/dynamic-buffer.c +++ b/src/common/dynamic-buffer.c @@ -1,23 +1,13 @@ /* - * 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 @@ -26,25 +16,22 @@ static size_t round_to_power_of_2(size_t val) { - int order; size_t rounded; + const int order = utils_get_count_order_u64(val); - order = utils_get_count_order_u64(val); - assert(order >= 0); + LTTNG_ASSERT(order >= 0); rounded = (1ULL << order); - assert(rounded >= val); + LTTNG_ASSERT(rounded >= val); return rounded; } -LTTNG_HIDDEN void lttng_dynamic_buffer_init(struct lttng_dynamic_buffer *buffer) { - assert(buffer); + LTTNG_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) { @@ -60,7 +47,7 @@ int lttng_dynamic_buffer_append(struct lttng_dynamic_buffer *buffer, goto end; } - assert(buffer->_capacity >= buffer->size); + LTTNG_ASSERT(buffer->_capacity >= buffer->size); if (buffer->_capacity < (len + buffer->size)) { ret = lttng_dynamic_buffer_set_capacity(buffer, buffer->_capacity + @@ -76,9 +63,8 @@ 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; @@ -93,7 +79,22 @@ end: return ret; } -LTTNG_HIDDEN +int lttng_dynamic_buffer_append_view(struct lttng_dynamic_buffer *buffer, + const struct lttng_buffer_view *src) +{ + int ret; + + if (!buffer || !src) { + ret = -1; + goto end; + } + + ret = lttng_dynamic_buffer_append(buffer, src->data, + src->size); +end: + return ret; +} + int lttng_dynamic_buffer_set_size(struct lttng_dynamic_buffer *buffer, size_t new_size) { @@ -108,22 +109,12 @@ int lttng_dynamic_buffer_set_size(struct lttng_dynamic_buffer *buffer, } if (new_size > buffer->_capacity) { - size_t original_size = buffer->size; - size_t original_capacity = 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 { @@ -137,18 +128,19 @@ int lttng_dynamic_buffer_set_size(struct lttng_dynamic_buffer *buffer, * size _before_ making such calls. */ } + buffer->size = new_size; 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) { /* @@ -169,6 +161,7 @@ int lttng_dynamic_buffer_set_capacity(struct lttng_dynamic_buffer *buffer, ret = -1; goto end; } + buffer->data = new_buf; buffer->_capacity = new_capacity; end: @@ -176,13 +169,24 @@ end: } /* 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; free(buffer->data); + buffer->data = NULL; +} + +size_t lttng_dynamic_buffer_get_capacity_left( + struct lttng_dynamic_buffer *buffer) +{ + if (!buffer) { + return 0; + } + + return buffer->_capacity - buffer->size; }