Skip to content

Commit 065feab

Browse files
jamillgitster
authored andcommitted
mem-pool: 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 96c47d1 commit 065feab

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
@@ -833,6 +833,7 @@ LIB_OBJS += log-tree.o
833833
LIB_OBJS += mailinfo.o
834834
LIB_OBJS += mailmap.o
835835
LIB_OBJS += match-trees.o
836+
LIB_OBJS += mem-pool.o
836837
LIB_OBJS += merge.o
837838
LIB_OBJS += merge-blobs.o
838839
LIB_OBJS += merge-recursive.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;
@@ -647,55 +628,6 @@ static unsigned int hc_str(const char *s, size_t len)
647628
return r;
648629
}
649630

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