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 int blocks_iter(unsigned int *map, unsigned int idx,
99 unsigned int nr_blocks,
100 int (*func)(unsigned int *map, unsigned int mask))
103 unsigned int this_blocks, mask;
105 this_blocks = nr_blocks;
106 if (this_blocks + idx > SMALLOC_BPI) {
107 this_blocks = SMALLOC_BPI - idx;
108 idx = SMALLOC_BPI - this_blocks;
111 if (this_blocks == SMALLOC_BPI)
114 mask = ((1U << this_blocks) - 1) << idx;
116 if (!func(map, mask))
119 nr_blocks -= this_blocks;
127 static int mask_cmp(unsigned int *map, unsigned int mask)
129 return !(*map & mask);
132 static int mask_clear(unsigned int *map, unsigned int mask)
138 static int mask_set(unsigned int *map, unsigned int mask)
144 static int blocks_free(unsigned int *map, unsigned int idx,
145 unsigned int nr_blocks)
147 return blocks_iter(map, idx, nr_blocks, mask_cmp);
150 static void set_blocks(unsigned int *map, unsigned int idx,
151 unsigned int nr_blocks)
153 blocks_iter(map, idx, nr_blocks, mask_set);
156 static void clear_blocks(unsigned int *map, unsigned int idx,
157 unsigned int nr_blocks)
159 blocks_iter(map, idx, nr_blocks, mask_clear);
162 static inline int __ffs(int word)
166 if (!(word & 0xffff)) {
170 if (!(word & 0xff)) {
190 static int find_next_zero(int word, int start)
193 word >>= (start + 1);
194 return __ffs(~word) + start + 1;
197 static int add_pool(struct pool *pool, unsigned int alloc_size)
200 int fd, bitmap_blocks;
202 strcpy(pool->file, "/tmp/.fio_smalloc.XXXXXX");
203 fd = mkstemp(pool->file);
207 #ifdef SMALLOC_REDZONE
208 alloc_size += sizeof(unsigned int);
210 alloc_size += sizeof(struct block_hdr);
211 if (alloc_size < INITIAL_SIZE)
212 alloc_size = INITIAL_SIZE;
214 /* round up to nearest full number of blocks */
215 alloc_size = (alloc_size + SMALLOC_BPL - 1) & ~(SMALLOC_BPL - 1);
216 bitmap_blocks = alloc_size / SMALLOC_BPL;
217 alloc_size += bitmap_blocks * sizeof(unsigned int);
218 pool->mmap_size = alloc_size;
220 pool->nr_blocks = bitmap_blocks;
221 pool->free_blocks = bitmap_blocks * SMALLOC_BPB;
223 if (ftruncate(fd, alloc_size) < 0)
226 ptr = mmap(NULL, alloc_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
227 if (ptr == MAP_FAILED)
230 memset(ptr, 0, alloc_size);
232 pool->bitmap = (void *) ptr + (pool->nr_blocks * SMALLOC_BPL);
235 pool->lock = fio_mutex_init(1);
244 global_write_unlock();
247 fprintf(stderr, "smalloc: failed adding pool\n");
249 munmap(pool->map, pool->mmap_size);
262 lock = fio_mutex_rw_init();
264 ret = add_pool(&mp[0], INITIAL_SIZE);
268 static void cleanup_pool(struct pool *pool)
272 munmap(pool->map, pool->mmap_size);
275 fio_mutex_remove(pool->lock);
282 for (i = 0; i < nr_pools; i++)
283 cleanup_pool(&mp[i]);
286 fio_mutex_remove(lock);
289 static void fill_redzone(struct block_hdr *hdr)
291 #ifdef SMALLOC_REDZONE
292 unsigned int *postred = (void *) hdr + hdr->size - sizeof(unsigned int);
294 hdr->prered = SMALLOC_PRE_RED;
295 *postred = SMALLOC_POST_RED;
299 static void sfree_check_redzone(struct block_hdr *hdr)
301 #ifdef SMALLOC_REDZONE
302 unsigned int *postred = (void *) hdr + hdr->size - sizeof(unsigned int);
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);
319 static void sfree_pool(struct pool *pool, void *ptr)
321 struct block_hdr *hdr;
323 unsigned long offset;
331 assert(ptr_valid(pool, ptr));
333 sfree_check_redzone(hdr);
335 offset = ptr - pool->map;
336 i = offset / SMALLOC_BPL;
337 idx = (offset % SMALLOC_BPL) / SMALLOC_BPB;
340 clear_blocks(&pool->bitmap[i], idx, size_to_blocks(hdr->size));
341 if (i < pool->next_non_full)
342 pool->next_non_full = i;
343 pool->free_blocks += size_to_blocks(hdr->size);
347 void sfree(void *ptr)
349 struct pool *pool = NULL;
357 for (i = 0; i < nr_pools; i++) {
358 if (ptr_valid(&mp[i], ptr)) {
364 global_read_unlock();
367 sfree_pool(pool, ptr);
370 static inline unsigned int size_to_blocks(unsigned int size)
372 return (size + SMALLOC_BPB - 1) / SMALLOC_BPB;
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);
443 printf("failed allocating %u\n", alloc_size);
448 hdr->size = alloc_size;
453 memset(ptr, 0, size);
457 void *smalloc(unsigned int size)
465 for (; i < nr_pools; i++) {
466 void *ptr = smalloc_pool(&mp[i], size);
470 global_read_unlock();
479 if (nr_pools + 1 > MAX_POOLS)
483 global_read_unlock();
484 if (add_pool(&mp[nr_pools], size))
490 global_read_unlock();
495 char *smalloc_strdup(const char *str)
499 ptr = smalloc(strlen(str) + 1);