2 * simple memory allocator, backed by mmap() so that it hands out memory
3 * that can be shared across processes and threads
12 #include <sys/types.h>
17 #include "arch/arch.h"
20 #define SMALLOC_REDZONE /* define to detect memory corruption */
22 #define SMALLOC_BPB 32 /* block size, bytes-per-bit in bitmap */
23 #define SMALLOC_BPI (sizeof(unsigned int) * 8)
24 #define SMALLOC_BPL (SMALLOC_BPB * SMALLOC_BPI)
26 #define INITIAL_SIZE 8192*1024 /* new pool size */
27 #define MAX_POOLS 128 /* maximum number of pools to setup */
29 #define SMALLOC_PRE_RED 0xdeadbeefU
30 #define SMALLOC_POST_RED 0x5aa55aa5U
32 unsigned int smalloc_pool_size = INITIAL_SIZE;
33 const int int_mask = sizeof(int) - 1;
36 struct fio_mutex *lock; /* protects this pool */
37 void *map; /* map of blocks */
38 unsigned int *bitmap; /* blocks free/busy map */
39 size_t free_blocks; /* free blocks */
40 size_t nr_blocks; /* total blocks */
47 #ifdef SMALLOC_REDZONE
52 static struct pool mp[MAX_POOLS];
53 static unsigned int nr_pools;
54 static unsigned int last_pool;
55 static struct fio_rwlock *lock;
57 static inline void pool_lock(struct pool *pool)
59 fio_mutex_down(pool->lock);
62 static inline void pool_unlock(struct pool *pool)
64 fio_mutex_up(pool->lock);
67 static inline void global_read_lock(void)
69 fio_rwlock_read(lock);
72 static inline void global_read_unlock(void)
74 fio_rwlock_unlock(lock);
77 static inline void global_write_lock(void)
79 fio_rwlock_write(lock);
82 static inline void global_write_unlock(void)
84 fio_rwlock_unlock(lock);
87 static inline int ptr_valid(struct pool *pool, void *ptr)
89 unsigned int pool_size = pool->nr_blocks * SMALLOC_BPL;
91 return (ptr >= pool->map) && (ptr < pool->map + pool_size);
94 static inline size_t size_to_blocks(size_t size)
96 return (size + SMALLOC_BPB - 1) / SMALLOC_BPB;
99 static int blocks_iter(struct pool *pool, unsigned int pool_idx,
100 unsigned int idx, size_t nr_blocks,
101 int (*func)(unsigned int *map, unsigned int mask))
105 unsigned int this_blocks, mask;
108 if (pool_idx >= pool->nr_blocks)
111 map = &pool->bitmap[pool_idx];
113 this_blocks = nr_blocks;
114 if (this_blocks + idx > SMALLOC_BPI) {
115 this_blocks = SMALLOC_BPI - idx;
116 idx = SMALLOC_BPI - this_blocks;
119 if (this_blocks == SMALLOC_BPI)
122 mask = ((1U << this_blocks) - 1) << idx;
124 if (!func(map, mask))
127 nr_blocks -= this_blocks;
135 static int mask_cmp(unsigned int *map, unsigned int mask)
137 return !(*map & mask);
140 static int mask_clear(unsigned int *map, unsigned int mask)
142 assert((*map & mask) == mask);
147 static int mask_set(unsigned int *map, unsigned int mask)
149 assert(!(*map & mask));
154 static int blocks_free(struct pool *pool, unsigned int pool_idx,
155 unsigned int idx, size_t nr_blocks)
157 return blocks_iter(pool, pool_idx, idx, nr_blocks, mask_cmp);
160 static void set_blocks(struct pool *pool, unsigned int pool_idx,
161 unsigned int idx, size_t nr_blocks)
163 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_set);
166 static void clear_blocks(struct pool *pool, unsigned int pool_idx,
167 unsigned int idx, size_t nr_blocks)
169 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_clear);
172 static int find_next_zero(int word, int start)
176 return ffz(word) + start;
179 static int add_pool(struct pool *pool, unsigned int alloc_size)
184 #ifdef SMALLOC_REDZONE
185 alloc_size += sizeof(unsigned int);
187 alloc_size += sizeof(struct block_hdr);
188 if (alloc_size < INITIAL_SIZE)
189 alloc_size = INITIAL_SIZE;
191 /* round up to nearest full number of blocks */
192 alloc_size = (alloc_size + SMALLOC_BPL - 1) & ~(SMALLOC_BPL - 1);
193 bitmap_blocks = alloc_size / SMALLOC_BPL;
194 alloc_size += bitmap_blocks * sizeof(unsigned int);
195 pool->mmap_size = alloc_size;
197 pool->nr_blocks = bitmap_blocks;
198 pool->free_blocks = bitmap_blocks * SMALLOC_BPB;
200 ptr = mmap(NULL, alloc_size, PROT_READ|PROT_WRITE,
201 MAP_SHARED | OS_MAP_ANON, -1, 0);
202 if (ptr == MAP_FAILED)
205 memset(ptr, 0, alloc_size);
207 pool->bitmap = (void *) ptr + (pool->nr_blocks * SMALLOC_BPL);
209 pool->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
216 fprintf(stderr, "smalloc: failed adding pool\n");
218 munmap(pool->map, pool->mmap_size);
226 lock = fio_rwlock_init();
227 ret = add_pool(&mp[0], INITIAL_SIZE);
231 static void cleanup_pool(struct pool *pool)
234 * This will also remove the temporary file we used as a backing
235 * store, it was already unlinked
237 munmap(pool->map, pool->mmap_size);
240 fio_mutex_remove(pool->lock);
247 for (i = 0; i < nr_pools; i++)
248 cleanup_pool(&mp[i]);
251 fio_rwlock_remove(lock);
254 #ifdef SMALLOC_REDZONE
255 static void *postred_ptr(struct block_hdr *hdr)
259 ptr = (uintptr_t) hdr + hdr->size - sizeof(unsigned int);
260 ptr = (ptr + int_mask) & ~int_mask;
265 static void fill_redzone(struct block_hdr *hdr)
267 unsigned int *postred = postred_ptr(hdr);
269 hdr->prered = SMALLOC_PRE_RED;
270 *postred = SMALLOC_POST_RED;
273 static void sfree_check_redzone(struct block_hdr *hdr)
275 unsigned int *postred = postred_ptr(hdr);
277 if (hdr->prered != SMALLOC_PRE_RED) {
278 fprintf(stderr, "smalloc pre redzone destroyed!\n");
279 fprintf(stderr, " ptr=%p, prered=%x, expected %x\n",
280 hdr, hdr->prered, SMALLOC_PRE_RED);
283 if (*postred != SMALLOC_POST_RED) {
284 fprintf(stderr, "smalloc post redzone destroyed!\n");
285 fprintf(stderr, " ptr=%p, postred=%x, expected %x\n",
286 hdr, *postred, SMALLOC_POST_RED);
291 static void fill_redzone(struct block_hdr *hdr)
295 static void sfree_check_redzone(struct block_hdr *hdr)
300 static void sfree_pool(struct pool *pool, void *ptr)
302 struct block_hdr *hdr;
304 unsigned long offset;
312 assert(ptr_valid(pool, ptr));
314 sfree_check_redzone(hdr);
316 offset = ptr - pool->map;
317 i = offset / SMALLOC_BPL;
318 idx = (offset % SMALLOC_BPL) / SMALLOC_BPB;
321 clear_blocks(pool, i, idx, size_to_blocks(hdr->size));
322 if (i < pool->next_non_full)
323 pool->next_non_full = i;
324 pool->free_blocks += size_to_blocks(hdr->size);
328 void sfree(void *ptr)
330 struct pool *pool = NULL;
338 for (i = 0; i < nr_pools; i++) {
339 if (ptr_valid(&mp[i], ptr)) {
345 global_read_unlock();
348 sfree_pool(pool, ptr);
351 static void *__smalloc_pool(struct pool *pool, size_t size)
356 unsigned int last_idx;
361 nr_blocks = size_to_blocks(size);
362 if (nr_blocks > pool->free_blocks)
365 i = pool->next_non_full;
368 while (i < pool->nr_blocks) {
371 if (pool->bitmap[i] == -1U) {
373 pool->next_non_full = i;
378 idx = find_next_zero(pool->bitmap[i], last_idx);
379 if (!blocks_free(pool, i, idx, nr_blocks)) {
381 if (idx < SMALLOC_BPI)
385 while (idx >= SMALLOC_BPI) {
392 set_blocks(pool, i, idx, nr_blocks);
393 offset = i * SMALLOC_BPL + idx * SMALLOC_BPB;
397 if (i < pool->nr_blocks) {
398 pool->free_blocks -= nr_blocks;
399 ret = pool->map + offset;
406 static void *smalloc_pool(struct pool *pool, size_t size)
408 size_t alloc_size = size + sizeof(struct block_hdr);
412 * Round to int alignment, so that the postred pointer will
413 * be naturally aligned as well.
415 #ifdef SMALLOC_REDZONE
416 alloc_size += sizeof(unsigned int);
417 alloc_size = (alloc_size + int_mask) & ~int_mask;
420 ptr = __smalloc_pool(pool, alloc_size);
422 struct block_hdr *hdr = ptr;
424 hdr->size = alloc_size;
428 memset(ptr, 0, size);
434 void *smalloc(size_t size)
438 if (size != (unsigned int) size)
445 for (; i < nr_pools; i++) {
446 void *ptr = smalloc_pool(&mp[i], size);
450 global_write_unlock();
459 if (nr_pools + 1 > MAX_POOLS)
463 if (add_pool(&mp[nr_pools], size))
469 global_write_unlock();
473 char *smalloc_strdup(const char *str)
477 ptr = smalloc(strlen(str) + 1);