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.hpp>
9 #include <common/buffer-view.hpp>
10 #include <common/utils.hpp>
13 * Round to (upper) power of two, val is returned if it already is a power of
17 size_t round_to_power_of_2(size_t val
)
20 const int order
= utils_get_count_order_u64(val
);
22 LTTNG_ASSERT(order
>= 0);
23 rounded
= (1ULL << order
);
24 LTTNG_ASSERT(rounded
>= val
);
29 void lttng_dynamic_buffer_init(struct lttng_dynamic_buffer
*buffer
)
32 memset(buffer
, 0, sizeof(*buffer
));
35 int lttng_dynamic_buffer_append(struct lttng_dynamic_buffer
*buffer
,
36 const void *buf
, size_t len
)
40 if (!buffer
|| (!buf
&& len
)) {
46 /* Not an error, no-op. */
50 LTTNG_ASSERT(buffer
->_capacity
>= buffer
->size
);
51 if (buffer
->_capacity
< (len
+ buffer
->size
)) {
52 ret
= lttng_dynamic_buffer_set_capacity(buffer
,
54 (len
- (buffer
->_capacity
- buffer
->size
)));
60 memcpy(buffer
->data
+ buffer
->size
, buf
, len
);
66 int lttng_dynamic_buffer_append_buffer(struct lttng_dynamic_buffer
*dst_buffer
,
67 const struct lttng_dynamic_buffer
*src_buffer
)
71 if (!dst_buffer
|| !src_buffer
) {
76 ret
= lttng_dynamic_buffer_append(dst_buffer
, src_buffer
->data
,
82 int lttng_dynamic_buffer_append_view(struct lttng_dynamic_buffer
*buffer
,
83 const struct lttng_buffer_view
*src
)
87 if (!buffer
|| !src
) {
92 ret
= lttng_dynamic_buffer_append(buffer
, src
->data
,
98 int lttng_dynamic_buffer_set_size(struct lttng_dynamic_buffer
*buffer
,
107 if (new_size
== buffer
->size
) {
111 if (new_size
> buffer
->_capacity
) {
112 ret
= lttng_dynamic_buffer_set_capacity(buffer
, new_size
);
117 memset(buffer
->data
+ buffer
->size
, 0, new_size
- buffer
->size
);
118 } else if (new_size
> buffer
->size
) {
119 memset(buffer
->data
+ buffer
->size
, 0, new_size
- buffer
->size
);
122 * Shrinking size. There is no need to zero-out the newly
123 * released memory as it will either be:
124 * - overwritten by lttng_dynamic_buffer_append,
125 * - expanded later, which will zero-out the memory
127 * Users of external APIs are encouraged to set the buffer's
128 * size _before_ making such calls.
132 buffer
->size
= new_size
;
137 int lttng_dynamic_buffer_set_capacity(struct lttng_dynamic_buffer
*buffer
,
138 size_t demanded_capacity
)
142 size_t new_capacity
= demanded_capacity
?
143 round_to_power_of_2(demanded_capacity
) : 0;
145 if (!buffer
|| demanded_capacity
< buffer
->size
) {
147 * Shrinking a buffer's size by changing its capacity is
154 if (new_capacity
== buffer
->_capacity
) {
158 /* Memory is initialized by the size increases. */
159 new_buf
= realloc(buffer
->data
, new_capacity
);
165 buffer
->data
= (char *) new_buf
;
166 buffer
->_capacity
= new_capacity
;
171 /* Release any memory used by the dynamic buffer. */
172 void lttng_dynamic_buffer_reset(struct lttng_dynamic_buffer
*buffer
)
179 buffer
->_capacity
= 0;
184 size_t lttng_dynamic_buffer_get_capacity_left(
185 struct lttng_dynamic_buffer
*buffer
)
191 return buffer
->_capacity
- buffer
->size
;
This page took 0.034249 seconds and 4 git commands to generate.