Helper to replace a string with a new one without reallocating the buffer, if not needed.
Unified · Split
| @@ -99,6 +99,16 @@ bool ta_vasprintf_append_buffer(char **str, const char *fmt, va_list ap) TA_PRF( · 98 unmodified lines | ||
| 99 | 99 | #define ta_dup(ta_parent, ptr) \ |
| 100 | 100 | (TA_TYPEOF(ptr))ta_memdup(ta_parent, ptr, sizeof(*(ptr))) |
| 101 | 101 | |
| 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 | + | |
| 102 | 112 | // Ugly macros that crash on OOM. |
| 103 | 113 | // All of these mirror real functions (with a 'x' added after the 'ta_' |
| 104 | 114 | // prefix), and the only difference is that they will call abort() on allocation |
| @@ -50,6 +50,7 @@ · 49 unmodified lines | ||
| 50 | 50 | #define talloc_strndup ta_xstrndup |
| 51 | 51 | #define talloc_asprintf ta_xasprintf |
| 52 | 52 | #define talloc_vasprintf ta_xvasprintf |
| 53 | +#define talloc_replace ta_replace | |
| 53 | 54 | |
| 54 | 55 | // Don't define linker-level symbols, as that would clash with real libtalloc. |
| 55 | 56 | #define talloc_strdup_append ta_talloc_strdup_append |