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 #define MP_SAFE /* define to make thread safe */
17 #define SMALLOC_REDZONE /* define to detect memory corruption */
19 #define SMALLOC_BPB 32 /* block size, bytes-per-bit in bitmap */
20 #define SMALLOC_BPI (sizeof(unsigned int) * 8)
21 #define SMALLOC_BPL (SMALLOC_BPB * SMALLOC_BPI)
23 #define INITIAL_SIZE 1024*1024 /* new pool size */
24 #define MAX_POOLS 4 /* 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;
32 struct fio_mutex *lock; /* protects this pool */
33 void *map; /* map of blocks */
34 unsigned int *bitmap; /* blocks free/busy map */
35 unsigned int free_blocks; /* free blocks */
36 unsigned int nr_blocks; /* total blocks */
37 unsigned int next_non_full;
38 int fd; /* memory backing fd */
39 char file[PATH_MAX]; /* filename for fd */
40 unsigned int mmap_size;
45 #ifdef SMALLOC_REDZONE
50 static struct pool mp[MAX_POOLS];
51 static unsigned int nr_pools;
52 static unsigned int last_pool;
53 static struct fio_mutex *lock;
55 static inline void pool_lock(struct pool *pool)
58 fio_mutex_down(pool->lock);
61 static inline void pool_unlock(struct pool *pool)
64 fio_mutex_up(pool->lock);
67 static inline void global_read_lock(void)
70 fio_mutex_down_read(lock);
73 static inline void global_read_unlock(void)
76 fio_mutex_up_read(lock);
79 static inline void global_write_lock(void)
82 fio_mutex_down_write(lock);
85 static inline void global_write_unlock(void)
88 fio_mutex_up_write(lock);
91 static inline int ptr_valid(struct pool *pool, void *ptr)
93 unsigned int pool_size = pool->nr_blocks * SMALLOC_BPL;
95 return (ptr >= pool->map) && (ptr < pool->map + pool_size);
98 static inline unsigned int size_to_blocks(unsigned int size)
100 return (size + SMALLOC_BPB - 1) / SMALLOC_BPB;
103 static int blocks_iter(struct pool *pool, unsigned int pool_idx,
104 unsigned int idx, unsigned int nr_blocks,
105 int (*func)(unsigned int *map, unsigned int mask))
109 unsigned int this_blocks, mask;
112 if (pool_idx >= pool->nr_blocks)
115 map = &pool->bitmap[pool_idx];
117 this_blocks = nr_blocks;
118 if (this_blocks + idx > SMALLOC_BPI) {
119 this_blocks = SMALLOC_BPI - idx;
120 idx = SMALLOC_BPI - this_blocks;
123 if (this_blocks == SMALLOC_BPI)
126 mask = ((1U << this_blocks) - 1) << idx;
128 if (!func(map, mask))
131 nr_blocks -= this_blocks;
139 static int mask_cmp(unsigned int *map, unsigned int mask)
141 return !(*map & mask);
144 static int mask_clear(unsigned int *map, unsigned int mask)
146 assert((*map & mask) == mask);
151 static int mask_set(unsigned int *map, unsigned int mask)
153 assert(!(*map & mask));
158 static int blocks_free(struct pool *pool, unsigned int pool_idx,
159 unsigned int idx, unsigned int nr_blocks)
161 return blocks_iter(pool, pool_idx, idx, nr_blocks, mask_cmp);
164 static void set_blocks(struct pool *pool, unsigned int pool_idx,
165 unsigned int idx, unsigned int nr_blocks)
167 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_set);
170 static void clear_blocks(struct pool *pool, unsigned int pool_idx,
171 unsigned int idx, unsigned int nr_blocks)
173 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_clear);
176 static inline int __ffs(int word)
180 if (!(word & 0xffff)) {
184 if (!(word & 0xff)) {
204 static int find_next_zero(int word, int start)
207 word >>= (start + 1);
208 return __ffs(~word) + start + 1;
211 static int add_pool(struct pool *pool, unsigned int alloc_size)
214 int fd, bitmap_blocks;
216 strcpy(pool->file, "/tmp/.fio_smalloc.XXXXXX");
217 fd = mkstemp(pool->file);
221 #ifdef SMALLOC_REDZONE
222 alloc_size += sizeof(unsigned int);
224 alloc_size += sizeof(struct block_hdr);
225 if (alloc_size < INITIAL_SIZE)
226 alloc_size = INITIAL_SIZE;
228 /* round up to nearest full number of blocks */
229 alloc_size = (alloc_size + SMALLOC_BPL - 1) & ~(SMALLOC_BPL - 1);
230 bitmap_blocks = alloc_size / SMALLOC_BPL;
231 alloc_size += bitmap_blocks * sizeof(unsigned int);
232 pool->mmap_size = alloc_size;
234 pool->nr_blocks = bitmap_blocks;
235 pool->free_blocks = bitmap_blocks * SMALLOC_BPB;
237 if (ftruncate(fd, alloc_size) < 0)
240 ptr = mmap(NULL, alloc_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
241 if (ptr == MAP_FAILED)
244 memset(ptr, 0, alloc_size);
246 pool->bitmap = (void *) ptr + (pool->nr_blocks * SMALLOC_BPL);
249 pool->lock = fio_mutex_init(1);
258 global_write_unlock();
261 fprintf(stderr, "smalloc: failed adding pool\n");
263 munmap(pool->map, pool->mmap_size);
276 lock = fio_mutex_rw_init();
278 ret = add_pool(&mp[0], INITIAL_SIZE);
282 static void cleanup_pool(struct pool *pool)
286 munmap(pool->map, pool->mmap_size);
289 fio_mutex_remove(pool->lock);
296 for (i = 0; i < nr_pools; i++)
297 cleanup_pool(&mp[i]);
300 fio_mutex_remove(lock);
303 #ifdef SMALLOC_REDZONE
304 static void fill_redzone(struct block_hdr *hdr)
306 unsigned int *postred = (void *) hdr + hdr->size - sizeof(unsigned int);
308 hdr->prered = SMALLOC_PRE_RED;
309 *postred = SMALLOC_POST_RED;
312 static void sfree_check_redzone(struct block_hdr *hdr)
314 unsigned int *postred = (void *) hdr + hdr->size - sizeof(unsigned int);
316 if (hdr->prered != SMALLOC_PRE_RED) {
317 fprintf(stderr, "smalloc pre redzone destroyed!\n");
318 fprintf(stderr, " ptr=%p, prered=%x, expected %x\n",
319 hdr, hdr->prered, SMALLOC_PRE_RED);
322 if (*postred != SMALLOC_POST_RED) {
323 fprintf(stderr, "smalloc post redzone destroyed!\n");
324 fprintf(stderr, " ptr=%p, postred=%x, expected %x\n",
325 hdr, *postred, SMALLOC_POST_RED);
330 static void fill_redzone(struct block_hdr *hdr)
334 static void sfree_check_redzone(struct block_hdr *hdr)
339 static void sfree_pool(struct pool *pool, void *ptr)
341 struct block_hdr *hdr;
343 unsigned long offset;
351 assert(ptr_valid(pool, ptr));
353 sfree_check_redzone(hdr);
355 offset = ptr - pool->map;
356 i = offset / SMALLOC_BPL;
357 idx = (offset % SMALLOC_BPL) / SMALLOC_BPB;
360 clear_blocks(pool, i, idx, size_to_blocks(hdr->size));
361 if (i < pool->next_non_full)
362 pool->next_non_full = i;
363 pool->free_blocks += size_to_blocks(hdr->size);
367 void sfree(void *ptr)
369 struct pool *pool = NULL;
377 for (i = 0; i < nr_pools; i++) {
378 if (ptr_valid(&mp[i], ptr)) {
384 global_read_unlock();
387 sfree_pool(pool, ptr);
390 static void *__smalloc_pool(struct pool *pool, unsigned int size)
392 unsigned int nr_blocks;
395 unsigned int last_idx;
400 nr_blocks = size_to_blocks(size);
401 if (nr_blocks > pool->free_blocks)
404 i = pool->next_non_full;
407 while (i < pool->nr_blocks) {
410 if (pool->bitmap[i] == -1U) {
412 pool->next_non_full = i;
417 idx = find_next_zero(pool->bitmap[i], last_idx);
418 if (!blocks_free(pool, i, idx, nr_blocks)) {
420 if (idx < SMALLOC_BPI)
424 while (idx >= SMALLOC_BPI) {
431 set_blocks(pool, i, idx, nr_blocks);
432 offset = i * SMALLOC_BPL + idx * SMALLOC_BPB;
436 if (i < pool->nr_blocks) {
437 pool->free_blocks -= nr_blocks;
438 ret = pool->map + offset;
445 static void *smalloc_pool(struct pool *pool, unsigned int size)
447 unsigned int alloc_size = size + sizeof(struct block_hdr);
450 #ifdef SMALLOC_REDZONE
451 alloc_size += sizeof(unsigned int);
454 ptr = __smalloc_pool(pool, alloc_size);
456 struct block_hdr *hdr = ptr;
458 hdr->size = alloc_size;
462 memset(ptr, 0, size);
468 void *smalloc(unsigned int size)
476 for (; i < nr_pools; i++) {
477 void *ptr = smalloc_pool(&mp[i], size);
481 global_read_unlock();
490 if (nr_pools + 1 > MAX_POOLS)
494 global_read_unlock();
495 if (add_pool(&mp[nr_pools], size))
501 global_read_unlock();
506 char *smalloc_strdup(const char *str)
510 ptr = smalloc(strlen(str) + 1);