]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
lib/utils: generalize kr_strcatdup() for mempools
authorVladimír Čunát <vladimir.cunat@nic.cz>
Sun, 1 Sep 2024 09:01:22 +0000 (11:01 +0200)
committerVladimír Čunát <vladimir.cunat@nic.cz>
Fri, 6 Sep 2024 10:26:37 +0000 (12:26 +0200)
It's trivial really, and I'd like to use it now.

lib/utils.c
lib/utils.h

index d04f5467bba86ae186e2f60dec435c410f18050a..882aeb3e2b31e37b6f33b39e5226c33194a77234 100644 (file)
@@ -107,7 +107,7 @@ static inline int u16tostr(uint8_t *dst, uint16_t num)
        return 5;
 }
 
-char* kr_strcatdup(unsigned n, ...)
+char* kr_strcatdup_pool(knot_mm_t *pool, unsigned n, ...)
 {
        if (n < 1) {
                return NULL;
@@ -132,7 +132,7 @@ char* kr_strcatdup(unsigned n, ...)
        char *result = NULL;
        if (total_len > 0) {
                if (unlikely(total_len == SIZE_MAX)) return NULL;
-               result = malloc(total_len + 1);
+               result = mm_alloc(pool, total_len + 1);
        }
        if (result) {
                char *stream = result;
index e03b473dcc1616bdb3031887009a6d881c20e2ff..e8122c9986e62d92bb7e7391322bb6123a6b643b 100644 (file)
@@ -170,9 +170,11 @@ typedef struct kr_http_header_array_entry {
 /** Array of HTTP headers for DoH. */
 typedef array_t(kr_http_header_array_entry_t) kr_http_header_array_t;
 
-/** Concatenate N strings. */
+/** Concatenate N strings and put the result into a mempool. */
 KR_EXPORT
-char* kr_strcatdup(unsigned n, ...);
+char* kr_strcatdup_pool(knot_mm_t *pool, unsigned n, ...);
+/** Concatenate N strings. */
+#define kr_strcatdup(n, ...) kr_strcatdup_pool(NULL, n, ## __VA_ARGS__)
 
 /** Construct absolute file path, without resolving symlinks.
  * \return malloc-ed string or NULL (+errno in that case) */