Skip to content

Commit 8fb8e3f

Browse files
jamillgitster
authored andcommitted
mem-pool: only search head block for available space
Instead of searching all memory blocks for available space to fulfill a memory request, only search the head block. If the head block does not have space, assume that previous block would most likely not be able to fulfill request either. This could potentially lead to more memory fragmentation, but also avoids searching memory blocks that probably will not be able to fulfill request. This pattern will benefit consumers that are able to generate a good estimate for how much memory will be needed, or if they are performing fixed sized allocations, so that once a block is exhausted it will never be able to fulfill a future request. Signed-off-by: Jameson Miller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a849735 commit 8fb8e3f

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

mem-pool.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ static struct mp_block *mem_pool_alloc_block(struct mem_pool *mem_pool, size_t b
2121

2222
void *mem_pool_alloc(struct mem_pool *mem_pool, size_t len)
2323
{
24-
struct mp_block *p;
24+
struct mp_block *p = NULL;
2525
void *r;
2626

2727
/* round up to a 'uintmax_t' alignment */
2828
if (len & (sizeof(uintmax_t) - 1))
2929
len += sizeof(uintmax_t) - (len & (sizeof(uintmax_t) - 1));
3030

31-
for (p = mem_pool->mp_block; p; p = p->next_block)
32-
if (p->end - p->next_free >= len)
33-
break;
31+
if (mem_pool->mp_block &&
32+
mem_pool->mp_block->end - mem_pool->mp_block->next_free >= len)
33+
p = mem_pool->mp_block;
3434

3535
if (!p) {
3636
if (len >= (mem_pool->block_alloc / 2)) {

0 commit comments

Comments
 (0)