2 * simple memory allocator, backed by mmap() so that it hands out memory
3 * that can be shared across processes and threads
15 #define SMALLOC_REDZONE /* define to detect memory corruption */
17 #define SMALLOC_BPB 32 /* block size, bytes-per-bit in bitmap */
18 #define SMALLOC_BPI (sizeof(unsigned int) * 8)
19 #define SMALLOC_BPL (SMALLOC_BPB * SMALLOC_BPI)
21 #define INITIAL_SIZE 16*1024*1024 /* new pool size */
22 #define INITIAL_POOLS 8 /* 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 #ifdef SMALLOC_REDZONE
31 static const int int_mask = sizeof(int) - 1;
35 struct fio_sem *lock; /* protects this pool */
36 void *map; /* map of blocks */
37 unsigned int *bitmap; /* blocks free/busy map */
38 size_t free_blocks; /* free blocks */
39 size_t nr_blocks; /* total blocks */
46 #ifdef SMALLOC_REDZONE
51 static struct pool mp[MAX_POOLS];
52 static unsigned int nr_pools;
53 static unsigned int last_pool;
55 static inline int ptr_valid(struct pool *pool, void *ptr)
57 unsigned int pool_size = pool->nr_blocks * SMALLOC_BPL;
59 return (ptr >= pool->map) && (ptr < pool->map + pool_size);
62 static inline size_t size_to_blocks(size_t size)
64 return (size + SMALLOC_BPB - 1) / SMALLOC_BPB;
67 static int blocks_iter(struct pool *pool, unsigned int pool_idx,
68 unsigned int idx, size_t nr_blocks,
69 int (*func)(unsigned int *map, unsigned int mask))
73 unsigned int this_blocks, mask;
76 if (pool_idx >= pool->nr_blocks)
79 map = &pool->bitmap[pool_idx];
81 this_blocks = nr_blocks;
82 if (this_blocks + idx > SMALLOC_BPI) {
83 this_blocks = SMALLOC_BPI - idx;
84 idx = SMALLOC_BPI - this_blocks;
87 if (this_blocks == SMALLOC_BPI)
90 mask = ((1U << this_blocks) - 1) << idx;
95 nr_blocks -= this_blocks;
103 static int mask_cmp(unsigned int *map, unsigned int mask)
105 return !(*map & mask);
108 static int mask_clear(unsigned int *map, unsigned int mask)
110 assert((*map & mask) == mask);
115 static int mask_set(unsigned int *map, unsigned int mask)
117 assert(!(*map & mask));
122 static int blocks_free(struct pool *pool, unsigned int pool_idx,
123 unsigned int idx, size_t nr_blocks)
125 return blocks_iter(pool, pool_idx, idx, nr_blocks, mask_cmp);
128 static void set_blocks(struct pool *pool, unsigned int pool_idx,
129 unsigned int idx, size_t nr_blocks)
131 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_set);
134 static void clear_blocks(struct pool *pool, unsigned int pool_idx,
135 unsigned int idx, size_t nr_blocks)
137 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_clear);
140 static int find_next_zero(int word, int start)
144 return ffz(word) + start;
147 static bool add_pool(struct pool *pool, unsigned int alloc_size)
153 if (nr_pools == MAX_POOLS)
156 #ifdef SMALLOC_REDZONE
157 alloc_size += sizeof(unsigned int);
159 alloc_size += sizeof(struct block_hdr);
160 if (alloc_size < INITIAL_SIZE)
161 alloc_size = INITIAL_SIZE;
163 /* round up to nearest full number of blocks */
164 alloc_size = (alloc_size + SMALLOC_BPL - 1) & ~(SMALLOC_BPL - 1);
165 bitmap_blocks = alloc_size / SMALLOC_BPL;
166 alloc_size += bitmap_blocks * sizeof(unsigned int);
167 pool->mmap_size = alloc_size;
169 pool->nr_blocks = bitmap_blocks;
170 pool->free_blocks = bitmap_blocks * SMALLOC_BPB;
172 mmap_flags = OS_MAP_ANON;
174 mmap_flags |= MAP_PRIVATE;
176 mmap_flags |= MAP_SHARED;
178 ptr = mmap(NULL, alloc_size, PROT_READ|PROT_WRITE, mmap_flags, -1, 0);
180 if (ptr == MAP_FAILED)
184 pool->bitmap = (unsigned int *)((char *) ptr + (pool->nr_blocks * SMALLOC_BPL));
185 memset(pool->bitmap, 0, bitmap_blocks * sizeof(unsigned int));
187 pool->lock = fio_sem_init(FIO_SEM_UNLOCKED);
194 log_err("smalloc: failed adding pool\n");
196 munmap(pool->map, pool->mmap_size);
205 for (i = 0; i < INITIAL_POOLS; i++) {
206 ret = add_pool(&mp[nr_pools], smalloc_pool_size);
212 * If we added at least one pool, we should be OK for most
218 static void cleanup_pool(struct pool *pool)
221 * This will also remove the temporary file we used as a backing
222 * store, it was already unlinked
224 munmap(pool->map, pool->mmap_size);
227 fio_sem_remove(pool->lock);
234 for (i = 0; i < nr_pools; i++)
235 cleanup_pool(&mp[i]);
238 #ifdef SMALLOC_REDZONE
239 static void *postred_ptr(struct block_hdr *hdr)
243 ptr = (uintptr_t) hdr + hdr->size - sizeof(unsigned int);
244 ptr = (uintptr_t) PTR_ALIGN(ptr, int_mask);
249 static void fill_redzone(struct block_hdr *hdr)
251 unsigned int *postred = postred_ptr(hdr);
253 hdr->prered = SMALLOC_PRE_RED;
254 *postred = SMALLOC_POST_RED;
257 static void sfree_check_redzone(struct block_hdr *hdr)
259 unsigned int *postred = postred_ptr(hdr);
261 if (hdr->prered != SMALLOC_PRE_RED) {
262 log_err("smalloc pre redzone destroyed!\n"
263 " ptr=%p, prered=%x, expected %x\n",
264 hdr, hdr->prered, SMALLOC_PRE_RED);
267 if (*postred != SMALLOC_POST_RED) {
268 log_err("smalloc post redzone destroyed!\n"
269 " ptr=%p, postred=%x, expected %x\n",
270 hdr, *postred, SMALLOC_POST_RED);
275 static void fill_redzone(struct block_hdr *hdr)
279 static void sfree_check_redzone(struct block_hdr *hdr)
284 static void sfree_pool(struct pool *pool, void *ptr)
286 struct block_hdr *hdr;
288 unsigned long offset;
296 assert(ptr_valid(pool, ptr));
298 sfree_check_redzone(hdr);
300 offset = ptr - pool->map;
301 i = offset / SMALLOC_BPL;
302 idx = (offset % SMALLOC_BPL) / SMALLOC_BPB;
304 fio_sem_down(pool->lock);
305 clear_blocks(pool, i, idx, size_to_blocks(hdr->size));
306 if (i < pool->next_non_full)
307 pool->next_non_full = i;
308 pool->free_blocks += size_to_blocks(hdr->size);
309 fio_sem_up(pool->lock);
312 void sfree(void *ptr)
314 struct pool *pool = NULL;
320 for (i = 0; i < nr_pools; i++) {
321 if (ptr_valid(&mp[i], ptr)) {
328 sfree_pool(pool, ptr);
332 log_err("smalloc: ptr %p not from smalloc pool\n", ptr);
335 static void *__smalloc_pool(struct pool *pool, size_t size)
340 unsigned int last_idx;
343 fio_sem_down(pool->lock);
345 nr_blocks = size_to_blocks(size);
346 if (nr_blocks > pool->free_blocks)
349 i = pool->next_non_full;
352 while (i < pool->nr_blocks) {
355 if (pool->bitmap[i] == -1U) {
357 pool->next_non_full = i;
362 idx = find_next_zero(pool->bitmap[i], last_idx);
363 if (!blocks_free(pool, i, idx, nr_blocks)) {
365 if (idx < SMALLOC_BPI)
369 while (idx >= SMALLOC_BPI) {
376 set_blocks(pool, i, idx, nr_blocks);
377 offset = i * SMALLOC_BPL + idx * SMALLOC_BPB;
381 if (i < pool->nr_blocks) {
382 pool->free_blocks -= nr_blocks;
383 ret = pool->map + offset;
386 fio_sem_up(pool->lock);
390 static void *smalloc_pool(struct pool *pool, size_t size)
392 size_t alloc_size = size + sizeof(struct block_hdr);
396 * Round to int alignment, so that the postred pointer will
397 * be naturally aligned as well.
399 #ifdef SMALLOC_REDZONE
400 alloc_size += sizeof(unsigned int);
401 alloc_size = (alloc_size + int_mask) & ~int_mask;
404 ptr = __smalloc_pool(pool, alloc_size);
406 struct block_hdr *hdr = ptr;
408 hdr->size = alloc_size;
412 memset(ptr, 0, size);
418 void *smalloc(size_t size)
420 unsigned int i, end_pool;
422 if (size != (unsigned int) size)
429 for (; i < end_pool; i++) {
430 void *ptr = smalloc_pool(&mp[i], size);
438 end_pool = last_pool;
446 log_err("smalloc: OOM. Consider using --alloc-size to increase the "
447 "shared memory available.\n");
451 void *scalloc(size_t nmemb, size_t size)
453 return smalloc(nmemb * size);
456 char *smalloc_strdup(const char *str)
460 ptr = smalloc(strlen(str) + 1);