blob: 76e314b9a4791c38e63752e8f6c465eb80dd4124 [file] [log] [blame]
Simon Glass143fa862021-09-25 07:03:07 -06001/* 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#include <linux/types.h>
13
14/**
15 * struct abuf - buffer that can be allocated and freed
16 *
17 * This is useful for a block of data which may be allocated with malloc(), or
18 * not, so that it needs to be freed correctly when finished with.
19 *
20 * For now it has a very simple purpose.
21 *
22 * Using memset() to zero all fields is guaranteed to be equivalent to
23 * abuf_init().
24 *
25 * @data: Pointer to data
26 * @size: Size of data in bytes
27 * @alloced: true if allocated with malloc(), so must be freed after use
28 */
29struct abuf {
30 void *data;
31 size_t size;
32 bool alloced;
33};
34
35static inline void *abuf_data(const struct abuf *abuf)
36{
37 return abuf->data;
38}
39
40static inline size_t abuf_size(const struct abuf *abuf)
41{
42 return abuf->size;
43}
44
45/**
Simon Glassb63269b2025-01-10 17:00:01 -070046 * abuf_addr() - Get the address of a buffer's data
47 *
48 * @abuf: Buffer to check
49 * Return: address of buffer
50 */
51ulong abuf_addr(const struct abuf *abuf);
52
53/**
Simon Glass143fa862021-09-25 07:03:07 -060054 * abuf_set() - set the (unallocated) data in a buffer
55 *
56 * This simply makes the abuf point to the supplied data, which must be live
57 * for the lifetime of the abuf. It is not alloced.
58 *
59 * Any existing data in the abuf is freed and the alloced member is set to
60 * false.
61 *
62 * @abuf: abuf to adjust
63 * @data: New contents of abuf
64 * @size: New size of abuf
65 */
66void abuf_set(struct abuf *abuf, void *data, size_t size);
67
68/**
69 * abuf_map_sysmem() - calls map_sysmem() to set up an abuf
70 *
71 * This is equivalent to abuf_set(abuf, map_sysmem(addr, size), size)
72 *
73 * Any existing data in the abuf is freed and the alloced member is set to
74 * false.
75 *
76 * @abuf: abuf to adjust
77 * @addr: Address to set the abuf to
78 * @size: New size of abuf
79 */
80void abuf_map_sysmem(struct abuf *abuf, ulong addr, size_t size);
81
82/**
83 * abuf_realloc() - Change the size of a buffer
84 *
85 * This uses realloc() to change the size of the buffer, with the same semantics
86 * as that function. If the abuf is not currently alloced, then it will alloc
87 * it if the size needs to increase (i.e. set the alloced member to true)
88 *
89 * @abuf: abuf to adjust
90 * @new_size: new size in bytes.
91 * if 0, the abuf is freed
92 * if greater than the current size, the abuf is extended and the new
93 * space is not inited. The alloced member is set to true
94 * if less than the current size, the abuf is contracted and the data at
95 * the end is lost. If @new_size is 0, this sets the alloced member to
96 * false
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010097 * Return: true if OK, false if out of memory
Simon Glass143fa862021-09-25 07:03:07 -060098 */
99bool abuf_realloc(struct abuf *abuf, size_t new_size);
100
101/**
Simon Glass733c2622023-08-14 16:40:22 -0600102 * abuf_realloc_inc() - Increment abuf size by a given amount
103 *
104 * @abuf: abuf to adjust
105 * @inc: Size incrmement to use (the buffer size will be increased by this much)
106 * Return: true if OK, false if out of memory
107 */
108bool abuf_realloc_inc(struct abuf *abuf, size_t inc);
109
110/**
Simon Glass143fa862021-09-25 07:03:07 -0600111 * abuf_uninit_move() - Return the allocated contents and uninit the abuf
112 *
113 * This returns the abuf data to the caller, allocating it if necessary, so that
114 * the caller receives data that it can be sure will hang around. The caller is
115 * responsible for freeing the data.
116 *
117 * If the abuf has allocated data, it is returned. If the abuf has data but it
118 * is not allocated, then it is first allocated, then returned.
119 *
120 * If the abuf size is 0, this returns NULL
121 *
122 * The abuf is uninited as part of this, except if the allocation fails, in
123 * which NULL is returned and the abuf remains untouched.
124 *
125 * The abuf must be inited before this can be called.
126 *
127 * @abuf: abuf to uninit
128 * @sizep: if non-NULL, returns the size of the returned data
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100129 * Return: data contents, allocated with malloc(), or NULL if the data could not
Simon Glass143fa862021-09-25 07:03:07 -0600130 * be allocated, or the data size is 0
131 */
132void *abuf_uninit_move(struct abuf *abuf, size_t *sizep);
133
134/**
135 * abuf_init_move() - Make abuf take over the management of an allocated region
136 *
137 * After this, @data must not be used. All access must be via the abuf.
138 *
139 * @abuf: abuf to init
140 * @data: Existing allocated buffer to place in the abuf
141 * @size: Size of allocated buffer
142 */
143void abuf_init_move(struct abuf *abuf, void *data, size_t size);
144
145/**
146 * abuf_init_set() - Set up a new abuf
147 *
148 * Inits a new abuf and sets up its (unallocated) data
149 *
150 * @abuf: abuf to set up
151 * @data: New contents of abuf
152 * @size: New size of abuf
153 */
154void abuf_init_set(struct abuf *abuf, void *data, size_t size);
155
156/**
157 * abuf_uninit() - Free any memory used by an abuf
158 *
159 * The buffer must be inited before this can be called.
160 *
161 * @abuf: abuf to uninit
162 */
163void abuf_uninit(struct abuf *abuf);
164
165/**
166 * abuf_init() - Set up a new abuf
167 *
168 * This initially has no data and alloced is set to false. This is equivalent to
169 * setting all fields to 0, e.g. with memset(), so callers can do that instead
170 * if desired.
171 *
172 * @abuf: abuf to set up
173 */
174void abuf_init(struct abuf *abuf);
175
176#endif