blob: 7625da167ccb9a212c7d360fba3cc48f29f4fa1b [file] [log] [blame]
Sylvain Munaut12ba7782014-06-16 10:13:40 +02001#pragma once
Harald Welteec8b4502010-02-20 20:34:29 +01002
Neels Hofmeyr4335bad2017-10-07 04:39:14 +02003#include <stdbool.h>
Vadim Yanitskiy55944302018-09-05 02:58:03 +07004#include <stdint.h>
5#include <stdio.h>
Neels Hofmeyr823073a2019-10-28 04:58:04 +01006#include <string.h>
Neels Hofmeyr4335bad2017-10-07 04:39:14 +02007
Harald Welteb8add362013-07-06 23:49:41 +02008#include <osmocom/core/backtrace.h>
Harald Weltef3c7e852014-08-20 22:50:47 +02009#include <osmocom/core/talloc.h>
Harald Welte459a1802018-06-28 09:24:17 +020010#include <osmocom/core/panic.h>
Neels Hofmeyr7c749892018-09-07 03:01:38 +020011#include <osmocom/core/defs.h>
Harald Welteb8add362013-07-06 23:49:41 +020012
Harald Welte8598f182011-08-17 14:19:27 +020013/*! \defgroup utils General-purpose utility functions
14 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020015 * \file utils.h */
Harald Welte8598f182011-08-17 14:19:27 +020016
Neels Hofmeyr87e45502017-06-20 00:17:59 +020017/*! Determine number of elements in an array of static size */
Kévin Redond1e220f2019-06-13 13:41:00 +020018#ifndef ARRAY_SIZE
Harald Welteec8b4502010-02-20 20:34:29 +010019#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
Kévin Redond1e220f2019-06-13 13:41:00 +020020#endif
Neels Hofmeyr87e45502017-06-20 00:17:59 +020021/*! Return the maximum of two specified values */
Holger Hans Peter Freyther08b28622012-08-15 17:02:59 +020022#define OSMO_MAX(a, b) ((a) >= (b) ? (a) : (b))
Neels Hofmeyr87e45502017-06-20 00:17:59 +020023/*! Return the minimum of two specified values */
Holger Hans Peter Freyther08b28622012-08-15 17:02:59 +020024#define OSMO_MIN(a, b) ((a) >= (b) ? (b) : (a))
Neels Hofmeyr002a51d2019-10-30 04:37:47 +010025/*! Return a typical cmp result for comparable entities a and b. */
26#define OSMO_CMP(a, b) ((a) < (b)? -1 : ((a) > (b)? 1 : 0))
Neels Hofmeyrdc75b112017-12-09 05:51:42 +010027/*! Stringify the name of a macro x, e.g. an FSM event name.
28 * Note: if nested within another preprocessor macro, this will
29 * stringify the value of x instead of its name. */
Neels Hofmeyr18080962016-12-16 13:43:54 +010030#define OSMO_STRINGIFY(x) #x
Neels Hofmeyr84ea2e02017-12-09 05:53:18 +010031/*! Stringify the value of a macro x, e.g. a port number. */
32#define OSMO_STRINGIFY_VAL(x) OSMO_STRINGIFY(x)
Neels Hofmeyr87e45502017-06-20 00:17:59 +020033/*! Make a value_string entry from an enum value name */
Neels Hofmeyr8a5d60b2017-03-09 23:01:37 +010034#define OSMO_VALUE_STRING(x) { x, #x }
Neels Hofmeyr87e45502017-06-20 00:17:59 +020035/*! Number of bytes necessary to store given BITS */
Maxdd75bac2017-06-13 15:07:01 +020036#define OSMO_BYTES_FOR_BITS(BITS) ((BITS + 8 - 1) / 8)
Harald Welteec8b4502010-02-20 20:34:29 +010037
Max18c014d2018-01-30 14:33:01 +010038/*! Copy a C-string into a sized buffer using sizeof to detect buffer's size */
39#define OSMO_STRLCPY_ARRAY(array, src) osmo_strlcpy(array, src, sizeof(array))
40
Vadim Yanitskiye59e8392022-02-02 01:17:45 +060041/*! Branch prediction optimizations */
42#if defined(__GNUC__)
43#define OSMO_LIKELY(exp) __builtin_expect(!!(exp), 1)
44#define OSMO_UNLIKELY(exp) __builtin_expect(!!(exp), 0)
45#else
46#define OSMO_LIKELY(exp) exp
47#define OSMO_UNLIKELY(exp) exp
48#endif
49
Neels Hofmeyr87e45502017-06-20 00:17:59 +020050/*! A mapping between human-readable string and numeric value */
Harald Welted284cd92010-03-01 21:58:31 +010051struct value_string {
Harald Welte7d6166a2022-01-09 11:57:01 +010052 uint32_t value; /*!< numeric value */
Neels Hofmeyr87e45502017-06-20 00:17:59 +020053 const char *str; /*!< human-readable string */
Harald Welted284cd92010-03-01 21:58:31 +010054};
55
56const char *get_value_string(const struct value_string *vs, uint32_t val);
Neels Hofmeyr8d6dcd92016-06-06 18:05:23 +020057const char *get_value_string_or_null(const struct value_string *vs,
58 uint32_t val);
Harald Weltebd598e32011-08-16 23:26:52 +020059
Harald Welted284cd92010-03-01 21:58:31 +010060int get_string_value(const struct value_string *vs, const char *str);
61
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +020062char osmo_bcd2char(uint8_t bcd);
Kévin Redon77021c72019-06-13 18:12:33 +020063/* only works for numbers in ASCII */
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +020064uint8_t osmo_char2bcd(char c);
Harald Welted284cd92010-03-01 21:58:31 +010065
Neels Hofmeyr7079e692018-12-05 21:02:36 +010066int osmo_bcd2str(char *dst, size_t dst_size, const uint8_t *bcd, int start_nibble, int end_nibble, bool allow_hex);
Neels Hofmeyr83025bf2020-05-26 02:45:23 +020067int osmo_str2bcd(uint8_t *dst, size_t dst_size, const char *digits, int start_nibble, int end_nibble, bool allow_hex);
Neels Hofmeyr7079e692018-12-05 21:02:36 +010068
Harald Welte7d6166a2022-01-09 11:57:01 +010069int osmo_hexparse(const char *str, uint8_t *b, unsigned int max_len);
Harald Weltebd598e32011-08-16 23:26:52 +020070
Harald Welte4a62eda2019-03-18 18:27:00 +010071char *osmo_ubit_dump_buf(char *buf, size_t buf_len, const uint8_t *bits, unsigned int len);
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +020072char *osmo_ubit_dump(const uint8_t *bits, unsigned int len);
Harald Welte8598f182011-08-17 14:19:27 +020073char *osmo_hexdump(const unsigned char *buf, int len);
Harald Welte179f3572019-03-18 18:38:47 +010074char *osmo_hexdump_c(const void *ctx, const unsigned char *buf, int len);
Sylvain Munautff23d242011-11-10 23:03:18 +010075char *osmo_hexdump_nospc(const unsigned char *buf, int len);
Harald Welte179f3572019-03-18 18:38:47 +010076char *osmo_hexdump_nospc_c(const void *ctx, const unsigned char *buf, int len);
Neels Hofmeyr0423b612019-01-14 23:32:53 +010077const char *osmo_hexdump_buf(char *out_buf, size_t out_buf_size, const unsigned char *buf, int len, const char *delim,
78 bool delim_after_last);
79
Sylvain Munaut4cfbae82011-11-13 23:04:00 +010080char *osmo_osmo_hexdump_nospc(const unsigned char *buf, int len) __attribute__((__deprecated__));
Harald Welte3eba9912010-07-30 10:37:29 +020081
Holger Hans Peter Freyther0f40ae12015-04-13 09:21:05 +020082#define osmo_static_assert(exp, name) typedef int dummy##name [(exp) ? 1 : -1] __attribute__((__unused__));
Holger Hans Peter Freyther52c07ca2011-01-16 17:37:27 +010083
Neels Hofmeyr7c749892018-09-07 03:01:38 +020084void osmo_str2lower(char *out, const char *in)
85 OSMO_DEPRECATED("Use osmo_str_tolower() or osmo_str_tolower_buf() instead,"
86 " to properly check target memory bounds");
87void osmo_str2upper(char *out, const char *in)
88 OSMO_DEPRECATED("Use osmo_str_toupper() or osmo_str_toupper_buf() instead,"
89 " to properly check target memory bounds");
90
91size_t osmo_str_tolower_buf(char *dest, size_t dest_len, const char *src);
92const char *osmo_str_tolower(const char *src);
Harald Welte179f3572019-03-18 18:38:47 +010093char *osmo_str_tolower_c(const void *ctx, const char *src);
Neels Hofmeyr7c749892018-09-07 03:01:38 +020094
95size_t osmo_str_toupper_buf(char *dest, size_t dest_len, const char *src);
96const char *osmo_str_toupper(const char *src);
Harald Welte179f3572019-03-18 18:38:47 +010097char *osmo_str_toupper_c(const void *ctx, const char *src);
Harald Welte28222962011-02-18 20:37:04 +010098
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +020099#define OSMO_SNPRINTF_RET(ret, rem, offset, len) \
100do { \
101 len += ret; \
102 if (ret > rem) \
103 ret = rem; \
104 offset += ret; \
105 rem -= ret; \
106} while (0)
107
Kévin Redon77021c72019-06-13 18:12:33 +0200108/*! Helper macro to terminate when an assertion fails
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200109 * \param[in] exp Predicate to verify
110 * This function will generate a backtrace and terminate the program if
111 * the predicate evaluates to false (0).
112 */
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000113#define OSMO_ASSERT(exp) \
Vadim Yanitskiye2425482019-07-09 13:56:24 +0700114 if (!(exp)) { \
Neels Hofmeyr983dcb92018-08-20 12:33:22 +0200115 osmo_panic("Assert failed %s %s:%d\n", #exp, __FILE__, __LINE__); \
Vadim Yanitskiye2425482019-07-09 13:56:24 +0700116 }
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000117
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200118/*! duplicate a string using talloc and release its prior content (if any)
119 * \param[in] ctx Talloc context to use for allocation
120 * \param[out] dst pointer to string, will be updated with ptr to new string
Kévin Redon77021c72019-06-13 18:12:33 +0200121 * \param[in] newstr String that will be copied to newly allocated string */
Harald Welte881dcaf2016-07-28 08:02:48 +0200122static inline void osmo_talloc_replace_string(void *ctx, char **dst, const char *newstr)
Harald Weltef3c7e852014-08-20 22:50:47 +0200123{
124 if (*dst)
125 talloc_free(*dst);
126 *dst = talloc_strdup(ctx, newstr);
127}
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000128
Vadim Yanitskiyc5497192021-06-07 16:30:28 +0200129void osmo_talloc_replace_string_fmt(void *ctx, char **dst, const char *fmt, ...);
130
Neels Hofmeyrabdd7a22017-10-19 02:40:01 +0200131/*! Append to a string and re-/allocate if necessary.
132 * \param[in] ctx Talloc context to use for initial allocation.
133 * \param[in,out] dest char* to re-/allocate and append to.
134 * \param[in] fmt printf-like string format.
135 * \param[in] args Arguments for fmt.
136 *
137 * \a dest may be passed in NULL, or a string previously allocated by talloc.
138 * If an existing string is passed in, it will remain associated with whichever
139 * ctx it was allocated before, regardless whether it matches \a ctx or not.
140 */
141#define osmo_talloc_asprintf(ctx, dest, fmt, args ...) \
142 do { \
143 if (!dest) \
144 dest = talloc_asprintf(ctx, fmt, ## args); \
145 else \
146 dest = talloc_asprintf_append((char*)dest, fmt, ## args); \
147 } while (0)
148
Harald Welte9709b2e2016-04-25 18:47:53 +0200149int osmo_constant_time_cmp(const uint8_t *exp, const uint8_t *rel, const int count);
150uint64_t osmo_decode_big_endian(const uint8_t *data, size_t data_len);
151uint8_t *osmo_encode_big_endian(uint64_t value, size_t data_len);
152
Harald Welteaeecc482016-11-26 10:41:40 +0100153size_t osmo_strlcpy(char *dst, const char *src, size_t siz);
Neels Hofmeyr06356fd2019-11-19 01:38:10 +0100154const char *osmo_strnchr(const char *str, size_t str_size, char c);
Harald Welteaeecc482016-11-26 10:41:40 +0100155
Neels Hofmeyr4335bad2017-10-07 04:39:14 +0200156bool osmo_is_hexstr(const char *str, int min_digits, int max_digits,
157 bool require_even);
158
Harald Weltefebe83c2017-10-03 17:41:59 +0800159bool osmo_identifier_valid(const char *str);
Neels Hofmeyr937ddea2017-12-16 00:46:50 +0100160bool osmo_separated_identifiers_valid(const char *str, const char *sep_chars);
Neels Hofmeyrb480b742019-04-11 06:58:44 +0200161void osmo_identifier_sanitize_buf(char *str, const char *sep_chars, char replace_with);
Harald Weltefebe83c2017-10-03 17:41:59 +0800162
Neels Hofmeyr8a7eed52019-11-21 00:12:10 +0100163size_t osmo_escape_cstr_buf(char *buf, size_t bufsize, const char *str, int in_len);
164char *osmo_escape_cstr_c(void *ctx, const char *str, int in_len);
165size_t osmo_quote_cstr_buf(char *buf, size_t bufsize, const char *str, int in_len);
166char *osmo_quote_cstr_c(void *ctx, const char *str, int in_len);
167
Neels Hofmeyr9910bbc2017-12-16 00:54:52 +0100168const char *osmo_escape_str(const char *str, int len);
Neels Hofmeyr16337352022-01-31 15:56:02 +0100169int osmo_escape_str_buf3(char *buf, size_t bufsize, const char *str, int in_len);
Neels Hofmeyrecef7ec2019-03-05 16:42:50 +0100170char *osmo_escape_str_buf2(char *buf, size_t bufsize, const char *str, int in_len);
171const char *osmo_escape_str_buf(const char *str, int in_len, char *buf, size_t bufsize);
Harald Welte179f3572019-03-18 18:38:47 +0100172char *osmo_escape_str_c(const void *ctx, const char *str, int in_len);
Neels Hofmeyr04eb56f2018-04-09 00:41:28 +0200173const char *osmo_quote_str(const char *str, int in_len);
Neels Hofmeyr16337352022-01-31 15:56:02 +0100174int osmo_quote_str_buf3(char *buf, size_t bufsize, const char *str, int in_len);
Neels Hofmeyrecef7ec2019-03-05 16:42:50 +0100175char *osmo_quote_str_buf2(char *buf, size_t bufsize, const char *str, int in_len);
176const char *osmo_quote_str_buf(const char *str, int in_len, char *buf, size_t bufsize);
Harald Welte179f3572019-03-18 18:38:47 +0100177char *osmo_quote_str_c(const void *ctx, const char *str, int in_len);
Neels Hofmeyr9910bbc2017-12-16 00:54:52 +0100178
Neels Hofmeyrecef7ec2019-03-05 16:42:50 +0100179int osmo_print_n(char *buf, size_t bufsize, const char *str, size_t n);
180
Harald Welte15a5f8d2018-06-06 16:58:17 +0200181uint32_t osmo_isqrt32(uint32_t x);
182
Vadim Yanitskiyd9fc6042019-06-12 15:49:03 +0700183char osmo_luhn(const char* in, int in_len);
Oliver Smith894be2d2019-01-11 13:13:37 +0100184
Neels Hofmeyr2cbe25f2019-02-11 20:32:06 +0100185/*! State for OSMO_STRBUF_APPEND() and OSMO_STRBUF_PRINTF(). See there for examples. */
186struct osmo_strbuf {
187 /*! Point to the start of a string buffer. */
188 char *buf;
189 /*! Total sizeof() the buffer buf points at. */
190 size_t len;
191 /*! Current writing position in buf (end of the string written so far). */
192 char *pos;
193 /*! After all OSMO_STRBUF_APPEND operations, reflects the total number of characters that would be written had
194 * buf been large enough. Like snprintf()'s return value, this does not include the terminating nul character.
195 * Hence, to allocate an adequately sized buffer, add 1 to this number. */
196 size_t chars_needed;
197};
198
199/*! Append a string to a buffer, as printed by an snprintf()-like function and with similar bounds checking.
200 * Make sure to never write past the end of the buffer, and collect the total size that would be needed.
201 *
202 * // an example function implementation to append: write N spaces.
203 * int print_spaces(char *dst, size_t dst_len, int n)
204 * {
205 * int i;
206 * if (n < 0)
207 * return -EINVAL;
208 * for (i = 0; i < n && i < dst_len; i++)
209 * dst[i] = ' ';
210 * if (dst_len)
211 * dst[OSMO_MIN(dst_len - 1, n)] = '\0';
212 * // return the n that we would have liked to write if space were available:
213 * return n;
214 * }
215 *
216 * // append above spaces as well as an snprintf()
217 * void strbuf_example()
218 * {
219 * char buf[23];
220 * struct osmo_strbuf sb = { .buf = buf, .len = sizeof(buf) };
221 *
222 * OSMO_STRBUF_APPEND(sb, print_spaces, 5);
223 * OSMO_STRBUF_APPEND(sb, snprintf, "The answer is %d but what is the question?", 42);
224 * OSMO_STRBUF_APPEND(sb, print_spaces, 423423);
225 *
226 * printf("%s\n", buf);
227 * printf("would have needed %zu bytes\n", sb.chars_needed);
228 * }
229 *
230 * \param[inout] STRBUF A struct osmo_strbuf instance.
231 * \param[in] func A function with a signature of int func(char *dst, size_t dst_len [, args]) with semantics like
232 * snprintf().
233 * \param[in] args Arguments passed to func, if any.
234 */
235#define OSMO_STRBUF_APPEND(STRBUF, func, args...) do { \
236 if (!(STRBUF).pos) \
237 (STRBUF).pos = (STRBUF).buf; \
Neels Hofmeyr8531d662019-04-11 07:16:02 +0200238 size_t _sb_remain = (STRBUF).buf ? (STRBUF).len - ((STRBUF).pos - (STRBUF).buf) : 0; \
239 int _sb_l = func((STRBUF).pos, _sb_remain, ##args); \
Pau Espin Pedrol7af860f2021-07-27 16:32:36 +0200240 if (_sb_l < 0 || (size_t)_sb_l > _sb_remain) \
Neels Hofmeyr2cbe25f2019-02-11 20:32:06 +0100241 (STRBUF).pos = (STRBUF).buf + (STRBUF).len; \
Neels Hofmeyr8531d662019-04-11 07:16:02 +0200242 else if ((STRBUF).pos) \
243 (STRBUF).pos += _sb_l; \
244 if (_sb_l > 0) \
245 (STRBUF).chars_needed += _sb_l; \
Neels Hofmeyr2cbe25f2019-02-11 20:32:06 +0100246 } while(0)
247
248/*! Shortcut for OSMO_STRBUF_APPEND() invocation using snprintf().
249 *
250 * int strbuf_example2(char *buf, size_t buflen)
251 * {
252 * int i;
253 * struct osmo_strbuf sb = { .buf = buf, .len = buflen };
254 *
255 * OSMO_STRBUF_PRINTF(sb, "T minus");
256 * for (i = 10; i; i--)
257 * OSMO_STRBUF_PRINTF(sb, " %d", i);
258 * OSMO_STRBUF_PRINTF(sb, " ... Lift off!");
259 *
260 * return sb.chars_needed;
261 * }
262 *
263 * \param[inout] STRBUF A struct osmo_strbuf instance.
264 * \param[in] fmt Format string passed to snprintf.
265 * \param[in] args Additional arguments passed to snprintf, if any.
266 */
267#define OSMO_STRBUF_PRINTF(STRBUF, fmt, args...) \
268 OSMO_STRBUF_APPEND(STRBUF, snprintf, fmt, ##args)
269
Neels Hofmeyr8531d662019-04-11 07:16:02 +0200270/*! Like OSMO_STRBUF_APPEND(), but for function signatures that return the char* buffer instead of a length.
271 * When using this function, the final STRBUF.chars_needed may not reflect the actual number of characters needed, since
272 * that number cannot be obtained from this kind of function signature.
273 * \param[inout] STRBUF A struct osmo_strbuf instance.
274 * \param[in] func A function with a signature of char *func(char *dst, size_t dst_len [, args]) where
275 * the returned string is always written to dst.
276 * \param[in] args Arguments passed to func, if any.
277 */
278#define OSMO_STRBUF_APPEND_NOLEN(STRBUF, func, args...) do { \
279 if (!(STRBUF).pos) \
280 (STRBUF).pos = (STRBUF).buf; \
281 size_t _sb_remain = (STRBUF).buf ? (STRBUF).len - ((STRBUF).pos - (STRBUF).buf) : 0; \
282 if (_sb_remain) { \
283 func((STRBUF).pos, _sb_remain, ##args); \
284 } \
285 size_t _sb_l = (STRBUF).pos ? strnlen((STRBUF).pos, _sb_remain) : 0; \
286 if (_sb_l > _sb_remain) \
287 (STRBUF).pos = (STRBUF).buf + (STRBUF).len; \
288 else if ((STRBUF).pos) \
289 (STRBUF).pos += _sb_l; \
290 (STRBUF).chars_needed += _sb_l; \
291 } while(0)
292
Neels Hofmeyrd79ccc62019-03-07 23:08:40 +0100293bool osmo_str_startswith(const char *str, const char *startswith_str);
294
Neels Hofmeyr87c3afb2020-09-30 21:47:47 +0000295int osmo_float_str_to_int(int64_t *val, const char *str, unsigned int precision);
296int osmo_int_to_float_str_buf(char *buf, size_t buflen, int64_t val, unsigned int precision);
297char *osmo_int_to_float_str_c(void *ctx, int64_t val, unsigned int precision);
298
Neels Hofmeyr47773342021-09-05 18:48:31 +0200299int osmo_str_to_int64(int64_t *result, const char *str, int base, int64_t min_val, int64_t max_val);
300int osmo_str_to_int(int *result, const char *str, int base, int min_val, int max_val);
301
Neels Hofmeyr823073a2019-10-28 04:58:04 +0100302/*! Translate a buffer function to a talloc context function.
303 * This is the full function body of a char *foo_name_c(void *ctx, val...) function, implemented by an
304 * int foo_name_buf(buf, buflen, val...) function:
305 *
306 * char *foo_name_c(void *ctx, example_t arg)
307 * {
308 * OSMO_NAME_C_IMPL(ctx, 64, "ERROR", foo_name_buf, arg)
309 * }
310 *
311 * Return a talloc'd string containing the result of the given foo_name_buf() function, or ON_ERROR on error in the called
312 * foo_name_buf() function.
313 *
314 * If ON_ERROR is NULL, the function returns NULL on error rc from FUNC_BUF. Take care: returning NULL in printf() like
315 * formats (LOGP()) makes the program crash. If ON_ERROR is non-NULL, it must be a string constant, which is not
316 * returned directly, but written to an allocated string buffer first.
317 *
318 * \param[in] INITIAL_BUFSIZE Which size to first talloc from ctx -- a larger size makes a reallocation less likely, a
319 * smaller size allocates less unused bytes, zero allocates once but still runs the string composition twice.
320 * \param[in] ON_ERROR String constant to copy on error rc returned by FUNC_BUF, or NULL to return NULL.
Neels Hofmeyrdacac992020-05-26 02:44:21 +0200321 * \param[in] FUNC_BUF Name of a function with signature int foo_buf(char *buf, size_t buflen, ...).
322 * The function must return the strlen() that it would write to a sufficiently large buffer or
323 * negative on error, like snprintf().
Neels Hofmeyr823073a2019-10-28 04:58:04 +0100324 * \param[in] FUNC_BUF_ARGS Additional arguments to pass to FUNC_BUF after the buf and buflen.
325 */
326#define OSMO_NAME_C_IMPL(CTX, INITIAL_BUFSIZE, ON_ERROR, FUNC_BUF, FUNC_BUF_ARGS...) \
327 size_t _len = INITIAL_BUFSIZE; \
328 int _needed; \
329 char *_str = NULL; \
330 if ((INITIAL_BUFSIZE) > 0) { \
331 _str = (char*)talloc_named_const(CTX, _len, __func__); \
332 OSMO_ASSERT(_str); \
333 } \
334 _needed = FUNC_BUF(_str, _len, ## FUNC_BUF_ARGS); \
335 if (_needed < 0) \
336 goto OSMO_NAME_C_on_error; \
Harald Welte7d6166a2022-01-09 11:57:01 +0100337 if ((unsigned int) _needed < _len) \
Neels Hofmeyr823073a2019-10-28 04:58:04 +0100338 return _str; \
339 _len = _needed + 1; \
340 if (_str) \
341 talloc_free(_str); \
342 _str = (char*)talloc_named_const(CTX, _len, __func__); \
343 OSMO_ASSERT(_str); \
344 _needed = FUNC_BUF(_str, _len, ## FUNC_BUF_ARGS); \
345 if (_needed < 0) \
346 goto OSMO_NAME_C_on_error; \
347 return _str; \
348OSMO_NAME_C_on_error: \
349 /* Re-using and re-sizing above allocated buf ends up in very complex code. Just free and strdup. */ \
350 if (_str) \
351 talloc_free(_str); \
352 if (!(ON_ERROR)) \
353 return NULL; \
354 _str = talloc_strdup(CTX, ON_ERROR); \
355 OSMO_ASSERT(_str); \
356 talloc_set_name_const(_str, __func__); \
357 return _str;
358
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200359/*! @} */