Skip to content

Commit cac7a2b

Browse files
committed
Merge branch 'jm/mem-pool'
An reusable "memory pool" implementation has been extracted from fast-import.c, which in turn has become the first user of the mem-pool API. * jm/mem-pool: mem-pool: move reusable parts of memory pool into its own file fast-import: introduce mem_pool type fast-import: rename mem_pool type to mp_block
2 parents bedb10c + 065feab commit cac7a2b

File tree

4 files changed

+106
-61
lines changed

4 files changed

+106
-61
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,7 @@ LIB_OBJS += log-tree.o
845845
LIB_OBJS += mailinfo.o
846846
LIB_OBJS += mailmap.o
847847
LIB_OBJS += match-trees.o
848+
LIB_OBJS += mem-pool.o
848849
LIB_OBJS += merge.o
849850
LIB_OBJS += merge-blobs.o
850851
LIB_OBJS += merge-recursive.o

fast-import.c

Lines changed: 16 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ Format of STDIN stream:
170170
#include "run-command.h"
171171
#include "packfile.h"
172172
#include "object-store.h"
173+
#include "mem-pool.h"
173174

174175
#define PACK_ID_BITS 16
175176
#define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
@@ -211,13 +212,6 @@ struct last_object {
211212
unsigned no_swap : 1;
212213
};
213214

214-
struct mem_pool {
215-
struct mem_pool *next_pool;
216-
char *next_free;
217-
char *end;
218-
uintmax_t space[FLEX_ARRAY]; /* more */
219-
};
220-
221215
struct atom_str {
222216
struct atom_str *next_atom;
223217
unsigned short str_len;
@@ -306,9 +300,8 @@ static int global_argc;
306300
static const char **global_argv;
307301

308302
/* Memory pools */
309-
static size_t mem_pool_alloc = 2*1024*1024 - sizeof(struct mem_pool);
310-
static size_t total_allocd;
311-
static struct mem_pool *mem_pool;
303+
static struct mem_pool fi_mem_pool = {NULL, 2*1024*1024 -
304+
sizeof(struct mp_block), 0 };
312305

313306
/* Atom management */
314307
static unsigned int atom_table_sz = 4451;
@@ -343,6 +336,7 @@ static unsigned int tree_entry_alloc = 1000;
343336
static void *avail_tree_entry;
344337
static unsigned int avail_tree_table_sz = 100;
345338
static struct avail_tree_content **avail_tree_table;
339+
static size_t tree_entry_allocd;
346340
static struct strbuf old_tree = STRBUF_INIT;
347341
static struct strbuf new_tree = STRBUF_INIT;
348342

@@ -636,49 +630,10 @@ static unsigned int hc_str(const char *s, size_t len)
636630
return r;
637631
}
638632

639-
static void *pool_alloc(size_t len)
640-
{
641-
struct mem_pool *p;
642-
void *r;
643-
644-
/* round up to a 'uintmax_t' alignment */
645-
if (len & (sizeof(uintmax_t) - 1))
646-
len += sizeof(uintmax_t) - (len & (sizeof(uintmax_t) - 1));
647-
648-
for (p = mem_pool; p; p = p->next_pool)
649-
if ((p->end - p->next_free >= len))
650-
break;
651-
652-
if (!p) {
653-
if (len >= (mem_pool_alloc/2)) {
654-
total_allocd += len;
655-
return xmalloc(len);
656-
}
657-
total_allocd += sizeof(struct mem_pool) + mem_pool_alloc;
658-
p = xmalloc(st_add(sizeof(struct mem_pool), mem_pool_alloc));
659-
p->next_pool = mem_pool;
660-
p->next_free = (char *) p->space;
661-
p->end = p->next_free + mem_pool_alloc;
662-
mem_pool = p;
663-
}
664-
665-
r = p->next_free;
666-
p->next_free += len;
667-
return r;
668-
}
669-
670-
static void *pool_calloc(size_t count, size_t size)
671-
{
672-
size_t len = count * size;
673-
void *r = pool_alloc(len);
674-
memset(r, 0, len);
675-
return r;
676-
}
677-
678633
static char *pool_strdup(const char *s)
679634
{
680635
size_t len = strlen(s) + 1;
681-
char *r = pool_alloc(len);
636+
char *r = mem_pool_alloc(&fi_mem_pool, len);
682637
memcpy(r, s, len);
683638
return r;
684639
}
@@ -687,7 +642,7 @@ static void insert_mark(uintmax_t idnum, struct object_entry *oe)
687642
{
688643
struct mark_set *s = marks;
689644
while ((idnum >> s->shift) >= 1024) {
690-
s = pool_calloc(1, sizeof(struct mark_set));
645+
s = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct mark_set));
691646
s->shift = marks->shift + 10;
692647
s->data.sets[0] = marks;
693648
marks = s;
@@ -696,7 +651,7 @@ static void insert_mark(uintmax_t idnum, struct object_entry *oe)
696651
uintmax_t i = idnum >> s->shift;
697652
idnum -= i << s->shift;
698653
if (!s->data.sets[i]) {
699-
s->data.sets[i] = pool_calloc(1, sizeof(struct mark_set));
654+
s->data.sets[i] = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct mark_set));
700655
s->data.sets[i]->shift = s->shift - 10;
701656
}
702657
s = s->data.sets[i];
@@ -734,7 +689,7 @@ static struct atom_str *to_atom(const char *s, unsigned short len)
734689
if (c->str_len == len && !strncmp(s, c->str_dat, len))
735690
return c;
736691

