| 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 | ret = lttng_dynamic_buffer_set_capacity(buffer, new_size); |
| 105 | if (ret) { |
| 106 | goto end; |
| 107 | } |
| 108 | } else if (new_size > buffer->size) { |
| 109 | memset(buffer->data + buffer->size, 0, new_size - buffer->size); |
| 110 | } else { |
| 111 | /* |
| 112 | * Shrinking size. There is no need to zero-out the newly |
| 113 | * released memory as it will either be: |
| 114 | * - overwritten by lttng_dynamic_buffer_append, |
| 115 | * - expanded later, which will zero-out the memory |
| 116 | * |
| 117 | * Users of external APIs are encouraged to set the buffer's |
| 118 | * size _before_ making such calls. |
| 119 | */ |
| 120 | } |
| 121 | buffer->size = new_size; |
| 122 | end: |
| 123 | return ret; |
| 124 | } |
| 125 | |
| 126 | int lttng_dynamic_buffer_set_capacity(struct lttng_dynamic_buffer *buffer, |
| 127 | size_t new_capacity) |
| 128 | { |
| 129 | int ret = 0; |
| 130 | size_t rounded_capacity = round_to_power_of_2(new_capacity); |
| 131 | |
| 132 | if (!buffer || new_capacity < buffer->size) { |
| 133 | ret = -1; |
| 134 | goto end; |
| 135 | } |
| 136 | |
| 137 | if (rounded_capacity == buffer->capacity) { |
| 138 | goto end; |
| 139 | } |
| 140 | |
| 141 | if (!buffer->data) { |
| 142 | buffer->data = zmalloc(rounded_capacity); |
| 143 | if (!buffer->data) { |
| 144 | ret = -1; |
| 145 | goto end; |
| 146 | } |
| 147 | } else { |
| 148 | void *new_buf; |
| 149 | |
| 150 | new_buf = realloc(buffer->data, rounded_capacity); |
| 151 | if (new_buf) { |
| 152 | if (rounded_capacity > buffer->capacity) { |
| 153 | memset(new_buf + buffer->capacity, 0, |
| 154 | rounded_capacity - buffer->capacity); |
| 155 | } |
| 156 | } else { |
| 157 | /* Realloc failed, try to acquire a new block. */ |
| 158 | new_buf = zmalloc(rounded_capacity); |
| 159 | if (!new_buf) { |
| 160 | ret = -1; |
| 161 | goto end; |
| 162 | } |
| 163 | memcpy(new_buf, buffer->data, buffer->size); |
| 164 | free(buffer->data); |
| 165 | } |
| 166 | buffer->data = new_buf; |
| 167 | } |
| 168 | buffer->capacity = rounded_capacity; |
| 169 | end: |
| 170 | return ret; |
| 171 | } |
| 172 | |
| 173 | /* Release any memory used by the dynamic buffer. */ |
| 174 | void lttng_dynamic_buffer_reset(struct lttng_dynamic_buffer *buffer) |
| 175 | { |
| 176 | if (!buffer) { |
| 177 | return; |
| 178 | } |
| 179 | buffer->size = 0; |
| 180 | buffer->capacity = 0; |
| 181 | free(buffer->data); |
| 182 | } |