1 // SPDX-License-Identifier: GPL-2.0+
3 * Handles a buffer that can be allocated and freed
5 * Copyright 2021 Google LLC
6 * Written by Simon Glass <sjg@chromium.org>
15 void abuf_set(struct abuf *abuf, void *data, size_t size)
22 void abuf_map_sysmem(struct abuf *abuf, ulong addr, size_t size)
24 abuf_set(abuf, map_sysmem(addr, size), size);
27 bool abuf_realloc(struct abuf *abuf, size_t new_size)
32 /* easy case, just need to uninit, freeing any allocation */
35 } else if (abuf->alloced) {
36 /* currently allocated, so need to reallocate */
37 ptr = realloc(abuf->data, new_size);
41 abuf->size = new_size;
43 } else if (new_size <= abuf->size) {
45 * not currently alloced and new size is no larger. Just update
46 * it. Data is lost off the end if new_size < abuf->size
48 abuf->size = new_size;
51 /* not currently allocated and new size is larger. Alloc and
52 * copy in data. The new space is not inited.
54 ptr = malloc(new_size);
58 memcpy(ptr, abuf->data, abuf->size);
60 abuf->size = new_size;
66 void *abuf_uninit_move(struct abuf *abuf, size_t *sizep)
77 ptr = memdup(abuf->data, abuf->size);
81 /* Clear everything out so there is no record of the data */
87 void abuf_init_set(struct abuf *abuf, void *data, size_t size)
90 abuf_set(abuf, data, size);
93 void abuf_init_move(struct abuf *abuf, void *data, size_t size)
95 abuf_init_set(abuf, data, size);
99 void abuf_uninit(struct abuf *abuf)
106 void abuf_init(struct abuf *abuf)
110 abuf->alloced = false;