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/macros.h>
20 #include <common/utils.h>
24 size_t round_to_power_of_2(size_t val
)
29 order
= utils_get_count_order_u64(val
);
31 rounded
= (1ULL << order
);
32 assert(rounded
>= val
);
37 void lttng_dynamic_buffer_init(struct lttng_dynamic_buffer
*buffer
)
40 memset(buffer
, 0, sizeof(*buffer
));
43 int lttng_dynamic_buffer_append(struct lttng_dynamic_buffer
*buffer
,
44 const void *buf
, size_t len
)
48 if (!buffer
|| (!buf
&& len
)) {
54 /* Not an error, no-op. */
58 assert(buffer
->capacity
>= buffer
->size
);
59 if (buffer
->capacity
< (len
+ buffer
->size
)) {
60 ret
= lttng_dynamic_buffer_set_capacity(buffer
,
62 (len
- (buffer
->capacity
- buffer
->size
)));
68 memcpy(buffer
->data
+ buffer
->size
, buf
, len
);
74 int lttng_dynamic_buffer_append_buffer(struct lttng_dynamic_buffer
*dst_buffer
,
75 struct lttng_dynamic_buffer
*src_buffer
)
79 if (!dst_buffer
|| !src_buffer
) {
84 ret
= lttng_dynamic_buffer_append(dst_buffer
, src_buffer
->data
,
90 int lttng_dynamic_buffer_set_size(struct lttng_dynamic_buffer
*buffer
,
99 if (new_size
== buffer
->size
) {
103 if (new_size
> buffer
->capacity
) {
104 size_t original_size
= buffer
->size
;
105 size_t original_capacity
= buffer
->capacity
;
107 ret
= lttng_dynamic_buffer_set_capacity(buffer
, new_size
);
113 * Zero-initialize the space that was left in the buffer at the
114 * before we increased its capacity (original capacity - original size).
115 * The newly acquired capacity (new capacity - original capacity)
116 * is zeroed by lttng_dynamic_buffer_set_capacity().
118 memset(buffer
->data
+ original_size
, 0,
119 original_capacity
- original_size
);
120 } else if (new_size
> buffer
->size
) {
121 memset(buffer
->data
+ buffer
->size
, 0, new_size
- buffer
->size
);
124 * Shrinking size. There is no need to zero-out the newly
125 * released memory as it will either be:
126 * - overwritten by lttng_dynamic_buffer_append,
127 * - expanded later, which will zero-out the memory
129 * Users of external APIs are encouraged to set the buffer's
130 * size _before_ making such calls.
133 buffer
->size
= new_size
;
138 int lttng_dynamic_buffer_set_capacity(struct lttng_dynamic_buffer
*buffer
,
142 size_t rounded_capacity
= round_to_power_of_2(new_capacity
);
144 if (!buffer
|| new_capacity
< buffer
->size
) {
149 if (rounded_capacity
== buffer
->capacity
) {
154 buffer
->data
= zmalloc(rounded_capacity
);
162 new_buf
= realloc(buffer
->data
, rounded_capacity
);
164 if (rounded_capacity
> buffer
->capacity
) {
165 memset(new_buf
+ buffer
->capacity
, 0,
166 rounded_capacity
- buffer
->capacity
);
169 /* Realloc failed, try to acquire a new block. */
170 new_buf
= zmalloc(rounded_capacity
);
175 memcpy(new_buf
, buffer
->data
, buffer
->size
);
178 buffer
->data
= new_buf
;
180 buffer
->capacity
= rounded_capacity
;
185 /* Release any memory used by the dynamic buffer. */
186 void lttng_dynamic_buffer_reset(struct lttng_dynamic_buffer
*buffer
)
192 buffer
->capacity
= 0;
This page took 0.035132 seconds and 5 git commands to generate.