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"
22 #define SMALLOC_REDZONE /* define to detect memory corruption */
24 #define SMALLOC_BPB 32 /* block size, bytes-per-bit in bitmap */
25 #define SMALLOC_BPI (sizeof(unsigned int) * 8)
26 #define SMALLOC_BPL (SMALLOC_BPB * SMALLOC_BPI)
28 #define INITIAL_SIZE 16*1024*1024 /* new pool size */
29 #define MAX_POOLS 8 /* maximum number of pools to setup */
31 #define SMALLOC_PRE_RED 0xdeadbeefU
32 #define SMALLOC_POST_RED 0x5aa55aa5U
34 unsigned int smalloc_pool_size = INITIAL_SIZE;
35 static const int int_mask = sizeof(int) - 1;
38 struct fio_mutex *lock; /* protects this pool */
39 void *map; /* map of blocks */
40 unsigned int *bitmap; /* blocks free/busy map */
41 size_t free_blocks; /* free blocks */
42 size_t nr_blocks; /* total blocks */
49 #ifdef SMALLOC_REDZONE
54 static struct pool mp[MAX_POOLS];
55 static unsigned int nr_pools;
56 static unsigned int last_pool;
57 static struct fio_rwlock *lock;
59 static inline void pool_lock(struct pool *pool)
61 fio_mutex_down(pool->lock);
64 static inline void pool_unlock(struct pool *pool)
66 fio_mutex_up(pool->lock);
69 static inline void global_read_lock(void)
71 fio_rwlock_read(lock);
74 static inline void global_read_unlock(void)
76 fio_rwlock_unlock(lock);
79 static inline void global_write_lock(void)
81 fio_rwlock_write(lock);
84 static inline void global_write_unlock(void)
86 fio_rwlock_unlock(lock);
89 static inline int ptr_valid(struct pool *pool, void *ptr)
91 unsigned int pool_size = pool->nr_blocks * SMALLOC_BPL;
93 return (ptr >= pool->map) && (ptr < pool->map + pool_size);
96 static inline size_t size_to_blocks(size_t size)
98 return (size + SMALLOC_BPB - 1) / SMALLOC_BPB;
101 static int blocks_iter(struct pool *pool, unsigned int pool_idx,
102 unsigned int idx, size_t nr_blocks,
103 int (*func)(unsigned int *map, unsigned int mask))
107 unsigned int this_blocks, mask;
110 if (pool_idx >= pool->nr_blocks)
113 map = &pool->bitmap[pool_idx];
115 this_blocks = nr_blocks;
116 if (this_blocks + idx > SMALLOC_BPI) {
117 this_blocks = SMALLOC_BPI - idx;
118 idx = SMALLOC_BPI - this_blocks;
121 if (this_blocks == SMALLOC_BPI)
124 mask = ((1U << this_blocks) - 1) << idx;
126 if (!func(map, mask))
129 nr_blocks -= this_blocks;
137 static int mask_cmp(unsigned int *map, unsigned int mask)
139 return !(*map & mask);
142 static int mask_clear(unsigned int *map, unsigned int mask)
144 assert((*map & mask) == mask);
149 static int mask_set(unsigned int *map, unsigned int mask)
151 assert(!(*map & mask));
156 static int blocks_free(struct pool *pool, unsigned int pool_idx,
157 unsigned int idx, size_t nr_blocks)
159 return blocks_iter(pool, pool_idx, idx, nr_blocks, mask_cmp);
162 static void set_blocks(struct pool *pool, unsigned int pool_idx,
163 unsigned int idx, size_t nr_blocks)
165 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_set);
168 static void clear_blocks(struct pool *pool, unsigned int pool_idx,
169 unsigned int idx, size_t nr_blocks)
171 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_clear);
174 static int find_next_zero(int word, int start)
178 return ffz(word) + start;
181 static int add_pool(struct pool *pool, unsigned int alloc_size)
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 mmap_flags = OS_MAP_ANON;
205 mmap_flags |= MAP_PRIVATE;
207 mmap_flags |= MAP_SHARED;
209 ptr = mmap(NULL, alloc_size, PROT_READ|PROT_WRITE, mmap_flags, -1, 0);
211 if (ptr == MAP_FAILED)
214 memset(ptr, 0, alloc_size);
216 pool->bitmap = (void *) ptr + (pool->nr_blocks * SMALLOC_BPL);
218 pool->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
225 log_err("smalloc: failed adding pool\n");
227 munmap(pool->map, pool->mmap_size);
235 lock = fio_rwlock_init();
237 for (i = 0; i < MAX_POOLS; i++) {
238 ret = add_pool(&mp[i], INITIAL_SIZE);
244 * If we added at least one pool, we should be OK for most
250 static void cleanup_pool(struct pool *pool)
253 * This will also remove the temporary file we used as a backing
254 * store, it was already unlinked
256 munmap(pool->map, pool->mmap_size);
259 fio_mutex_remove(pool->lock);
266 for (i = 0; i < nr_pools; i++)
267 cleanup_pool(&mp[i]);
270 fio_rwlock_remove(lock);
273 #ifdef SMALLOC_REDZONE
274 static void *postred_ptr(struct block_hdr *hdr)
278 ptr = (uintptr_t) hdr + hdr->size - sizeof(unsigned int);
279 ptr = (ptr + int_mask) & ~int_mask;
284 static void fill_redzone(struct block_hdr *hdr)
286 unsigned int *postred = postred_ptr(hdr);
288 hdr->prered = SMALLOC_PRE_RED;
289 *postred = SMALLOC_POST_RED;
292 static void sfree_check_redzone(struct block_hdr *hdr)
294 unsigned int *postred = postred_ptr(hdr);
296 if (hdr->prered != SMALLOC_PRE_RED) {
297 log_err("smalloc pre redzone destroyed!\n"
298 " ptr=%p, prered=%x, expected %x\n",
299 hdr, hdr->prered, SMALLOC_PRE_RED);
302 if (*postred != SMALLOC_POST_RED) {
303 log_err("smalloc post redzone destroyed!\n"
304 " ptr=%p, postred=%x, expected %x\n",
305 hdr, *postred, SMALLOC_POST_RED);
310 static void fill_redzone(struct block_hdr *hdr)
314 static void sfree_check_redzone(struct block_hdr *hdr)
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, 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);
371 log_err("smalloc: ptr %p not from smalloc pool\n", ptr);
374 static void *__smalloc_pool(struct pool *pool, size_t size)
379 unsigned int last_idx;
384 nr_blocks = size_to_blocks(size);
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, i, idx, nr_blocks)) {
404 if (idx < SMALLOC_BPI)
408 while (idx >= SMALLOC_BPI) {
415 set_blocks(pool, 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, size_t size)
431 size_t alloc_size = size + sizeof(struct block_hdr);
435 * Round to int alignment, so that the postred pointer will
436 * be naturally aligned as well.
438 #ifdef SMALLOC_REDZONE
439 alloc_size += sizeof(unsigned int);
440 alloc_size = (alloc_size + int_mask) & ~int_mask;
443 ptr = __smalloc_pool(pool, alloc_size);
445 struct block_hdr *hdr = ptr;
447 hdr->size = alloc_size;
451 memset(ptr, 0, size);
457 void *smalloc(size_t size)
459 unsigned int i, end_pool;
461 if (size != (unsigned int) size)
469 for (; i < end_pool; i++) {
470 void *ptr = smalloc_pool(&mp[i], size);
474 global_write_unlock();
479 end_pool = last_pool;
487 global_write_unlock();
491 void *scalloc(size_t nmemb, size_t size)
495 ret = smalloc(nmemb * size);
497 memset(ret, 0, nmemb * size);
502 char *smalloc_strdup(const char *str)
506 ptr = smalloc(strlen(str) + 1);