2 * simple memory allocator, backed by mmap() so that it hands out memory
3 * that can be shared across processes and threads
11 #include <sys/types.h>
16 #include "arch/arch.h"
19 #define SMALLOC_REDZONE /* define to detect memory corruption */
21 #define SMALLOC_BPB 32 /* block size, bytes-per-bit in bitmap */
22 #define SMALLOC_BPI (sizeof(unsigned int) * 8)
23 #define SMALLOC_BPL (SMALLOC_BPB * SMALLOC_BPI)
25 #define INITIAL_SIZE 8192*1024 /* new pool size */
26 #define MAX_POOLS 128 /* maximum number of pools to setup */
28 #define SMALLOC_PRE_RED 0xdeadbeefU
29 #define SMALLOC_POST_RED 0x5aa55aa5U
31 unsigned int smalloc_pool_size = INITIAL_SIZE;
32 const int int_mask = sizeof(int) - 1;
35 struct fio_mutex *lock; /* protects this pool */
36 void *map; /* map of blocks */
37 unsigned int *bitmap; /* blocks free/busy map */
38 unsigned int free_blocks; /* free blocks */
39 unsigned int nr_blocks; /* total blocks */
40 unsigned int next_non_full;
41 int fd; /* memory backing fd */
42 unsigned int mmap_size;
47 #ifdef SMALLOC_REDZONE
52 static struct pool mp[MAX_POOLS];
53 static unsigned int nr_pools;
54 static unsigned int last_pool;
55 static struct fio_mutex *lock;
57 static inline void pool_lock(struct pool *pool)
59 fio_mutex_down(pool->lock);
62 static inline void pool_unlock(struct pool *pool)
64 fio_mutex_up(pool->lock);
67 static inline void global_read_lock(void)
69 fio_mutex_down_read(lock);
72 static inline void global_read_unlock(void)
74 fio_mutex_up_read(lock);
77 static inline void global_write_lock(void)
79 fio_mutex_down_write(lock);
82 static inline void global_write_unlock(void)
84 fio_mutex_up_write(lock);
87 static inline int ptr_valid(struct pool *pool, void *ptr)
89 unsigned int pool_size = pool->nr_blocks * SMALLOC_BPL;
91 return (ptr >= pool->map) && (ptr < pool->map + pool_size);
94 static inline unsigned int size_to_blocks(unsigned int size)
96 return (size + SMALLOC_BPB - 1) / SMALLOC_BPB;
99 static int blocks_iter(struct pool *pool, unsigned int pool_idx,
100 unsigned int idx, unsigned int nr_blocks,
101 int (*func)(unsigned int *map, unsigned int mask))
105 unsigned int this_blocks, mask;
108 if (pool_idx >= pool->nr_blocks)
111 map = &pool->bitmap[pool_idx];
113 this_blocks = nr_blocks;
114 if (this_blocks + idx > SMALLOC_BPI) {
115 this_blocks = SMALLOC_BPI - idx;
116 idx = SMALLOC_BPI - this_blocks;
119 if (this_blocks == SMALLOC_BPI)
122 mask = ((1U << this_blocks) - 1) << idx;
124 if (!func(map, mask))
127 nr_blocks -= this_blocks;
135 static int mask_cmp(unsigned int *map, unsigned int mask)
137 return !(*map & mask);
140 static int mask_clear(unsigned int *map, unsigned int mask)
142 assert((*map & mask) == mask);
147 static int mask_set(unsigned int *map, unsigned int mask)
149 assert(!(*map & mask));
154 static int blocks_free(struct pool *pool, unsigned int pool_idx,
155 unsigned int idx, unsigned int nr_blocks)
157 return blocks_iter(pool, pool_idx, idx, nr_blocks, mask_cmp);
160 static void set_blocks(struct pool *pool, unsigned int pool_idx,
161 unsigned int idx, unsigned int nr_blocks)
163 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_set);
166 static void clear_blocks(struct pool *pool, unsigned int pool_idx,
167 unsigned int idx, unsigned int nr_blocks)
169 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_clear);
172 static int find_next_zero(int word, int start)
175 word >>= (start + 1);
176 return ffz(word) + start + 1;
179 static int add_pool(struct pool *pool, unsigned int alloc_size)
181 int fd, bitmap_blocks, ret;
182 char file[] = "/tmp/.fio_smalloc.XXXXXX";
189 #ifdef SMALLOC_REDZONE
190 alloc_size += sizeof(unsigned int);
192 alloc_size += sizeof(struct block_hdr);
193 if (alloc_size < INITIAL_SIZE)
194 alloc_size = INITIAL_SIZE;
196 /* round up to nearest full number of blocks */
197 alloc_size = (alloc_size + SMALLOC_BPL - 1) & ~(SMALLOC_BPL - 1);
198 bitmap_blocks = alloc_size / SMALLOC_BPL;
199 alloc_size += bitmap_blocks * sizeof(unsigned int);
200 pool->mmap_size = alloc_size;
202 pool->nr_blocks = bitmap_blocks;
203 pool->free_blocks = bitmap_blocks * SMALLOC_BPB;
205 #ifdef FIO_HAVE_FALLOCATE
206 ret = posix_fallocate(fd, 0, alloc_size);
208 fprintf(stderr, "posix_fallocate pool file failed: %s\n", strerror(ret));
213 if (ftruncate(fd, alloc_size) < 0)
216 ptr = mmap(NULL, alloc_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
217 if (ptr == MAP_FAILED)
220 memset(ptr, 0, alloc_size);
222 pool->bitmap = (void *) ptr + (pool->nr_blocks * SMALLOC_BPL);
224 pool->lock = fio_mutex_init(1);
229 * Unlink pool file now. It wont get deleted until the fd is closed,
230 * which happens both for cleanup or unexpected quit. This way we
231 * don't leave temp files around in case of a crash.
239 fprintf(stderr, "smalloc: failed adding pool\n");
241 munmap(pool->map, pool->mmap_size);
252 lock = fio_mutex_rw_init();
253 ret = add_pool(&mp[0], INITIAL_SIZE);
257 static void cleanup_pool(struct pool *pool)
260 * This will also remove the temporary file we used as a backing
261 * store, it was already unlinked
264 munmap(pool->map, pool->mmap_size);
267 fio_mutex_remove(pool->lock);
274 for (i = 0; i < nr_pools; i++)
275 cleanup_pool(&mp[i]);
278 fio_mutex_remove(lock);
281 #ifdef SMALLOC_REDZONE
282 static void *postred_ptr(struct block_hdr *hdr)
286 ptr = (unsigned long) hdr + hdr->size - sizeof(unsigned int);
287 ptr = (ptr + int_mask) & ~int_mask;
292 static void fill_redzone(struct block_hdr *hdr)
294 unsigned int *postred = postred_ptr(hdr);
296 hdr->prered = SMALLOC_PRE_RED;
297 *postred = SMALLOC_POST_RED;
300 static void sfree_check_redzone(struct block_hdr *hdr)
302 unsigned int *postred = postred_ptr(hdr);
304 if (hdr->prered != SMALLOC_PRE_RED) {
305 fprintf(stderr, "smalloc pre redzone destroyed!\n");
306 fprintf(stderr, " ptr=%p, prered=%x, expected %x\n",
307 hdr, hdr->prered, SMALLOC_PRE_RED);
310 if (*postred != SMALLOC_POST_RED) {
311 fprintf(stderr, "smalloc post redzone destroyed!\n");
312 fprintf(stderr, " ptr=%p, postred=%x, expected %x\n",
313 hdr, *postred, SMALLOC_POST_RED);
318 static void fill_redzone(struct block_hdr *hdr)
322 static void sfree_check_redzone(struct block_hdr *hdr)
327 static void sfree_pool(struct pool *pool, void *ptr)
329 struct block_hdr *hdr;
331 unsigned long offset;
339 assert(ptr_valid(pool, ptr));
341 sfree_check_redzone(hdr);
343 offset = ptr - pool->map;
344 i = offset / SMALLOC_BPL;
345 idx = (offset % SMALLOC_BPL) / SMALLOC_BPB;
348 clear_blocks(pool, i, idx, size_to_blocks(hdr->size));
349 if (i < pool->next_non_full)
350 pool->next_non_full = i;
351 pool->free_blocks += size_to_blocks(hdr->size);
355 void sfree(void *ptr)
357 struct pool *pool = NULL;
365 for (i = 0; i < nr_pools; i++) {
366 if (ptr_valid(&mp[i], ptr)) {
372 global_read_unlock();
375 sfree_pool(pool, ptr);
378 static void *__smalloc_pool(struct pool *pool, unsigned int size)
380 unsigned int nr_blocks;
383 unsigned int last_idx;
388 nr_blocks = size_to_blocks(size);
389 if (nr_blocks > pool->free_blocks)
392 i = pool->next_non_full;
395 while (i < pool->nr_blocks) {
398 if (pool->bitmap[i] == -1U) {
400 pool->next_non_full = i;
405 idx = find_next_zero(pool->bitmap[i], last_idx);
406 if (!blocks_free(pool, i, idx, nr_blocks)) {
408 if (idx < SMALLOC_BPI)
412 while (idx >= SMALLOC_BPI) {
419 set_blocks(pool, i, idx, nr_blocks);
420 offset = i * SMALLOC_BPL + idx * SMALLOC_BPB;
424 if (i < pool->nr_blocks) {
425 pool->free_blocks -= nr_blocks;
426 ret = pool->map + offset;
433 static void *smalloc_pool(struct pool *pool, unsigned int size)
435 unsigned int alloc_size = size + sizeof(struct block_hdr);
439 * Round to int alignment, so that the postred pointer will
440 * be naturally aligned as well.
442 #ifdef SMALLOC_REDZONE
443 alloc_size += sizeof(unsigned int);
444 alloc_size = (alloc_size + int_mask) & ~int_mask;
447 ptr = __smalloc_pool(pool, alloc_size);
449 struct block_hdr *hdr = ptr;
451 hdr->size = alloc_size;
455 memset(ptr, 0, size);
461 void *smalloc(unsigned int size)
469 for (; i < nr_pools; i++) {
470 void *ptr = smalloc_pool(&mp[i], size);
474 global_write_unlock();
483 if (nr_pools + 1 > MAX_POOLS)
487 if (add_pool(&mp[nr_pools], size))
493 global_write_unlock();
497 char *smalloc_strdup(const char *str)
501 ptr = smalloc(strlen(str) + 1);