From 34e2ef67f056bc781fdc4a115fb6aea9826686b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Costa?= Date: Thu, 20 Feb 2025 05:20:03 +0100 Subject: [PATCH] feat(mem): add lv_reallocf (#7780) --- src/stdlib/lv_mem.c | 9 +++++++++ src/stdlib/lv_mem.h | 10 ++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/stdlib/lv_mem.c b/src/stdlib/lv_mem.c index ece0667834..41a002d745 100644 --- a/src/stdlib/lv_mem.c +++ b/src/stdlib/lv_mem.c @@ -136,6 +136,15 @@ void lv_free(void * data) lv_free_core(data); } +void * lv_reallocf(void * data_p, size_t new_size) +{ + void * new = lv_realloc(data_p, new_size); + if(!new) { + lv_free(data_p); + } + return new; +} + void * lv_realloc(void * data_p, size_t new_size) { LV_TRACE_MEM("reallocating %p with %lu size", data_p, (unsigned long)new_size); diff --git a/src/stdlib/lv_mem.h b/src/stdlib/lv_mem.h index e136aad6ad..2b9e2defb6 100644 --- a/src/stdlib/lv_mem.h +++ b/src/stdlib/lv_mem.h @@ -105,6 +105,16 @@ void lv_free(void * data); */ void * lv_realloc(void * data_p, size_t new_size); +/** + * Reallocate a memory with a new size. The old content will be kept. + * In case of failure, the old pointer is free'd. + * @param data_p pointer to an allocated memory. + * Its content will be copied to the new memory block and freed + * @param new_size the desired new size in byte + * @return pointer to the new memory, NULL on failure + */ +void * lv_reallocf(void * data_p, size_t new_size); + /** * Used internally to execute a plain `malloc` operation * @param size size in bytes to `malloc`