| 1 | /* |
| 2 | * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU Lesser General Public License, version 2.1 only, |
| 6 | * as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 11 | * for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU Lesser General Public License |
| 14 | * along with this program; if not, write to the Free Software Foundation, |
| 15 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 16 | */ |
| 17 | |
| 18 | #include <common/dynamic-buffer.h> |
| 19 | #include <common/macros.h> |
| 20 | #include <common/utils.h> |
| 21 | #include <assert.h> |
| 22 | |
| 23 | static |
| 24 | size_t round_to_power_of_2(size_t val) |
| 25 | { |
| 26 | int order; |
| 27 | size_t rounded; |
| 28 | |
| 29 | order = utils_get_count_order_u64(val); |
| 30 | assert(order >= 0); |
| 31 | rounded = (1ULL << order); |
| 32 | assert(rounded >= val); |
| 33 | |
| 34 | return rounded; |
| 35 | } |
| 36 | |
| 37 | void lttng_dynamic_buffer_init(struct lttng_dynamic_buffer *buffer) |
| 38 | { |
| 39 | assert(buffer); |
| 40 | memset(buffer, 0, sizeof(*buffer)); |
| 41 | } |
| 42 | |
| 43 | int lttng_dynamic_buffer_append(struct lttng_dynamic_buffer *buffer, |
| 44 | const void *buf, size_t len) |
| 45 | { |
| 46 | int ret = 0; |
| 47 | |
| 48 | if (!buffer || (!buf && len)) { |
| 49 | ret = -1; |
| 50 | goto end; |
| 51 | } |
| 52 | |
| 53 | if (len == 0) { |
| 54 | /* Not an error, no-op. */ |
| 55 | goto end; |
| 56 | } |
| 57 | |
| 58 | assert(buffer->capacity >= buffer->size); |
| 59 | if (buffer->capacity < (len + buffer->size)) { |
| 60 | ret = lttng_dynamic_buffer_set_capacity(buffer, |
| 61 | buffer->capacity + |
| 62 | (len - (buffer->capacity - buffer->size))); |
| 63 | if (ret) { |
| 64 | goto end; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | memcpy(buffer->data + buffer->size, buf, len); |
| 69 | buffer->size += len; |
| 70 | end: |
| 71 | return ret; |
| 72 | } |
| 73 | |
| 74 | int lttng_dynamic_buffer_append_buffer(struct lttng_dynamic_buffer *dst_buffer, |
| 75 | struct lttng_dynamic_buffer *src_buffer) |
| 76 | { |
| 77 | int ret; |
| 78 | |
| 79 | if (!dst_buffer || !src_buffer) { |
| 80 | ret = -1; |
| 81 | goto end; |
| 82 | } |
| 83 | |
| 84 | ret = lttng_dynamic_buffer_append(dst_buffer, src_buffer->data, |
| 85 | src_buffer->size); |
| 86 | end: |
| 87 | return ret; |
| 88 | } |
| 89 | |
| 90 | int lttng_dynamic_buffer_set_size(struct lttng_dynamic_buffer *buffer, |
| 91 | size_t new_size) |
| 92 | { |
| 93 | int ret = 0; |
| 94 | |
| 95 | if (!buffer) { |
| 96 | goto end; |
| 97 | } |
| 98 | |
| 99 | if (new_size == buffer->size) { |
| 100 | goto end; |
| 101 | } |
| 102 | |
| 103 | if (new_size > buffer->capacity) { |
| 104 | size_t original_size = buffer->size; |
| 105 | size_t original_capacity = buffer->capacity; |
| 106 | |
| 107 | ret = lttng_dynamic_buffer_set_capacity(buffer, new_size); |
| 108 | if (ret) { |
| 109 | goto end; |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * Zero-initialize the space that was left in the buffer at the |
| 114 | * before we increased its capacity (original capacity - original size). |
| 115 | * The newly acquired capacity (new capacity - original capacity) |
| 116 | * is zeroed by lttng_dynamic_buffer_set_capacity(). |
| 117 | */ |
| 118 | memset(buffer->data + original_size, 0, |
| 119 | original_capacity - original_size); |
| 120 | } else if (new_size > buffer->size) { |
| 121 | memset(buffer->data + buffer->size, 0, new_size - buffer->size); |
| 122 | } else { |
| 123 | /* |
| 124 | * Shrinking size. There is no need to zero-out the newly |
| 125 | * released memory as it will either be: |
| 126 | * - overwritten by lttng_dynamic_buffer_append, |
| 127 | * - expanded later, which will zero-out the memory |
| 128 | * |
| 129 | * Users of external APIs are encouraged to set the buffer's |
| 130 | * size _before_ making such calls. |
| 131 | */ |
| 132 | } |
| 133 | buffer->size = new_size; |
| 134 | end: |
| 135 | return ret; |
| 136 | } |
| 137 | |
| 138 | int lttng_dynamic_buffer_set_capacity(struct lttng_dynamic_buffer *buffer, |
| 139 | size_t demanded_capacity) |
| 140 | { |
| 141 | int ret = 0; |
| 142 | void *new_buf; |
| 143 | size_t new_capacity = round_to_power_of_2(demanded_capacity); |
| 144 | |
| 145 | if (!buffer || demanded_capacity < buffer->size) { |
| 146 | /* |
| 147 | * Shrinking a buffer's size by changing its capacity is |
| 148 | * unsupported. |
| 149 | */ |
| 150 | ret = -1; |
| 151 | goto end; |
| 152 | } |
| 153 | |
| 154 | if (new_capacity == buffer->capacity) { |
| 155 | goto end; |
| 156 | } |
| 157 | |
| 158 | /* Memory is initialized by the size increases. */ |
| 159 | new_buf = realloc(buffer->data, new_capacity); |
| 160 | if (!new_buf) { |
| 161 | ret = -1; |
| 162 | goto end; |
| 163 | } |
| 164 | buffer->data = new_buf; |
| 165 | buffer->capacity = new_capacity; |
| 166 | end: |
| 167 | return ret; |
| 168 | } |
| 169 | |
| 170 | /* Release any memory used by the dynamic buffer. */ |
| 171 | void lttng_dynamic_buffer_reset(struct lttng_dynamic_buffer *buffer) |
| 172 | { |
| 173 | if (!buffer) { |
| 174 | return; |
| 175 | } |
| 176 | buffer->size = 0; |
| 177 | buffer->capacity = 0; |
| 178 | free(buffer->data); |
| 179 | } |