From 97b586695cd80821455ae06ee178c6c8cf759ce6 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Thu, 1 May 2025 07:37:01 -0600 Subject: abuf: Add a helper for initing and allocating a buffer This construct appears in various places. Reduce code size by adding a function for it. It inits the abuf, then allocates it to the requested size. Signed-off-by: Simon Glass --- lib/abuf.c | 9 +++++++++ lib/of_live.c | 3 +-- 2 files changed, 10 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/abuf.c b/lib/abuf.c index 61adf7fc6b1..3cbe320fb08 100644 --- a/lib/abuf.c +++ b/lib/abuf.c @@ -119,6 +119,15 @@ void abuf_init_set(struct abuf *abuf, void *data, size_t size) abuf_set(abuf, data, size); } +bool abuf_init_size(struct abuf *buf, size_t size) +{ + abuf_init(buf); + if (!abuf_realloc(buf, size)) + return false; + + 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 */ diff --git a/lib/of_live.c b/lib/of_live.c index c1620616513..24200b948a6 100644 --- a/lib/of_live.c +++ b/lib/of_live.c @@ -448,8 +448,7 @@ int of_live_flatten(const struct device_node *root, struct abuf *buf) { int ret; - abuf_init(buf); - if (!abuf_realloc(buf, BUF_STEP)) + if (!abuf_init_size(buf, BUF_STEP)) return log_msg_ret("ini", -ENOMEM); ret = fdt_create(abuf_data(buf), abuf_size(buf)); -- cgit v1.3.1