X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=src%2Fcommon%2Fdynamic-buffer.c;h=4a08aa80e53eb85b0e495c078b104a5cfd36b0f2;hb=a0377dfefe40662ba7d68617bce6ff467114136c;hp=aef80dded0bab2785820f7cf8acb1f7b9071cf7b;hpb=6ee96b4d634b95f8cb91537515016246638189e4;p=lttng-tools.git diff --git a/src/common/dynamic-buffer.c b/src/common/dynamic-buffer.c index aef80dded..4a08aa80e 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,13 +16,12 @@ 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; } @@ -40,7 +29,7 @@ size_t round_to_power_of_2(size_t val) LTTNG_HIDDEN void lttng_dynamic_buffer_init(struct lttng_dynamic_buffer *buffer) { - assert(buffer); + LTTNG_ASSERT(buffer); memset(buffer, 0, sizeof(*buffer)); } @@ -60,7 +49,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 + @@ -78,7 +67,7 @@ end: 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,6 +82,23 @@ 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; +} + LTTNG_HIDDEN int lttng_dynamic_buffer_set_size(struct lttng_dynamic_buffer *buffer, size_t new_size) @@ -127,6 +133,7 @@ int lttng_dynamic_buffer_set_size(struct lttng_dynamic_buffer *buffer, * size _before_ making such calls. */ } + buffer->size = new_size; end: return ret; @@ -160,6 +167,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: @@ -173,9 +181,11 @@ 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; } LTTNG_HIDDEN @@ -185,5 +195,6 @@ size_t lttng_dynamic_buffer_get_capacity_left( if (!buffer) { return 0; } + return buffer->_capacity - buffer->size; }