Skip to content

Commit

Permalink
ta_utils: add talloc_replace
Browse files Browse the repository at this point in the history
Helper to replace a string with a new one without reallocating the
buffer, if not needed.
  • Loading branch information
kasper93 committed Sep 8, 2024
1 parent 077f05f commit 3edbd30
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
10 changes: 10 additions & 0 deletions ta/ta.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions ta/ta_talloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3edbd30

Please sign in to comment.