2 * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
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.
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
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
18 #include <common/dynamic-buffer.h>
19 #include <common/buffer-view.h>
20 #include <common/utils.h>
24 * Round to (upper) power of two, val is returned if it already is a power of
28 size_t round_to_power_of_2(size_t val
)
33 order
= utils_get_count_order_u64(val
);
35 rounded
= (1ULL << order
);
36 assert(rounded
>= val
);
42 void lttng_dynamic_buffer_init(struct lttng_dynamic_buffer
*buffer
)
45 memset(buffer
, 0, sizeof(*buffer
));
49 int lttng_dynamic_buffer_append(struct lttng_dynamic_buffer
*buffer
,
50 const void *buf
, size_t len
)
54 if (!buffer
|| (!buf
&& len
)) {
60 /* Not an error, no-op. */
64 assert(buffer
->_capacity
>= buffer
->size
);
65 if (buffer
->_capacity
< (len
+ buffer
->size
)) {
66 ret
= lttng_dynamic_buffer_set_capacity(buffer
,
68 (len
- (buffer
->_capacity
- buffer
->size
)));
74 memcpy(buffer
->data
+ buffer
->size
, buf
, len
);
81 int lttng_dynamic_buffer_append_buffer(struct lttng_dynamic_buffer
*dst_buffer
,
82 struct lttng_dynamic_buffer
*src_buffer
)
86 if (!dst_buffer
|| !src_buffer
) {
91 ret
= lttng_dynamic_buffer_append(dst_buffer
, src_buffer
->data
,
98 int lttng_dynamic_buffer_append_view(struct lttng_dynamic_buffer
*buffer
,
99 const struct lttng_buffer_view
*src
)
103 if (!buffer
|| !src
) {
108 ret
= lttng_dynamic_buffer_append(buffer
, src
->data
,
115 int lttng_dynamic_buffer_set_size(struct lttng_dynamic_buffer
*buffer
,
124 if (new_size
== buffer
->size
) {
128 if (new_size
> buffer
->_capacity
) {
129 ret
= lttng_dynamic_buffer_set_capacity(buffer
, new_size
);
134 memset(buffer
->data
+ buffer
->size
, 0, new_size
- buffer
->size
);
135 } else if (new_size
> buffer
->size
) {
136 memset(buffer
->data
+ buffer
->size
, 0, new_size
- buffer
->size
);
139 * Shrinking size. There is no need to zero-out the newly
140 * released memory as it will either be:
141 * - overwritten by lttng_dynamic_buffer_append,
142 * - expanded later, which will zero-out the memory
144 * Users of external APIs are encouraged to set the buffer's
145 * size _before_ making such calls.
148 buffer
->size
= new_size
;
154 int lttng_dynamic_buffer_set_capacity(struct lttng_dynamic_buffer
*buffer
,
155 size_t demanded_capacity
)
159 size_t new_capacity
= demanded_capacity
?
160 round_to_power_of_2(demanded_capacity
) : 0;
162 if (!buffer
|| demanded_capacity
< buffer
->size
) {
164 * Shrinking a buffer's size by changing its capacity is
171 if (new_capacity
== buffer
->_capacity
) {
175 /* Memory is initialized by the size increases. */
176 new_buf
= realloc(buffer
->data
, new_capacity
);
181 buffer
->data
= new_buf
;
182 buffer
->_capacity
= new_capacity
;
187 /* Release any memory used by the dynamic buffer. */
189 void lttng_dynamic_buffer_reset(struct lttng_dynamic_buffer
*buffer
)
195 buffer
->_capacity
= 0;
200 size_t lttng_dynamic_buffer_get_capacity_left(
201 struct lttng_dynamic_buffer
*buffer
)
206 return buffer
->_capacity
- buffer
->size
;
This page took 0.03296 seconds and 4 git commands to generate.