Skip to content

Commit f1f2817

Browse files
jamillgitster
authored andcommitted
Move reusable parts of memory pool into its own file
This moves the reusable parts of the memory pool logic used by fast-import.c into its own file for use by other components. Signed-off-by: Jameson Miller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a8dfa11 commit f1f2817

File tree

4 files changed

+91
-69
lines changed

4 files changed

+91
-69
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,7 @@ LIB_OBJS += lockfile.o
832832
LIB_OBJS += log-tree.o
833833
LIB_OBJS += mailinfo.o
834834
LIB_OBJS += mailmap.o
835+
LIB_OBJS += mem-pool.o
835836
LIB_OBJS += match-trees.o
836837
LIB_OBJS += merge.o
837838
LIB_OBJS += merge-blobs.o

fast-import.c

Lines changed: 1 addition & 69 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,26 +210,6 @@ struct last_object {
209210
unsigned no_swap : 1;
210211
};
211212

212-
struct mp_block {
213-
struct mp_block *next_block;
214-
char *next_free;
215-
char *end;
216-
uintmax_t space[FLEX_ARRAY]; /* more */
217-
};
218-
219-
struct mem_pool {
220-
struct mp_block *mp_block;
221-
222-
/*
223-
* The amount of available memory to grow the pool by.
224-
* This size does not include the overhead for the mp_block.
225-
*/
226-
size_t block_alloc;
227-
228-
/* The total amount of memory allocated by the pool. */
229-
size_t pool_alloc;
230-
};
231-
232213
struct atom_str {
233214
struct atom_str *next_atom;
234215
unsigned short str_len;
@@ -646,55 +627,6 @@ static unsigned int hc_str(const char *s, size_t len)
646627
return r;
647628
}
648629

649-
static struct mp_block *mem_pool_alloc_block(struct mem_pool *mem_pool, size_t block_alloc)
650-
{
651-
struct mp_block *p;
652-
653-
mem_pool->pool_alloc += sizeof(struct mp_block) + block_alloc;
654-
p = xmalloc(st_add(sizeof(struct mp_block), block_alloc));
655-
p->next_block = mem_pool->mp_block;
656-
p->next_free = (char *)p->space;
657-
p->end = p->next_free + block_alloc;
658-
mem_pool->mp_block = p;
659-
660-
return p;
661-
}
662-
663-
static void *mem_pool_alloc(struct mem_pool *mem_pool, size_t len)
664-
{
665-
struct mp_block *p;
666-
void *r;
667-
668-
/* round up to a 'uintmax_t' alignment */
669-
if (len & (sizeof(uintmax_t) - 1))
670-
len += sizeof(uintmax_t) - (len & (sizeof(uintmax_t) - 1));
671-
672-
for (p = mem_pool->mp_block; p; p = p->next_block)
673-
if (p->end - p->next_free >= len)
674-
break;
675-
676-
if (!p) {
677-
if (len >= (mem_pool->block_alloc / 2)) {
678-
mem_pool->pool_alloc += len;
679-
return xmalloc(len);
680-
}
681-
682-
p = mem_pool_alloc_block(mem_pool, mem_pool->block_alloc);
683-
}
684-
685-
r = p->next_free;
686-
p->next_free += len;
687-
return r;
688-
}
689-
690-
static void *mem_pool_calloc(struct mem_pool *mem_pool, size_t count, size_t size)
691-
{
692-
size_t len = st_mult(count, size);
693-
void *r = mem_pool_alloc(mem_pool, len);
694-
memset(r, 0, len);
695-
return r;
696-
}
697-
698630
static char *pool_strdup(const char *s)
699631
{
700632
size_t len = strlen(s) + 1;

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)