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>
15 #include "arch/arch.h"
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 128 /* 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;
30 const int int_mask = sizeof(int) - 1;
33 struct fio_mutex *lock; /* protects this pool */
34 void *map; /* map of blocks */
35 unsigned int *bitmap; /* blocks free/busy map */
36 unsigned int free_blocks; /* free blocks */
37 unsigned int nr_blocks; /* total blocks */
38 unsigned int next_non_full;
39 int fd; /* memory backing 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)
57 fio_mutex_down(pool->lock);
60 static inline void pool_unlock(struct pool *pool)
62 fio_mutex_up(pool->lock);
65 static inline void global_read_lock(void)
67 fio_mutex_down_read(lock);
70 static inline void global_read_unlock(void)
72 fio_mutex_up_read(lock);
75 static inline void global_write_lock(void)
77 fio_mutex_down_write(lock);
80 static inline void global_write_unlock(void)
82 fio_mutex_up_write(lock);
85 static inline int ptr_valid(struct pool *pool, void *ptr)
87 unsigned int pool_size = pool->nr_blocks * SMALLOC_BPL;
89 return (ptr >= pool->map) && (ptr < pool->map + pool_size);
92 static inline unsigned int size_to_blocks(unsigned int size)
94 return (size + SMALLOC_BPB - 1) / SMALLOC_BPB;
97 static int blocks_iter(struct pool *pool, unsigned int pool_idx,
98 unsigned int idx, unsigned int nr_blocks,
99 int (*func)(unsigned int *map, unsigned int mask))
103 unsigned int this_blocks, mask;
106 if (pool_idx >= pool->nr_blocks)
109 map = &pool->bitmap[pool_idx];
111 this_blocks = nr_blocks;
112 if (this_blocks + idx > SMALLOC_BPI) {
113 this_blocks = SMALLOC_BPI - idx;
114 idx = SMALLOC_BPI - this_blocks;
117 if (this_blocks == SMALLOC_BPI)
120 mask = ((1U << this_blocks) - 1) << idx;
122 if (!func(map, mask))
125 nr_blocks -= this_blocks;
133 static int mask_cmp(unsigned int *map, unsigned int mask)
135 return !(*map & mask);
138 static int mask_clear(unsigned int *map, unsigned int mask)
140 assert((*map & mask) == mask);
145 static int mask_set(unsigned int *map, unsigned int mask)
147 assert(!(*map & mask));
152 static int blocks_free(struct pool *pool, unsigned int pool_idx,
153 unsigned int idx, unsigned int nr_blocks)
155 return blocks_iter(pool, pool_idx, idx, nr_blocks, mask_cmp);
158 static void set_blocks(struct pool *pool, unsigned int pool_idx,
159 unsigned int idx, unsigned int nr_blocks)
161 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_set);
164 static void clear_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_clear);
170 static int find_next_zero(int word, int start)
173 word >>= (start + 1);
174 return ffz(word) + start + 1;
177 static int add_pool(struct pool *pool, unsigned int alloc_size)
179 int fd, bitmap_blocks;
180 char file[] = "/tmp/.fio_smalloc.XXXXXX";
187 #ifdef SMALLOC_REDZONE
188 alloc_size += sizeof(unsigned int);
190 alloc_size += sizeof(struct block_hdr);
191 if (alloc_size < INITIAL_SIZE)
192 alloc_size = INITIAL_SIZE;
194 /* round up to nearest full number of blocks */
195 alloc_size = (alloc_size + SMALLOC_BPL - 1) & ~(SMALLOC_BPL - 1);
196 bitmap_blocks = alloc_size / SMALLOC_BPL;
197 alloc_size += bitmap_blocks * sizeof(unsigned int);
198 pool->mmap_size = alloc_size;
200 pool->nr_blocks = bitmap_blocks;
201 pool->free_blocks = bitmap_blocks * SMALLOC_BPB;
203 if (ftruncate(fd, alloc_size) < 0)
206 ptr = mmap(NULL, alloc_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
207 if (ptr == MAP_FAILED)
210 memset(ptr, 0, alloc_size);
212 pool->bitmap = (void *) ptr + (pool->nr_blocks * SMALLOC_BPL);
214 pool->lock = fio_mutex_init(1);
219 * Unlink pool file now. It wont get deleted until the fd is closed,
220 * which happens both for cleanup or unexpected quit. This way we
221 * don't leave temp files around in case of a crash.
229 fprintf(stderr, "smalloc: failed adding pool\n");
231 munmap(pool->map, pool->mmap_size);
242 lock = fio_mutex_rw_init();
243 ret = add_pool(&mp[0], INITIAL_SIZE);
247 static void cleanup_pool(struct pool *pool)
250 * This will also remove the temporary file we used as a backing
251 * store, it was already unlinked
254 munmap(pool->map, pool->mmap_size);
257 fio_mutex_remove(pool->lock);
264 for (i = 0; i < nr_pools; i++)
265 cleanup_pool(&mp[i]);
268 fio_mutex_remove(lock);
271 #ifdef SMALLOC_REDZONE
272 static void *postred_ptr(struct block_hdr *hdr)
276 ptr = (unsigned long) hdr + hdr->size - sizeof(unsigned int);
277 ptr = (ptr + int_mask) & ~int_mask;
282 static void fill_redzone(struct block_hdr *hdr)
284 unsigned int *postred = postred_ptr(hdr);
286 hdr->prered = SMALLOC_PRE_RED;
287 *postred = SMALLOC_POST_RED;
290 static void sfree_check_redzone(struct block_hdr *hdr)
292 unsigned int *postred = postred_ptr(hdr);
294 if (hdr->prered != SMALLOC_PRE_RED) {
295 fprintf(stderr, "smalloc pre redzone destroyed!\n");
296 fprintf(stderr, " ptr=%p, prered=%x, expected %x\n",
297 hdr, hdr->prered, SMALLOC_PRE_RED);
300 if (*postred != SMALLOC_POST_RED) {
301 fprintf(stderr, "smalloc post redzone destroyed!\n");
302 fprintf(stderr, " ptr=%p, postred=%x, expected %x\n",
303 hdr, *postred, SMALLOC_POST_RED);
308 static void fill_redzone(struct block_hdr *hdr)
312 static void sfree_check_redzone(struct block_hdr *hdr)
317 static void sfree_pool(struct pool *pool, void *ptr)
319 struct block_hdr *hdr;
321 unsigned long offset;
329 assert(ptr_valid(pool, ptr));
331 sfree_check_redzone(hdr);
333 offset = ptr - pool->map;
334 i = offset / SMALLOC_BPL;
335 idx = (offset % SMALLOC_BPL) / SMALLOC_BPB;
338 clear_blocks(pool, i, idx, size_to_blocks(hdr->size));
339 if (i < pool->next_non_full)
340 pool->next_non_full = i;
341 pool->free_blocks += size_to_blocks(hdr->size);
345 void sfree(void *ptr)
347 struct pool *pool = NULL;
355 for (i = 0; i < nr_pools; i++) {
356 if (ptr_valid(&mp[i], ptr)) {
362 global_read_unlock();
365 sfree_pool(pool, ptr);
368 static void *__smalloc_pool(struct pool *pool, unsigned int size)
370 unsigned int nr_blocks;
373 unsigned int last_idx;
378 nr_blocks = size_to_blocks(size);
379 if (nr_blocks > pool->free_blocks)
382 i = pool->next_non_full;
385 while (i < pool->nr_blocks) {
388 if (pool->bitmap[i] == -1U) {
390 pool->next_non_full = i;
395 idx = find_next_zero(pool->bitmap[i], last_idx);
396 if (!blocks_free(pool, i, idx, nr_blocks)) {
398 if (idx < SMALLOC_BPI)
402 while (idx >= SMALLOC_BPI) {
409 set_blocks(pool, i, idx, nr_blocks);
410 offset = i * SMALLOC_BPL + idx * SMALLOC_BPB;
414 if (i < pool->nr_blocks) {
415 pool->free_blocks -= nr_blocks;
416 ret = pool->map + offset;
423 static void *smalloc_pool(struct pool *pool, unsigned int size)
425 unsigned int alloc_size = size + sizeof(struct block_hdr);
429 * Round to int alignment, so that the postred pointer will
430 * be naturally aligned as well.
432 #ifdef SMALLOC_REDZONE
433 alloc_size += sizeof(unsigned int);
434 alloc_size = (alloc_size + int_mask) & ~int_mask;
437 ptr = __smalloc_pool(pool, alloc_size);
439 struct block_hdr *hdr = ptr;
441 hdr->size = alloc_size;
445 memset(ptr, 0, size);
451 void *smalloc(unsigned int size)
459 for (; i < nr_pools; i++) {
460 void *ptr = smalloc_pool(&mp[i], size);
464 global_write_unlock();
473 if (nr_pools + 1 > MAX_POOLS)
477 if (add_pool(&mp[nr_pools], size))
483 global_write_unlock();
487 char *smalloc_strdup(const char *str)
491 ptr = smalloc(strlen(str) + 1);