Skip to content

Commit 7f0969f

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a5a15a4 commit 7f0969f

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

reftable/basics.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ void *reftable_calloc(size_t nelem, size_t elsize)
4343
return p;
4444
}
4545

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+
4656
void reftable_set_alloc(void *(*malloc)(size_t),
4757
void *(*realloc)(void *, size_t), void (*free)(void *))
4858
{

reftable/basics.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ void *reftable_malloc(size_t sz);
5454
void *reftable_realloc(void *p, size_t sz);
5555
void reftable_free(void *p);
5656
void *reftable_calloc(size_t nelem, size_t elsize);
57+
char *reftable_strdup(const char *str);
5758

5859
#define REFTABLE_ALLOC_ARRAY(x, alloc) (x) = reftable_malloc(st_mult(sizeof(*(x)), (alloc)))
5960
#define REFTABLE_CALLOC_ARRAY(x, alloc) (x) = reftable_calloc((alloc), sizeof(*(x)))

0 commit comments

Comments
 (0)