ta_utils: add talloc_replace

Helper to replace a string with a new one without reallocating the
buffer, if not needed.
This commit is contained in:
Kacper Michajłow 2024-09-05 22:13:15 +02:00
parent 077f05f7a1
commit 3edbd30798
2 changed files with 11 additions and 0 deletions

10
ta/ta.h
View File

@ -99,6 +99,16 @@ bool ta_vasprintf_append_buffer(char **str, const char *fmt, va_list ap) TA_PRF(
#define ta_dup(ta_parent, ptr) \ #define ta_dup(ta_parent, ptr) \
(TA_TYPEOF(ptr))ta_memdup(ta_parent, ptr, sizeof(*(ptr))) (TA_TYPEOF(ptr))ta_memdup(ta_parent, ptr, sizeof(*(ptr)))
#define ta_replace(ta_parent, str, replace) \
do { \
if (!(str)) { \
(str) = ta_xstrdup((ta_parent), (replace)); \
} else { \
*(str) = '\0'; \
ta_xstrdup_append(&(str), (replace)); \
} \
} while (0)
// Ugly macros that crash on OOM. // Ugly macros that crash on OOM.
// All of these mirror real functions (with a 'x' added after the 'ta_' // All of these mirror real functions (with a 'x' added after the 'ta_'
// prefix), and the only difference is that they will call abort() on allocation // prefix), and the only difference is that they will call abort() on allocation

View File

@ -50,6 +50,7 @@
#define talloc_strndup ta_xstrndup #define talloc_strndup ta_xstrndup
#define talloc_asprintf ta_xasprintf #define talloc_asprintf ta_xasprintf
#define talloc_vasprintf ta_xvasprintf #define talloc_vasprintf ta_xvasprintf
#define talloc_replace ta_replace
// Don't define linker-level symbols, as that would clash with real libtalloc. // Don't define linker-level symbols, as that would clash with real libtalloc.
#define talloc_strdup_append ta_talloc_strdup_append #define talloc_strdup_append ta_talloc_strdup_append