Skip to content

Commit bfce05d

Browse files
committed
Merge branch 'jm/mem-pool' into next
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: 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 2e64650 + f1f2817 commit bfce05d

File tree

4 files changed

+104
-60
lines changed

4 files changed

+104
-60
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,7 @@ LIB_OBJS += log-tree.o
841841
LIB_OBJS += ls-refs.o
842842
LIB_OBJS += mailinfo.o
843843
LIB_OBJS += mailmap.o
844+
LIB_OBJS += mem-pool.o
844845
LIB_OBJS += match-trees.o
845846
LIB_OBJS += merge.o
846847
LIB_OBJS += merge-blobs.o

fast-import.c

Lines changed: 14 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ Format of STDIN stream:
168168
#include "dir.h"
169169
#include "run-command.h"
170170
#include "packfile.h"
171+
#include "mem-pool.h"
171172

172173
#define PACK_ID_BITS 16
173174
#define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
@@ -209,13 +210,6 @@ struct last_object {
209210
unsigned no_swap : 1;
210211
};
211212

212-
struct mem_pool {
213-
struct mem_pool *next_pool;
214-
char *next_free;
215-
char *end;
216-
uintmax_t space[FLEX_ARRAY]; /* more */
217-
};
218-
219213
struct atom_str {
220214
struct atom_str *next_atom;
221215
unsigned short str_len;
@@ -304,9 +298,7 @@ static int global_argc;
304298
static const char **global_argv;
305299

306300
/* Memory pools */
307-
static size_t mem_pool_alloc = 2*1024*1024 - sizeof(struct mem_pool);
308-
static size_t total_allocd;
309-
static struct mem_pool *mem_pool;
301+
static struct mem_pool fi_mem_pool = {0, 2*1024*1024 - sizeof(struct mp_block), 0 };
310302

311303
/* Atom management */
312304
static unsigned int atom_table_sz = 4451;
@@ -324,6 +316,7 @@ static off_t pack_size;
324316
/* Table of objects we've written. */
325317
static unsigned int object_entry_alloc = 5000;
326318
static struct object_entry_pool *blocks;
319+
static size_t total_allocd;
327320
static struct object_entry *object_table[1 << 16];
328321
static struct mark_set *marks;
329322
static const char *export_marks_file;
@@ -634,49 +627,10 @@ static unsigned int hc_str(const char *s, size_t len)
634627
return r;
635628
}
636629

637-
static void *pool_alloc(size_t len)
638-
{
639-
struct mem_pool *p;
640-
void *r;
641-
642-
/* round up to a 'uintmax_t' alignment */
643-
if (len & (sizeof(uintmax_t) - 1))
644-
len += sizeof(uintmax_t) - (len & (sizeof(uintmax_t) - 1));
645-
646-
for (p = mem_pool; p; p = p->next_pool)
647-
if ((p->end - p->next_free >= len))
648-
break;
649-
650-
if (!p) {
651-
if (len >= (mem_pool_alloc/2)) {
652-
total_allocd += len;
653-
return xmalloc(len);
654-
}
655-
total_allocd += sizeof(struct mem_pool) + mem_pool_alloc;
656-
p = xmalloc(st_add(sizeof(struct mem_pool), mem_pool_alloc));
657-
p->next_pool = mem_pool;
658-
p->next_free = (char *) p->space;
659-
p->end = p->next_free + mem_pool_alloc;
660-
mem_pool = p;
661-
}
662-
663-
r = p->next_free;
664-
p->next_free += len;
665-
return r;
666-
}
667-
668-
static void *pool_calloc(size_t count, size_t size)
669-
{
670-
size_t len = count * size;
671-
void *r = pool_alloc(len);
672-
memset(r, 0, len);
673-
return r;
674-
}
675-
676630
static char *pool_strdup(const char *s)
677631
{
678632
size_t len = strlen(s) + 1;
679-
char *r = pool_alloc(len);
633+
char *r = mem_pool_alloc(&fi_mem_pool, len);
680634
memcpy(r, s, len);
681635
return r;
682636
}
@@ -685,7 +639,7 @@ static void insert_mark(uintmax_t idnum, struct object_entry *oe)
685639
{
686640
struct mark_set *s = marks;
687641
while ((idnum >> s->shift) >= 1024) {
688-
s = pool_calloc(1, sizeof(struct mark_set));
642+
s = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct mark_set));
689643
s->shift = marks->shift + 10;
690644
s->data.sets[0] = marks;
691645
marks = s;
@@ -694,7 +648,7 @@ static void insert_mark(uintmax_t idnum, struct object_entry *oe)
694648
uintmax_t i = idnum >> s->shift;
695649
idnum -= i << s->shift;
696650
if (!s->data.sets[i]) {
697-
s->data.sets[i] = pool_calloc(1, sizeof(struct mark_set));
651+
s->data.sets[i] = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct mark_set));
698652
s->data.sets[i]->shift = s->shift - 10;
699653
}
700654
s = s->data.sets[i];
@@ -732,7 +686,7 @@ static struct atom_str *to_atom(const char *s, unsigned short len)
732686
if (c->str_len == len && !strncmp(s, c->str_dat, len))
733687
return c;
734688

