Move to kernel style SPDX license identifiers
[lttng-tools.git] / src / common / dynamic-buffer.h
CommitLineData
5966417e 1/*
ab5be9fa 2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5966417e 3 *
ab5be9fa 4 * SPDX-License-Identifier: LGPL-2.1-only
5966417e 5 *
5966417e
JG
6 */
7
8#ifndef LTTNG_DYNAMIC_BUFFER_H
9#define LTTNG_DYNAMIC_BUFFER_H
10
11#include <stddef.h>
12#include <stdint.h>
c90647fe 13#include <common/macros.h>
5966417e
JG
14
15struct lttng_dynamic_buffer {
16 char *data;
63557623 17 /* size is the buffer's currently used capacity. */
5966417e 18 size_t size;
63557623
JG
19 /*
20 * capacity shall not be accessed by users directly, it is meant for
21 * internal use only.
22 */
23 size_t _capacity;
5966417e
JG
24};
25
63557623
JG
26/*
27 * Initialize a dynamic buffer. This performs no allocation and is meant
28 * to be used instead of memset or explicit initialization of the buffer.
29 */
c90647fe 30LTTNG_HIDDEN
5966417e
JG
31void lttng_dynamic_buffer_init(struct lttng_dynamic_buffer *buffer);
32
63557623
JG
33/*
34 * Append the content of a raw memory buffer to the end of a dynamic buffer
35 * (after its current "size"). The dynamic buffer's size is increased by
36 * "len", and its capacity is adjusted automatically.
37 */
c90647fe 38LTTNG_HIDDEN
5966417e
JG
39int lttng_dynamic_buffer_append(struct lttng_dynamic_buffer *buffer,
40 const void *buf, size_t len);
41
63557623
JG
42/*
43 * Performs the same action as lttng_dynamic_buffer_append(), but using another
44 * dynamic buffer as the source buffer. The source buffer's size is used in lieu
45 * of "len".
46 */
c90647fe 47LTTNG_HIDDEN
5966417e
JG
48int lttng_dynamic_buffer_append_buffer(struct lttng_dynamic_buffer *dst_buffer,
49 struct lttng_dynamic_buffer *src_buffer);
50
51/*
52 * Set the buffer's size to new_size. The capacity of the buffer will
53 * be expanded (if necessary) to accomodate new_size. Areas acquired by
63557623 54 * a size increase will be zeroed.
5966417e
JG
55 *
56 * Be careful to expand the buffer's size _before_ calling out external
57 * APIs (e.g. read(3)) which may populate the buffer as setting the size
63557623
JG
58 * after will zero-out the result of the operation.
59 *
60 * Shrinking a buffer does not zero the old content. If the buffer may contain
61 * sensititve information, it must be cleared manually _before_ changing the
62 * size.
63 *
64 * NOTE: It is striclty _invalid_ to access memory after _size_, regardless
65 * of prior calls to set_capacity().
5966417e 66 */
c90647fe 67LTTNG_HIDDEN
5966417e
JG
68int lttng_dynamic_buffer_set_size(struct lttng_dynamic_buffer *buffer,
69 size_t new_size);
70
71/*
72 * Set the buffer's capacity to accomodate the new_capacity, allocating memory
63557623
JG
73 * as necessary. The buffer's content is preserved. Setting a buffer's capacity
74 * is meant as a _hint_ to the underlying buffer and is only optimization; no
75 * guarantee is offered that subsequent calls to append or set_size will succeed.
5966417e
JG
76 *
77 * If the current size > new_capacity, the operation will fail.
78 */
c90647fe 79LTTNG_HIDDEN
5966417e
JG
80int lttng_dynamic_buffer_set_capacity(struct lttng_dynamic_buffer *buffer,
81 size_t new_capacity);
82
83/* Release any memory used by the dynamic buffer. */
c90647fe 84LTTNG_HIDDEN
5966417e
JG
85void lttng_dynamic_buffer_reset(struct lttng_dynamic_buffer *buffer);
86
201bd415
JG
87/* Get the space left in the buffer before a new resize is needed. */
88LTTNG_HIDDEN
89size_t lttng_dynamic_buffer_get_capacity_left(
90 struct lttng_dynamic_buffer *buffer);
91
5966417e 92#endif /* LTTNG_DYNAMIC_BUFFER_H */
This page took 0.034646 seconds and 4 git commands to generate.