2 * simple memory allocator, backed by mmap() so that it hands out memory
3 * that can be shared across processes and threads
15 #define SMALLOC_REDZONE /* define to detect memory corruption */
17 #define SMALLOC_BPB 32 /* block size, bytes-per-bit in bitmap */
18 #define SMALLOC_BPI (sizeof(unsigned int) * 8)
19 #define SMALLOC_BPL (SMALLOC_BPB * SMALLOC_BPI)
21 #define INITIAL_SIZE 16*1024*1024 /* new pool size */
22 #define INITIAL_POOLS 8 /* maximum number of pools to setup */
26 #define SMALLOC_PRE_RED 0xdeadbeefU
27 #define SMALLOC_POST_RED 0x5aa55aa5U
29 unsigned int smalloc_pool_size = INITIAL_SIZE;
30 #ifdef SMALLOC_REDZONE
31 static const int int_mask = sizeof(int) - 1;
35 struct fio_sem *lock; /* protects this pool */
36 void *map; /* map of blocks */
37 unsigned int *bitmap; /* blocks free/busy map */
38 size_t free_blocks; /* free blocks */
39 size_t nr_blocks; /* total blocks */
46 #ifdef SMALLOC_REDZONE
52 * This suppresses the voluminous potential bitmap printout when
53 * smalloc encounters an OOM error
55 static const bool enable_smalloc_debug = false;
57 static struct pool *mp;
58 static unsigned int nr_pools;
59 static unsigned int last_pool;
61 static inline int ptr_valid(struct pool *pool, void *ptr)
63 unsigned int pool_size = pool->nr_blocks * SMALLOC_BPL;
65 return (ptr >= pool->map) && (ptr < pool->map + pool_size);
68 static inline size_t size_to_blocks(size_t size)
70 return (size + SMALLOC_BPB - 1) / SMALLOC_BPB;
73 static int blocks_iter(struct pool *pool, unsigned int pool_idx,
74 unsigned int idx, size_t nr_blocks,
75 int (*func)(unsigned int *map, unsigned int mask))
79 unsigned int this_blocks, mask;
82 if (pool_idx >= pool->nr_blocks)
85 map = &pool->bitmap[pool_idx];
87 this_blocks = nr_blocks;
88 if (this_blocks + idx > SMALLOC_BPI) {
89 this_blocks = SMALLOC_BPI - idx;
90 idx = SMALLOC_BPI - this_blocks;
93 if (this_blocks == SMALLOC_BPI)
96 mask = ((1U << this_blocks) - 1) << idx;
101 nr_blocks -= this_blocks;
109 static int mask_cmp(unsigned int *map, unsigned int mask)
111 return !(*map & mask);
114 static int mask_clear(unsigned int *map, unsigned int mask)
116 assert((*map & mask) == mask);
121 static int mask_set(unsigned int *map, unsigned int mask)
123 assert(!(*map & mask));
128 static int blocks_free(struct pool *pool, unsigned int pool_idx,
129 unsigned int idx, size_t nr_blocks)
131 return blocks_iter(pool, pool_idx, idx, nr_blocks, mask_cmp);
134 static void set_blocks(struct pool *pool, unsigned int pool_idx,
135 unsigned int idx, size_t nr_blocks)
137 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_set);
140 static void clear_blocks(struct pool *pool, unsigned int pool_idx,
141 unsigned int idx, size_t nr_blocks)
143 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_clear);
146 static int find_next_zero(int word, int start)
150 return ffz(word) + start;
153 static bool add_pool(struct pool *pool, unsigned int alloc_size)
159 if (nr_pools == MAX_POOLS)
162 #ifdef SMALLOC_REDZONE
163 alloc_size += sizeof(unsigned int);
165 alloc_size += sizeof(struct block_hdr);
166 if (alloc_size < INITIAL_SIZE)
167 alloc_size = INITIAL_SIZE;
169 /* round up to nearest full number of blocks */
170 alloc_size = (alloc_size + SMALLOC_BPL - 1) & ~(SMALLOC_BPL - 1);
171 bitmap_blocks = alloc_size / SMALLOC_BPL;
172 alloc_size += bitmap_blocks * sizeof(unsigned int);
173 pool->mmap_size = alloc_size;
175 pool->nr_blocks = bitmap_blocks;
176 pool->free_blocks = bitmap_blocks * SMALLOC_BPI;
178 mmap_flags = OS_MAP_ANON;
180 mmap_flags |= MAP_PRIVATE;
182 mmap_flags |= MAP_SHARED;
184 ptr = mmap(NULL, alloc_size, PROT_READ|PROT_WRITE, mmap_flags, -1, 0);
186 if (ptr == MAP_FAILED)
190 pool->bitmap = (unsigned int *)((char *) ptr + (pool->nr_blocks * SMALLOC_BPL));
191 memset(pool->bitmap, 0, bitmap_blocks * sizeof(unsigned int));
193 pool->lock = fio_sem_init(FIO_SEM_UNLOCKED);
200 log_err("smalloc: failed adding pool\n");
202 munmap(pool->map, pool->mmap_size);
212 * sinit() can be called more than once if alloc-size is
213 * set. But we want to allocate space for the struct pool
214 * instances only once.
217 mp = (struct pool *) mmap(NULL,
218 MAX_POOLS * sizeof(struct pool),
219 PROT_READ | PROT_WRITE,
220 OS_MAP_ANON | MAP_SHARED, -1, 0);
222 assert(mp != MAP_FAILED);
225 for (i = 0; i < INITIAL_POOLS; i++) {
226 ret = add_pool(&mp[nr_pools], smalloc_pool_size);
232 * If we added at least one pool, we should be OK for most
238 static void cleanup_pool(struct pool *pool)
241 * This will also remove the temporary file we used as a backing
242 * store, it was already unlinked
244 munmap(pool->map, pool->mmap_size);
247 fio_sem_remove(pool->lock);
254 for (i = 0; i < nr_pools; i++)
255 cleanup_pool(&mp[i]);
257 munmap(mp, MAX_POOLS * sizeof(struct pool));
260 #ifdef SMALLOC_REDZONE
261 static void *postred_ptr(struct block_hdr *hdr)
265 ptr = (uintptr_t) hdr + hdr->size - sizeof(unsigned int);
266 ptr = (uintptr_t) PTR_ALIGN(ptr, int_mask);
271 static void fill_redzone(struct block_hdr *hdr)
273 unsigned int *postred = postred_ptr(hdr);
275 hdr->prered = SMALLOC_PRE_RED;
276 *postred = SMALLOC_POST_RED;
279 static void sfree_check_redzone(struct block_hdr *hdr)
281 unsigned int *postred = postred_ptr(hdr);
283 if (hdr->prered != SMALLOC_PRE_RED) {
284 log_err("smalloc pre redzone destroyed!\n"
285 " ptr=%p, prered=%x, expected %x\n",
286 hdr+1, hdr->prered, SMALLOC_PRE_RED);
289 if (*postred != SMALLOC_POST_RED) {
290 log_err("smalloc post redzone destroyed!\n"
291 " ptr=%p, postred=%x, expected %x\n",
292 hdr+1, *postred, SMALLOC_POST_RED);
297 static void fill_redzone(struct block_hdr *hdr)
301 static void sfree_check_redzone(struct block_hdr *hdr)
306 static void sfree_pool(struct pool *pool, void *ptr)
308 struct block_hdr *hdr;
310 unsigned long offset;
318 assert(ptr_valid(pool, ptr));
320 sfree_check_redzone(hdr);
322 offset = ptr - pool->map;
323 i = offset / SMALLOC_BPL;
324 idx = (offset % SMALLOC_BPL) / SMALLOC_BPB;
326 fio_sem_down(pool->lock);
327 clear_blocks(pool, i, idx, size_to_blocks(hdr->size));
328 if (i < pool->next_non_full)
329 pool->next_non_full = i;
330 pool->free_blocks += size_to_blocks(hdr->size);
331 fio_sem_up(pool->lock);
334 void sfree(void *ptr)
336 struct pool *pool = NULL;
342 for (i = 0; i < nr_pools; i++) {
343 if (ptr_valid(&mp[i], ptr)) {
350 sfree_pool(pool, ptr);
354 log_err("smalloc: ptr %p not from smalloc pool\n", ptr);
357 static unsigned int find_best_index(struct pool *pool)
361 assert(pool->free_blocks);
363 for (i = pool->next_non_full; pool->bitmap[i] == -1U; i++) {
364 if (i == pool->nr_blocks - 1) {
367 for (j = 0; j < pool->nr_blocks; j++)
368 if (pool->bitmap[j] != -1U)
376 static void *__smalloc_pool(struct pool *pool, size_t size)
381 unsigned int last_idx;
384 fio_sem_down(pool->lock);
386 nr_blocks = size_to_blocks(size);
387 if (nr_blocks > pool->free_blocks)
390 pool->next_non_full = find_best_index(pool);
394 i = pool->next_non_full;
395 while (i < pool->nr_blocks) {
398 if (pool->bitmap[i] == -1U) {
404 idx = find_next_zero(pool->bitmap[i], last_idx);
405 if (!blocks_free(pool, i, idx, nr_blocks)) {
407 if (idx < SMALLOC_BPI)
411 while (idx >= SMALLOC_BPI) {
418 set_blocks(pool, i, idx, nr_blocks);
419 offset = i * SMALLOC_BPL + idx * SMALLOC_BPB;
423 if (i < pool->nr_blocks) {
424 pool->free_blocks -= nr_blocks;
425 ret = pool->map + offset;
428 fio_sem_up(pool->lock);
432 static size_t size_to_alloc_size(size_t size)
434 size_t alloc_size = size + sizeof(struct block_hdr);
437 * Round to int alignment, so that the postred pointer will
438 * be naturally aligned as well.
440 #ifdef SMALLOC_REDZONE
441 alloc_size += sizeof(unsigned int);
442 alloc_size = (alloc_size + int_mask) & ~int_mask;
448 static void *smalloc_pool(struct pool *pool, size_t size)
450 size_t alloc_size = size_to_alloc_size(size);
453 ptr = __smalloc_pool(pool, alloc_size);
455 struct block_hdr *hdr = ptr;
457 hdr->size = alloc_size;
461 memset(ptr, 0, size);
467 static void smalloc_print_bitmap(struct pool *pool)
469 size_t nr_blocks = pool->nr_blocks;
470 unsigned int *bitmap = pool->bitmap;
474 if (!enable_smalloc_debug)
477 buffer = malloc(SMALLOC_BPI + 1);
480 buffer[SMALLOC_BPI] = '\0';
482 for (i = 0; i < nr_blocks; i++) {
483 unsigned int line = bitmap[i];
485 /* skip completely full lines */
489 for (j = 0; j < SMALLOC_BPI; j++)
491 buffer[SMALLOC_BPI-1-j] = '1';
493 buffer[SMALLOC_BPI-1-j] = '0';
495 log_err("smalloc: bitmap %5u, %s\n", i, buffer);
501 void smalloc_debug(size_t size)
504 size_t alloc_size = size_to_alloc_size(size);
507 alloc_blocks = size_to_blocks(alloc_size);
510 log_err("smalloc: size = %lu, alloc_size = %lu, blocks = %lu\n",
511 (unsigned long) size, (unsigned long) alloc_size,
512 (unsigned long) alloc_blocks);
513 for (i = 0; i < nr_pools; i++) {
514 log_err("smalloc: pool %u, free/total blocks %u/%u\n", i,
515 (unsigned int) (mp[i].free_blocks),
516 (unsigned int) (mp[i].nr_blocks*sizeof(unsigned int)*8));
517 if (size && mp[i].free_blocks >= alloc_blocks) {
518 void *ptr = smalloc_pool(&mp[i], size);
522 log_err("smalloc: smalloc_pool %u succeeded\n", i);
524 log_err("smalloc: smalloc_pool %u failed\n", i);
525 log_err("smalloc: next_non_full=%u, nr_blocks=%u\n",
526 (unsigned int) mp[i].next_non_full, (unsigned int) mp[i].nr_blocks);
527 smalloc_print_bitmap(&mp[i]);
533 void *smalloc(size_t size)
535 unsigned int i, end_pool;
537 if (size != (unsigned int) size)
544 for (; i < end_pool; i++) {
545 void *ptr = smalloc_pool(&mp[i], size);
553 end_pool = last_pool;
561 log_err("smalloc: OOM. Consider using --alloc-size to increase the "
562 "shared memory available.\n");
567 void *scalloc(size_t nmemb, size_t size)
569 return smalloc(nmemb * size);
572 char *smalloc_strdup(const char *str)
576 ptr = smalloc(strlen(str) + 1);