mirror of
https://github.com/systemd/systemd.git
synced 2024-11-23 18:23:32 +08:00
alloc-util: add realloc0() helper than is like realloc() but zero-initializes appended space
This commit is contained in:
parent
3846d3aa29
commit
5e71f86dff
@ -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"
|
||||
|
Loading…
Reference in New Issue
Block a user