reftable: introduce `reftable_strdup()`

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>

Patrick Steinhardt committed Oct 2, 2024 at 12:55 UTC 7f0969febf974f017b92e7152a17c98105583167
2 files changed +11
reftable/basics.c
+10
@@ -43,6 +43,16 @@ void *reftable_calloc(size_t nelem, size_t elsize)
43 return p;
44 }
45
46 +char *reftable_strdup(const char *str)
47 +{
48 + size_t len = strlen(str);
49 + char *result = reftable_malloc(len + 1);
50 + if (!result)
51 + return NULL;
52 + memcpy(result, str, len + 1);
53 + return result;
54 +}
55 +
56 void reftable_set_alloc(void *(*malloc)(size_t),
57 void *(*realloc)(void *, size_t), void (*free)(void *))
58 {
reftable/basics.h
+1
@@ -54,6 +54,7 @@ void *reftable_malloc(size_t sz);
54 void *reftable_realloc(void *p, size_t sz);
55 void reftable_free(void *p);
56 void *reftable_calloc(size_t nelem, size_t elsize);
57 +char *reftable_strdup(const char *str);
58
59 #define REFTABLE_ALLOC_ARRAY(x, alloc) (x) = reftable_malloc(st_mult(sizeof(*(x)), (alloc)))
60 #define REFTABLE_CALLOC_ARRAY(x, alloc) (x) = reftable_calloc((alloc), sizeof(*(x)))