Git 2.32
[git] / mem-pool.c
1 /*
2  * Memory Pool implementation logic.
3  */
4
5 #include "cache.h"
6 #include "mem-pool.h"
7
8 #define BLOCK_GROWTH_SIZE (1024 * 1024 - sizeof(struct mp_block))
9
10 /*
11  * Allocate a new mp_block and insert it after the block specified in
12  * `insert_after`. If `insert_after` is NULL, then insert block at the
13  * head of the linked list.
14  */
15 static struct mp_block *mem_pool_alloc_block(struct mem_pool *pool,
16                                              size_t block_alloc,
17                                              struct mp_block *insert_after)
18 {
19         struct mp_block *p;
20
21         pool->pool_alloc += sizeof(struct mp_block) + block_alloc;
22         p = xmalloc(st_add(sizeof(struct mp_block), block_alloc));
23
24         p->next_free = (char *)p->space;
25         p->end = p->next_free + block_alloc;
26
27         if (insert_after) {
28                 p->next_block = insert_after->next_block;
29                 insert_after->next_block = p;
30         } else {
31                 p->next_block = pool->mp_block;
32                 pool->mp_block = p;
33         }
34
35         return p;
36 }
37
38 void mem_pool_init(struct mem_pool *pool, size_t initial_size)
39 {
40         memset(pool, 0, sizeof(*pool));
41         pool->block_alloc = BLOCK_GROWTH_SIZE;
42
43         if (initial_size > 0)
44                 mem_pool_alloc_block(pool, initial_size, NULL);
45 }
46
47 void mem_pool_discard(struct mem_pool *pool, int invalidate_memory)
48 {
49         struct mp_block *block, *block_to_free;
50
51         block = pool->mp_block;
52         while (block)
53         {
54                 block_to_free = block;
55                 block = block->next_block;
56
57                 if (invalidate_memory)
58                         memset(block_to_free->space, 0xDD, ((char *)block_to_free->end) - ((char *)block_to_free->space));
59
60                 free(block_to_free);
61         }
62
63         pool->mp_block = NULL;
64         pool->pool_alloc = 0;
65 }
66
67 void *mem_pool_alloc(struct mem_pool *pool, size_t len)
68 {
69         struct mp_block *p = NULL;
70         void *r;
71
72         /* round up to a 'uintmax_t' alignment */
73         if (len & (sizeof(uintmax_t) - 1))
74                 len += sizeof(uintmax_t) - (len & (sizeof(uintmax_t) - 1));
75
76         if (pool->mp_block &&
77             pool->mp_block->end - pool->mp_block->next_free >= len)
78                 p = pool->mp_block;
79
80         if (!p) {
81                 if (len >= (pool->block_alloc / 2))
82                         return mem_pool_alloc_block(pool, len, pool->mp_block);
83
84                 p = mem_pool_alloc_block(pool, pool->block_alloc, NULL);
85         }
86
87         r = p->next_free;
88         p->next_free += len;
89         return r;
90 }
91
92 void *mem_pool_calloc(struct mem_pool *pool, size_t count, size_t size)
93 {
94         size_t len = st_mult(count, size);
95         void *r = mem_pool_alloc(pool, len);
96         memset(r, 0, len);
97         return r;
98 }
99
100 char *mem_pool_strdup(struct mem_pool *pool, const char *str)
101 {
102         size_t len = strlen(str) + 1;
103         char *ret = mem_pool_alloc(pool, len);
104
105         return memcpy(ret, str, len);
106 }
107
108 char *mem_pool_strndup(struct mem_pool *pool, const char *str, size_t len)
109 {
110         char *p = memchr(str, '\0', len);
111         size_t actual_len = (p ? p - str : len);
112         char *ret = mem_pool_alloc(pool, actual_len+1);
113
114         ret[actual_len] = '\0';
115         return memcpy(ret, str, actual_len);
116 }
117
118 int mem_pool_contains(struct mem_pool *pool, void *mem)
119 {
120         struct mp_block *p;
121
122         /* Check if memory is allocated in a block */
123         for (p = pool->mp_block; p; p = p->next_block)
124                 if ((mem >= ((void *)p->space)) &&
125                     (mem < ((void *)p->end)))
126                         return 1;
127
128         return 0;
129 }
130
131 void mem_pool_combine(struct mem_pool *dst, struct mem_pool *src)
132 {
133         struct mp_block *p;
134
135         /* Append the blocks from src to dst */
136         if (dst->mp_block && src->mp_block) {
137                 /*
138                  * src and dst have blocks, append
139                  * blocks from src to dst.
140                  */
141                 p = dst->mp_block;
142                 while (p->next_block)
143                         p = p->next_block;
144
145                 p->next_block = src->mp_block;
146         } else if (src->mp_block) {
147                 /*
148                  * src has blocks, dst is empty.
149                  */
150                 dst->mp_block = src->mp_block;
151         } else {
152                 /* src is empty, nothing to do. */
153         }
154
155         dst->pool_alloc += src->pool_alloc;
156         src->pool_alloc = 0;
157         src->mp_block = NULL;
158 }