fadd4505a3e97024918fd955da4bb45cab083f15
2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #include <common/dynamic-buffer.h>
9 #include <common/buffer-view.h>
10 #include <common/utils.h>
14 * Round to (upper) power of two, val is returned if it already is a power of
18 size_t round_to_power_of_2(size_t val
)
23 order
= utils_get_count_order_u64(val
);
25 rounded
= (1ULL << order
);
26 assert(rounded
>= val
);
32 void lttng_dynamic_buffer_init(struct lttng_dynamic_buffer
*buffer
)
35 memset(buffer
, 0, sizeof(*buffer
));
39 int lttng_dynamic_buffer_append(struct lttng_dynamic_buffer
*buffer
,
40 const void *buf
, size_t len
)
44 if (!buffer
|| (!buf
&& len
)) {
50 /* Not an error, no-op. */
54 assert(buffer
->_capacity
>= buffer
->size
);
55 if (buffer
->_capacity
< (len
+ buffer
->size
)) {
56 ret
= lttng_dynamic_buffer_set_capacity(buffer
,
58 (len
- (buffer
->_capacity
- buffer
->size
)));
64 memcpy(buffer
->data
+ buffer
->size
, buf
, len
);
71 int lttng_dynamic_buffer_append_buffer(struct lttng_dynamic_buffer
*dst_buffer
,
72 struct lttng_dynamic_buffer
*src_buffer
)
76 if (!dst_buffer
|| !src_buffer
) {
81 ret
= lttng_dynamic_buffer_append(dst_buffer
, src_buffer
->data
,
88 int lttng_dynamic_buffer_append_view(struct lttng_dynamic_buffer
*buffer
,
89 const struct lttng_buffer_view
*src
)
93 if (!buffer
|| !src
) {
98 ret
= lttng_dynamic_buffer_append(buffer
, src
->data
,
105 int lttng_dynamic_buffer_set_size(struct lttng_dynamic_buffer
*buffer
,
114 if (new_size
== buffer
->size
) {
118 if (new_size
> buffer
->_capacity
) {
119 ret
= lttng_dynamic_buffer_set_capacity(buffer
, new_size
);
124 memset(buffer
->data
+ buffer
->size
, 0, new_size
- buffer
->size
);
125 } else if (new_size
> buffer
->size
) {
126 memset(buffer
->data
+ buffer
->size
, 0, new_size
- buffer
->size
);
129 * Shrinking size. There is no need to zero-out the newly
130 * released memory as it will either be:
131 * - overwritten by lttng_dynamic_buffer_append,
132 * - expanded later, which will zero-out the memory
134 * Users of external APIs are encouraged to set the buffer's
135 * size _before_ making such calls.
138 buffer
->size
= new_size
;
144 int lttng_dynamic_buffer_set_capacity(struct lttng_dynamic_buffer
*buffer
,
145 size_t demanded_capacity
)
149 size_t new_capacity
= demanded_capacity
?
150 round_to_power_of_2(demanded_capacity
) : 0;
152 if (!buffer
|| demanded_capacity
< buffer
->size
) {
154 * Shrinking a buffer's size by changing its capacity is
161 if (new_capacity
== buffer
->_capacity
) {
165 /* Memory is initialized by the size increases. */
166 new_buf
= realloc(buffer
->data
, new_capacity
);
171 buffer
->data
= new_buf
;
172 buffer
->_capacity
= new_capacity
;
177 /* Release any memory used by the dynamic buffer. */
179 void lttng_dynamic_buffer_reset(struct lttng_dynamic_buffer
*buffer
)
185 buffer
->_capacity
= 0;
190 size_t lttng_dynamic_buffer_get_capacity_left(
191 struct lttng_dynamic_buffer
*buffer
)
196 return buffer
->_capacity
- buffer
->size
;
This page took 0.035023 seconds and 5 git commands to generate.