| 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 | #ifndef LTTNG_DYNAMIC_BUFFER_H |
| 19 | #define LTTNG_DYNAMIC_BUFFER_H |
| 20 | |
| 21 | #include <stddef.h> |
| 22 | #include <stdint.h> |
| 23 | |
| 24 | struct lttng_dynamic_buffer { |
| 25 | char *data; |
| 26 | size_t size; |
| 27 | size_t capacity; |
| 28 | }; |
| 29 | |
| 30 | void lttng_dynamic_buffer_init(struct lttng_dynamic_buffer *buffer); |
| 31 | |
| 32 | int lttng_dynamic_buffer_append(struct lttng_dynamic_buffer *buffer, |
| 33 | const void *buf, size_t len); |
| 34 | |
| 35 | int lttng_dynamic_buffer_append_buffer(struct lttng_dynamic_buffer *dst_buffer, |
| 36 | struct lttng_dynamic_buffer *src_buffer); |
| 37 | |
| 38 | /* |
| 39 | * Set the buffer's size to new_size. The capacity of the buffer will |
| 40 | * be expanded (if necessary) to accomodate new_size. Areas acquired by |
| 41 | * an enlarging new_size _will be zeroed_. |
| 42 | * |
| 43 | * Be careful to expand the buffer's size _before_ calling out external |
| 44 | * APIs (e.g. read(3)) which may populate the buffer as setting the size |
| 45 | * _after_ will zero-out the result of the operation. |
| 46 | */ |
| 47 | int lttng_dynamic_buffer_set_size(struct lttng_dynamic_buffer *buffer, |
| 48 | size_t new_size); |
| 49 | |
| 50 | /* |
| 51 | * Set the buffer's capacity to accomodate the new_capacity, allocating memory |
| 52 | * as necessary. The buffer's content is preserved. |
| 53 | * |
| 54 | * If the current size > new_capacity, the operation will fail. |
| 55 | */ |
| 56 | int lttng_dynamic_buffer_set_capacity(struct lttng_dynamic_buffer *buffer, |
| 57 | size_t new_capacity); |
| 58 | |
| 59 | /* Release any memory used by the dynamic buffer. */ |
| 60 | void lttng_dynamic_buffer_reset(struct lttng_dynamic_buffer *buffer); |
| 61 | |
| 62 | #endif /* LTTNG_DYNAMIC_BUFFER_H */ |