dutil: Adopt the zalloc()/zfree() from perf

So that we handle const pointers, also zalloc() is much simpler just
calling calloc(1, size).

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
This commit is contained in:
Arnaldo Carvalho de Melo 2021-05-27 10:30:46 -03:00
parent d7f5824a9e
commit 6784b03fd4
2 changed files with 12 additions and 7 deletions

15
dutil.c
View File

@ -13,12 +13,15 @@
#include <stdlib.h>
#include <string.h>
void *zalloc(const size_t size)
void *zalloc(size_t size)
{
void *s = malloc(size);
if (s != NULL)
memset(s, 0, size);
return s;
return calloc(1, size);
}
void __zfree(void **ptr)
{
free(*ptr);
*ptr = NULL;
}
struct str_node *str_node__new(const char *s, bool dupstr)
@ -44,7 +47,7 @@ out_delete:
static void str_node__delete(struct str_node *snode, bool dupstr)
{
if (dupstr)
free((void *)snode->s);
zfree(&snode->s);
free(snode);
}

View File

@ -336,6 +336,8 @@ static inline int elf_getshdrstrndx(Elf *elf, size_t *dst)
char *strlwr(char *s);
#define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
void __zfree(void **ptr);
#define zfree(ptr) __zfree((void **)(ptr))
#endif /* _DUTIL_H_ */