Make lttng_dynamic_buffer_append_buffer const-correct
[lttng-tools.git] / src / common / dynamic-buffer.c
index fd39f813b3c2568beec2dff1be91343161ec8517..7005ef501776c0801f951e00b3a01aa17c699c85 100644 (file)
@@ -1,18 +1,8 @@
 /*
- * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
  *
- * 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 <common/dynamic-buffer.h>
@@ -78,7 +68,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;
 
@@ -108,22 +98,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 {
@@ -148,7 +128,8 @@ int lttng_dynamic_buffer_set_capacity(struct lttng_dynamic_buffer *buffer,
 {
        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) {
                /*
@@ -186,3 +167,13 @@ void lttng_dynamic_buffer_reset(struct lttng_dynamic_buffer *buffer)
        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;
+}
This page took 0.025012 seconds and 5 git commands to generate.