From d58cebbbc7617fbc45e604c883e8501a58d25e62 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Fri, 2 May 2025 08:46:03 -0600 Subject: abuf: Add a function to copy a buffer It is useful to be able to copy an abuf, to allow changes while preserving the original. Add a function for this. Signed-off-by: Simon Glass --- lib/abuf.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'lib/abuf.c') diff --git a/lib/abuf.c b/lib/abuf.c index 3cbe320fb08..28c748acb9f 100644 --- a/lib/abuf.c +++ b/lib/abuf.c @@ -128,6 +128,20 @@ bool abuf_init_size(struct abuf *buf, size_t size) return true; } +bool abuf_copy(const struct abuf *old, struct abuf *copy) +{ + char *data; + + data = malloc(old->size); + if (!data) + return false; + memcpy(data, old->data, old->size); + abuf_init_set(copy, data, old->size); + copy->alloced = true; + + return true; +} + void abuf_init_const(struct abuf *abuf, const void *data, size_t size) { /* for now there is no flag indicating that the abuf data is constant */ -- cgit v1.2.3