2 * simple memory allocator, backed by mmap() so that it hands out memory
3 * that can be shared across processes and threads
12 #include <sys/types.h>
18 #include "arch/arch.h"
23 #define SMALLOC_REDZONE /* define to detect memory corruption */
25 #define SMALLOC_BPB 32 /* block size, bytes-per-bit in bitmap */
26 #define SMALLOC_BPI (sizeof(unsigned int) * 8)
27 #define SMALLOC_BPL (SMALLOC_BPB * SMALLOC_BPI)
29 #define INITIAL_SIZE 16*1024*1024 /* new pool size */
30 #define INITIAL_POOLS 8 /* maximum number of pools to setup */
34 #define SMALLOC_PRE_RED 0xdeadbeefU
35 #define SMALLOC_POST_RED 0x5aa55aa5U
37 unsigned int smalloc_pool_size = INITIAL_SIZE;
38 #ifdef SMALLOC_REDZONE
39 static const int int_mask = sizeof(int) - 1;
43 struct fio_mutex *lock; /* protects this pool */
44 void *map; /* map of blocks */
45 unsigned int *bitmap; /* blocks free/busy map */
46 size_t free_blocks; /* free blocks */
47 size_t nr_blocks; /* total blocks */
54 #ifdef SMALLOC_REDZONE
59 static struct pool mp[MAX_POOLS];
60 static unsigned int nr_pools;
61 static unsigned int last_pool;
63 static inline int ptr_valid(struct pool *pool, void *ptr)
65 unsigned int pool_size = pool->nr_blocks * SMALLOC_BPL;
67 return (ptr >= pool->map) && (ptr < pool->map + pool_size);
70 static inline size_t size_to_blocks(size_t size)
72 return (size + SMALLOC_BPB - 1) / SMALLOC_BPB;
75 static int blocks_iter(struct pool *pool, unsigned int pool_idx,
76 unsigned int idx, size_t nr_blocks,
77 int (*func)(unsigned int *map, unsigned int mask))
81 unsigned int this_blocks, mask;
84 if (pool_idx >= pool->nr_blocks)
87 map = &pool->bitmap[pool_idx];
89 this_blocks = nr_blocks;
90 if (this_blocks + idx > SMALLOC_BPI) {
91 this_blocks = SMALLOC_BPI - idx;
92 idx = SMALLOC_BPI - this_blocks;
95 if (this_blocks == SMALLOC_BPI)
98 mask = ((1U << this_blocks) - 1) << idx;
100 if (!func(map, mask))
103 nr_blocks -= this_blocks;
111 static int mask_cmp(unsigned int *map, unsigned int mask)
113 return !(*map & mask);
116 static int mask_clear(unsigned int *map, unsigned int mask)
118 assert((*map & mask) == mask);
123 static int mask_set(unsigned int *map, unsigned int mask)
125 assert(!(*map & mask));
130 static int blocks_free(struct pool *pool, unsigned int pool_idx,
131 unsigned int idx, size_t nr_blocks)
133 return blocks_iter(pool, pool_idx, idx, nr_blocks, mask_cmp);
136 static void set_blocks(struct pool *pool, unsigned int pool_idx,
137 unsigned int idx, size_t nr_blocks)
139 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_set);
142 static void clear_blocks(struct pool *pool, unsigned int pool_idx,
143 unsigned int idx, size_t nr_blocks)
145 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_clear);
148 static int find_next_zero(int word, int start)
152 return ffz(word) + start;
155 static bool add_pool(struct pool *pool, unsigned int alloc_size)
161 if (nr_pools == MAX_POOLS)
164 #ifdef SMALLOC_REDZONE
165 alloc_size += sizeof(unsigned int);
167 alloc_size += sizeof(struct block_hdr);
168 if (alloc_size < INITIAL_SIZE)
169 alloc_size = INITIAL_SIZE;
171 /* round up to nearest full number of blocks */
172 alloc_size = (alloc_size + SMALLOC_BPL - 1) & ~(SMALLOC_BPL - 1);
173 bitmap_blocks = alloc_size / SMALLOC_BPL;
174 alloc_size += bitmap_blocks * sizeof(unsigned int);
175 pool->mmap_size = alloc_size;
177 pool->nr_blocks = bitmap_blocks;
178 pool->free_blocks = bitmap_blocks * SMALLOC_BPB;
180 mmap_flags = OS_MAP_ANON;
182 mmap_flags |= MAP_PRIVATE;
184 mmap_flags |= MAP_SHARED;
186 ptr = mmap(NULL, alloc_size, PROT_READ|PROT_WRITE, mmap_flags, -1, 0);
188 if (ptr == MAP_FAILED)
192 pool->bitmap = (unsigned int *)((char *) ptr + (pool->nr_blocks * SMALLOC_BPL));
193 memset(pool->bitmap, 0, bitmap_blocks * sizeof(unsigned int));
195 pool->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
202 log_err("smalloc: failed adding pool\n");
204 munmap(pool->map, pool->mmap_size);
213 for (i = 0; i < INITIAL_POOLS; i++) {
214 ret = add_pool(&mp[nr_pools], smalloc_pool_size);
220 * If we added at least one pool, we should be OK for most
226 static void cleanup_pool(struct pool *pool)
229 * This will also remove the temporary file we used as a backing
230 * store, it was already unlinked
232 munmap(pool->map, pool->mmap_size);
235 fio_mutex_remove(pool->lock);
242 for (i = 0; i < nr_pools; i++)
243 cleanup_pool(&mp[i]);
246 #ifdef SMALLOC_REDZONE
247 static void *postred_ptr(struct block_hdr *hdr)
251 ptr = (uintptr_t) hdr + hdr->size - sizeof(unsigned int);
252 ptr = (uintptr_t) PTR_ALIGN(ptr, int_mask);
257 static void fill_redzone(struct block_hdr *hdr)
259 unsigned int *postred = postred_ptr(hdr);
261 hdr->prered = SMALLOC_PRE_RED;
262 *postred = SMALLOC_POST_RED;
265 static void sfree_check_redzone(struct block_hdr *hdr)
267 unsigned int *postred = postred_ptr(hdr);
269 if (hdr->prered != SMALLOC_PRE_RED) {
270 log_err("smalloc pre redzone destroyed!\n"
271 " ptr=%p, prered=%x, expected %x\n",
272 hdr, hdr->prered, SMALLOC_PRE_RED);
275 if (*postred != SMALLOC_POST_RED) {
276 log_err("smalloc post redzone destroyed!\n"
277 " ptr=%p, postred=%x, expected %x\n",
278 hdr, *postred, SMALLOC_POST_RED);
283 static void fill_redzone(struct block_hdr *hdr)
287 static void sfree_check_redzone(struct block_hdr *hdr)
292 static void sfree_pool(struct pool *pool, void *ptr)
294 struct block_hdr *hdr;
296 unsigned long offset;
304 assert(ptr_valid(pool, ptr));
306 sfree_check_redzone(hdr);
308 offset = ptr - pool->map;
309 i = offset / SMALLOC_BPL;
310 idx = (offset % SMALLOC_BPL) / SMALLOC_BPB;
312 fio_mutex_down(pool->lock);
313 clear_blocks(pool, i, idx, size_to_blocks(hdr->size));
314 if (i < pool->next_non_full)
315 pool->next_non_full = i;
316 pool->free_blocks += size_to_blocks(hdr->size);
317 fio_mutex_up(pool->lock);
320 void sfree(void *ptr)
322 struct pool *pool = NULL;
328 for (i = 0; i < nr_pools; i++) {
329 if (ptr_valid(&mp[i], ptr)) {
336 sfree_pool(pool, ptr);
340 log_err("smalloc: ptr %p not from smalloc pool\n", ptr);
343 static void *__smalloc_pool(struct pool *pool, size_t size)
348 unsigned int last_idx;
351 fio_mutex_down(pool->lock);
353 nr_blocks = size_to_blocks(size);
354 if (nr_blocks > pool->free_blocks)
357 i = pool->next_non_full;
360 while (i < pool->nr_blocks) {
363 if (pool->bitmap[i] == -1U) {
365 pool->next_non_full = i;
370 idx = find_next_zero(pool->bitmap[i], last_idx);
371 if (!blocks_free(pool, i, idx, nr_blocks)) {
373 if (idx < SMALLOC_BPI)
377 while (idx >= SMALLOC_BPI) {
384 set_blocks(pool, i, idx, nr_blocks);
385 offset = i * SMALLOC_BPL + idx * SMALLOC_BPB;
389 if (i < pool->nr_blocks) {
390 pool->free_blocks -= nr_blocks;
391 ret = pool->map + offset;
394 fio_mutex_up(pool->lock);
398 static void *smalloc_pool(struct pool *pool, size_t size)
400 size_t alloc_size = size + sizeof(struct block_hdr);
404 * Round to int alignment, so that the postred pointer will
405 * be naturally aligned as well.
407 #ifdef SMALLOC_REDZONE
408 alloc_size += sizeof(unsigned int);
409 alloc_size = (alloc_size + int_mask) & ~int_mask;
412 ptr = __smalloc_pool(pool, alloc_size);
414 struct block_hdr *hdr = ptr;
416 hdr->size = alloc_size;
420 memset(ptr, 0, size);
426 void *smalloc(size_t size)
428 unsigned int i, end_pool;
430 if (size != (unsigned int) size)
437 for (; i < end_pool; i++) {
438 void *ptr = smalloc_pool(&mp[i], size);
446 end_pool = last_pool;
454 log_err("smalloc: OOM. Consider using --alloc-size to increase the "
455 "shared memory available.\n");
459 void *scalloc(size_t nmemb, size_t size)
461 return smalloc(nmemb * size);
464 char *smalloc_strdup(const char *str)
468 ptr = smalloc(strlen(str) + 1);