Clean-up: simplify the implementation of dynamic buffer set_capacity
[lttng-tools.git] / src / common / dynamic-buffer.c
CommitLineData
5966417e
JG
1/*
2 * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License, version 2.1 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
11 * for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software Foundation,
15 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
18#include <common/dynamic-buffer.h>
19#include <common/macros.h>
20#include <common/utils.h>
21#include <assert.h>
22
23static
24size_t round_to_power_of_2(size_t val)
25{
26 int order;
27 size_t rounded;
28
29 order = utils_get_count_order_u64(val);
30 assert(order >= 0);
31 rounded = (1ULL << order);
32 assert(rounded >= val);
33
34 return rounded;
35}
36
37void lttng_dynamic_buffer_init(struct lttng_dynamic_buffer *buffer)
38{
39 assert(buffer);
40 memset(buffer, 0, sizeof(*buffer));
41}
42
43int lttng_dynamic_buffer_append(struct lttng_dynamic_buffer *buffer,
44 const void *buf, size_t len)
45{
46 int ret = 0;
47
48 if (!buffer || (!buf && len)) {
49 ret = -1;
50 goto end;
51 }
52
53 if (len == 0) {
54 /* Not an error, no-op. */
55 goto end;
56 }
57
4270baea 58 assert(buffer->capacity >= buffer->size);
83063067 59 if (buffer->capacity < (len + buffer->size)) {
5966417e
JG
60 ret = lttng_dynamic_buffer_set_capacity(buffer,
61 buffer->capacity +
62 (len - (buffer->capacity - buffer->size)));
63 if (ret) {
64 goto end;
65 }
66 }
67
68 memcpy(buffer->data + buffer->size, buf, len);
69 buffer->size += len;
70end:
71 return ret;
72}
73
74int lttng_dynamic_buffer_append_buffer(struct lttng_dynamic_buffer *dst_buffer,
75 struct lttng_dynamic_buffer *src_buffer)
76{
77 int ret;
78
79 if (!dst_buffer || !src_buffer) {
80 ret = -1;
81 goto end;
82 }
83
84 ret = lttng_dynamic_buffer_append(dst_buffer, src_buffer->data,
85 src_buffer->size);
86end:
87 return ret;
88}
89
90int lttng_dynamic_buffer_set_size(struct lttng_dynamic_buffer *buffer,
91 size_t new_size)
92{
93 int ret = 0;
94
95 if (!buffer) {
96 goto end;
97 }
98
99 if (new_size == buffer->size) {
100 goto end;
101 }
102
103 if (new_size > buffer->capacity) {
1c6def05
JG
104 size_t original_size = buffer->size;
105 size_t original_capacity = buffer->capacity;
106
5966417e
JG
107 ret = lttng_dynamic_buffer_set_capacity(buffer, new_size);
108 if (ret) {
109 goto end;
110 }
1c6def05
JG
111
112 /*
113 * Zero-initialize the space that was left in the buffer at the
114 * before we increased its capacity (original capacity - original size).
115 * The newly acquired capacity (new capacity - original capacity)
116 * is zeroed by lttng_dynamic_buffer_set_capacity().
117 */
118 memset(buffer->data + original_size, 0,
119 original_capacity - original_size);
5966417e
JG
120 } else if (new_size > buffer->size) {
121 memset(buffer->data + buffer->size, 0, new_size - buffer->size);
122 } else {
123 /*
124 * Shrinking size. There is no need to zero-out the newly
125 * released memory as it will either be:
126 * - overwritten by lttng_dynamic_buffer_append,
127 * - expanded later, which will zero-out the memory
128 *
129 * Users of external APIs are encouraged to set the buffer's
130 * size _before_ making such calls.
131 */
132 }
133 buffer->size = new_size;
134end:
135 return ret;
136}
137
138int lttng_dynamic_buffer_set_capacity(struct lttng_dynamic_buffer *buffer,
fc804d0f 139 size_t demanded_capacity)
5966417e
JG
140{
141 int ret = 0;
fc804d0f
JG
142 void *new_buf;
143 size_t new_capacity = round_to_power_of_2(demanded_capacity);
5966417e 144
fc804d0f
JG
145 if (!buffer || demanded_capacity < buffer->size) {
146 /*
147 * Shrinking a buffer's size by changing its capacity is
148 * unsupported.
149 */
5966417e
JG
150 ret = -1;
151 goto end;
152 }
153
fc804d0f 154 if (new_capacity == buffer->capacity) {
5966417e
JG
155 goto end;
156 }
157
fc804d0f
JG
158 /* Memory is initialized by the size increases. */
159 new_buf = realloc(buffer->data, new_capacity);
160 if (!new_buf) {
161 ret = -1;
162 goto end;
5966417e 163 }
fc804d0f
JG
164 buffer->data = new_buf;
165 buffer->capacity = new_capacity;
5966417e
JG
166end:
167 return ret;
168}
169
170/* Release any memory used by the dynamic buffer. */
171void lttng_dynamic_buffer_reset(struct lttng_dynamic_buffer *buffer)
172{
173 if (!buffer) {
174 return;
175 }
176 buffer->size = 0;
177 buffer->capacity = 0;
178 free(buffer->data);
179}
This page took 0.031091 seconds and 5 git commands to generate.