diff options
author | Vincent Ambo <mail@tazj.in> | 2020-11-21T18·20+0100 |
---|---|---|
committer | Vincent Ambo <mail@tazj.in> | 2020-11-21T18·45+0100 |
commit | f4609b896fac842433bd495c166d5987852a6a73 (patch) | |
tree | 95511c465c54c4f5d27e5d39ce187e2a1dd82bd3 /third_party/git/mem-pool.c | |
parent | 082c006c04343a78d87b6c6ab3608c25d6213c3f (diff) |
merge(3p/git): Merge git subtree at v2.29.2 r/1890
This also bumps the stable nixpkgs to 20.09 as of 2020-11-21, because there is some breakage in the git build related to the netrc credentials helper which someone has taken care of in nixpkgs. The stable channel is not used for anything other than git, so this should be fine. Change-Id: I3575a19dab09e1e9556cf8231d717de9890484fb
Diffstat (limited to 'third_party/git/mem-pool.c')
-rw-r--r-- | third_party/git/mem-pool.c | 69 |
1 files changed, 41 insertions, 28 deletions
diff --git a/third_party/git/mem-pool.c b/third_party/git/mem-pool.c index a2841a4a9ad7..8401761dda0a 100644 --- a/third_party/git/mem-pool.c +++ b/third_party/git/mem-pool.c @@ -12,11 +12,13 @@ * `insert_after`. If `insert_after` is NULL, then insert block at the * head of the linked list. */ -static struct mp_block *mem_pool_alloc_block(struct mem_pool *mem_pool, size_t block_alloc, struct mp_block *insert_after) +static struct mp_block *mem_pool_alloc_block(struct mem_pool *pool, + size_t block_alloc, + struct mp_block *insert_after) { struct mp_block *p; - mem_pool->pool_alloc += sizeof(struct mp_block) + block_alloc; + pool->pool_alloc += sizeof(struct mp_block) + block_alloc; p = xmalloc(st_add(sizeof(struct mp_block), block_alloc)); p->next_free = (char *)p->space; @@ -26,35 +28,27 @@ static struct mp_block *mem_pool_alloc_block(struct mem_pool *mem_pool, size_t b p->next_block = insert_after->next_block; insert_after->next_block = p; } else { - p->next_block = mem_pool->mp_block; - mem_pool->mp_block = p; + p->next_block = pool->mp_block; + pool->mp_block = p; } return p; } -void mem_pool_init(struct mem_pool **mem_pool, size_t initial_size) +void mem_pool_init(struct mem_pool *pool, size_t initial_size) { - struct mem_pool *pool; - - if (*mem_pool) - return; - - pool = xcalloc(1, sizeof(*pool)); - + memset(pool, 0, sizeof(*pool)); pool->block_alloc = BLOCK_GROWTH_SIZE; if (initial_size > 0) mem_pool_alloc_block(pool, initial_size, NULL); - - *mem_pool = pool; } -void mem_pool_discard(struct mem_pool *mem_pool, int invalidate_memory) +void mem_pool_discard(struct mem_pool *pool, int invalidate_memory) { struct mp_block *block, *block_to_free; - block = mem_pool->mp_block; + block = pool->mp_block; while (block) { block_to_free = block; @@ -66,10 +60,11 @@ void mem_pool_discard(struct mem_pool *mem_pool, int invalidate_memory) free(block_to_free); } - free(mem_pool); + pool->mp_block = NULL; + pool->pool_alloc = 0; } -void *mem_pool_alloc(struct mem_pool *mem_pool, size_t len) +void *mem_pool_alloc(struct mem_pool *pool, size_t len) { struct mp_block *p = NULL; void *r; @@ -78,15 +73,15 @@ void *mem_pool_alloc(struct mem_pool *mem_pool, size_t len) if (len & (sizeof(uintmax_t) - 1)) len += sizeof(uintmax_t) - (len & (sizeof(uintmax_t) - 1)); - if (mem_pool->mp_block && - mem_pool->mp_block->end - mem_pool->mp_block->next_free >= len) - p = mem_pool->mp_block; + if (pool->mp_block && + pool->mp_block->end - pool->mp_block->next_free >= len) + p = pool->mp_block; if (!p) { - if (len >= (mem_pool->block_alloc / 2)) - return mem_pool_alloc_block(mem_pool, len, mem_pool->mp_block); + if (len >= (pool->block_alloc / 2)) + return mem_pool_alloc_block(pool, len, pool->mp_block); - p = mem_pool_alloc_block(mem_pool, mem_pool->block_alloc, NULL); + p = mem_pool_alloc_block(pool, pool->block_alloc, NULL); } r = p->next_free; @@ -94,20 +89,38 @@ void *mem_pool_alloc(struct mem_pool *mem_pool, size_t len) return r; } -void *mem_pool_calloc(struct mem_pool *mem_pool, size_t count, size_t size) +void *mem_pool_calloc(struct mem_pool *pool, size_t count, size_t size) { size_t len = st_mult(count, size); - void *r = mem_pool_alloc(mem_pool, len); + void *r = mem_pool_alloc(pool, len); memset(r, 0, len); return r; } -int mem_pool_contains(struct mem_pool *mem_pool, void *mem) +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 *pool, void *mem) { struct mp_block *p; /* Check if memory is allocated in a block */ - for (p = mem_pool->mp_block; p; p = p->next_block) + for (p = pool->mp_block; p; p = p->next_block) if ((mem >= ((void *)p->space)) && (mem < ((void *)p->end))) return 1; |