1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Handles a buffer that can be allocated and freed
4  *
5  * Copyright 2021 Google LLC
6  * Written by Simon Glass <sjg@chromium.org>
7  */
8 
9 #ifndef __ABUF_H
10 #define __ABUF_H
11 
12 #ifdef USE_HOSTCC
13 #include <sys/types.h>
14 #else
15 #include <linux/types.h>
16 #endif
17 
18 /**
19  * struct abuf - buffer that can be allocated and freed
20  *
21  * This is useful for a block of data which may be allocated with malloc(), or
22  * not, so that it needs to be freed correctly when finished with.
23  *
24  * For now it has a very simple purpose.
25  *
26  * Using memset() to zero all fields is guaranteed to be equivalent to
27  * abuf_init().
28  *
29  * @data: Pointer to data
30  * @size: Size of data in bytes
31  * @alloced: true if allocated with malloc(), so must be freed after use
32  */
33 struct abuf {
34 	void *data;
35 	size_t size;
36 	bool alloced;
37 };
38 
abuf_data(const struct abuf * abuf)39 static inline void *abuf_data(const struct abuf *abuf)
40 {
41 	return abuf->data;
42 }
43 
abuf_size(const struct abuf * abuf)44 static inline size_t abuf_size(const struct abuf *abuf)
45 {
46 	return abuf->size;
47 }
48 
49 /**
50  * abuf_addr() - Get the address of a buffer's data
51  *
52  * @abuf: Buffer to check
53  * Return: address of buffer
54  */
55 ulong abuf_addr(const struct abuf *abuf);
56 
57 /**
58  * abuf_set() - set the (unallocated) data in a buffer
59  *
60  * This simply makes the abuf point to the supplied data, which must be live
61  * for the lifetime of the abuf. It is not alloced.
62  *
63  * Any existing data in the abuf is freed and the alloced member is set to
64  * false.
65  *
66  * @abuf: abuf to adjust
67  * @data: New contents of abuf
68  * @size: New size of abuf
69  */
70 void abuf_set(struct abuf *abuf, void *data, size_t size);
71 
72 /**
73  * abuf_map_sysmem() - calls map_sysmem() to set up an abuf
74  *
75  * This is equivalent to abuf_set(abuf, map_sysmem(addr, size), size)
76  *
77  * Any existing data in the abuf is freed and the alloced member is set to
78  * false.
79  *
80  * @abuf: abuf to adjust
81  * @addr: Address to set the abuf to
82  * @size: New size of abuf
83  */
84 void abuf_map_sysmem(struct abuf *abuf, ulong addr, size_t size);
85 
86 /**
87  * abuf_realloc() - Change the size of a buffer
88  *
89  * This uses realloc() to change the size of the buffer, with the same semantics
90  * as that function. If the abuf is not currently alloced, then it will alloc
91  * it if the size needs to increase (i.e. set the alloced member to true)
92  *
93  * @abuf: abuf to adjust
94  * @new_size: new size in bytes.
95  *	if 0, the abuf is freed
96  *	if greater than the current size, the abuf is extended and the new
97  *	   space is not inited. The alloced member is set to true
98  *	if less than the current size, the abuf is contracted and the data at
99  *	   the end is lost. If @new_size is 0, this sets the alloced member to
100  *	   false
101  * Return: true if OK, false if out of memory
102  */
103 bool abuf_realloc(struct abuf *abuf, size_t new_size);
104 
105 /**
106  * abuf_realloc_inc() - Increment abuf size by a given amount
107  *
108  * @abuf: abuf to adjust
109  * @inc: Size incrmement to use (the buffer size will be increased by this much)
110  * Return: true if OK, false if out of memory
111  */
112 bool abuf_realloc_inc(struct abuf *abuf, size_t inc);
113 
114 /**
115  * abuf_copy() - Make a copy of an abuf
116  *
117  * Creates an allocated copy of @old in @new
118  *
119  * @old: abuf to copy
120  * @new: new abuf to hold the copy (inited by this function)
121  * Return: true if OK, false if out of memory
122  */
123 bool abuf_copy(const struct abuf *old, struct abuf *new);
124 
125 /**
126  * abuf_printf() - Format a string and place it in an abuf
127  *
128  * @buf: The buffer to place the result into
129  * @fmt: The format string to use
130  * @...: Arguments for the format string
131  * Return: the number of characters writtenwhich would be
132  * generated for the given input, excluding the trailing null,
133  * as per ISO C99.
134  *
135  * The abuf is expanded as necessary to fit the formated string
136  *
137  * See the vsprintf() documentation for format string extensions over C99.
138  *
139  * Returns: number of characters written (excluding trailing nul) on success,
140  * -E2BIG if the size exceeds 4K, -ENOMEM if out of memory, -EFAULT if there is
141  * an internal bug in the vsnprintf() implementation
142  */
143 int abuf_printf(struct abuf *buf, const char *fmt, ...)
144 		__attribute__ ((format (__printf__, 2, 3)));
145 
146 /**
147  * abuf_uninit_move() - Return the allocated contents and uninit the abuf
148  *
149  * This returns the abuf data to the caller, allocating it if necessary, so that
150  * the caller receives data that it can be sure will hang around. The caller is
151  * responsible for freeing the data.
152  *
153  * If the abuf has allocated data, it is returned. If the abuf has data but it
154  * is not allocated, then it is first allocated, then returned.
155  *
156  * If the abuf size is 0, this returns NULL
157  *
158  * The abuf is uninited as part of this, except if the allocation fails, in
159  * which NULL is returned and the abuf remains untouched.
160  *
161  * The abuf must be inited before this can be called.
162  *
163  * @abuf: abuf to uninit
164  * @sizep: if non-NULL, returns the size of the returned data
165  * Return: data contents, allocated with malloc(), or NULL if the data could not
166  *	be allocated, or the data size is 0
167  */
168 void *abuf_uninit_move(struct abuf *abuf, size_t *sizep);
169 
170 /**
171  * abuf_init_move() - Make abuf take over the management of an allocated region
172  *
173  * After this, @data must not be used. All access must be via the abuf.
174  *
175  * @abuf: abuf to init
176  * @data: Existing allocated buffer to place in the abuf
177  * @size: Size of allocated buffer
178  */
179 void abuf_init_move(struct abuf *abuf, void *data, size_t size);
180 
181 /**
182  * abuf_init_set() - Set up a new abuf
183  *
184  * Inits a new abuf and sets up its (unallocated) data
185  *
186  * @abuf: abuf to set up
187  * @data: New contents of abuf
188  * @size: New size of abuf
189  */
190 void abuf_init_set(struct abuf *abuf, void *data, size_t size);
191 
192 /**
193  * abuf_init_const() - Set up a new const abuf
194  *
195  * Inits a new abuf and sets up its (unallocated) data. The only current
196  * difference between this and abuf_init_set() is the 'data' parameter is a
197  * const pointer. At some point a flag could be used to indicate const-ness.
198  *
199  * @abuf: abuf to set up
200  * @data: New contents of abuf
201  * @size: New size of abuf
202  */
203 void abuf_init_const(struct abuf *abuf, const void *data, size_t size);
204 
205 /**
206  * abuf_init_size() - Set up an allocated abuf
207  *
208  * Init a new abuf and allocate its size.
209  *
210  * @abuf: abuf to set up
211  * @data: New contents of abuf
212  * @size: New size of abuf
213  */
214 bool abuf_init_size(struct abuf *buf, size_t size);
215 
216 /**
217  * abuf_uninit() - Free any memory used by an abuf
218  *
219  * The buffer must be inited before this can be called.
220  *
221  * @abuf: abuf to uninit
222  */
223 void abuf_uninit(struct abuf *abuf);
224 
225 /**
226  * abuf_init() - Set up a new abuf
227  *
228  * This initially has no data and alloced is set to false. This is equivalent to
229  * setting all fields to 0, e.g. with memset(), so callers can do that instead
230  * if desired.
231  *
232  * @abuf: abuf to set up
233  */
234 void abuf_init(struct abuf *abuf);
235 
236 #endif
237