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> | |
5966417e JG |
19 | #include <common/utils.h> |
20 | #include <assert.h> | |
21 | ||
df94ef0f JG |
22 | /* |
23 | * Round to (upper) power of two, val is returned if it already is a power of | |
24 | * two. | |
25 | */ | |
5966417e JG |
26 | static |
27 | size_t round_to_power_of_2(size_t val) | |
28 | { | |
29 | int order; | |
30 | size_t rounded; | |
31 | ||
32 | order = utils_get_count_order_u64(val); | |
33 | assert(order >= 0); | |
34 | rounded = (1ULL << order); | |
35 | assert(rounded >= val); | |
36 | ||
37 | return rounded; | |
38 | } | |
39 | ||
c90647fe | 40 | LTTNG_HIDDEN |
5966417e JG |
41 | void lttng_dynamic_buffer_init(struct lttng_dynamic_buffer *buffer) |
42 | { | |
43 | assert(buffer); | |
44 | memset(buffer, 0, sizeof(*buffer)); | |
45 | } | |
46 | ||
c90647fe | 47 | LTTNG_HIDDEN |
5966417e JG |
48 | int lttng_dynamic_buffer_append(struct lttng_dynamic_buffer *buffer, |
49 | const void *buf, size_t len) | |
50 | { | |
51 | int ret = 0; | |
52 | ||
53 | if (!buffer || (!buf && len)) { | |
54 | ret = -1; | |
55 | goto end; | |
56 | } | |
57 | ||
58 | if (len == 0) { | |
59 | /* Not an error, no-op. */ | |
60 | goto end; | |
61 | } | |
62 | ||
63557623 JG |
63 | assert(buffer->_capacity >= buffer->size); |
64 | if (buffer->_capacity < (len + buffer->size)) { | |
5966417e | 65 | ret = lttng_dynamic_buffer_set_capacity(buffer, |
63557623 JG |
66 | buffer->_capacity + |
67 | (len - (buffer->_capacity - buffer->size))); | |
5966417e JG |
68 | if (ret) { |
69 | goto end; | |
70 | } | |
71 | } | |
72 | ||
73 | memcpy(buffer->data + buffer->size, buf, len); | |
74 | buffer->size += len; | |
75 | end: | |
76 | return ret; | |
77 | } | |
78 | ||
c90647fe | 79 | LTTNG_HIDDEN |
5966417e JG |
80 | int lttng_dynamic_buffer_append_buffer(struct lttng_dynamic_buffer *dst_buffer, |
81 | struct lttng_dynamic_buffer *src_buffer) | |
82 | { | |
83 | int ret; | |
84 | ||
85 | if (!dst_buffer || !src_buffer) { | |
86 | ret = -1; | |
87 | goto end; | |
88 | } | |
89 | ||
90 | ret = lttng_dynamic_buffer_append(dst_buffer, src_buffer->data, | |
91 | src_buffer->size); | |
92 | end: | |
93 | return ret; | |
94 | } | |
95 | ||
c90647fe | 96 | LTTNG_HIDDEN |
5966417e JG |
97 | int lttng_dynamic_buffer_set_size(struct lttng_dynamic_buffer *buffer, |
98 | size_t new_size) | |
99 | { | |
100 | int ret = 0; | |
101 | ||
102 | if (!buffer) { | |
103 | goto end; | |
104 | } | |
105 | ||
106 | if (new_size == buffer->size) { | |
107 | goto end; | |
108 | } | |
109 | ||
63557623 | 110 | if (new_size > buffer->_capacity) { |
1c6def05 | 111 | size_t original_size = buffer->size; |
63557623 | 112 | size_t original_capacity = buffer->_capacity; |
1c6def05 | 113 | |
5966417e JG |
114 | ret = lttng_dynamic_buffer_set_capacity(buffer, new_size); |
115 | if (ret) { | |
116 | goto end; | |
117 | } | |
1c6def05 JG |
118 | |
119 | /* | |
120 | * Zero-initialize the space that was left in the buffer at the | |
121 | * before we increased its capacity (original capacity - original size). | |
122 | * The newly acquired capacity (new capacity - original capacity) | |
123 | * is zeroed by lttng_dynamic_buffer_set_capacity(). | |
124 | */ | |
125 | memset(buffer->data + original_size, 0, | |
126 | original_capacity - original_size); | |
5966417e JG |
127 | } else if (new_size > buffer->size) { |
128 | memset(buffer->data + buffer->size, 0, new_size - buffer->size); | |
129 | } else { | |
130 | /* | |
131 | * Shrinking size. There is no need to zero-out the newly | |
132 | * released memory as it will either be: | |
133 | * - overwritten by lttng_dynamic_buffer_append, | |
134 | * - expanded later, which will zero-out the memory | |
135 | * | |
136 | * Users of external APIs are encouraged to set the buffer's | |
137 | * size _before_ making such calls. | |
138 | */ | |
139 | } | |
140 | buffer->size = new_size; | |
141 | end: | |
142 | return ret; | |
143 | } | |
144 | ||
c90647fe | 145 | LTTNG_HIDDEN |
5966417e | 146 | int lttng_dynamic_buffer_set_capacity(struct lttng_dynamic_buffer *buffer, |
fc804d0f | 147 | size_t demanded_capacity) |
5966417e JG |
148 | { |
149 | int ret = 0; | |
fc804d0f JG |
150 | void *new_buf; |
151 | size_t new_capacity = round_to_power_of_2(demanded_capacity); | |
5966417e | 152 | |
fc804d0f JG |
153 | if (!buffer || demanded_capacity < buffer->size) { |
154 | /* | |
155 | * Shrinking a buffer's size by changing its capacity is | |
156 | * unsupported. | |
157 | */ | |
5966417e JG |
158 | ret = -1; |
159 | goto end; | |
160 | } | |
161 | ||
63557623 | 162 | if (new_capacity == buffer->_capacity) { |
5966417e JG |
163 | goto end; |
164 | } | |
165 | ||
fc804d0f JG |
166 | /* Memory is initialized by the size increases. */ |
167 | new_buf = realloc(buffer->data, new_capacity); | |
168 | if (!new_buf) { | |
169 | ret = -1; | |
170 | goto end; | |
5966417e | 171 | } |
fc804d0f | 172 | buffer->data = new_buf; |
63557623 | 173 | buffer->_capacity = new_capacity; |
5966417e JG |
174 | end: |
175 | return ret; | |
176 | } | |
177 | ||
178 | /* Release any memory used by the dynamic buffer. */ | |
c90647fe | 179 | LTTNG_HIDDEN |
5966417e JG |
180 | void lttng_dynamic_buffer_reset(struct lttng_dynamic_buffer *buffer) |
181 | { | |
182 | if (!buffer) { | |
183 | return; | |
184 | } | |
185 | buffer->size = 0; | |
63557623 | 186 | buffer->_capacity = 0; |
5966417e JG |
187 | free(buffer->data); |
188 | } |