Make lttng_dynamic_buffer_append_buffer const-correct
[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>
5966417e
JG
9#include <common/utils.h>
10#include <assert.h>
11
df94ef0f
JG
12/*
13 * Round to (upper) power of two, val is returned if it already is a power of
14 * two.
15 */
5966417e
JG
16static
17size_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
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
c90647fe 86LTTNG_HIDDEN
5966417e
JG
87int 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
63557623 100 if (new_size > buffer->_capacity) {
5966417e
JG
101 ret = lttng_dynamic_buffer_set_capacity(buffer, new_size);
102 if (ret) {
103 goto end;
104 }
1c6def05 105
6ee96b4d 106 memset(buffer->data + buffer->size, 0, new_size - buffer->size);
5966417e
JG
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;
121end:
122 return ret;
123}
124
c90647fe 125LTTNG_HIDDEN
5966417e 126int lttng_dynamic_buffer_set_capacity(struct lttng_dynamic_buffer *buffer,
fc804d0f 127 size_t demanded_capacity)
5966417e
JG
128{
129 int ret = 0;
fc804d0f 130 void *new_buf;
e9347dad
JG
131 size_t new_capacity = demanded_capacity ?
132 round_to_power_of_2(demanded_capacity) : 0;
5966417e 133
fc804d0f
JG
134 if (!buffer || demanded_capacity < buffer->size) {
135 /*
136 * Shrinking a buffer's size by changing its capacity is
137 * unsupported.
138 */
5966417e
JG
139 ret = -1;
140 goto end;
141 }
142
63557623 143 if (new_capacity == buffer->_capacity) {
5966417e
JG
144 goto end;
145 }
146
fc804d0f
JG
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;
5966417e 152 }
fc804d0f 153 buffer->data = new_buf;
63557623 154 buffer->_capacity = new_capacity;
5966417e
JG
155end:
156 return ret;
157}
158
159/* Release any memory used by the dynamic buffer. */
c90647fe 160LTTNG_HIDDEN
5966417e
JG
161void lttng_dynamic_buffer_reset(struct lttng_dynamic_buffer *buffer)
162{
163 if (!buffer) {
164 return;
165 }
166 buffer->size = 0;
63557623 167 buffer->_capacity = 0;
5966417e
JG
168 free(buffer->data);
169}
201bd415
JG
170
171LTTNG_HIDDEN
172size_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.076741 seconds and 5 git commands to generate.