Commit | Line | Data |
---|---|---|
5966417e | 1 | /* |
ab5be9fa | 2 | * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
5966417e | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: LGPL-2.1-only |
5966417e | 5 | * |
5966417e JG |
6 | */ |
7 | ||
8 | #include <common/dynamic-buffer.h> | |
fbd55aae | 9 | #include <common/buffer-view.h> |
5966417e JG |
10 | #include <common/utils.h> |
11 | #include <assert.h> | |
12 | ||
df94ef0f JG |
13 | /* |
14 | * Round to (upper) power of two, val is returned if it already is a power of | |
15 | * two. | |
16 | */ | |
5966417e JG |
17 | static |
18 | size_t round_to_power_of_2(size_t val) | |
19 | { | |
20 | int order; | |
21 | size_t rounded; | |
22 | ||
23 | order = utils_get_count_order_u64(val); | |
24 | assert(order >= 0); | |
25 | rounded = (1ULL << order); | |
26 | assert(rounded >= val); | |
27 | ||
28 | return rounded; | |
29 | } | |
30 | ||
c90647fe | 31 | LTTNG_HIDDEN |
5966417e JG |
32 | void lttng_dynamic_buffer_init(struct lttng_dynamic_buffer *buffer) |
33 | { | |
34 | assert(buffer); | |
35 | memset(buffer, 0, sizeof(*buffer)); | |
36 | } | |
37 | ||
c90647fe | 38 | LTTNG_HIDDEN |
5966417e JG |
39 | int lttng_dynamic_buffer_append(struct lttng_dynamic_buffer *buffer, |
40 | const void *buf, size_t len) | |
41 | { | |
42 | int ret = 0; | |
43 | ||
44 | if (!buffer || (!buf && len)) { | |
45 | ret = -1; | |
46 | goto end; | |
47 | } | |
48 | ||
49 | if (len == 0) { | |
50 | /* Not an error, no-op. */ | |
51 | goto end; | |
52 | } | |
53 | ||
63557623 JG |
54 | assert(buffer->_capacity >= buffer->size); |
55 | if (buffer->_capacity < (len + buffer->size)) { | |
5966417e | 56 | ret = lttng_dynamic_buffer_set_capacity(buffer, |
63557623 JG |
57 | buffer->_capacity + |
58 | (len - (buffer->_capacity - buffer->size))); | |
5966417e JG |
59 | if (ret) { |
60 | goto end; | |
61 | } | |
62 | } | |
63 | ||
64 | memcpy(buffer->data + buffer->size, buf, len); | |
65 | buffer->size += len; | |
66 | end: | |
67 | return ret; | |
68 | } | |
69 | ||
c90647fe | 70 | LTTNG_HIDDEN |
5966417e | 71 | int lttng_dynamic_buffer_append_buffer(struct lttng_dynamic_buffer *dst_buffer, |
38b6c4f5 | 72 | const struct lttng_dynamic_buffer *src_buffer) |
5966417e JG |
73 | { |
74 | int ret; | |
75 | ||
76 | if (!dst_buffer || !src_buffer) { | |
77 | ret = -1; | |
78 | goto end; | |
79 | } | |
80 | ||
81 | ret = lttng_dynamic_buffer_append(dst_buffer, src_buffer->data, | |
82 | src_buffer->size); | |
83 | end: | |
84 | return ret; | |
85 | } | |
86 | ||
fbd55aae JG |
87 | LTTNG_HIDDEN |
88 | int lttng_dynamic_buffer_append_view(struct lttng_dynamic_buffer *buffer, | |
89 | const struct lttng_buffer_view *src) | |
90 | { | |
91 | int ret; | |
92 | ||
93 | if (!buffer || !src) { | |
94 | ret = -1; | |
95 | goto end; | |
96 | } | |
97 | ||
98 | ret = lttng_dynamic_buffer_append(buffer, src->data, | |
99 | src->size); | |
100 | end: | |
101 | return ret; | |
102 | } | |
103 | ||
c90647fe | 104 | LTTNG_HIDDEN |
5966417e JG |
105 | int lttng_dynamic_buffer_set_size(struct lttng_dynamic_buffer *buffer, |
106 | size_t new_size) | |
107 | { | |
108 | int ret = 0; | |
109 | ||
110 | if (!buffer) { | |
111 | goto end; | |
112 | } | |
113 | ||
114 | if (new_size == buffer->size) { | |
115 | goto end; | |
116 | } | |
117 | ||
63557623 | 118 | if (new_size > buffer->_capacity) { |
5966417e JG |
119 | ret = lttng_dynamic_buffer_set_capacity(buffer, new_size); |
120 | if (ret) { | |
121 | goto end; | |
122 | } | |
1c6def05 | 123 | |
6ee96b4d | 124 | memset(buffer->data + buffer->size, 0, new_size - buffer->size); |
5966417e JG |
125 | } else if (new_size > buffer->size) { |
126 | memset(buffer->data + buffer->size, 0, new_size - buffer->size); | |
127 | } else { | |
128 | /* | |
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 | |
133 | * | |
134 | * Users of external APIs are encouraged to set the buffer's | |
135 | * size _before_ making such calls. | |
136 | */ | |
137 | } | |
138 | buffer->size = new_size; | |
139 | end: | |
140 | return ret; | |
141 | } | |
142 | ||
c90647fe | 143 | LTTNG_HIDDEN |
5966417e | 144 | int lttng_dynamic_buffer_set_capacity(struct lttng_dynamic_buffer *buffer, |
fc804d0f | 145 | size_t demanded_capacity) |
5966417e JG |
146 | { |
147 | int ret = 0; | |
fc804d0f | 148 | void *new_buf; |
e9347dad JG |
149 | size_t new_capacity = demanded_capacity ? |
150 | round_to_power_of_2(demanded_capacity) : 0; | |
5966417e | 151 | |
fc804d0f JG |
152 | if (!buffer || demanded_capacity < buffer->size) { |
153 | /* | |
154 | * Shrinking a buffer's size by changing its capacity is | |
155 | * unsupported. | |
156 | */ | |
5966417e JG |
157 | ret = -1; |
158 | goto end; | |
159 | } | |
160 | ||
63557623 | 161 | if (new_capacity == buffer->_capacity) { |
5966417e JG |
162 | goto end; |
163 | } | |
164 | ||
fc804d0f JG |
165 | /* Memory is initialized by the size increases. */ |
166 | new_buf = realloc(buffer->data, new_capacity); | |
167 | if (!new_buf) { | |
168 | ret = -1; | |
169 | goto end; | |
5966417e | 170 | } |
fc804d0f | 171 | buffer->data = new_buf; |
63557623 | 172 | buffer->_capacity = new_capacity; |
5966417e JG |
173 | end: |
174 | return ret; | |
175 | } | |
176 | ||
177 | /* Release any memory used by the dynamic buffer. */ | |
c90647fe | 178 | LTTNG_HIDDEN |
5966417e JG |
179 | void lttng_dynamic_buffer_reset(struct lttng_dynamic_buffer *buffer) |
180 | { | |
181 | if (!buffer) { | |
182 | return; | |
183 | } | |
184 | buffer->size = 0; | |
63557623 | 185 | buffer->_capacity = 0; |
5966417e JG |
186 | free(buffer->data); |
187 | } | |
201bd415 JG |
188 | |
189 | LTTNG_HIDDEN | |
190 | size_t lttng_dynamic_buffer_get_capacity_left( | |
191 | struct lttng_dynamic_buffer *buffer) | |
192 | { | |
193 | if (!buffer) { | |
194 | return 0; | |
195 | } | |
196 | return buffer->_capacity - buffer->size; | |
197 | } |