Commit | Line | Data |
---|---|---|
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 | 14 | |
7966af57 SM |
15 | #ifdef __cplusplus |
16 | extern "C" { | |
17 | #endif | |
18 | ||
fbd55aae JG |
19 | struct lttng_buffer_view; |
20 | ||
5966417e JG |
21 | struct lttng_dynamic_buffer { |
22 | char *data; | |
63557623 | 23 | /* size is the buffer's currently used capacity. */ |
5966417e | 24 | size_t size; |
63557623 JG |
25 | /* |
26 | * capacity shall not be accessed by users directly, it is meant for | |
27 | * internal use only. | |
28 | */ | |
29 | size_t _capacity; | |
5966417e JG |
30 | }; |
31 | ||
63557623 JG |
32 | /* |
33 | * Initialize a dynamic buffer. This performs no allocation and is meant | |
34 | * to be used instead of memset or explicit initialization of the buffer. | |
35 | */ | |
5966417e JG |
36 | void lttng_dynamic_buffer_init(struct lttng_dynamic_buffer *buffer); |
37 | ||
63557623 JG |
38 | /* |
39 | * Append the content of a raw memory buffer to the end of a dynamic buffer | |
40 | * (after its current "size"). The dynamic buffer's size is increased by | |
41 | * "len", and its capacity is adjusted automatically. | |
42 | */ | |
5966417e JG |
43 | int lttng_dynamic_buffer_append(struct lttng_dynamic_buffer *buffer, |
44 | const void *buf, size_t len); | |
45 | ||
63557623 JG |
46 | /* |
47 | * Performs the same action as lttng_dynamic_buffer_append(), but using another | |
48 | * dynamic buffer as the source buffer. The source buffer's size is used in lieu | |
49 | * of "len". | |
50 | */ | |
5966417e | 51 | int lttng_dynamic_buffer_append_buffer(struct lttng_dynamic_buffer *dst_buffer, |
38b6c4f5 | 52 | const struct lttng_dynamic_buffer *src_buffer); |
5966417e | 53 | |
fbd55aae JG |
54 | /* |
55 | * Performs the same action as lttng_dynamic_buffer_append(), but using a | |
56 | * buffer view as the source buffer. The source buffer's size is used in lieu | |
57 | * of "len". | |
58 | */ | |
fbd55aae JG |
59 | int lttng_dynamic_buffer_append_view(struct lttng_dynamic_buffer *buffer, |
60 | const struct lttng_buffer_view *view); | |
61 | ||
5966417e JG |
62 | /* |
63 | * Set the buffer's size to new_size. The capacity of the buffer will | |
d49487dc | 64 | * be expanded (if necessary) to accommodates new_size. Areas acquired by |
63557623 | 65 | * a size increase will be zeroed. |
5966417e JG |
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 | |
63557623 JG |
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(). | |
5966417e JG |
77 | */ |
78 | int lttng_dynamic_buffer_set_size(struct lttng_dynamic_buffer *buffer, | |
79 | size_t new_size); | |
80 | ||
81 | /* | |
d49487dc | 82 | * Set the buffer's capacity to accommodates the new_capacity, allocating memory |
63557623 JG |
83 | * as necessary. The buffer's content is preserved. Setting a buffer's capacity |
84 | * is meant as a _hint_ to the underlying buffer and is only optimization; no | |
85 | * guarantee is offered that subsequent calls to append or set_size will succeed. | |
5966417e JG |
86 | * |
87 | * If the current size > new_capacity, the operation will fail. | |
88 | */ | |
89 | int lttng_dynamic_buffer_set_capacity(struct lttng_dynamic_buffer *buffer, | |
90 | size_t new_capacity); | |
91 | ||
92 | /* Release any memory used by the dynamic buffer. */ | |
93 | void lttng_dynamic_buffer_reset(struct lttng_dynamic_buffer *buffer); | |
94 | ||
201bd415 | 95 | /* Get the space left in the buffer before a new resize is needed. */ |
201bd415 JG |
96 | size_t lttng_dynamic_buffer_get_capacity_left( |
97 | struct lttng_dynamic_buffer *buffer); | |
98 | ||
7966af57 SM |
99 | #ifdef __cplusplus |
100 | } | |
101 | #endif | |
102 | ||
5966417e | 103 | #endif /* LTTNG_DYNAMIC_BUFFER_H */ |