]>
Commit | Line | Data |
---|---|---|
065feab4 JM |
1 | /* |
2 | * Memory Pool implementation logic. | |
3 | */ | |
4 | ||
15db4e7f | 5 | #include "git-compat-util.h" |
065feab4 JM |
6 | #include "mem-pool.h" |
7 | ||
116affac | 8 | #define BLOCK_GROWTH_SIZE (1024 * 1024 - sizeof(struct mp_block)) |
158dfeff | 9 | |
e38bcc66 JC |
10 | /* |
11 | * The inner union is an approximation for C11's max_align_t, and the | |
12 | * struct + offsetof computes _Alignof. This can all just be replaced | |
13 | * with _Alignof(max_align_t) if/when C11 is part of the baseline. | |
14 | * Note that _Alignof(X) need not be the same as sizeof(X); it's only | |
15 | * required to be a (possibly trivial) factor. They are the same for | |
16 | * most architectures, but m68k for example has only 2-byte alignment | |
17 | * for its 4-byte and 8-byte types, so using sizeof would waste space. | |
18 | * | |
19 | * Add more types to the union if the current set is insufficient. | |
20 | */ | |
21 | struct git_max_alignment { | |
22 | char unalign; | |
23 | union { | |
24 | uintmax_t max_align_uintmax; | |
25 | void *max_align_pointer; | |
26 | } aligned; | |
27 | }; | |
28 | #define GIT_MAX_ALIGNMENT offsetof(struct git_max_alignment, aligned) | |
29 | ||
158dfeff JM |
30 | /* |
31 | * Allocate a new mp_block and insert it after the block specified in | |
32 | * `insert_after`. If `insert_after` is NULL, then insert block at the | |
33 | * head of the linked list. | |
34 | */ | |
f87bf284 EN |
35 | static struct mp_block *mem_pool_alloc_block(struct mem_pool *pool, |
36 | size_t block_alloc, | |
37 | struct mp_block *insert_after) | |
065feab4 JM |
38 | { |
39 | struct mp_block *p; | |
40 | ||
f87bf284 | 41 | pool->pool_alloc += sizeof(struct mp_block) + block_alloc; |
065feab4 | 42 | p = xmalloc(st_add(sizeof(struct mp_block), block_alloc)); |
158dfeff | 43 | |
065feab4 JM |
44 | p->next_free = (char *)p->space; |
45 | p->end = p->next_free + block_alloc; | |
158dfeff JM |
46 | |
47 | if (insert_after) { | |
48 | p->next_block = insert_after->next_block; | |
49 | insert_after->next_block = p; | |
50 | } else { | |
f87bf284 EN |
51 | p->next_block = pool->mp_block; |
52 | pool->mp_block = p; | |
158dfeff | 53 | } |
065feab4 JM |
54 | |
55 | return p; | |
56 | } | |
57 | ||
44c7e1a7 | 58 | void mem_pool_init(struct mem_pool *pool, size_t initial_size) |
158dfeff | 59 | { |
44c7e1a7 | 60 | memset(pool, 0, sizeof(*pool)); |
158dfeff JM |
61 | pool->block_alloc = BLOCK_GROWTH_SIZE; |
62 | ||
63 | if (initial_size > 0) | |
64 | mem_pool_alloc_block(pool, initial_size, NULL); | |
158dfeff JM |
65 | } |
66 | ||
f87bf284 | 67 | void mem_pool_discard(struct mem_pool *pool, int invalidate_memory) |
158dfeff JM |
68 | { |
69 | struct mp_block *block, *block_to_free; | |
70 | ||
f87bf284 | 71 | block = pool->mp_block; |
8e72d675 | 72 | while (block) |
158dfeff JM |
73 | { |
74 | block_to_free = block; | |
75 | block = block->next_block; | |
8616a2d0 JM |
76 | |
77 | if (invalidate_memory) | |
78 | memset(block_to_free->space, 0xDD, ((char *)block_to_free->end) - ((char *)block_to_free->space)); | |
79 | ||
158dfeff JM |
80 | free(block_to_free); |
81 | } | |
82 | ||
f87bf284 EN |
83 | pool->mp_block = NULL; |
84 | pool->pool_alloc = 0; | |
158dfeff JM |
85 | } |
86 | ||
f87bf284 | 87 | void *mem_pool_alloc(struct mem_pool *pool, size_t len) |
065feab4 | 88 | { |
8fb8e3f6 | 89 | struct mp_block *p = NULL; |
065feab4 JM |
90 | void *r; |
91 | ||
e38bcc66 JC |
92 | /* round up to a 'GIT_MAX_ALIGNMENT' alignment */ |
93 | if (len & (GIT_MAX_ALIGNMENT - 1)) | |
94 | len += GIT_MAX_ALIGNMENT - (len & (GIT_MAX_ALIGNMENT - 1)); | |
065feab4 | 95 | |
f87bf284 EN |
96 | if (pool->mp_block && |
97 | pool->mp_block->end - pool->mp_block->next_free >= len) | |
98 | p = pool->mp_block; | |
065feab4 JM |
99 | |
100 | if (!p) { | |
f87bf284 EN |
101 | if (len >= (pool->block_alloc / 2)) |
102 | return mem_pool_alloc_block(pool, len, pool->mp_block); | |
065feab4 | 103 | |
f87bf284 | 104 | p = mem_pool_alloc_block(pool, pool->block_alloc, NULL); |
065feab4 JM |
105 | } |
106 | ||
107 | r = p->next_free; | |
108 | p->next_free += len; | |
109 | return r; | |
110 | } | |
111 | ||
f87bf284 | 112 | void *mem_pool_calloc(struct mem_pool *pool, size_t count, size_t size) |
065feab4 JM |
113 | { |
114 | size_t len = st_mult(count, size); | |
f87bf284 | 115 | void *r = mem_pool_alloc(pool, len); |
065feab4 JM |
116 | memset(r, 0, len); |
117 | return r; | |
118 | } | |
0e58301d | 119 | |
a762c8c1 EN |
120 | char *mem_pool_strdup(struct mem_pool *pool, const char *str) |
121 | { | |
122 | size_t len = strlen(str) + 1; | |
123 | char *ret = mem_pool_alloc(pool, len); | |
124 | ||
125 | return memcpy(ret, str, len); | |
126 | } | |
127 | ||
128 | char *mem_pool_strndup(struct mem_pool *pool, const char *str, size_t len) | |
129 | { | |
130 | char *p = memchr(str, '\0', len); | |
131 | size_t actual_len = (p ? p - str : len); | |
132 | char *ret = mem_pool_alloc(pool, actual_len+1); | |
133 | ||
134 | ret[actual_len] = '\0'; | |
135 | return memcpy(ret, str, actual_len); | |
136 | } | |
137 | ||
f87bf284 | 138 | int mem_pool_contains(struct mem_pool *pool, void *mem) |
0e58301d JM |
139 | { |
140 | struct mp_block *p; | |
141 | ||
142 | /* Check if memory is allocated in a block */ | |
f87bf284 | 143 | for (p = pool->mp_block; p; p = p->next_block) |
0e58301d JM |
144 | if ((mem >= ((void *)p->space)) && |
145 | (mem < ((void *)p->end))) | |
146 | return 1; | |
147 | ||
148 | return 0; | |
149 | } | |
150 | ||
151 | void mem_pool_combine(struct mem_pool *dst, struct mem_pool *src) | |
152 | { | |
153 | struct mp_block *p; | |
154 | ||
155 | /* Append the blocks from src to dst */ | |
156 | if (dst->mp_block && src->mp_block) { | |
157 | /* | |
158 | * src and dst have blocks, append | |
159 | * blocks from src to dst. | |
160 | */ | |
161 | p = dst->mp_block; | |
162 | while (p->next_block) | |
163 | p = p->next_block; | |
164 | ||
165 | p->next_block = src->mp_block; | |
166 | } else if (src->mp_block) { | |
167 | /* | |
168 | * src has blocks, dst is empty. | |
169 | */ | |
170 | dst->mp_block = src->mp_block; | |
171 | } else { | |
172 | /* src is empty, nothing to do. */ | |
173 | } | |
174 | ||
175 | dst->pool_alloc += src->pool_alloc; | |
176 | src->pool_alloc = 0; | |
177 | src->mp_block = NULL; | |
178 | } |