@matheusbsbs / matheus-bagliano / commits / a762c8c1e1
Fork 0

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>

Elijah Newren committed Aug 15, 2020 at 17:37 UTC a762c8c1e1e5b6352f027db80be4ca1c0077403d
3 files changed +26 -10
fast-import.c
+2 -10
@@ -526,14 +526,6 @@ static unsigned int hc_str(const char *s, size_t len)
526 return r;
527 }
528
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 -
529 static void insert_mark(struct mark_set *s, uintmax_t idnum, struct object_entry *oe)
530 {
531 while ((idnum >> s->shift) >= 1024) {
@@ -615,7 +607,7 @@ static struct branch *new_branch(const char *name)
607 die("Branch name doesn't conform to GIT standards: %s", name);
608
609 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);
611 b->table_next_branch = branch_table[hc];
612 b->branch_tree.versions[0].mode = S_IFDIR;
613 b->branch_tree.versions[1].mode = S_IFDIR;
@@ -2806,7 +2798,7 @@ static void parse_new_tag(const char *arg)
2798
2799 t = mem_pool_alloc(&fi_mem_pool, sizeof(struct tag));
2800 memset(t, 0, sizeof(struct tag));
2809 - t->name = pool_strdup(arg);
2801 + t->name = mem_pool_strdup(&fi_mem_pool, arg);
2802 if (last_tag)
2803 last_tag->next_tag = t;
2804 else
mem-pool.c
+18
@@ -102,6 +102,24 @@ void *mem_pool_calloc(struct mem_pool *mem_pool, size_t count, size_t size)
102 return r;
103 }
104
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 +
123 int mem_pool_contains(struct mem_pool *mem_pool, void *mem)
124 {
125 struct mp_block *p;
mem-pool.h
+6
@@ -41,6 +41,12 @@ void *mem_pool_alloc(struct mem_pool *pool, size_t len);
41 */
42 void *mem_pool_calloc(struct mem_pool *pool, size_t count, size_t size);
43
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 +
50 /*
51 * Move the memory associated with the 'src' pool to the 'dst' pool. The 'src'
52 * pool will be empty and not contain any memory. It still needs to be free'd