From 3edbd30798a50671cc63d214b7f7b49fcb814ea7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Michaj=C5=82ow?= Date: Thu, 5 Sep 2024 22:13:15 +0200 Subject: [PATCH] ta_utils: add talloc_replace Helper to replace a string with a new one without reallocating the buffer, if not needed. --- ta/ta.h | 10 ++++++++++ ta/ta_talloc.h | 1 + 2 files changed, 11 insertions(+) diff --git a/ta/ta.h b/ta/ta.h index 485f38f58b..4fbb0635e2 100644 --- a/ta/ta.h +++ b/ta/ta.h @@ -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) \ (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. // 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 diff --git a/ta/ta_talloc.h b/ta/ta_talloc.h index a65e149164..5cbc5c1c56 100644 --- a/ta/ta_talloc.h +++ b/ta/ta_talloc.h @@ -50,6 +50,7 @@ #define talloc_strndup ta_xstrndup #define talloc_asprintf ta_xasprintf #define talloc_vasprintf ta_xvasprintf +#define talloc_replace ta_replace // Don't define linker-level symbols, as that would clash with real libtalloc. #define talloc_strdup_append ta_talloc_strdup_append