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