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>
15 #ifdef CONFIG_VALGRIND_DEV
16 #include <valgrind/valgrind.h>
18 #define RUNNING_ON_VALGRIND 0
19 #define VALGRIND_MALLOCLIKE_BLOCK(addr, size, rzB, is_zeroed) do { } while (0)
20 #define VALGRIND_FREELIKE_BLOCK(addr, rzB) do { } while (0)
25 #include "arch/arch.h"
30 #define SMALLOC_REDZONE /* define to detect memory corruption */
32 #define SMALLOC_BPB 32 /* block size, bytes-per-bit in bitmap */
33 #define SMALLOC_BPI (sizeof(unsigned int) * 8)
34 #define SMALLOC_BPL (SMALLOC_BPB * SMALLOC_BPI)
36 #define INITIAL_SIZE 16*1024*1024 /* new pool size */
37 #define INITIAL_POOLS 8 /* maximum number of pools to setup */
41 #define SMALLOC_PRE_RED 0xdeadbeefU
42 #define SMALLOC_POST_RED 0x5aa55aa5U
44 unsigned int smalloc_pool_size = INITIAL_SIZE;
45 #ifdef SMALLOC_REDZONE
46 static const int int_mask = sizeof(int) - 1;
50 struct fio_sem *lock; /* protects this pool */
51 void *map; /* map of blocks */
52 unsigned int *bitmap; /* blocks free/busy map */
53 size_t free_blocks; /* free blocks */
54 size_t nr_blocks; /* total blocks */
59 #ifdef SMALLOC_REDZONE
60 #define REDZONE_SIZE sizeof(unsigned int)
62 #define REDZONE_SIZE 0
67 #ifdef SMALLOC_REDZONE
72 static struct pool mp[MAX_POOLS];
73 static unsigned int nr_pools;
74 static unsigned int last_pool;
76 static inline int ptr_valid(struct pool *pool, void *ptr)
78 unsigned int pool_size = pool->nr_blocks * SMALLOC_BPL;
80 return (ptr >= pool->map) && (ptr < pool->map + pool_size);
83 static inline size_t size_to_blocks(size_t size)
85 return (size + SMALLOC_BPB - 1) / SMALLOC_BPB;
88 static int blocks_iter(struct pool *pool, unsigned int pool_idx,
89 unsigned int idx, size_t nr_blocks,
90 int (*func)(unsigned int *map, unsigned int mask))
94 unsigned int this_blocks, mask;
97 if (pool_idx >= pool->nr_blocks)
100 map = &pool->bitmap[pool_idx];
102 this_blocks = nr_blocks;
103 if (this_blocks + idx > SMALLOC_BPI) {
104 this_blocks = SMALLOC_BPI - idx;
105 idx = SMALLOC_BPI - this_blocks;
108 if (this_blocks == SMALLOC_BPI)
111 mask = ((1U << this_blocks) - 1) << idx;
113 if (!func(map, mask))
116 nr_blocks -= this_blocks;
124 static int mask_cmp(unsigned int *map, unsigned int mask)
126 return !(*map & mask);
129 static int mask_clear(unsigned int *map, unsigned int mask)
131 assert((*map & mask) == mask);
136 static int mask_set(unsigned int *map, unsigned int mask)
138 assert(!(*map & mask));
143 static int blocks_free(struct pool *pool, unsigned int pool_idx,
144 unsigned int idx, size_t nr_blocks)
146 return blocks_iter(pool, pool_idx, idx, nr_blocks, mask_cmp);
149 static void set_blocks(struct pool *pool, unsigned int pool_idx,
150 unsigned int idx, size_t nr_blocks)
152 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_set);
155 static void clear_blocks(struct pool *pool, unsigned int pool_idx,
156 unsigned int idx, size_t nr_blocks)
158 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_clear);
161 static int find_next_zero(int word, int start)
165 return ffz(word) + start;
168 static bool add_pool(struct pool *pool, unsigned int alloc_size)
174 if (nr_pools == MAX_POOLS)
177 #ifdef SMALLOC_REDZONE
178 alloc_size += sizeof(unsigned int);
180 alloc_size += sizeof(struct block_hdr);
181 if (alloc_size < INITIAL_SIZE)
182 alloc_size = INITIAL_SIZE;
184 /* round up to nearest full number of blocks */
185 alloc_size = (alloc_size + SMALLOC_BPL - 1) & ~(SMALLOC_BPL - 1);
186 bitmap_blocks = alloc_size / SMALLOC_BPL;
187 alloc_size += bitmap_blocks * sizeof(unsigned int);
188 pool->mmap_size = alloc_size;
190 pool->nr_blocks = bitmap_blocks;
191 pool->free_blocks = bitmap_blocks * SMALLOC_BPB;
193 mmap_flags = OS_MAP_ANON;
195 mmap_flags |= MAP_PRIVATE;
197 mmap_flags |= MAP_SHARED;
199 ptr = mmap(NULL, alloc_size, PROT_READ|PROT_WRITE, mmap_flags, -1, 0);
201 if (ptr == MAP_FAILED)
205 pool->bitmap = (unsigned int *)((char *) ptr + (pool->nr_blocks * SMALLOC_BPL));
206 memset(pool->bitmap, 0, bitmap_blocks * sizeof(unsigned int));
208 pool->lock = fio_sem_init(FIO_SEM_UNLOCKED);
215 log_err("smalloc: failed adding pool\n");
217 munmap(pool->map, pool->mmap_size);
226 for (i = 0; i < INITIAL_POOLS; i++) {
227 ret = add_pool(&mp[nr_pools], smalloc_pool_size);
233 * If we added at least one pool, we should be OK for most
239 static void cleanup_pool(struct pool *pool)
242 * This will also remove the temporary file we used as a backing
243 * store, it was already unlinked
245 munmap(pool->map, pool->mmap_size);
248 fio_sem_remove(pool->lock);
255 for (i = 0; i < nr_pools; i++)
256 cleanup_pool(&mp[i]);
259 #ifdef SMALLOC_REDZONE
260 static void *postred_ptr(struct block_hdr *hdr)
264 ptr = (uintptr_t) hdr + hdr->size - sizeof(unsigned int);
265 ptr = (uintptr_t) PTR_ALIGN(ptr, int_mask);
270 static void fill_redzone(struct block_hdr *hdr)
272 unsigned int *postred = postred_ptr(hdr);
274 /* Let Valgrind fill the red zones. */
275 if (RUNNING_ON_VALGRIND)
278 hdr->prered = SMALLOC_PRE_RED;
279 *postred = SMALLOC_POST_RED;
282 static void sfree_check_redzone(struct block_hdr *hdr)
284 unsigned int *postred = postred_ptr(hdr);
286 /* Let Valgrind check the red zones. */
287 if (RUNNING_ON_VALGRIND)
290 if (hdr->prered != SMALLOC_PRE_RED) {
291 log_err("smalloc pre redzone destroyed!\n"
292 " ptr=%p, prered=%x, expected %x\n",
293 hdr, hdr->prered, SMALLOC_PRE_RED);
296 if (*postred != SMALLOC_POST_RED) {
297 log_err("smalloc post redzone destroyed!\n"
298 " ptr=%p, postred=%x, expected %x\n",
299 hdr, *postred, SMALLOC_POST_RED);
304 static void fill_redzone(struct block_hdr *hdr)
308 static void sfree_check_redzone(struct block_hdr *hdr)
313 static void sfree_pool(struct pool *pool, void *ptr)
315 struct block_hdr *hdr;
317 unsigned long offset;
325 assert(ptr_valid(pool, ptr));
327 sfree_check_redzone(hdr);
329 offset = ptr - pool->map;
330 i = offset / SMALLOC_BPL;
331 idx = (offset % SMALLOC_BPL) / SMALLOC_BPB;
333 fio_sem_down(pool->lock);
334 clear_blocks(pool, i, idx, size_to_blocks(hdr->size));
335 if (i < pool->next_non_full)
336 pool->next_non_full = i;
337 pool->free_blocks += size_to_blocks(hdr->size);
338 fio_sem_up(pool->lock);
341 void sfree(void *ptr)
343 struct pool *pool = NULL;
349 for (i = 0; i < nr_pools; i++) {
350 if (ptr_valid(&mp[i], ptr)) {
357 VALGRIND_FREELIKE_BLOCK(ptr, REDZONE_SIZE);
358 sfree_pool(pool, ptr);
362 log_err("smalloc: ptr %p not from smalloc pool\n", ptr);
365 static void *__smalloc_pool(struct pool *pool, size_t size)
370 unsigned int last_idx;
373 fio_sem_down(pool->lock);
375 nr_blocks = size_to_blocks(size);
376 if (nr_blocks > pool->free_blocks)
379 i = pool->next_non_full;
382 while (i < pool->nr_blocks) {
385 if (pool->bitmap[i] == -1U) {
387 pool->next_non_full = i;
392 idx = find_next_zero(pool->bitmap[i], last_idx);
393 if (!blocks_free(pool, i, idx, nr_blocks)) {
395 if (idx < SMALLOC_BPI)
399 while (idx >= SMALLOC_BPI) {
406 set_blocks(pool, i, idx, nr_blocks);
407 offset = i * SMALLOC_BPL + idx * SMALLOC_BPB;
411 if (i < pool->nr_blocks) {
412 pool->free_blocks -= nr_blocks;
413 ret = pool->map + offset;
416 fio_sem_up(pool->lock);
420 static void *smalloc_pool(struct pool *pool, size_t size)
422 size_t alloc_size = size + sizeof(struct block_hdr);
426 * Round to int alignment, so that the postred pointer will
427 * be naturally aligned as well.
429 #ifdef SMALLOC_REDZONE
430 alloc_size += sizeof(unsigned int);
431 alloc_size = (alloc_size + int_mask) & ~int_mask;
434 ptr = __smalloc_pool(pool, alloc_size);
436 struct block_hdr *hdr = ptr;
438 hdr->size = alloc_size;
442 memset(ptr, 0, size);
448 static void *__smalloc(size_t size, bool is_zeroed)
450 unsigned int i, end_pool;
452 if (size != (unsigned int) size)
459 for (; i < end_pool; i++) {
460 void *ptr = smalloc_pool(&mp[i], size);
464 VALGRIND_MALLOCLIKE_BLOCK(ptr, size,
471 end_pool = last_pool;
479 log_err("smalloc: OOM. Consider using --alloc-size to increase the "
480 "shared memory available.\n");
484 void *smalloc(size_t size)
486 return __smalloc(size, false);
489 void *scalloc(size_t nmemb, size_t size)
491 return __smalloc(nmemb * size, true);
494 char *smalloc_strdup(const char *str)
498 ptr = smalloc(strlen(str) + 1);