| 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 | if ((buffer->capacity - buffer->size) < len) { |
| 59 | ret = lttng_dynamic_buffer_set_capacity(buffer, |
| 60 | buffer->capacity + |
| 61 | (len - (buffer->capacity - buffer->size))); |
| 62 | if (ret) { |
| 63 | goto end; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | memcpy(buffer->data + buffer->size, buf, len); |
| 68 | buffer->size += len; |
| 69 | end: |
| 70 | return ret; |
| 71 | } |
| 72 | |
| 73 | int lttng_dynamic_buffer_append_buffer(struct lttng_dynamic_buffer *dst_buffer, |
| 74 | struct lttng_dynamic_buffer *src_buffer) |
| 75 | { |
| 76 | int ret; |
| 77 | |
| 78 | if (!dst_buffer || !src_buffer) { |
| 79 | ret = -1; |
| 80 | goto end; |
| 81 | } |
| 82 | |
| 83 | ret = lttng_dynamic_buffer_append(dst_buffer, src_buffer->data, |
| 84 | src_buffer->size); |
| 85 | end: |
| 86 | return ret; |
| 87 | } |
| 88 | |
| 89 | int lttng_dynamic_buffer_set_size(struct lttng_dynamic_buffer *buffer, |
| 90 | size_t new_size) |
| 91 | { |
| 92 | int ret = 0; |
| 93 | |
| 94 | if (!buffer) { |
| 95 | goto end; |
| 96 | } |
| 97 | |
| 98 | if (new_size == buffer->size) { |
| 99 | goto end; |
| 100 | } |
| 101 | |
| 102 | if (new_size > buffer->capacity) { |
| 103 | ret = lttng_dynamic_buffer_set_capacity(buffer, new_size); |
| 104 | if (ret) { |
| 105 | goto end; |
| 106 | } |
| 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; |
| 121 | end: |
| 122 | return ret; |
| 123 | } |
| 124 | |
| 125 | int lttng_dynamic_buffer_set_capacity(struct lttng_dynamic_buffer *buffer, |
| 126 | size_t new_capacity) |
| 127 | { |
| 128 | int ret = 0; |
| 129 | size_t rounded_capacity = round_to_power_of_2(new_capacity); |
| 130 | |
| 131 | if (!buffer || new_capacity < buffer->size) { |
| 132 | ret = -1; |
| 133 | goto end; |
| 134 | } |
| 135 | |
| 136 | if (rounded_capacity == buffer->capacity) { |
| 137 | goto end; |
| 138 | } |
| 139 | |
| 140 | if (!buffer->data) { |
| 141 | buffer->data = zmalloc(rounded_capacity); |
| 142 | if (!buffer->data) { |
| 143 | ret = -1; |
| 144 | goto end; |
| 145 | } |
| 146 | } else { |
| 147 | void *new_buf; |
| 148 | |
| 149 | new_buf = realloc(buffer->data, rounded_capacity); |
| 150 | if (new_buf) { |
| 151 | if (rounded_capacity > buffer->capacity) { |
| 152 | memset(new_buf + buffer->capacity, 0, |
| 153 | rounded_capacity - buffer->capacity); |
| 154 | } |
| 155 | } else { |
| 156 | /* Realloc failed, try to acquire a new block. */ |
| 157 | new_buf = zmalloc(rounded_capacity); |
| 158 | if (!new_buf) { |
| 159 | ret = -1; |
| 160 | goto end; |
| 161 | } |
| 162 | memcpy(new_buf, buffer->data, buffer->size); |
| 163 | free(buffer->data); |
| 164 | } |
| 165 | buffer->data = new_buf; |
| 166 | } |
| 167 | buffer->capacity = rounded_capacity; |
| 168 | end: |
| 169 | return ret; |
| 170 | } |
| 171 | |
| 172 | /* Release any memory used by the dynamic buffer. */ |
| 173 | void lttng_dynamic_buffer_reset(struct lttng_dynamic_buffer *buffer) |
| 174 | { |
| 175 | if (!buffer) { |
| 176 | return; |
| 177 | } |
| 178 | buffer->size = 0; |
| 179 | buffer->capacity = 0; |
| 180 | free(buffer->data); |
| 181 | } |