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 #ifdef SMALLOC_REDZONE
36 static const int int_mask = sizeof(int) - 1;
40 struct fio_mutex *lock; /* protects this pool */
41 void *map; /* map of blocks */
42 unsigned int *bitmap; /* blocks free/busy map */
43 size_t free_blocks; /* free blocks */
44 size_t nr_blocks; /* total blocks */
51 #ifdef SMALLOC_REDZONE
56 static struct pool mp[MAX_POOLS];
57 static unsigned int nr_pools;
58 static unsigned int last_pool;
60 static inline int ptr_valid(struct pool *pool, void *ptr)
62 unsigned int pool_size = pool->nr_blocks * SMALLOC_BPL;
64 return (ptr >= pool->map) && (ptr < pool->map + pool_size);
67 static inline size_t size_to_blocks(size_t size)
69 return (size + SMALLOC_BPB - 1) / SMALLOC_BPB;
72 static int blocks_iter(struct pool *pool, unsigned int pool_idx,
73 unsigned int idx, size_t nr_blocks,
74 int (*func)(unsigned int *map, unsigned int mask))
78 unsigned int this_blocks, mask;
81 if (pool_idx >= pool->nr_blocks)
84 map = &pool->bitmap[pool_idx];
86 this_blocks = nr_blocks;
87 if (this_blocks + idx > SMALLOC_BPI) {
88 this_blocks = SMALLOC_BPI - idx;
89 idx = SMALLOC_BPI - this_blocks;
92 if (this_blocks == SMALLOC_BPI)
95 mask = ((1U << this_blocks) - 1) << idx;
100 nr_blocks -= this_blocks;
108 static int mask_cmp(unsigned int *map, unsigned int mask)
110 return !(*map & mask);
113 static int mask_clear(unsigned int *map, unsigned int mask)
115 assert((*map & mask) == mask);
120 static int mask_set(unsigned int *map, unsigned int mask)
122 assert(!(*map & mask));
127 static int blocks_free(struct pool *pool, unsigned int pool_idx,
128 unsigned int idx, size_t nr_blocks)
130 return blocks_iter(pool, pool_idx, idx, nr_blocks, mask_cmp);
133 static void set_blocks(struct pool *pool, unsigned int pool_idx,
134 unsigned int idx, size_t nr_blocks)
136 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_set);
139 static void clear_blocks(struct pool *pool, unsigned int pool_idx,
140 unsigned int idx, size_t nr_blocks)
142 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_clear);
145 static int find_next_zero(int word, int start)
149 return ffz(word) + start;
152 static int add_pool(struct pool *pool, unsigned int alloc_size)
158 #ifdef SMALLOC_REDZONE
159 alloc_size += sizeof(unsigned int);
161 alloc_size += sizeof(struct block_hdr);
162 if (alloc_size < INITIAL_SIZE)
163 alloc_size = INITIAL_SIZE;
165 /* round up to nearest full number of blocks */
166 alloc_size = (alloc_size + SMALLOC_BPL - 1) & ~(SMALLOC_BPL - 1);
167 bitmap_blocks = alloc_size / SMALLOC_BPL;
168 alloc_size += bitmap_blocks * sizeof(unsigned int);
169 pool->mmap_size = alloc_size;
171 pool->nr_blocks = bitmap_blocks;
172 pool->free_blocks = bitmap_blocks * SMALLOC_BPB;
174 mmap_flags = OS_MAP_ANON;
176 mmap_flags |= MAP_PRIVATE;
178 mmap_flags |= MAP_SHARED;
180 ptr = mmap(NULL, alloc_size, PROT_READ|PROT_WRITE, mmap_flags, -1, 0);
182 if (ptr == MAP_FAILED)
186 pool->bitmap = (void *) ptr + (pool->nr_blocks * SMALLOC_BPL);
187 memset(pool->bitmap, 0, bitmap_blocks * sizeof(unsigned int));
189 pool->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
196 log_err("smalloc: failed adding pool\n");
198 munmap(pool->map, pool->mmap_size);
206 for (i = 0; i < MAX_POOLS; i++) {
207 ret = add_pool(&mp[i], smalloc_pool_size);
213 * If we added at least one pool, we should be OK for most
219 static void cleanup_pool(struct pool *pool)
222 * This will also remove the temporary file we used as a backing
223 * store, it was already unlinked
225 munmap(pool->map, pool->mmap_size);
228 fio_mutex_remove(pool->lock);
235 for (i = 0; i < nr_pools; i++)
236 cleanup_pool(&mp[i]);
239 #ifdef SMALLOC_REDZONE
240 static void *postred_ptr(struct block_hdr *hdr)
244 ptr = (uintptr_t) hdr + hdr->size - sizeof(unsigned int);
245 ptr = (ptr + int_mask) & ~int_mask;
250 static void fill_redzone(struct block_hdr *hdr)
252 unsigned int *postred = postred_ptr(hdr);
254 hdr->prered = SMALLOC_PRE_RED;
255 *postred = SMALLOC_POST_RED;
258 static void sfree_check_redzone(struct block_hdr *hdr)
260 unsigned int *postred = postred_ptr(hdr);
262 if (hdr->prered != SMALLOC_PRE_RED) {
263 log_err("smalloc pre redzone destroyed!\n"
264 " ptr=%p, prered=%x, expected %x\n",
265 hdr, hdr->prered, SMALLOC_PRE_RED);
268 if (*postred != SMALLOC_POST_RED) {
269 log_err("smalloc post redzone destroyed!\n"
270 " ptr=%p, postred=%x, expected %x\n",
271 hdr, *postred, SMALLOC_POST_RED);
276 static void fill_redzone(struct block_hdr *hdr)
280 static void sfree_check_redzone(struct block_hdr *hdr)
285 static void sfree_pool(struct pool *pool, void *ptr)
287 struct block_hdr *hdr;
289 unsigned long offset;
297 assert(ptr_valid(pool, ptr));
299 sfree_check_redzone(hdr);
301 offset = ptr - pool->map;
302 i = offset / SMALLOC_BPL;
303 idx = (offset % SMALLOC_BPL) / SMALLOC_BPB;
305 fio_mutex_down(pool->lock);
306 clear_blocks(pool, i, idx, size_to_blocks(hdr->size));
307 if (i < pool->next_non_full)
308 pool->next_non_full = i;
309 pool->free_blocks += size_to_blocks(hdr->size);
310 fio_mutex_up(pool->lock);
313 void sfree(void *ptr)
315 struct pool *pool = NULL;
321 for (i = 0; i < nr_pools; i++) {
322 if (ptr_valid(&mp[i], ptr)) {
329 sfree_pool(pool, ptr);
333 log_err("smalloc: ptr %p not from smalloc pool\n", ptr);
336 static void *__smalloc_pool(struct pool *pool, size_t size)
341 unsigned int last_idx;
344 fio_mutex_down(pool->lock);
346 nr_blocks = size_to_blocks(size);
347 if (nr_blocks > pool->free_blocks)
350 i = pool->next_non_full;
353 while (i < pool->nr_blocks) {
356 if (pool->bitmap[i] == -1U) {
358 pool->next_non_full = i;
363 idx = find_next_zero(pool->bitmap[i], last_idx);
364 if (!blocks_free(pool, i, idx, nr_blocks)) {
366 if (idx < SMALLOC_BPI)
370 while (idx >= SMALLOC_BPI) {
377 set_blocks(pool, i, idx, nr_blocks);
378 offset = i * SMALLOC_BPL + idx * SMALLOC_BPB;
382 if (i < pool->nr_blocks) {
383 pool->free_blocks -= nr_blocks;
384 ret = pool->map + offset;
387 fio_mutex_up(pool->lock);
391 static void *smalloc_pool(struct pool *pool, size_t size)
393 size_t alloc_size = size + sizeof(struct block_hdr);
397 * Round to int alignment, so that the postred pointer will
398 * be naturally aligned as well.
400 #ifdef SMALLOC_REDZONE
401 alloc_size += sizeof(unsigned int);
402 alloc_size = (alloc_size + int_mask) & ~int_mask;
405 ptr = __smalloc_pool(pool, alloc_size);
407 struct block_hdr *hdr = ptr;
409 hdr->size = alloc_size;
413 memset(ptr, 0, size);
419 void *smalloc(size_t size)
421 unsigned int i, end_pool;
423 if (size != (unsigned int) size)
430 for (; i < end_pool; i++) {
431 void *ptr = smalloc_pool(&mp[i], size);
439 end_pool = last_pool;
450 void *scalloc(size_t nmemb, size_t size)
452 return smalloc(nmemb * size);
455 char *smalloc_strdup(const char *str)
459 ptr = smalloc(strlen(str) + 1);