Skip to content

Commit ae8604d

Browse files
newrendscho
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7a5c757 commit ae8604d

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

fast-import.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -526,14 +526,6 @@ static unsigned int hc_str(const char *s, size_t len)
526526
return r;
527527
}
528528

529-
static char *pool_strdup(const char *s)
530-
{
531-
size_t len = strlen(s) + 1;
532-
char *r = mem_pool_alloc(&fi_mem_pool, len);
533-
memcpy(r, s, len);
534-
return r;
535-
}
536-
537529
static void insert_mark(struct mark_set *s, uintmax_t idnum, struct object_entry *oe)
538530
{
539531
while ((idnum >> s->shift) >= 1024) {
@@ -615,7 +607,7 @@ static struct branch *new_branch(const char *name)
615607
die("Branch name doesn't conform to GIT standards: %s", name);
616608

617609
b = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct branch));
618-
b->name = pool_strdup(name);
610+
b->name = mem_pool_strdup(&fi_mem_pool, name);
619611
b->table_next_branch = branch_table[hc];
620612
b->branch_tree.versions[0].mode = S_IFDIR;
621613
b->branch_tree.versions[1].mode = S_IFDIR;
@@ -2806,7 +2798,7 @@ static void parse_new_tag(const char *arg)
28062798

28072799
t = mem_pool_alloc(&fi_mem_pool, sizeof(struct tag));
28082800
memset(t, 0, sizeof(struct tag));
2809-
t->name = pool_strdup(arg);
2801+
t->name = mem_pool_strdup(&fi_mem_pool, arg);
28102802
if (last_tag)
28112803
last_tag->next_tag = t;
28122804
else

mem-pool.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,24 @@ void *mem_pool_calloc(struct mem_pool *mem_pool, size_t count, size_t size)
102102
return r;
103103
}
104104

105+
char *mem_pool_strdup(struct mem_pool *pool, const char *str)
106+
{
107+
size_t len = strlen(str) + 1;
108+
char *ret = mem_pool_alloc(pool, len);
109+
110+
return memcpy(ret, str, len);
111+
}
112+
113+
char *mem_pool_strndup(struct mem_pool *pool, const char *str, size_t len)
114+
{
115+
char *p = memchr(str, '\0', len);
116+
size_t actual_len = (p ? p - str : len);
117+
char *ret = mem_pool_alloc(pool, actual_len+1);
118+
119+
ret[actual_len] = '\0';
120+
return memcpy(ret, str, actual_len);
121+
}
122+
105123
int mem_pool_contains(struct mem_pool *mem_pool, void *mem)
106124
{
107125
struct mp_block *p;

mem-pool.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ void *mem_pool_alloc(struct mem_pool *pool, size_t len);
4141
*/
4242
void *mem_pool_calloc(struct mem_pool *pool, size_t count, size_t size);
4343

44+
/*
45+
* Allocate memory from the memory pool and copy str into it.
46+
*/
47+
char *mem_pool_strdup(struct mem_pool *pool, const char *str);
48+
char *mem_pool_strndup(struct mem_pool *pool, const char *str, size_t len);
49+
4450
/*
4551
* Move the memory associated with the 'src' pool to the 'dst' pool. The 'src'
4652
* pool will be empty and not contain any memory. It still needs to be free'd

0 commit comments

Comments
 (0)