Commit | Line | Data |
---|---|---|
5966417e JG |
1 | /* |
2 | * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
3 | * | |
4 | * This program 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 program 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 program; 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> | |
c90647fe | 23 | #include <common/macros.h> |
5966417e | 24 | |
3f4ef714 JG |
25 | struct lttng_buffer_view; |
26 | ||
5966417e JG |
27 | struct lttng_dynamic_buffer { |
28 | char *data; | |
63557623 | 29 | /* size is the buffer's currently used capacity. */ |
5966417e | 30 | size_t size; |
63557623 JG |
31 | /* |
32 | * capacity shall not be accessed by users directly, it is meant for | |
33 | * internal use only. | |
34 | */ | |
35 | size_t _capacity; | |
5966417e JG |
36 | }; |
37 | ||
63557623 JG |
38 | /* |
39 | * Initialize a dynamic buffer. This performs no allocation and is meant | |
40 | * to be used instead of memset or explicit initialization of the buffer. | |
41 | */ | |
c90647fe | 42 | LTTNG_HIDDEN |
5966417e JG |
43 | void lttng_dynamic_buffer_init(struct lttng_dynamic_buffer *buffer); |
44 | ||
63557623 JG |
45 | /* |
46 | * Append the content of a raw memory buffer to the end of a dynamic buffer | |
47 | * (after its current "size"). The dynamic buffer's size is increased by | |
48 | * "len", and its capacity is adjusted automatically. | |
49 | */ | |
c90647fe | 50 | LTTNG_HIDDEN |
5966417e JG |
51 | int lttng_dynamic_buffer_append(struct lttng_dynamic_buffer *buffer, |
52 | const void *buf, size_t len); | |
53 | ||
63557623 JG |
54 | /* |
55 | * Performs the same action as lttng_dynamic_buffer_append(), but using another | |
56 | * dynamic buffer as the source buffer. The source buffer's size is used in lieu | |
57 | * of "len". | |
58 | */ | |
c90647fe | 59 | LTTNG_HIDDEN |
5966417e JG |
60 | int lttng_dynamic_buffer_append_buffer(struct lttng_dynamic_buffer *dst_buffer, |
61 | struct lttng_dynamic_buffer *src_buffer); | |
62 | ||
3f4ef714 JG |
63 | /* |
64 | * Performs the same action as lttng_dynamic_buffer_append(), but using a | |
65 | * buffer view as the source buffer. The source buffer's size is used in lieu | |
66 | * of "len". | |
67 | */ | |
68 | LTTNG_HIDDEN | |
69 | int lttng_dynamic_buffer_append_view(struct lttng_dynamic_buffer *buffer, | |
70 | const struct lttng_buffer_view *view); | |
71 | ||
5966417e JG |
72 | /* |
73 | * Set the buffer's size to new_size. The capacity of the buffer will | |
74 | * be expanded (if necessary) to accomodate new_size. Areas acquired by | |
63557623 | 75 | * a size increase will be zeroed. |
5966417e JG |
76 | * |
77 | * Be careful to expand the buffer's size _before_ calling out external | |
78 | * APIs (e.g. read(3)) which may populate the buffer as setting the size | |
63557623 JG |
79 | * after will zero-out the result of the operation. |
80 | * | |
81 | * Shrinking a buffer does not zero the old content. If the buffer may contain | |
82 | * sensititve information, it must be cleared manually _before_ changing the | |
83 | * size. | |
84 | * | |
85 | * NOTE: It is striclty _invalid_ to access memory after _size_, regardless | |
86 | * of prior calls to set_capacity(). | |
5966417e | 87 | */ |
c90647fe | 88 | LTTNG_HIDDEN |
5966417e JG |
89 | int lttng_dynamic_buffer_set_size(struct lttng_dynamic_buffer *buffer, |
90 | size_t new_size); | |
91 | ||
92 | /* | |
93 | * Set the buffer's capacity to accomodate the new_capacity, allocating memory | |
63557623 JG |
94 | * as necessary. The buffer's content is preserved. Setting a buffer's capacity |
95 | * is meant as a _hint_ to the underlying buffer and is only optimization; no | |
96 | * guarantee is offered that subsequent calls to append or set_size will succeed. | |
5966417e JG |
97 | * |
98 | * If the current size > new_capacity, the operation will fail. | |
99 | */ | |
c90647fe | 100 | LTTNG_HIDDEN |
5966417e JG |
101 | int lttng_dynamic_buffer_set_capacity(struct lttng_dynamic_buffer *buffer, |
102 | size_t new_capacity); | |
103 | ||
104 | /* Release any memory used by the dynamic buffer. */ | |
c90647fe | 105 | LTTNG_HIDDEN |
5966417e JG |
106 | void lttng_dynamic_buffer_reset(struct lttng_dynamic_buffer *buffer); |
107 | ||
201bd415 JG |
108 | /* Get the space left in the buffer before a new resize is needed. */ |
109 | LTTNG_HIDDEN | |
110 | size_t lttng_dynamic_buffer_get_capacity_left( | |
111 | struct lttng_dynamic_buffer *buffer); | |
112 | ||
5966417e | 113 | #endif /* LTTNG_DYNAMIC_BUFFER_H */ |