737-
c = pool_alloc(sizeof(struct atom_str) + len + 1);
692+
c = mem_pool_alloc(&fi_mem_pool, sizeof(struct atom_str) + len + 1);
738693
c->str_len = len;
739694
memcpy(c->str_dat, s, len);
740695
c->str_dat[len] = 0;
@@ -765,7 +720,7 @@ static struct branch *new_branch(const char *name)
765720
if (check_refname_format(name, REFNAME_ALLOW_ONELEVEL))
766721
die("Branch name doesn't conform to GIT standards: %s", name);
767722

768-
b = pool_calloc(1, sizeof(struct branch));
723+
b = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct branch));
769724
b->name = pool_strdup(name);
770725
b->table_next_branch = branch_table[hc];
771726
b->branch_tree.versions[0].mode = S_IFDIR;
@@ -801,7 +756,7 @@ static struct tree_content *new_tree_content(unsigned int cnt)
801756
avail_tree_table[hc] = f->next_avail;
802757
} else {
803758
cnt = cnt & 7 ? ((cnt / 8) + 1) * 8 : cnt;
804-
f = pool_alloc(sizeof(*t) + sizeof(t->entries[0]) * cnt);
759+
f = mem_pool_alloc(&fi_mem_pool, sizeof(*t) + sizeof(t->entries[0]) * cnt);
805760
f->entry_capacity = cnt;
806761
}
807762

@@ -846,7 +801,7 @@ static struct tree_entry *new_tree_entry(void)
846801

847802
if (!avail_tree_entry) {
848803
unsigned int n = tree_entry_alloc;
849-
total_allocd += n * sizeof(struct tree_entry);
804+
tree_entry_allocd += n * sizeof(struct tree_entry);
850805
ALLOC_ARRAY(e, n);
851806
avail_tree_entry = e;
852807
while (n-- > 1) {
@@ -2867,7 +2822,7 @@ static void parse_new_tag(const char *arg)
28672822
enum object_type type;
28682823
const char *v;
28692824

2870-
t = pool_alloc(sizeof(struct tag));
2825+
t = mem_pool_alloc(&fi_mem_pool, sizeof(struct tag));
28712826
memset(t, 0, sizeof(struct tag));
28722827
t->name = pool_strdup(arg);
28732828
if (last_tag)
@@ -3466,12 +3421,12 @@ int cmd_main(int argc, const char **argv)
34663421
atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*));
34673422
branch_table = xcalloc(branch_table_sz, sizeof(struct branch*));
34683423
avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*));
3469-
marks = pool_calloc(1, sizeof(struct mark_set));
3424+
marks = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct mark_set));
34703425

34713426
global_argc = argc;
34723427
global_argv = argv;
34733428

