| 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 | size_t rounded; |
| 21 | const int order = utils_get_count_order_u64(val); |
| 22 | |
| 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_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); |
| 99 | end: |
| 100 | return ret; |
| 101 | } |
| 102 | |
| 103 | LTTNG_HIDDEN |
| 104 | int 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 | |
| 117 | if (new_size > buffer->_capacity) { |
| 118 | ret = lttng_dynamic_buffer_set_capacity(buffer, new_size); |
| 119 | if (ret) { |
| 120 | goto end; |
| 121 | } |
| 122 | |
| 123 | memset(buffer->data + buffer->size, 0, new_size - buffer->size); |
| 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 | |
| 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 | |
| 172 | buffer->data = new_buf; |
| 173 | buffer->_capacity = new_capacity; |
| 174 | end: |
| 175 | return ret; |
| 176 | } |
| 177 | |
| 178 | /* Release any memory used by the dynamic buffer. */ |
| 179 | LTTNG_HIDDEN |
| 180 | void lttng_dynamic_buffer_reset(struct lttng_dynamic_buffer *buffer) |
| 181 | { |
| 182 | if (!buffer) { |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | buffer->size = 0; |
| 187 | buffer->_capacity = 0; |
| 188 | free(buffer->data); |
| 189 | } |
| 190 | |
| 191 | LTTNG_HIDDEN |
| 192 | size_t lttng_dynamic_buffer_get_capacity_left( |
| 193 | struct lttng_dynamic_buffer *buffer) |
| 194 | { |
| 195 | if (!buffer) { |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | return buffer->_capacity - buffer->size; |
| 200 | } |