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