Deliverables 3 and 4
[deliverable/lttng-tools.git] / src / common / dynamic-buffer.h
1 /*
2 * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * This library 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 library 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 library; 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 /*
36 * Set the buffer's size to new_size. The capacity of the buffer will
37 * be expanded (if necessary) to accomodate new_size. Areas acquired by
38 * an enlarging new_size _will be zeroed_.
39 *
40 * Be careful to expand the buffer's size _before_ calling out external
41 * APIs (e.g. read(3)) which may populate the buffer as setting the size
42 * _after_ will zero-out the result of the operation.
43 */
44 int lttng_dynamic_buffer_set_size(struct lttng_dynamic_buffer *buffer,
45 size_t new_size);
46
47 /*
48 * Set the buffer's capacity to accomodate the new_capacity, allocating memory
49 * as necessary. The buffer's content is preserved.
50 *
51 * If the current size > new_capacity, the operation will fail.
52 */
53 int lttng_dynamic_buffer_set_capacity(struct lttng_dynamic_buffer *buffer,
54 size_t new_capacity);
55
56 /* Release any memory used by the dynamic buffer. */
57 void lttng_dynamic_buffer_reset(struct lttng_dynamic_buffer *buffer);
58
59 #endif /* LTTNG_DYNAMIC_BUFFER_H */
This page took 0.038606 seconds and 5 git commands to generate.