]> git.ipfire.org Git - thirdparty/git.git/commitdiff
reftable: introduce `reftable_strdup()`
authorPatrick Steinhardt <ps@pks.im>
Wed, 2 Oct 2024 10:55:30 +0000 (12:55 +0200)
committerJunio C Hamano <gitster@pobox.com>
Wed, 2 Oct 2024 14:53:51 +0000 (07:53 -0700)
The reftable library provides the ability to swap out allocators. There
is a gap here though, because we continue to use `xstrdup()` even in the
case where all the other allocators have been swapped out.

Introduce `reftable_strdup()` that uses `reftable_malloc()` to do the
allocation.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
reftable/basics.c
reftable/basics.h

index cf072935c88060310ff1c5f189d0f3f8dc7ba4ea..4adc98cf5ded5626e4b79caf724bb543fe841808 100644 (file)
@@ -43,6 +43,16 @@ void *reftable_calloc(size_t nelem, size_t elsize)
        return p;
 }
 
+char *reftable_strdup(const char *str)
+{
+       size_t len = strlen(str);
+       char *result = reftable_malloc(len + 1);
+       if (!result)
+               return NULL;
+       memcpy(result, str, len + 1);
+       return result;
+}
+
 void reftable_set_alloc(void *(*malloc)(size_t),
                        void *(*realloc)(void *, size_t), void (*free)(void *))
 {
index 4e2e76014aa761560879fe0a6bdd8866fa075fa3..f107e1486057b07984a36ce0cc67e68ef3e5401c 100644 (file)
@@ -54,6 +54,7 @@ void *reftable_malloc(size_t sz);
 void *reftable_realloc(void *p, size_t sz);
 void reftable_free(void *p);
 void *reftable_calloc(size_t nelem, size_t elsize);
+char *reftable_strdup(const char *str);
 
 #define REFTABLE_ALLOC_ARRAY(x, alloc) (x) = reftable_malloc(st_mult(sizeof(*(x)), (alloc)))
 #define REFTABLE_CALLOC_ARRAY(x, alloc) (x) = reftable_calloc((alloc), sizeof(*(x)))