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;
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
209 ret = posix_fallocate(fd, 0, alloc_size);
211 fprintf(stderr, "posix_fallocate pool file failed: %s\n", strerror(ret));
217 if (ftruncate(fd, alloc_size) < 0)
220 ptr = mmap(NULL, alloc_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
221 if (ptr == MAP_FAILED)
224 memset(ptr, 0, alloc_size);
226 pool->bitmap = (void *) ptr + (pool->nr_blocks * SMALLOC_BPL);
228 pool->lock = fio_mutex_init(1);
233 * Unlink pool file now. It wont get deleted until the fd is closed,
234 * which happens both for cleanup or unexpected quit. This way we
235 * don't leave temp files around in case of a crash.
243 fprintf(stderr, "smalloc: failed adding pool\n");
245 munmap(pool->map, pool->mmap_size);
256 lock = fio_mutex_rw_init();
257 ret = add_pool(&mp[0], INITIAL_SIZE);
261 static void cleanup_pool(struct pool *pool)
264 * This will also remove the temporary file we used as a backing
265 * store, it was already unlinked
268 munmap(pool->map, pool->mmap_size);
271 fio_mutex_remove(pool->lock);
278 for (i = 0; i < nr_pools; i++)
279 cleanup_pool(&mp[i]);
282 fio_mutex_remove(lock);
285 #ifdef SMALLOC_REDZONE
286 static void *postred_ptr(struct block_hdr *hdr)
290 ptr = (unsigned long) hdr + hdr->size - sizeof(unsigned int);
291 ptr = (ptr + int_mask) & ~int_mask;
296 static void fill_redzone(struct block_hdr *hdr)
298 unsigned int *postred = postred_ptr(hdr);
300 hdr->prered = SMALLOC_PRE_RED;
301 *postred = SMALLOC_POST_RED;
304 static void sfree_check_redzone(struct block_hdr *hdr)
306 unsigned int *postred = postred_ptr(hdr);
308 if (hdr->prered != SMALLOC_PRE_RED) {
309 fprintf(stderr, "smalloc pre redzone destroyed!\n");
310 fprintf(stderr, " ptr=%p, prered=%x, expected %x\n",
311 hdr, hdr->prered, SMALLOC_PRE_RED);
314 if (*postred != SMALLOC_POST_RED) {
315 fprintf(stderr, "smalloc post redzone destroyed!\n");
316 fprintf(stderr, " ptr=%p, postred=%x, expected %x\n",
317 hdr, *postred, SMALLOC_POST_RED);
322 static void fill_redzone(struct block_hdr *hdr)
326 static void sfree_check_redzone(struct block_hdr *hdr)
331 static void sfree_pool(struct pool *pool, void *ptr)
333 struct block_hdr *hdr;
335 unsigned long offset;
343 assert(ptr_valid(pool, ptr));
345 sfree_check_redzone(hdr);
347 offset = ptr - pool->map;
348 i = offset / SMALLOC_BPL;
349 idx = (offset % SMALLOC_BPL) / SMALLOC_BPB;
352 clear_blocks(pool, i, idx, size_to_blocks(hdr->size));
353 if (i < pool->next_non_full)
354 pool->next_non_full = i;
355 pool->free_blocks += size_to_blocks(hdr->size);
359 void sfree(void *ptr)
361 struct pool *pool = NULL;
369 for (i = 0; i < nr_pools; i++) {
370 if (ptr_valid(&mp[i], ptr)) {
376 global_read_unlock();
379 sfree_pool(pool, ptr);
382 static void *__smalloc_pool(struct pool *pool, unsigned int size)
384 unsigned int nr_blocks;
387 unsigned int last_idx;
392 nr_blocks = size_to_blocks(size);
393 if (nr_blocks > pool->free_blocks)
396 i = pool->next_non_full;
399 while (i < pool->nr_blocks) {
402 if (pool->bitmap[i] == -1U) {
404 pool->next_non_full = i;
409 idx = find_next_zero(pool->bitmap[i], last_idx);
410 if (!blocks_free(pool, i, idx, nr_blocks)) {
412 if (idx < SMALLOC_BPI)
416 while (idx >= SMALLOC_BPI) {
423 set_blocks(pool, i, idx, nr_blocks);
424 offset = i * SMALLOC_BPL + idx * SMALLOC_BPB;
428 if (i < pool->nr_blocks) {
429 pool->free_blocks -= nr_blocks;
430 ret = pool->map + offset;
437 static void *smalloc_pool(struct pool *pool, unsigned int size)
439 unsigned int alloc_size = size + sizeof(struct block_hdr);
443 * Round to int alignment, so that the postred pointer will
444 * be naturally aligned as well.
446 #ifdef SMALLOC_REDZONE
447 alloc_size += sizeof(unsigned int);
448 alloc_size = (alloc_size + int_mask) & ~int_mask;
451 ptr = __smalloc_pool(pool, alloc_size);
453 struct block_hdr *hdr = ptr;
455 hdr->size = alloc_size;
459 memset(ptr, 0, size);
465 void *smalloc(unsigned int size)
473 for (; i < nr_pools; i++) {
474 void *ptr = smalloc_pool(&mp[i], size);
478 global_write_unlock();
487 if (nr_pools + 1 > MAX_POOLS)
491 if (add_pool(&mp[nr_pools], size))
497 global_write_unlock();
501 char *smalloc_strdup(const char *str)
505 ptr = smalloc(strlen(str) + 1);