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 | ||
28f23191 JG |
11 | #include <common/macros.hpp> |
12 | ||
5966417e JG |
13 | #include <stddef.h> |
14 | #include <stdint.h> | |
15 | ||
fbd55aae JG |
16 | struct lttng_buffer_view; |
17 | ||
5966417e JG |
18 | struct lttng_dynamic_buffer { |
19 | char *data; | |
63557623 | 20 | /* size is the buffer's currently used capacity. */ |
5966417e | 21 | size_t size; |
63557623 JG |
22 | /* |
23 | * capacity shall not be accessed by users directly, it is meant for | |
24 | * internal use only. | |
25 | */ | |
26 | size_t _capacity; | |
5966417e JG |
27 | }; |
28 | ||
63557623 JG |
29 | /* |
30 | * Initialize a dynamic buffer. This performs no allocation and is meant | |
31 | * to be used instead of memset or explicit initialization of the buffer. | |
32 | */ | |
5966417e JG |
33 | void lttng_dynamic_buffer_init(struct lttng_dynamic_buffer *buffer); |
34 | ||
63557623 JG |
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 | */ | |
28f23191 | 40 | int lttng_dynamic_buffer_append(struct lttng_dynamic_buffer *buffer, const void *buf, size_t len); |
5966417e | 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 | */ | |
5966417e | 47 | int lttng_dynamic_buffer_append_buffer(struct lttng_dynamic_buffer *dst_buffer, |
28f23191 | 48 | const struct lttng_dynamic_buffer *src_buffer); |
5966417e | 49 | |
fbd55aae JG |
50 | /* |
51 | * Performs the same action as lttng_dynamic_buffer_append(), but using a | |
52 | * buffer view as the source buffer. The source buffer's size is used in lieu | |
53 | * of "len". | |
54 | */ | |
fbd55aae | 55 | int lttng_dynamic_buffer_append_view(struct lttng_dynamic_buffer *buffer, |
28f23191 | 56 | const struct lttng_buffer_view *view); |
fbd55aae | 57 | |
5966417e JG |
58 | /* |
59 | * Set the buffer's size to new_size. The capacity of the buffer will | |
d49487dc | 60 | * be expanded (if necessary) to accommodates new_size. Areas acquired by |
63557623 | 61 | * a size increase will be zeroed. |
5966417e JG |
62 | * |
63 | * Be careful to expand the buffer's size _before_ calling out external | |
64 | * APIs (e.g. read(3)) which may populate the buffer as setting the size | |
63557623 JG |
65 | * after will zero-out the result of the operation. |
66 | * | |
67 | * Shrinking a buffer does not zero the old content. If the buffer may contain | |
68 | * sensititve information, it must be cleared manually _before_ changing the | |
69 | * size. | |
70 | * | |
71 | * NOTE: It is striclty _invalid_ to access memory after _size_, regardless | |
72 | * of prior calls to set_capacity(). | |
5966417e | 73 | */ |
28f23191 | 74 | int lttng_dynamic_buffer_set_size(struct lttng_dynamic_buffer *buffer, size_t new_size); |
5966417e JG |
75 | |
76 | /* | |
d49487dc | 77 | * Set the buffer's capacity to accommodates the new_capacity, allocating memory |
63557623 JG |
78 | * as necessary. The buffer's content is preserved. Setting a buffer's capacity |
79 | * is meant as a _hint_ to the underlying buffer and is only optimization; no | |
80 | * guarantee is offered that subsequent calls to append or set_size will succeed. | |
5966417e JG |
81 | * |
82 | * If the current size > new_capacity, the operation will fail. | |
83 | */ | |
28f23191 | 84 | int lttng_dynamic_buffer_set_capacity(struct lttng_dynamic_buffer *buffer, size_t new_capacity); |
5966417e JG |
85 | |
86 | /* Release any memory used by the dynamic buffer. */ | |
87 | void lttng_dynamic_buffer_reset(struct lttng_dynamic_buffer *buffer); | |
88 | ||
201bd415 | 89 | /* Get the space left in the buffer before a new resize is needed. */ |
28f23191 | 90 | size_t lttng_dynamic_buffer_get_capacity_left(struct lttng_dynamic_buffer *buffer); |
201bd415 | 91 | |
5966417e | 92 | #endif /* LTTNG_DYNAMIC_BUFFER_H */ |