2 * simple memory allocator, backed by mmap() so that it hands out memory
3 * that can be shared across processes and threads
8 #ifdef CONFIG_VALGRIND_DEV
9 #include <valgrind/valgrind.h>
11 #define RUNNING_ON_VALGRIND 0
12 #define VALGRIND_MALLOCLIKE_BLOCK(addr, size, rzB, is_zeroed) do { } while (0)
13 #define VALGRIND_FREELIKE_BLOCK(addr, rzB) do { } while (0)
22 #define SMALLOC_REDZONE /* define to detect memory corruption */
24 #define SMALLOC_BPB 32 /* block size, bytes-per-bit in bitmap */
25 #define SMALLOC_BPI (sizeof(unsigned int) * 8)
26 #define SMALLOC_BPL (SMALLOC_BPB * SMALLOC_BPI)
28 #define INITIAL_SIZE 16*1024*1024 /* new pool size */
29 #define INITIAL_POOLS 8 /* maximum number of pools to setup */
33 #define SMALLOC_PRE_RED 0xdeadbeefU
34 #define SMALLOC_POST_RED 0x5aa55aa5U
36 unsigned int smalloc_pool_size = INITIAL_SIZE;
37 #ifdef SMALLOC_REDZONE
38 static const int int_mask = sizeof(int) - 1;
42 struct fio_sem *lock; /* protects this pool */
43 void *map; /* map of blocks */
44 unsigned int *bitmap; /* blocks free/busy map */
45 size_t free_blocks; /* free blocks */
46 size_t nr_blocks; /* total blocks */
51 #ifdef SMALLOC_REDZONE
52 #define REDZONE_SIZE sizeof(unsigned int)
54 #define REDZONE_SIZE 0
59 #ifdef SMALLOC_REDZONE
64 static struct pool mp[MAX_POOLS];
65 static unsigned int nr_pools;
66 static unsigned int last_pool;
68 static inline int ptr_valid(struct pool *pool, void *ptr)
70 unsigned int pool_size = pool->nr_blocks * SMALLOC_BPL;
72 return (ptr >= pool->map) && (ptr < pool->map + pool_size);
75 static inline size_t size_to_blocks(size_t size)
77 return (size + SMALLOC_BPB - 1) / SMALLOC_BPB;
80 static int blocks_iter(struct pool *pool, unsigned int pool_idx,
81 unsigned int idx, size_t nr_blocks,
82 int (*func)(unsigned int *map, unsigned int mask))
86 unsigned int this_blocks, mask;
89 if (pool_idx >= pool->nr_blocks)
92 map = &pool->bitmap[pool_idx];
94 this_blocks = nr_blocks;
95 if (this_blocks + idx > SMALLOC_BPI) {
96 this_blocks = SMALLOC_BPI - idx;
97 idx = SMALLOC_BPI - this_blocks;
100 if (this_blocks == SMALLOC_BPI)
103 mask = ((1U << this_blocks) - 1) << idx;
105 if (!func(map, mask))
108 nr_blocks -= this_blocks;
116 static int mask_cmp(unsigned int *map, unsigned int mask)
118 return !(*map & mask);
121 static int mask_clear(unsigned int *map, unsigned int mask)
123 assert((*map & mask) == mask);
128 static int mask_set(unsigned int *map, unsigned int mask)
130 assert(!(*map & mask));
135 static int blocks_free(struct pool *pool, unsigned int pool_idx,
136 unsigned int idx, size_t nr_blocks)
138 return blocks_iter(pool, pool_idx, idx, nr_blocks, mask_cmp);
141 static void set_blocks(struct pool *pool, unsigned int pool_idx,
142 unsigned int idx, size_t nr_blocks)
144 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_set);
147 static void clear_blocks(struct pool *pool, unsigned int pool_idx,
148 unsigned int idx, size_t nr_blocks)
150 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_clear);
153 static int find_next_zero(int word, int start)
157 return ffz(word) + start;
160 static bool add_pool(struct pool *pool, unsigned int alloc_size)
166 if (nr_pools == MAX_POOLS)
169 #ifdef SMALLOC_REDZONE
170 alloc_size += sizeof(unsigned int);
172 alloc_size += sizeof(struct block_hdr);
173 if (alloc_size < INITIAL_SIZE)
174 alloc_size = INITIAL_SIZE;
176 /* round up to nearest full number of blocks */
177 alloc_size = (alloc_size + SMALLOC_BPL - 1) & ~(SMALLOC_BPL - 1);
178 bitmap_blocks = alloc_size / SMALLOC_BPL;
179 alloc_size += bitmap_blocks * sizeof(unsigned int);
180 pool->mmap_size = alloc_size;
182 pool->nr_blocks = bitmap_blocks;
183 pool->free_blocks = bitmap_blocks * SMALLOC_BPB;
185 mmap_flags = OS_MAP_ANON;
187 mmap_flags |= MAP_PRIVATE;
189 mmap_flags |= MAP_SHARED;
191 ptr = mmap(NULL, alloc_size, PROT_READ|PROT_WRITE, mmap_flags, -1, 0);
193 if (ptr == MAP_FAILED)
197 pool->bitmap = (unsigned int *)((char *) ptr + (pool->nr_blocks * SMALLOC_BPL));
198 memset(pool->bitmap, 0, bitmap_blocks * sizeof(unsigned int));
200 pool->lock = fio_sem_init(FIO_SEM_UNLOCKED);
207 log_err("smalloc: failed adding pool\n");
209 munmap(pool->map, pool->mmap_size);
218 for (i = 0; i < INITIAL_POOLS; i++) {
219 ret = add_pool(&mp[nr_pools], smalloc_pool_size);
225 * If we added at least one pool, we should be OK for most
231 static void cleanup_pool(struct pool *pool)
234 * This will also remove the temporary file we used as a backing
235 * store, it was already unlinked
237 munmap(pool->map, pool->mmap_size);
240 fio_sem_remove(pool->lock);
247 for (i = 0; i < nr_pools; i++)
248 cleanup_pool(&mp[i]);
251 #ifdef SMALLOC_REDZONE
252 static void *postred_ptr(struct block_hdr *hdr)
256 ptr = (uintptr_t) hdr + hdr->size - sizeof(unsigned int);
257 ptr = (uintptr_t) PTR_ALIGN(ptr, int_mask);
262 static void fill_redzone(struct block_hdr *hdr)
264 unsigned int *postred = postred_ptr(hdr);
266 /* Let Valgrind fill the red zones. */
267 if (RUNNING_ON_VALGRIND)
270 hdr->prered = SMALLOC_PRE_RED;
271 *postred = SMALLOC_POST_RED;
274 static void sfree_check_redzone(struct block_hdr *hdr)
276 unsigned int *postred = postred_ptr(hdr);
278 /* Let Valgrind check the red zones. */
279 if (RUNNING_ON_VALGRIND)
282 if (hdr->prered != SMALLOC_PRE_RED) {
283 log_err("smalloc pre redzone destroyed!\n"
284 " ptr=%p, prered=%x, expected %x\n",
285 hdr, hdr->prered, SMALLOC_PRE_RED);
288 if (*postred != SMALLOC_POST_RED) {
289 log_err("smalloc post redzone destroyed!\n"
290 " ptr=%p, postred=%x, expected %x\n",
291 hdr, *postred, SMALLOC_POST_RED);
296 static void fill_redzone(struct block_hdr *hdr)
300 static void sfree_check_redzone(struct block_hdr *hdr)
305 static void sfree_pool(struct pool *pool, void *ptr)
307 struct block_hdr *hdr;
309 unsigned long offset;
317 assert(ptr_valid(pool, ptr));
319 sfree_check_redzone(hdr);
321 offset = ptr - pool->map;
322 i = offset / SMALLOC_BPL;
323 idx = (offset % SMALLOC_BPL) / SMALLOC_BPB;
325 fio_sem_down(pool->lock);
326 clear_blocks(pool, i, idx, size_to_blocks(hdr->size));
327 if (i < pool->next_non_full)
328 pool->next_non_full = i;
329 pool->free_blocks += size_to_blocks(hdr->size);
330 fio_sem_up(pool->lock);
333 void sfree(void *ptr)
335 struct pool *pool = NULL;
341 for (i = 0; i < nr_pools; i++) {
342 if (ptr_valid(&mp[i], ptr)) {
349 VALGRIND_FREELIKE_BLOCK(ptr, REDZONE_SIZE);
350 sfree_pool(pool, ptr);
354 log_err("smalloc: ptr %p not from smalloc pool\n", ptr);
357 static void *__smalloc_pool(struct pool *pool, size_t size)
362 unsigned int last_idx;
365 fio_sem_down(pool->lock);
367 nr_blocks = size_to_blocks(size);
368 if (nr_blocks > pool->free_blocks)
371 i = pool->next_non_full;
374 while (i < pool->nr_blocks) {
377 if (pool->bitmap[i] == -1U) {
379 pool->next_non_full = i;
384 idx = find_next_zero(pool->bitmap[i], last_idx);
385 if (!blocks_free(pool, i, idx, nr_blocks)) {
387 if (idx < SMALLOC_BPI)
391 while (idx >= SMALLOC_BPI) {
398 set_blocks(pool, i, idx, nr_blocks);
399 offset = i * SMALLOC_BPL + idx * SMALLOC_BPB;
403 if (i < pool->nr_blocks) {
404 pool->free_blocks -= nr_blocks;
405 ret = pool->map + offset;
408 fio_sem_up(pool->lock);
412 static void *smalloc_pool(struct pool *pool, size_t size)
414 size_t alloc_size = size + sizeof(struct block_hdr);
418 * Round to int alignment, so that the postred pointer will
419 * be naturally aligned as well.
421 #ifdef SMALLOC_REDZONE
422 alloc_size += sizeof(unsigned int);
423 alloc_size = (alloc_size + int_mask) & ~int_mask;
426 ptr = __smalloc_pool(pool, alloc_size);
428 struct block_hdr *hdr = ptr;
430 hdr->size = alloc_size;
434 memset(ptr, 0, size);
440 static void *__smalloc(size_t size, bool is_zeroed)
442 unsigned int i, end_pool;
444 if (size != (unsigned int) size)
451 for (; i < end_pool; i++) {
452 void *ptr = smalloc_pool(&mp[i], size);
456 VALGRIND_MALLOCLIKE_BLOCK(ptr, size,
463 end_pool = last_pool;
471 log_err("smalloc: OOM. Consider using --alloc-size to increase the "
472 "shared memory available.\n");
476 void *smalloc(size_t size)
478 return __smalloc(size, false);
481 void *scalloc(size_t nmemb, size_t size)
483 return __smalloc(nmemb * size, true);
486 char *smalloc_strdup(const char *str)
490 ptr = smalloc(strlen(str) + 1);