common: Add index allocator for error counters
[lttng-tools.git] / src / common / dynamic-buffer.c
CommitLineData
5966417e 1/*
ab5be9fa 2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5966417e 3 *
ab5be9fa 4 * SPDX-License-Identifier: LGPL-2.1-only
5966417e 5 *
5966417e
JG
6 */
7
8#include <common/dynamic-buffer.h>
fbd55aae 9#include <common/buffer-view.h>
5966417e
JG
10#include <common/utils.h>
11#include <assert.h>
12
df94ef0f
JG
13/*
14 * Round to (upper) power of two, val is returned if it already is a power of
15 * two.
16 */
5966417e
JG
17static
18size_t round_to_power_of_2(size_t val)
19{
5966417e 20 size_t rounded;
12aacdf6 21 const int order = utils_get_count_order_u64(val);
5966417e 22
5966417e
JG
23 assert(order >= 0);
24 rounded = (1ULL << order);
25 assert(rounded >= val);
26
27 return rounded;
28}
29
c90647fe 30LTTNG_HIDDEN
5966417e
JG
31void lttng_dynamic_buffer_init(struct lttng_dynamic_buffer *buffer)
32{
33 assert(buffer);
34 memset(buffer, 0, sizeof(*buffer));
35}
36
c90647fe 37LTTNG_HIDDEN
5966417e
JG
38int 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
63557623
JG
53 assert(buffer->_capacity >= buffer->size);
54 if (buffer->_capacity < (len + buffer->size)) {
5966417e 55 ret = lttng_dynamic_buffer_set_capacity(buffer,
63557623
JG
56 buffer->_capacity +
57 (len - (buffer->_capacity - buffer->size)));
5966417e
JG
58 if (ret) {
59 goto end;
60 }
61 }
62
63 memcpy(buffer->data + buffer->size, buf, len);
64 buffer->size += len;
65end:
66 return ret;
67}
68
c90647fe 69LTTNG_HIDDEN
5966417e 70int lttng_dynamic_buffer_append_buffer(struct lttng_dynamic_buffer *dst_buffer,
38b6c4f5 71 const struct lttng_dynamic_buffer *src_buffer)
5966417e
JG
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);
82end:
83 return ret;
84}
85
fbd55aae
JG
86LTTNG_HIDDEN
87int lttng_dynamic_buffer_append_view(struct lttng_dynamic_buffer *buffer,
88 const struct lttng_buffer_view *src)
89{
90 int ret;
91
92 if (!buffer || !src) {
93 ret = -1;
94 goto end;
95 }
96
97 ret = lttng_dynamic_buffer_append(buffer, src->data,
98 src->size);
99end:
100 return ret;
101}
102
c90647fe 103LTTNG_HIDDEN
5966417e
JG
104int lttng_dynamic_buffer_set_size(struct lttng_dynamic_buffer *buffer,
105 size_t new_size)
106{
107 int ret = 0;
108
109 if (!buffer) {
110 goto end;
111 }
112
113 if (new_size == buffer->size) {
114 goto end;
115 }
116
63557623 117 if (new_size > buffer->_capacity) {
5966417e
JG
118 ret = lttng_dynamic_buffer_set_capacity(buffer, new_size);
119 if (ret) {
120 goto end;
121 }
1c6def05 122
6ee96b4d 123 memset(buffer->data + buffer->size, 0, new_size - buffer->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 }
12aacdf6 137
5966417e
JG
138 buffer->size = new_size;
139end:
140 return ret;
141}
142
c90647fe 143LTTNG_HIDDEN
5966417e 144int lttng_dynamic_buffer_set_capacity(struct lttng_dynamic_buffer *buffer,
fc804d0f 145 size_t demanded_capacity)
5966417e
JG
146{
147 int ret = 0;
fc804d0f 148 void *new_buf;
e9347dad
JG
149 size_t new_capacity = demanded_capacity ?
150 round_to_power_of_2(demanded_capacity) : 0;
5966417e 151
fc804d0f
JG
152 if (!buffer || demanded_capacity < buffer->size) {
153 /*
154 * Shrinking a buffer's size by changing its capacity is
155 * unsupported.
156 */
5966417e
JG
157 ret = -1;
158 goto end;
159 }
160
63557623 161 if (new_capacity == buffer->_capacity) {
5966417e
JG
162 goto end;
163 }
164
fc804d0f
JG
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;
5966417e 170 }
12aacdf6 171
fc804d0f 172 buffer->data = new_buf;
63557623 173 buffer->_capacity = new_capacity;
5966417e
JG
174end:
175 return ret;
176}
177
178/* Release any memory used by the dynamic buffer. */
c90647fe 179LTTNG_HIDDEN
5966417e
JG
180void lttng_dynamic_buffer_reset(struct lttng_dynamic_buffer *buffer)
181{
182 if (!buffer) {
183 return;
184 }
12aacdf6 185
5966417e 186 buffer->size = 0;
63557623 187 buffer->_capacity = 0;
5966417e 188 free(buffer->data);
427d8469 189 buffer->data = NULL;
5966417e 190}
201bd415
JG
191
192LTTNG_HIDDEN
193size_t lttng_dynamic_buffer_get_capacity_left(
194 struct lttng_dynamic_buffer *buffer)
195{
196 if (!buffer) {
197 return 0;
198 }
12aacdf6 199
201bd415
JG
200 return buffer->_capacity - buffer->size;
201}
This page took 0.044318 seconds and 4 git commands to generate.