3474-
rc_free = pool_alloc(cmd_save * sizeof(*rc_free));
3429+
rc_free = mem_pool_alloc(&fi_mem_pool, cmd_save * sizeof(*rc_free));
34753430
for (i = 0; i < (cmd_save - 1); i++)
34763431
rc_free[i].next = &rc_free[i + 1];
34773432
rc_free[cmd_save - 1].next = NULL;
@@ -3545,8 +3500,8 @@ int cmd_main(int argc, const char **argv)
35453500
fprintf(stderr, "Total branches: %10lu (%10lu loads )\n", branch_count, branch_load_count);
35463501
fprintf(stderr, " marks: %10" PRIuMAX " (%10" PRIuMAX " unique )\n", (((uintmax_t)1) << marks->shift) * 1024, marks_set_count);
35473502
fprintf(stderr, " atoms: %10u\n", atom_cnt);
3548-
fprintf(stderr, "Memory total: %10" PRIuMAX " KiB\n", (total_allocd + alloc_count*sizeof(struct object_entry))/1024);
3549-
fprintf(stderr, " pools: %10lu KiB\n", (unsigned long)(total_allocd/1024));
3503+
fprintf(stderr, "Memory total: %10" PRIuMAX " KiB\n", (tree_entry_allocd + fi_mem_pool.pool_alloc + alloc_count*sizeof(struct object_entry))/1024);
3504+
fprintf(stderr, " pools: %10lu KiB\n", (unsigned long)((tree_entry_allocd + fi_mem_pool.pool_alloc) /1024));
35503505
fprintf(stderr, " objects: %10" PRIuMAX " KiB\n", (alloc_count*sizeof(struct object_entry))/1024);
35513506
fprintf(stderr, "---------------------------------------------------------------------\n");
35523507
pack_report();

mem-pool.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Memory Pool implementation logic.
3+
*/
4+
5+
#include "cache.h"
6+
#include "mem-pool.h"
7+
8+
static struct mp_block *mem_pool_alloc_block(struct mem_pool *mem_pool, size_t block_alloc)
9+
{
10+
struct mp_block *p;
11+
12+
mem_pool->pool_alloc += sizeof(struct mp_block) + block_alloc;
13+
p = xmalloc(st_add(sizeof(struct mp_block), block_alloc));
14+
p->next_block = mem_pool->mp_block;
15+
p->next_free = (char *)p->space;
16+
p->end = p->next_free + block_alloc;
17+
mem_pool->mp_block = p;
18+
19+
return p;
20+
}
21+
22+
void *mem_pool_alloc(struct mem_pool *mem_pool, size_t len)
23+
{
24+
struct mp_block *p;
25+
void *r;
26+
27+
/* round up to a 'uintmax_t' alignment */
28+
if (len & (sizeof(uintmax_t) - 1))
29+
len += sizeof(uintmax_t) - (len & (sizeof(uintmax_t) - 1));
30+
31+
for (p = mem_pool->mp_block; p; p = p->next_block)
32+
if (p->end - p->next_free >= len)
33+
break;
34+
35+
if (!p) {
36+
if (len >= (mem_pool->block_alloc / 2)) {
37+
mem_pool->pool_alloc += len;
38+
return xmalloc(len);
39+
}
40+
41+
p = mem_pool_alloc_block(mem_pool, mem_pool->block_alloc);
42+
}
43+
44+
r = p->next_free;
45+
p->next_free += len;
46+
return r;
47+
}
48+
49+
void *mem_pool_calloc(struct mem_pool *mem_pool, size_t count, size_t size)
50+
{
51+
size_t len = st_mult(count, size);
52+
void *r = mem_pool_alloc(mem_pool, len);
53+
memset(r, 0, len);
54+
return r;
55+
}

mem-pool.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#ifndef MEM_POOL_H
2+
#define MEM_POOL_H
3+
4+
struct mp_block {
5+
struct mp_block *next_block;
6+
char *next_free;
7+
char *end;
8+
uintmax_t space[FLEX_ARRAY]; /* more */
9+
};
10+
11+
struct mem_pool {
12+
struct mp_block *mp_block;
13+
14+
/*
15+
* The amount of available memory to grow the pool by.
16+
* This size does not include the overhead for the mp_block.
17+
*/
18+
size_t block_alloc;
19+
20+
/* The total amount of memory allocated by the pool. */
21+
size_t pool_alloc;
22+
};
23+
24+
/*
25+
* Alloc memory from the mem_pool.
26+
*/
27+
void *mem_pool_alloc(struct mem_pool *pool, size_t len);
28+
29+
/*
30+
* Allocate and zero memory from the memory pool.
31+
*/
32+
void *mem_pool_calloc(struct mem_pool *pool, size_t count, size_t size);
33+
34+
#endif

0 commit comments

Comments
 (0)