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(unsigned int *map, unsigned int idx,
104 unsigned int nr_blocks,
105 int (*func)(unsigned int *map, unsigned int mask))
108 unsigned int this_blocks, mask;
110 this_blocks = nr_blocks;
111 if (this_blocks + idx > SMALLOC_BPI) {
112 this_blocks = SMALLOC_BPI - idx;
113 idx = SMALLOC_BPI - this_blocks;
116 if (this_blocks == SMALLOC_BPI)
119 mask = ((1U << this_blocks) - 1) << idx;
121 if (!func(map, mask))
124 nr_blocks -= this_blocks;
132 static int mask_cmp(unsigned int *map, unsigned int mask)
134 return !(*map & mask);
137 static int mask_clear(unsigned int *map, unsigned int mask)
143 static int mask_set(unsigned int *map, unsigned int mask)
149 static int blocks_free(unsigned int *map, unsigned int idx,
150 unsigned int nr_blocks)
152 return blocks_iter(map, idx, nr_blocks, mask_cmp);
155 static void set_blocks(unsigned int *map, unsigned int idx,
156 unsigned int nr_blocks)
158 blocks_iter(map, idx, nr_blocks, mask_set);
161 static void clear_blocks(unsigned int *map, unsigned int idx,
162 unsigned int nr_blocks)
164 blocks_iter(map, idx, nr_blocks, mask_clear);
167 static inline int __ffs(int word)
171 if (!(word & 0xffff)) {
175 if (!(word & 0xff)) {
195 static int find_next_zero(int word, int start)
198 word >>= (start + 1);
199 return __ffs(~word) + start + 1;
202 static int add_pool(struct pool *pool, unsigned int alloc_size)
205 int fd, bitmap_blocks;
207 strcpy(pool->file, "/tmp/.fio_smalloc.XXXXXX");
208 fd = mkstemp(pool->file);
212 #ifdef SMALLOC_REDZONE
213 alloc_size += sizeof(unsigned int);
215 alloc_size += sizeof(struct block_hdr);
216 if (alloc_size < INITIAL_SIZE)
217 alloc_size = INITIAL_SIZE;
219 /* round up to nearest full number of blocks */
220 alloc_size = (alloc_size + SMALLOC_BPL - 1) & ~(SMALLOC_BPL - 1);
221 bitmap_blocks = alloc_size / SMALLOC_BPL;
222 alloc_size += bitmap_blocks * sizeof(unsigned int);
223 pool->mmap_size = alloc_size;
225 pool->nr_blocks = bitmap_blocks;
226 pool->free_blocks = bitmap_blocks * SMALLOC_BPB;
228 if (ftruncate(fd, alloc_size) < 0)
231 ptr = mmap(NULL, alloc_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
232 if (ptr == MAP_FAILED)
235 memset(ptr, 0, alloc_size);
237 pool->bitmap = (void *) ptr + (pool->nr_blocks * SMALLOC_BPL);
240 pool->lock = fio_mutex_init(1);
249 global_write_unlock();
252 fprintf(stderr, "smalloc: failed adding pool\n");
254 munmap(pool->map, pool->mmap_size);
267 lock = fio_mutex_rw_init();
269 ret = add_pool(&mp[0], INITIAL_SIZE);
273 static void cleanup_pool(struct pool *pool)
277 munmap(pool->map, pool->mmap_size);
280 fio_mutex_remove(pool->lock);
287 for (i = 0; i < nr_pools; i++)
288 cleanup_pool(&mp[i]);
291 fio_mutex_remove(lock);
294 static void fill_redzone(struct block_hdr *hdr)
296 #ifdef SMALLOC_REDZONE
297 unsigned int *postred = (void *) hdr + hdr->size - sizeof(unsigned int);
299 hdr->prered = SMALLOC_PRE_RED;
300 *postred = SMALLOC_POST_RED;
304 static void sfree_check_redzone(struct block_hdr *hdr)
306 #ifdef SMALLOC_REDZONE
307 unsigned int *postred = (void *) hdr + hdr->size - sizeof(unsigned int);
309 if (hdr->prered != SMALLOC_PRE_RED) {
310 fprintf(stderr, "smalloc pre redzone destroyed!\n");
311 fprintf(stderr, " ptr=%p, prered=%x, expected %x\n",
312 hdr, hdr->prered, SMALLOC_PRE_RED);
315 if (*postred != SMALLOC_POST_RED) {
316 fprintf(stderr, "smalloc post redzone destroyed!\n");
317 fprintf(stderr, " ptr=%p, postred=%x, expected %x\n",
318 hdr, *postred, SMALLOC_POST_RED);
324 static void sfree_pool(struct pool *pool, void *ptr)
326 struct block_hdr *hdr;
328 unsigned long offset;
336 assert(ptr_valid(pool, ptr));
338 sfree_check_redzone(hdr);
340 offset = ptr - pool->map;
341 i = offset / SMALLOC_BPL;
342 idx = (offset % SMALLOC_BPL) / SMALLOC_BPB;
345 clear_blocks(&pool->bitmap[i], idx, size_to_blocks(hdr->size));
346 if (i < pool->next_non_full)
347 pool->next_non_full = i;
348 pool->free_blocks += size_to_blocks(hdr->size);
352 void sfree(void *ptr)
354 struct pool *pool = NULL;
362 for (i = 0; i < nr_pools; i++) {
363 if (ptr_valid(&mp[i], ptr)) {
369 global_read_unlock();
372 sfree_pool(pool, ptr);
375 static void *__smalloc_pool(struct pool *pool, unsigned int size)
377 unsigned int nr_blocks;
380 unsigned int last_idx;
385 nr_blocks = size_to_blocks(size);
386 if (nr_blocks > pool->free_blocks)
389 i = pool->next_non_full;
392 while (i < pool->nr_blocks) {
395 if (pool->bitmap[i] == -1U) {
397 pool->next_non_full = i;
402 idx = find_next_zero(pool->bitmap[i], last_idx);
403 if (!blocks_free(&pool->bitmap[i], idx, nr_blocks)) {
405 if (idx < SMALLOC_BPI)
409 while (idx >= SMALLOC_BPI) {
416 set_blocks(&pool->bitmap[i], idx, nr_blocks);
417 offset = i * SMALLOC_BPL + idx * SMALLOC_BPB;
421 if (i < pool->nr_blocks) {
422 pool->free_blocks -= nr_blocks;
423 ret = pool->map + offset;
430 static void *smalloc_pool(struct pool *pool, unsigned int size)
432 struct block_hdr *hdr;
433 unsigned int alloc_size;
436 alloc_size = size + sizeof(*hdr);
437 #ifdef SMALLOC_REDZONE
438 alloc_size += sizeof(unsigned int);
441 ptr = __smalloc_pool(pool, alloc_size);
446 hdr->size = alloc_size;
451 memset(ptr, 0, size);
455 void *smalloc(unsigned int size)
463 for (; i < nr_pools; i++) {
464 void *ptr = smalloc_pool(&mp[i], size);
468 global_read_unlock();
477 if (nr_pools + 1 > MAX_POOLS)
481 global_read_unlock();
482 if (add_pool(&mp[nr_pools], size))
488 global_read_unlock();
493 char *smalloc_strdup(const char *str)
497 ptr = smalloc(strlen(str) + 1);