Commit | Line | Data |
---|---|---|
5966417e JG |
1 | /* |
2 | * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
3 | * | |
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. | |
7 | * | |
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 | |
11 | * for more details. | |
12 | * | |
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 | |
16 | */ | |
17 | ||
18 | #include <common/dynamic-buffer.h> | |
19 | #include <common/macros.h> | |
20 | #include <common/utils.h> | |
21 | #include <assert.h> | |
22 | ||
23 | static | |
24 | size_t round_to_power_of_2(size_t val) | |
25 | { | |
26 | int order; | |
27 | size_t rounded; | |
28 | ||
29 | order = utils_get_count_order_u64(val); | |
30 | assert(order >= 0); | |
31 | rounded = (1ULL << order); | |
32 | assert(rounded >= val); | |
33 | ||
34 | return rounded; | |
35 | } | |
36 | ||
37 | void lttng_dynamic_buffer_init(struct lttng_dynamic_buffer *buffer) | |
38 | { | |
39 | assert(buffer); | |
40 | memset(buffer, 0, sizeof(*buffer)); | |
41 | } | |
42 | ||
43 | int lttng_dynamic_buffer_append(struct lttng_dynamic_buffer *buffer, | |
44 | const void *buf, size_t len) | |
45 | { | |
46 | int ret = 0; | |
47 | ||
48 | if (!buffer || (!buf && len)) { | |
49 | ret = -1; | |
50 | goto end; | |
51 | } | |
52 | ||
53 | if (len == 0) { | |
54 | /* Not an error, no-op. */ | |
55 | goto end; | |
56 | } | |
57 | ||
4270baea | 58 | assert(buffer->capacity >= buffer->size); |
83063067 | 59 | if (buffer->capacity < (len + buffer->size)) { |
5966417e JG |
60 | ret = lttng_dynamic_buffer_set_capacity(buffer, |
61 | buffer->capacity + | |
62 | (len - (buffer->capacity - buffer->size))); | |
63 | if (ret) { | |
64 | goto end; | |
65 | } | |
66 | } | |
67 | ||
68 | memcpy(buffer->data + buffer->size, buf, len); | |
69 | buffer->size += len; | |
70 | end: | |
71 | return ret; | |
72 | } | |
73 | ||
74 | int lttng_dynamic_buffer_append_buffer(struct lttng_dynamic_buffer *dst_buffer, | |
75 | struct lttng_dynamic_buffer *src_buffer) | |
76 | { | |
77 | int ret; | |
78 | ||
79 | if (!dst_buffer || !src_buffer) { | |
80 | ret = -1; | |
81 | goto end; | |
82 | } | |
83 | ||
84 | ret = lttng_dynamic_buffer_append(dst_buffer, src_buffer->data, | |
85 | src_buffer->size); | |
86 | end: | |
87 | return ret; | |
88 | } | |
89 | ||
90 | int lttng_dynamic_buffer_set_size(struct lttng_dynamic_buffer *buffer, | |
91 | size_t new_size) | |
92 | { | |
93 | int ret = 0; | |
94 | ||
95 | if (!buffer) { | |
96 | goto end; | |
97 | } | |
98 | ||
99 | if (new_size == buffer->size) { | |
100 | goto end; | |
101 | } | |
102 | ||
103 | if (new_size > buffer->capacity) { | |
1c6def05 JG |
104 | size_t original_size = buffer->size; |
105 | size_t original_capacity = buffer->capacity; | |
106 | ||
5966417e JG |
107 | ret = lttng_dynamic_buffer_set_capacity(buffer, new_size); |
108 | if (ret) { | |
109 | goto end; | |
110 | } | |
1c6def05 JG |
111 | |
112 | /* | |
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(). | |
117 | */ | |
118 | memset(buffer->data + original_size, 0, | |
119 | original_capacity - original_size); | |
5966417e JG |
120 | } else if (new_size > buffer->size) { |
121 | memset(buffer->data + buffer->size, 0, new_size - buffer->size); | |
122 | } else { | |
123 | /* | |
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 | |
128 | * | |
129 | * Users of external APIs are encouraged to set the buffer's | |
130 | * size _before_ making such calls. | |
131 | */ | |
132 | } | |
133 | buffer->size = new_size; | |
134 | end: | |
135 | return ret; | |
136 | } | |
137 | ||
138 | int lttng_dynamic_buffer_set_capacity(struct lttng_dynamic_buffer *buffer, | |
139 | size_t new_capacity) | |
140 | { | |
141 | int ret = 0; | |
142 | size_t rounded_capacity = round_to_power_of_2(new_capacity); | |
143 | ||
144 | if (!buffer || new_capacity < buffer->size) { | |
145 | ret = -1; | |
146 | goto end; | |
147 | } | |
148 | ||
149 | if (rounded_capacity == buffer->capacity) { | |
150 | goto end; | |
151 | } | |
152 | ||
153 | if (!buffer->data) { | |
154 | buffer->data = zmalloc(rounded_capacity); | |
155 | if (!buffer->data) { | |
156 | ret = -1; | |
157 | goto end; | |
158 | } | |
159 | } else { | |
160 | void *new_buf; | |
161 | ||
162 | new_buf = realloc(buffer->data, rounded_capacity); | |
163 | if (new_buf) { | |
164 | if (rounded_capacity > buffer->capacity) { | |
165 | memset(new_buf + buffer->capacity, 0, | |
166 | rounded_capacity - buffer->capacity); | |
167 | } | |
168 | } else { | |
169 | /* Realloc failed, try to acquire a new block. */ | |
170 | new_buf = zmalloc(rounded_capacity); | |
171 | if (!new_buf) { | |
172 | ret = -1; | |
173 | goto end; | |
174 | } | |
175 | memcpy(new_buf, buffer->data, buffer->size); | |
176 | free(buffer->data); | |
177 | } | |
178 | buffer->data = new_buf; | |
179 | } | |
180 | buffer->capacity = rounded_capacity; | |
181 | end: | |
182 | return ret; | |
183 | } | |
184 | ||
185 | /* Release any memory used by the dynamic buffer. */ | |
186 | void lttng_dynamic_buffer_reset(struct lttng_dynamic_buffer *buffer) | |
187 | { | |
188 | if (!buffer) { | |
189 | return; | |
190 | } | |
191 | buffer->size = 0; | |
192 | buffer->capacity = 0; | |
193 | free(buffer->data); | |
194 | } |