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 INITIAL_POOLS 8 /* maximum number of pools to setup */
33 #define SMALLOC_PRE_RED 0xdeadbeefU
34 #define SMALLOC_POST_RED 0x5aa55aa5U
36 unsigned int smalloc_pool_size = INITIAL_SIZE;
37 #ifdef SMALLOC_REDZONE
38 static const int int_mask = sizeof(int) - 1;
42 struct fio_mutex *lock; /* protects this pool */
43 void *map; /* map of blocks */
44 unsigned int *bitmap; /* blocks free/busy map */
45 size_t free_blocks; /* free blocks */
46 size_t nr_blocks; /* total blocks */
53 #ifdef SMALLOC_REDZONE
58 static struct pool mp[MAX_POOLS];
59 static unsigned int nr_pools;
60 static unsigned int last_pool;
62 static inline int ptr_valid(struct pool *pool, void *ptr)
64 unsigned int pool_size = pool->nr_blocks * SMALLOC_BPL;
66 return (ptr >= pool->map) && (ptr < pool->map + pool_size);
69 static inline size_t size_to_blocks(size_t size)
71 return (size + SMALLOC_BPB - 1) / SMALLOC_BPB;
74 static int blocks_iter(struct pool *pool, unsigned int pool_idx,
75 unsigned int idx, size_t nr_blocks,
76 int (*func)(unsigned int *map, unsigned int mask))
80 unsigned int this_blocks, mask;
83 if (pool_idx >= pool->nr_blocks)
86 map = &pool->bitmap[pool_idx];
88 this_blocks = nr_blocks;
89 if (this_blocks + idx > SMALLOC_BPI) {
90 this_blocks = SMALLOC_BPI - idx;
91 idx = SMALLOC_BPI - this_blocks;
94 if (this_blocks == SMALLOC_BPI)
97 mask = ((1U << this_blocks) - 1) << idx;
102 nr_blocks -= this_blocks;
110 static int mask_cmp(unsigned int *map, unsigned int mask)
112 return !(*map & mask);
115 static int mask_clear(unsigned int *map, unsigned int mask)
117 assert((*map & mask) == mask);
122 static int mask_set(unsigned int *map, unsigned int mask)
124 assert(!(*map & mask));
129 static int blocks_free(struct pool *pool, unsigned int pool_idx,
130 unsigned int idx, size_t nr_blocks)
132 return blocks_iter(pool, pool_idx, idx, nr_blocks, mask_cmp);
135 static void set_blocks(struct pool *pool, unsigned int pool_idx,
136 unsigned int idx, size_t nr_blocks)
138 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_set);
141 static void clear_blocks(struct pool *pool, unsigned int pool_idx,
142 unsigned int idx, size_t nr_blocks)
144 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_clear);
147 static int find_next_zero(int word, int start)
151 return ffz(word) + start;
154 static bool add_pool(struct pool *pool, unsigned int alloc_size)
160 if (nr_pools == MAX_POOLS)
163 #ifdef SMALLOC_REDZONE
164 alloc_size += sizeof(unsigned int);
166 alloc_size += sizeof(struct block_hdr);
167 if (alloc_size < INITIAL_SIZE)
168 alloc_size = INITIAL_SIZE;
170 /* round up to nearest full number of blocks */
171 alloc_size = (alloc_size + SMALLOC_BPL - 1) & ~(SMALLOC_BPL - 1);
172 bitmap_blocks = alloc_size / SMALLOC_BPL;
173 alloc_size += bitmap_blocks * sizeof(unsigned int);
174 pool->mmap_size = alloc_size;
176 pool->nr_blocks = bitmap_blocks;
177 pool->free_blocks = bitmap_blocks * SMALLOC_BPB;
179 mmap_flags = OS_MAP_ANON;
181 mmap_flags |= MAP_PRIVATE;
183 mmap_flags |= MAP_SHARED;
185 ptr = mmap(NULL, alloc_size, PROT_READ|PROT_WRITE, mmap_flags, -1, 0);
187 if (ptr == MAP_FAILED)
191 pool->bitmap = (void *) ptr + (pool->nr_blocks * SMALLOC_BPL);
192 memset(pool->bitmap, 0, bitmap_blocks * sizeof(unsigned int));
194 pool->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
201 log_err("smalloc: failed adding pool\n");
203 munmap(pool->map, pool->mmap_size);
212 for (i = 0; i < INITIAL_POOLS; i++) {
213 ret = add_pool(&mp[nr_pools], smalloc_pool_size);
219 * If we added at least one pool, we should be OK for most
225 static void cleanup_pool(struct pool *pool)
228 * This will also remove the temporary file we used as a backing
229 * store, it was already unlinked
231 munmap(pool->map, pool->mmap_size);
234 fio_mutex_remove(pool->lock);
241 for (i = 0; i < nr_pools; i++)
242 cleanup_pool(&mp[i]);
245 #ifdef SMALLOC_REDZONE
246 static void *postred_ptr(struct block_hdr *hdr)
250 ptr = (uintptr_t) hdr + hdr->size - sizeof(unsigned int);
251 ptr = (ptr + int_mask) & ~int_mask;
256 static void fill_redzone(struct block_hdr *hdr)
258 unsigned int *postred = postred_ptr(hdr);
260 hdr->prered = SMALLOC_PRE_RED;
261 *postred = SMALLOC_POST_RED;
264 static void sfree_check_redzone(struct block_hdr *hdr)
266 unsigned int *postred = postred_ptr(hdr);
268 if (hdr->prered != SMALLOC_PRE_RED) {
269 log_err("smalloc pre redzone destroyed!\n"
270 " ptr=%p, prered=%x, expected %x\n",
271 hdr, hdr->prered, SMALLOC_PRE_RED);
274 if (*postred != SMALLOC_POST_RED) {
275 log_err("smalloc post redzone destroyed!\n"
276 " ptr=%p, postred=%x, expected %x\n",
277 hdr, *postred, SMALLOC_POST_RED);
282 static void fill_redzone(struct block_hdr *hdr)
286 static void sfree_check_redzone(struct block_hdr *hdr)
291 static void sfree_pool(struct pool *pool, void *ptr)
293 struct block_hdr *hdr;
295 unsigned long offset;
303 assert(ptr_valid(pool, ptr));
305 sfree_check_redzone(hdr);
307 offset = ptr - pool->map;
308 i = offset / SMALLOC_BPL;
309 idx = (offset % SMALLOC_BPL) / SMALLOC_BPB;
311 fio_mutex_down(pool->lock);
312 clear_blocks(pool, i, idx, size_to_blocks(hdr->size));
313 if (i < pool->next_non_full)
314 pool->next_non_full = i;
315 pool->free_blocks += size_to_blocks(hdr->size);
316 fio_mutex_up(pool->lock);
319 void sfree(void *ptr)
321 struct pool *pool = NULL;
327 for (i = 0; i < nr_pools; i++) {
328 if (ptr_valid(&mp[i], ptr)) {
335 sfree_pool(pool, ptr);
339 log_err("smalloc: ptr %p not from smalloc pool\n", ptr);
342 static void *__smalloc_pool(struct pool *pool, size_t size)
347 unsigned int last_idx;
350 fio_mutex_down(pool->lock);
352 nr_blocks = size_to_blocks(size);
353 if (nr_blocks > pool->free_blocks)
356 i = pool->next_non_full;
359 while (i < pool->nr_blocks) {
362 if (pool->bitmap[i] == -1U) {
364 pool->next_non_full = i;
369 idx = find_next_zero(pool->bitmap[i], last_idx);
370 if (!blocks_free(pool, i, idx, nr_blocks)) {
372 if (idx < SMALLOC_BPI)
376 while (idx >= SMALLOC_BPI) {
383 set_blocks(pool, i, idx, nr_blocks);
384 offset = i * SMALLOC_BPL + idx * SMALLOC_BPB;
388 if (i < pool->nr_blocks) {
389 pool->free_blocks -= nr_blocks;
390 ret = pool->map + offset;
393 fio_mutex_up(pool->lock);
397 static void *smalloc_pool(struct pool *pool, size_t size)
399 size_t alloc_size = size + sizeof(struct block_hdr);
403 * Round to int alignment, so that the postred pointer will
404 * be naturally aligned as well.
406 #ifdef SMALLOC_REDZONE
407 alloc_size += sizeof(unsigned int);
408 alloc_size = (alloc_size + int_mask) & ~int_mask;
411 ptr = __smalloc_pool(pool, alloc_size);
413 struct block_hdr *hdr = ptr;
415 hdr->size = alloc_size;
419 memset(ptr, 0, size);
425 void *smalloc(size_t size)
427 unsigned int i, end_pool;
429 if (size != (unsigned int) size)
436 for (; i < end_pool; i++) {
437 void *ptr = smalloc_pool(&mp[i], size);
445 end_pool = last_pool;
453 log_err("smalloc: OOM. Consider using --alloc-size to increase the "
454 "shared memory available.\n");
458 void *scalloc(size_t nmemb, size_t size)
460 return smalloc(nmemb * size);
463 char *smalloc_strdup(const char *str)
467 ptr = smalloc(strlen(str) + 1);