common/mem.h: introduce xstrdup_replace()

This commit is contained in:
Consolatis 2024-11-15 22:59:57 +01:00 committed by Johan Malm
parent 6754801052
commit a4204d3335

View file

@ -44,6 +44,14 @@ void *xrealloc(void *ptr, size_t size);
*/
char *xstrdup(const char *str);
/*
* Same as ptr = xstrdup(str) but free
* <ptr> before assigning the result.
*/
#define xstrdup_replace(ptr, str) do { \
free(ptr); (ptr) = xstrdup(str); \
} while (0)
/*
* Frees memory pointed to by <ptr> and sets <ptr> to NULL.
* Does nothing if <ptr> is already NULL.