]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - lib/abuf.c
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>
18 void abuf_set(struct abuf
*abuf
, void *data
, size_t size
)
26 void abuf_map_sysmem(struct abuf
*abuf
, ulong addr
, size_t size
)
28 abuf_set(abuf
, map_sysmem(addr
, size
), size
);
31 /* copied from lib/string.c for convenience */
32 static char *memdup(const void *src
, size_t len
)
46 bool abuf_realloc(struct abuf
*abuf
, size_t new_size
)
51 /* easy case, just need to uninit, freeing any allocation */
54 } else if (abuf
->alloced
) {
55 /* currently allocated, so need to reallocate */
56 ptr
= realloc(abuf
->data
, new_size
);
60 abuf
->size
= new_size
;
62 } else if (new_size
<= abuf
->size
) {
64 * not currently alloced and new size is no larger. Just update
65 * it. Data is lost off the end if new_size < abuf->size
67 abuf
->size
= new_size
;
70 /* not currently allocated and new size is larger. Alloc and
71 * copy in data. The new space is not inited.
73 ptr
= malloc(new_size
);
77 memcpy(ptr
, abuf
->data
, abuf
->size
);
79 abuf
->size
= new_size
;
85 void *abuf_uninit_move(struct abuf
*abuf
, size_t *sizep
)
96 ptr
= memdup(abuf
->data
, abuf
->size
);
100 /* Clear everything out so there is no record of the data */
106 void abuf_init_set(struct abuf
*abuf
, void *data
, size_t size
)
109 abuf_set(abuf
, data
, size
);
112 void abuf_init_move(struct abuf
*abuf
, void *data
, size_t size
)
114 abuf_init_set(abuf
, data
, size
);
115 abuf
->alloced
= true;
118 void abuf_uninit(struct abuf
*abuf
)
125 void abuf_init(struct abuf
*abuf
)
129 abuf
->alloced
= false;