2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #include <common/buffer-view.hpp>
9 #include <common/dynamic-buffer.hpp>
10 #include <common/utils.hpp>
13 * Round to (upper) power of two, val is returned if it already is a power of
16 static size_t round_to_power_of_2(size_t val
)
19 const int order
= utils_get_count_order_u64(val
);
21 LTTNG_ASSERT(order
>= 0);
22 rounded
= (1ULL << order
);
23 LTTNG_ASSERT(rounded
>= val
);
28 void lttng_dynamic_buffer_init(struct lttng_dynamic_buffer
*buffer
)
31 memset(buffer
, 0, sizeof(*buffer
));
34 int lttng_dynamic_buffer_append(struct lttng_dynamic_buffer
*buffer
, const void *buf
, size_t len
)
38 if (!buffer
|| (!buf
&& len
)) {
44 /* Not an error, no-op. */
48 LTTNG_ASSERT(buffer
->_capacity
>= buffer
->size
);
49 if (buffer
->_capacity
< (len
+ buffer
->size
)) {
50 ret
= lttng_dynamic_buffer_set_capacity(
51 buffer
, buffer
->_capacity
+ (len
- (buffer
->_capacity
- buffer
->size
)));
57 memcpy(buffer
->data
+ buffer
->size
, buf
, len
);
63 int lttng_dynamic_buffer_append_buffer(struct lttng_dynamic_buffer
*dst_buffer
,
64 const struct lttng_dynamic_buffer
*src_buffer
)
68 if (!dst_buffer
|| !src_buffer
) {
73 ret
= lttng_dynamic_buffer_append(dst_buffer
, src_buffer
->data
, src_buffer
->size
);
78 int lttng_dynamic_buffer_append_view(struct lttng_dynamic_buffer
*buffer
,
79 const struct lttng_buffer_view
*src
)
83 if (!buffer
|| !src
) {
88 ret
= lttng_dynamic_buffer_append(buffer
, src
->data
, src
->size
);
93 int lttng_dynamic_buffer_set_size(struct lttng_dynamic_buffer
*buffer
, size_t new_size
)
101 if (new_size
== buffer
->size
) {
105 if (new_size
> buffer
->_capacity
) {
106 ret
= lttng_dynamic_buffer_set_capacity(buffer
, new_size
);
111 memset(buffer
->data
+ buffer
->size
, 0, new_size
- buffer
->size
);
112 } else if (new_size
> buffer
->size
) {
113 memset(buffer
->data
+ buffer
->size
, 0, new_size
- buffer
->size
);
116 * Shrinking size. There is no need to zero-out the newly
117 * released memory as it will either be:
118 * - overwritten by lttng_dynamic_buffer_append,
119 * - expanded later, which will zero-out the memory
121 * Users of external APIs are encouraged to set the buffer's
122 * size _before_ making such calls.
126 buffer
->size
= new_size
;
131 int lttng_dynamic_buffer_set_capacity(struct lttng_dynamic_buffer
*buffer
, size_t demanded_capacity
)
135 size_t new_capacity
= demanded_capacity
? round_to_power_of_2(demanded_capacity
) : 0;
137 if (!buffer
|| demanded_capacity
< buffer
->size
) {
139 * Shrinking a buffer's size by changing its capacity is
146 if (new_capacity
== buffer
->_capacity
) {
150 /* Memory is initialized by the size increases. */
151 new_buf
= realloc(buffer
->data
, new_capacity
);
157 buffer
->data
= (char *) new_buf
;
158 buffer
->_capacity
= new_capacity
;
163 /* Release any memory used by the dynamic buffer. */
164 void lttng_dynamic_buffer_reset(struct lttng_dynamic_buffer
*buffer
)
171 buffer
->_capacity
= 0;
173 buffer
->data
= nullptr;
176 size_t lttng_dynamic_buffer_get_capacity_left(struct lttng_dynamic_buffer
*buffer
)
182 return buffer
->_capacity
- buffer
->size
;
This page took 0.032171 seconds and 4 git commands to generate.