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 MP_SAFE /* define to make thread safe */
18 #define SMALLOC_REDZONE /* define to detect memory corruption */
20 #define SMALLOC_BPB 32 /* block size, bytes-per-bit in bitmap */
21 #define SMALLOC_BPI (sizeof(unsigned int) * 8)
22 #define SMALLOC_BPL (SMALLOC_BPB * SMALLOC_BPI)
24 #define INITIAL_SIZE 1024*1024 /* new pool size */
25 #define MAX_POOLS 128 /* maximum number of pools to setup */
27 #define SMALLOC_PRE_RED 0xdeadbeefU
28 #define SMALLOC_POST_RED 0x5aa55aa5U
30 unsigned int smalloc_pool_size = INITIAL_SIZE;
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)
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 int find_next_zero(int word, int start)
179 word >>= (start + 1);
180 return ffz(word) + start + 1;
183 static int add_pool(struct pool *pool, unsigned int alloc_size)
185 int fd, bitmap_blocks;
186 char file[] = "/tmp/.fio_smalloc.XXXXXX";
193 #ifdef SMALLOC_REDZONE
194 alloc_size += sizeof(unsigned int);
196 alloc_size += sizeof(struct block_hdr);
197 if (alloc_size < INITIAL_SIZE)
198 alloc_size = INITIAL_SIZE;
200 /* round up to nearest full number of blocks */
201 alloc_size = (alloc_size + SMALLOC_BPL - 1) & ~(SMALLOC_BPL - 1);
202 bitmap_blocks = alloc_size / SMALLOC_BPL;
203 alloc_size += bitmap_blocks * sizeof(unsigned int);
204 pool->mmap_size = alloc_size;
206 pool->nr_blocks = bitmap_blocks;
207 pool->free_blocks = bitmap_blocks * SMALLOC_BPB;
209 if (ftruncate(fd, alloc_size) < 0)
212 ptr = mmap(NULL, alloc_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
213 if (ptr == MAP_FAILED)
216 memset(ptr, 0, alloc_size);
218 pool->bitmap = (void *) ptr + (pool->nr_blocks * SMALLOC_BPL);
221 pool->lock = fio_mutex_init(1);
227 * Unlink pool file now. It wont get deleted until the fd is closed,
228 * which happens both for cleanup or unexpected quit. This way we
229 * don't leave temp files around in case of a crash.
237 fprintf(stderr, "smalloc: failed adding pool\n");
239 munmap(pool->map, pool->mmap_size);
251 lock = fio_mutex_rw_init();
253 ret = add_pool(&mp[0], INITIAL_SIZE);
257 static void cleanup_pool(struct pool *pool)
260 * This will also remove the temporary file we used as a backing
261 * store, it was already unlinked
264 munmap(pool->map, pool->mmap_size);
267 fio_mutex_remove(pool->lock);
274 for (i = 0; i < nr_pools; i++)
275 cleanup_pool(&mp[i]);
278 fio_mutex_remove(lock);
281 #ifdef SMALLOC_REDZONE
282 static void fill_redzone(struct block_hdr *hdr)
284 unsigned int *postred = (void *) hdr + hdr->size - sizeof(unsigned int);
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 = (void *) hdr + hdr->size - sizeof(unsigned int);
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);
428 #ifdef SMALLOC_REDZONE
429 alloc_size += sizeof(unsigned int);
432 ptr = __smalloc_pool(pool, alloc_size);
434 struct block_hdr *hdr = ptr;
436 hdr->size = alloc_size;
440 memset(ptr, 0, size);
446 void *smalloc(unsigned int size)
454 for (; i < nr_pools; i++) {
455 void *ptr = smalloc_pool(&mp[i], size);
459 global_write_unlock();
468 if (nr_pools + 1 > MAX_POOLS)
472 if (add_pool(&mp[nr_pools], size))
478 global_write_unlock();
482 char *smalloc_strdup(const char *str)
486 ptr = smalloc(strlen(str) + 1);