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
)
21 const int order
= utils_get_count_order_u64(val
);
24 rounded
= (1ULL << order
);
25 assert(rounded
>= val
);
31 void lttng_dynamic_buffer_init(struct lttng_dynamic_buffer
*buffer
)
34 memset(buffer
, 0, sizeof(*buffer
));
38 int lttng_dynamic_buffer_append(struct lttng_dynamic_buffer
*buffer
,
39 const void *buf
, size_t len
)
43 if (!buffer
|| (!buf
&& len
)) {
49 /* Not an error, no-op. */
53 assert(buffer
->_capacity
>= buffer
->size
);
54 if (buffer
->_capacity
< (len
+ buffer
->size
)) {
55 ret
= lttng_dynamic_buffer_set_capacity(buffer
,
57 (len
- (buffer
->_capacity
- buffer
->size
)));
63 memcpy(buffer
->data
+ buffer
->size
, buf
, len
);
70 int lttng_dynamic_buffer_append_buffer(struct lttng_dynamic_buffer
*dst_buffer
,
71 const struct lttng_dynamic_buffer
*src_buffer
)
75 if (!dst_buffer
|| !src_buffer
) {
80 ret
= lttng_dynamic_buffer_append(dst_buffer
, src_buffer
->data
,
87 int lttng_dynamic_buffer_append_view(struct lttng_dynamic_buffer
*buffer
,
88 const struct lttng_buffer_view
*src
)
92 if (!buffer
|| !src
) {
97 ret
= lttng_dynamic_buffer_append(buffer
, src
->data
,
104 int lttng_dynamic_buffer_set_size(struct lttng_dynamic_buffer
*buffer
,
113 if (new_size
== buffer
->size
) {
117 if (new_size
> buffer
->_capacity
) {
118 ret
= lttng_dynamic_buffer_set_capacity(buffer
, new_size
);
123 memset(buffer
->data
+ buffer
->size
, 0, new_size
- buffer
->size
);
124 } else if (new_size
> buffer
->size
) {
125 memset(buffer
->data
+ buffer
->size
, 0, new_size
- buffer
->size
);
128 * Shrinking size. There is no need to zero-out the newly
129 * released memory as it will either be:
130 * - overwritten by lttng_dynamic_buffer_append,
131 * - expanded later, which will zero-out the memory
133 * Users of external APIs are encouraged to set the buffer's
134 * 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
);
172 buffer
->data
= new_buf
;
173 buffer
->_capacity
= new_capacity
;
178 /* Release any memory used by the dynamic buffer. */
180 void lttng_dynamic_buffer_reset(struct lttng_dynamic_buffer
*buffer
)
187 buffer
->_capacity
= 0;
193 size_t lttng_dynamic_buffer_get_capacity_left(
194 struct lttng_dynamic_buffer
*buffer
)
200 return buffer
->_capacity
- buffer
->size
;
This page took 0.035847 seconds and 4 git commands to generate.