alloc-util: add realloc0() helper than is like realloc() but zero-initializes appended space

This commit is contained in:
Lennart Poettering 2023-09-22 22:22:12 +02:00
parent 3846d3aa29
commit 5e71f86dff

View File

@ -254,4 +254,24 @@ static inline void free_many_charp(char **c, size_t n) {
free_many((void**) c, n);
}
_alloc_(2) static inline void *realloc0(void *p, size_t new_size) {
size_t old_size;
void *q;
/* Like realloc(), but initializes anything appended to zero */
old_size = MALLOC_SIZEOF_SAFE(p);
q = realloc(p, new_size);
if (!q)
return NULL;
new_size = MALLOC_SIZEOF_SAFE(q); /* Update with actually allocated space */
if (new_size > old_size)
memset((uint8_t*) q + old_size, 0, new_size - old_size);
return q;
}
#include "memory-util.h"