diff options
author | Elijah Newren <newren@gmail.com> | 2020-08-15 17:37:55 +0000 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2020-08-18 12:14:37 -0700 |
commit | a762c8c1e1e5b6352f027db80be4ca1c0077403d (patch) | |
tree | f2ce4542c00e8d27d94a0855edd0c0cc5eb5ae2d /mem-pool.c | |
parent | 2befe97201e1f3175cce557866c5822793624b5a (diff) | |
download | git-a762c8c1e1e5b6352f027db80be4ca1c0077403d.tar.gz |
mem-pool: add convenience functions for strdup and strndup
fast-import had a special mem_pool_strdup() convenience function that I
want to be able to use from the new merge algorithm I am writing. Move
it from fast-import to mem-pool, and also add a mem_pool_strndup()
while at it that I also want to use.
Signed-off-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'mem-pool.c')
-rw-r--r-- | mem-pool.c | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/mem-pool.c b/mem-pool.c index a2841a4a9a..020b51e0c5 100644 --- a/mem-pool.c +++ b/mem-pool.c @@ -102,6 +102,24 @@ void *mem_pool_calloc(struct mem_pool *mem_pool, size_t count, size_t size) return r; } +char *mem_pool_strdup(struct mem_pool *pool, const char *str) +{ + size_t len = strlen(str) + 1; + char *ret = mem_pool_alloc(pool, len); + + return memcpy(ret, str, len); +} + +char *mem_pool_strndup(struct mem_pool *pool, const char *str, size_t len) +{ + char *p = memchr(str, '\0', len); + size_t actual_len = (p ? p - str : len); + char *ret = mem_pool_alloc(pool, actual_len+1); + + ret[actual_len] = '\0'; + return memcpy(ret, str, actual_len); +} + int mem_pool_contains(struct mem_pool *mem_pool, void *mem) { struct mp_block *p; |