From: Simon Glass Date: Mon, 14 Aug 2023 22:40:22 +0000 (-0600) Subject: abuf: Allow incrementing the size X-Git-Url: http://git.dujemihanovic.xyz/?a=commitdiff_plain;h=34ecba1f7616b388ee28490e8a3ed43fb1463011;p=u-boot.git abuf: Allow incrementing the size Provide a convenience function to increment the size of the abuf. Signed-off-by: Simon Glass --- diff --git a/include/abuf.h b/include/abuf.h index 9badda64e4..be98ec78c8 100644 --- a/include/abuf.h +++ b/include/abuf.h @@ -90,6 +90,15 @@ void abuf_map_sysmem(struct abuf *abuf, ulong addr, size_t size); */ bool abuf_realloc(struct abuf *abuf, size_t new_size); +/** + * abuf_realloc_inc() - Increment abuf size by a given amount + * + * @abuf: abuf to adjust + * @inc: Size incrmement to use (the buffer size will be increased by this much) + * Return: true if OK, false if out of memory + */ +bool abuf_realloc_inc(struct abuf *abuf, size_t inc); + /** * abuf_uninit_move() - Return the allocated contents and uninit the abuf * diff --git a/lib/abuf.c b/lib/abuf.c index bd270467dd..ce2cff53dc 100644 --- a/lib/abuf.c +++ b/lib/abuf.c @@ -82,6 +82,11 @@ bool abuf_realloc(struct abuf *abuf, size_t new_size) } } +bool abuf_realloc_inc(struct abuf *abuf, size_t inc) +{ + return abuf_realloc(abuf, abuf->size + inc); +} + void *abuf_uninit_move(struct abuf *abuf, size_t *sizep) { void *ptr; diff --git a/test/lib/abuf.c b/test/lib/abuf.c index 42ee4c1755..42803b20e2 100644 --- a/test/lib/abuf.c +++ b/test/lib/abuf.c @@ -155,6 +155,31 @@ static int lib_test_abuf_realloc_size(struct unit_test_state *uts) } LIB_TEST(lib_test_abuf_realloc_size, 0); +/* Test abuf_realloc_inc() */ +static int lib_test_abuf_realloc_inc(struct unit_test_state *uts) +{ + struct abuf buf; + ulong start; + + start = ut_check_free(); + + abuf_init(&buf); + ut_asserteq(0, buf.size); + ut_asserteq(false, buf.alloced); + + abuf_realloc_inc(&buf, 20); + ut_asserteq(20, buf.size); + ut_asserteq(true, buf.alloced); + + abuf_uninit(&buf); + + /* Check for memory leaks */ + ut_assertok(ut_check_delta(start)); + + return 0; +} +LIB_TEST(lib_test_abuf_realloc_inc, 0); + /* Test handling of buffers that are too large */ static int lib_test_abuf_large(struct unit_test_state *uts) {