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