]>
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>
17 void abuf_set(struct abuf
*abuf
, void *data
, size_t size
)
25 void abuf_map_sysmem(struct abuf
*abuf
, ulong addr
, size_t size
)
27 abuf_set(abuf
, map_sysmem(addr
, size
), size
);
30 /* copied from lib/string.c for convenience */
31 static char *memdup(const void *src
, size_t len
)
45 bool abuf_realloc(struct abuf
*abuf
, size_t new_size
)
50 /* easy case, just need to uninit, freeing any allocation */
53 } else if (abuf
->alloced
) {
54 /* currently allocated, so need to reallocate */
55 ptr
= realloc(abuf
->data
, new_size
);
59 abuf
->size
= new_size
;
61 } else if (new_size
<= abuf
->size
) {
63 * not currently alloced and new size is no larger. Just update
64 * it. Data is lost off the end if new_size < abuf->size
66 abuf
->size
= new_size
;
69 /* not currently allocated and new size is larger. Alloc and
70 * copy in data. The new space is not inited.
72 ptr
= malloc(new_size
);
76 memcpy(ptr
, abuf
->data
, abuf
->size
);
78 abuf
->size
= new_size
;
84 bool abuf_realloc_inc(struct abuf
*abuf
, size_t inc
)
86 return abuf_realloc(abuf
, abuf
->size
+ inc
);
89 void *abuf_uninit_move(struct abuf
*abuf
, size_t *sizep
)
100 ptr
= memdup(abuf
->data
, abuf
->size
);
104 /* Clear everything out so there is no record of the data */
110 void abuf_init_set(struct abuf
*abuf
, void *data
, size_t size
)
113 abuf_set(abuf
, data
, size
);
116 void abuf_init_move(struct abuf
*abuf
, void *data
, size_t size
)
118 abuf_init_set(abuf
, data
, size
);
119 abuf
->alloced
= true;
122 void abuf_uninit(struct abuf
*abuf
)
129 void abuf_init(struct abuf
*abuf
)
133 abuf
->alloced
= false;