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;
128 static int mask_cmp(unsigned int *map, unsigned int mask)
130 return !(*map & mask);
133 static int mask_clear(unsigned int *map, unsigned int mask)
139 static int mask_set(unsigned int *map, unsigned int mask)
145 static int blocks_free(unsigned int *map, unsigned int idx,
146 unsigned int nr_blocks)
148 return blocks_iter(map, idx, nr_blocks, mask_cmp);
151 static void set_blocks(unsigned int *map, unsigned int idx,
152 unsigned int nr_blocks)
154 blocks_iter(map, idx, nr_blocks, mask_set);
157 static void clear_blocks(unsigned int *map, unsigned int idx,
158 unsigned int nr_blocks)
160 blocks_iter(map, idx, nr_blocks, mask_clear);
163 static inline int __ffs(int word)
167 if (!(word & 0xffff)) {
171 if (!(word & 0xff)) {
191 static int find_next_zero(int word, int start)
194 word >>= (start + 1);
195 return __ffs(~word) + start + 1;
198 static int add_pool(struct pool *pool, unsigned int alloc_size)
201 int fd, bitmap_blocks;
203 printf("add pool %u\n", alloc_size);
205 strcpy(pool->file, "/tmp/.fio_smalloc.XXXXXX");
206 fd = mkstemp(pool->file);
210 #ifdef SMALLOC_REDZONE
211 alloc_size += sizeof(unsigned int);
213 alloc_size += sizeof(struct block_hdr);
214 if (alloc_size < INITIAL_SIZE)
215 alloc_size = INITIAL_SIZE;
217 /* round up to nearest full number of blocks */
218 alloc_size = (alloc_size + SMALLOC_BPL - 1) & ~(SMALLOC_BPL - 1);
219 bitmap_blocks = alloc_size / SMALLOC_BPL;
220 alloc_size += bitmap_blocks * sizeof(unsigned int);
221 pool->mmap_size = alloc_size;
223 pool->nr_blocks = bitmap_blocks;
224 pool->free_blocks = bitmap_blocks * SMALLOC_BPB;
226 if (ftruncate(fd, alloc_size) < 0)
229 ptr = mmap(NULL, alloc_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
230 if (ptr == MAP_FAILED)
233 memset(ptr, 0, alloc_size);
235 pool->bitmap = (void *) ptr + (pool->nr_blocks * SMALLOC_BPL);
238 pool->lock = fio_mutex_init(1);
247 global_write_unlock();
250 fprintf(stderr, "smalloc: failed adding pool\n");
252 munmap(pool->map, pool->mmap_size);
265 lock = fio_mutex_rw_init();
267 ret = add_pool(&mp[0], INITIAL_SIZE);
271 static void cleanup_pool(struct pool *pool)
275 munmap(pool->map, pool->mmap_size);
278 fio_mutex_remove(pool->lock);
285 for (i = 0; i < nr_pools; i++)
286 cleanup_pool(&mp[i]);
289 fio_mutex_remove(lock);
292 static void fill_redzone(struct block_hdr *hdr)
294 #ifdef SMALLOC_REDZONE
295 unsigned int *postred = (void *) hdr + hdr->size - sizeof(unsigned int);
297 hdr->prered = SMALLOC_PRE_RED;
298 *postred = SMALLOC_POST_RED;
302 static void sfree_check_redzone(struct block_hdr *hdr)
304 #ifdef SMALLOC_REDZONE
305 unsigned int *postred = (void *) hdr + hdr->size - sizeof(unsigned int);
307 if (hdr->prered != SMALLOC_PRE_RED) {
308 fprintf(stderr, "smalloc pre redzone destroyed!\n");
309 fprintf(stderr, " ptr=%p, prered=%x, expected %x\n",
310 hdr, hdr->prered, SMALLOC_PRE_RED);
313 if (*postred != SMALLOC_POST_RED) {
314 fprintf(stderr, "smalloc post redzone destroyed!\n");
315 fprintf(stderr, " ptr=%p, postred=%x, expected %x\n",
316 hdr, *postred, SMALLOC_POST_RED);
322 static void sfree_pool(struct pool *pool, void *ptr)
324 struct block_hdr *hdr;
325 unsigned int nr_blocks, i, idx;
326 unsigned long offset;
334 assert(ptr_valid(pool, ptr));
336 nr_blocks = (hdr->size + SMALLOC_BPB - 1) / SMALLOC_BPB;
337 sfree_check_redzone(hdr);
339 offset = ptr - pool->map;
340 i = offset / SMALLOC_BPL;
341 idx = (offset % SMALLOC_BPL) / SMALLOC_BPB;
344 clear_blocks(&pool->bitmap[i], idx, nr_blocks);
345 if (i < pool->next_non_full)
346 pool->next_non_full = i;
347 pool->free_blocks += nr_blocks;
351 void sfree(void *ptr)
353 struct pool *pool = NULL;
361 for (i = 0; i < nr_pools; i++) {
362 if (ptr_valid(&mp[i], ptr)) {
368 global_read_unlock();
371 sfree_pool(pool, ptr);
374 static void *__smalloc_pool(struct pool *pool, unsigned int size)
376 unsigned int nr_blocks;
379 unsigned int last_idx;
382 nr_blocks = (size + SMALLOC_BPB - 1) / SMALLOC_BPB;
385 if (nr_blocks > pool->free_blocks)
388 i = pool->next_non_full;
391 while (i < pool->nr_blocks) {
394 if (pool->bitmap[i] == -1U) {
396 pool->next_non_full = i;
401 idx = find_next_zero(pool->bitmap[i], last_idx);
402 if (!blocks_free(&pool->bitmap[i], idx, nr_blocks)) {
404 if (idx < SMALLOC_BPI)
408 while (idx >= SMALLOC_BPI) {
415 set_blocks(&pool->bitmap[i], idx, nr_blocks);
416 offset = i * SMALLOC_BPL + idx * SMALLOC_BPB;
420 if (i < pool->nr_blocks) {
421 pool->free_blocks -= nr_blocks;
422 ret = pool->map + offset;
429 static void *smalloc_pool(struct pool *pool, unsigned int size)
431 struct block_hdr *hdr;
432 unsigned int alloc_size;
435 alloc_size = size + sizeof(*hdr);
436 #ifdef SMALLOC_REDZONE
437 alloc_size += sizeof(unsigned int);
440 ptr = __smalloc_pool(pool, alloc_size);
442 printf("failed allocating %u\n", alloc_size);
447 hdr->size = alloc_size;
452 memset(ptr, 0, size);
456 void *smalloc(unsigned int size)
464 for (; i < nr_pools; i++) {
465 void *ptr = smalloc_pool(&mp[i], size);
469 global_read_unlock();
478 if (nr_pools + 1 > MAX_POOLS)
482 global_read_unlock();
483 if (add_pool(&mp[nr_pools], size))
489 global_read_unlock();
494 char *smalloc_strdup(const char *str)
498 ptr = smalloc(strlen(str) + 1);