History

ta_utils: add talloc_replace

Helper to replace a string with a new one without reallocating the buffer, if not needed.

Kacper Michajłow committed 2 years ago

Commit 3edbd301 parent 077f05f2 files · +11 −0Browse files

Unified · Split

2 files
  1. ta/ta.h +10 −0
  2. ta/ta_talloc.h +1 −0
ta/ta.h+10−0
@@ -99,6 +99,16 @@ bool ta_vasprintf_append_buffer(char **str, const char *fmt, va_list ap) TA_PRF( · 98 unmodified lines
9999 #define ta_dup(ta_parent, ptr) \
100100 (TA_TYPEOF(ptr))ta_memdup(ta_parent, ptr, sizeof(*(ptr)))
101101
102+#define ta_replace(ta_parent, str, replace) \
103+ do { \
104+ if (!(str)) { \
105+ (str) = ta_xstrdup((ta_parent), (replace)); \
106+ } else { \
107+ *(str) = '\0'; \
108+ ta_xstrdup_append(&(str), (replace)); \
109+ } \
110+ } while (0)
111+
102112 // Ugly macros that crash on OOM.
103113 // All of these mirror real functions (with a 'x' added after the 'ta_'
104114 // prefix), and the only difference is that they will call abort() on allocation
ta/ta_talloc.h+1−0
@@ -50,6 +50,7 @@ · 49 unmodified lines
5050 #define talloc_strndup ta_xstrndup
5151 #define talloc_asprintf ta_xasprintf
5252 #define talloc_vasprintf ta_xvasprintf
53+#define talloc_replace ta_replace
5354
5455 // Don't define linker-level symbols, as that would clash with real libtalloc.
5556 #define talloc_strdup_append ta_talloc_strdup_append

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t