consumerd: tag metadata channel as being part of a live session
[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/buffer-view.h>
10 #include <common/utils.h>
11 #include <assert.h>
12
13 /*
14 * Round to (upper) power of two, val is returned if it already is a power of
15 * two.
16 */
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
31 LTTNG_HIDDEN
32 void lttng_dynamic_buffer_init(struct lttng_dynamic_buffer *buffer)
33 {
34 assert(buffer);
35 memset(buffer, 0, sizeof(*buffer));
36 }
37
38 LTTNG_HIDDEN
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
54 assert(buffer->_capacity >= buffer->size);
55 if (buffer->_capacity < (len + buffer->size)) {
56 ret = lttng_dynamic_buffer_set_capacity(buffer,
57 buffer->_capacity +
58 (len - (buffer->_capacity - buffer->size)));
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
70 LTTNG_HIDDEN
71 int lttng_dynamic_buffer_append_buffer(struct lttng_dynamic_buffer *dst_buffer,
72 const struct lttng_dynamic_buffer *src_buffer)
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
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
104 LTTNG_HIDDEN
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
118 if (new_size > buffer->_capacity) {
119 ret = lttng_dynamic_buffer_set_capacity(buffer, new_size);
120 if (ret) {
121 goto end;
122 }
123
124 memset(buffer->data + buffer->size, 0, new_size - buffer->size);
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
143 LTTNG_HIDDEN
144 int lttng_dynamic_buffer_set_capacity(struct lttng_dynamic_buffer *buffer,
145 size_t demanded_capacity)
146 {
147 int ret = 0;
148 void *new_buf;
149 size_t new_capacity = demanded_capacity ?
150 round_to_power_of_2(demanded_capacity) : 0;
151
152 if (!buffer || demanded_capacity < buffer->size) {
153 /*
154 * Shrinking a buffer's size by changing its capacity is
155 * unsupported.
156 */
157 ret = -1;
158 goto end;
159 }
160
161 if (new_capacity == buffer->_capacity) {
162 goto end;
163 }
164
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;
170 }
171 buffer->data = new_buf;
172 buffer->_capacity = new_capacity;
173 end:
174 return ret;
175 }
176
177 /* Release any memory used by the dynamic buffer. */
178 LTTNG_HIDDEN
179 void lttng_dynamic_buffer_reset(struct lttng_dynamic_buffer *buffer)
180 {
181 if (!buffer) {
182 return;
183 }
184 buffer->size = 0;
185 buffer->_capacity = 0;
186 free(buffer->data);
187 }
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 }
This page took 0.032124 seconds and 4 git commands to generate.