735-
c = pool_alloc(sizeof(struct atom_str) + len + 1);
689+
c = mem_pool_alloc(&fi_mem_pool, sizeof(struct atom_str) + len + 1);
736690
c->str_len = len;
737691
memcpy(c->str_dat, s, len);
738692
c->str_dat[len] = 0;
@@ -763,7 +717,7 @@ static struct branch *new_branch(const char *name)
763717
if (check_refname_format(name, REFNAME_ALLOW_ONELEVEL))
764718
die("Branch name doesn't conform to GIT standards: %s", name);
765719

766-
b = pool_calloc(1, sizeof(struct branch));
720+
b = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct branch));
767721
b->name = pool_strdup(name);
768722
b->table_next_branch = branch_table[hc];
769723
b->branch_tree.versions[0].mode = S_IFDIR;
@@ -799,7 +753,7 @@ static struct tree_content *new_tree_content(unsigned int cnt)
799753
avail_tree_table[hc] = f->next_avail;
800754
} else {
801755
cnt = cnt & 7 ? ((cnt / 8) + 1) * 8 : cnt;
802-
f = pool_alloc(sizeof(*t) + sizeof(t->entries[0]) * cnt);
756+
f = mem_pool_alloc(&fi_mem_pool, sizeof(*t) + sizeof(t->entries[0]) * cnt);
803757
f->entry_capacity = cnt;
804758
}
805759

@@ -2863,7 +2817,7 @@ static void parse_new_tag(const char *arg)
28632817
enum object_type type;
28642818
const char *v;
28652819

2866-
t = pool_alloc(sizeof(struct tag));
2820+
t = mem_pool_alloc(&fi_mem_pool, sizeof(struct tag));
28672821
memset(t, 0, sizeof(struct tag));
28682822
t->name = pool_strdup(arg);
28692823
if (last_tag)
@@ -3462,12 +3416,12 @@ int cmd_main(int argc, const char **argv)
34623416
atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*));
34633417
branch_table = xcalloc(branch_table_sz, sizeof(struct branch*));
34643418
avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*));
3465-
marks = pool_calloc(1, sizeof(struct mark_set));
3419+
marks = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct mark_set));
34663420

34673421
global_argc = argc;
34683422
global_argv = argv;
34693423

3470-
rc_free = pool_alloc(cmd_save * sizeof(*rc_free));
3424+
rc_free = mem_pool_alloc(&fi_mem_pool, cmd_save * sizeof(*rc_free));
34713425
for (i = 0; i < (cmd_save - 1); i++)
34723426
rc_free[i].next = &rc_free[i + 1];
34733427
rc_free[cmd_save - 1].next = NULL;
@@ -3542,8 +3496,8 @@ int cmd_main(int argc, const char **argv)
35423496
fprintf(stderr, "Total branches: %10lu (%10lu loads )\n", branch_count, branch_load_count);
35433497
fprintf(stderr, " marks: %10" PRIuMAX " (%10" PRIuMAX " unique )\n", (((uintmax_t)1) << marks->shift) * 1024, marks_set_count);
35443498
fprintf(stderr, " atoms: %10u\n", atom_cnt);
3545-
fprintf(stderr, "Memory total: %10" PRIuMAX " KiB\n", (total_allocd + alloc_count*sizeof(struct object_entry))/1024);
3546-
fprintf(stderr, " pools: %10lu KiB\n", (unsigned long)(total_allocd/1024));
3499+
fprintf(stderr, "Memory total: %10" PRIuMAX " KiB\n", (total_allocd + fi_mem_pool.pool_alloc + alloc_count*sizeof(struct object_entry))/1024);
3500+
fprintf(stderr, " pools: %10lu KiB\n", (unsigned long)((total_allocd + fi_mem_pool.pool_alloc) /1024));
35473501
fprintf(stderr, " objects: %10" PRIuMAX " KiB\n", (alloc_count*sizeof(struct object_entry))/1024);
35483502
fprintf(stderr, "---------------------------------------------------------------------\n");
35493503
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)