2 * simple memory allocator, backed by mmap() so that it hands out memory
3 * that can be shared across processes and threads
11 #include <sys/types.h>
16 #include "arch/arch.h"
19 #define SMALLOC_REDZONE /* define to detect memory corruption */
21 #define SMALLOC_BPB 32 /* block size, bytes-per-bit in bitmap */
22 #define SMALLOC_BPI (sizeof(unsigned int) * 8)
23 #define SMALLOC_BPL (SMALLOC_BPB * SMALLOC_BPI)
25 #define INITIAL_SIZE 8192*1024 /* new pool size */
26 #define MAX_POOLS 128 /* maximum number of pools to setup */
28 #define SMALLOC_PRE_RED 0xdeadbeefU
29 #define SMALLOC_POST_RED 0x5aa55aa5U
31 unsigned int smalloc_pool_size = INITIAL_SIZE;
32 const int int_mask = sizeof(int) - 1;
35 struct fio_mutex *lock; /* protects this pool */
36 void *map; /* map of blocks */
37 unsigned int *bitmap; /* blocks free/busy map */
38 unsigned int free_blocks; /* free blocks */
39 unsigned int nr_blocks; /* total blocks */
40 unsigned int next_non_full;
41 unsigned int mmap_size;
46 #ifdef SMALLOC_REDZONE
51 static struct pool mp[MAX_POOLS];
52 static unsigned int nr_pools;
53 static unsigned int last_pool;
54 static struct fio_mutex *lock;
56 static inline void pool_lock(struct pool *pool)
58 fio_mutex_down(pool->lock);
61 static inline void pool_unlock(struct pool *pool)
63 fio_mutex_up(pool->lock);
66 static inline void global_read_lock(void)
68 fio_mutex_down_read(lock);
71 static inline void global_read_unlock(void)
73 fio_mutex_up_read(lock);
76 static inline void global_write_lock(void)
78 fio_mutex_down_write(lock);
81 static inline void global_write_unlock(void)
83 fio_mutex_up_write(lock);
86 static inline int ptr_valid(struct pool *pool, void *ptr)
88 unsigned int pool_size = pool->nr_blocks * SMALLOC_BPL;
90 return (ptr >= pool->map) && (ptr < pool->map + pool_size);
93 static inline unsigned int size_to_blocks(unsigned int size)
95 return (size + SMALLOC_BPB - 1) / SMALLOC_BPB;
98 static int blocks_iter(struct pool *pool, unsigned int pool_idx,
99 unsigned int idx, unsigned int nr_blocks,
100 int (*func)(unsigned int *map, unsigned int mask))
104 unsigned int this_blocks, mask;
107 if (pool_idx >= pool->nr_blocks)
110 map = &pool->bitmap[pool_idx];
112 this_blocks = nr_blocks;
113 if (this_blocks + idx > SMALLOC_BPI) {
114 this_blocks = SMALLOC_BPI - idx;
115 idx = SMALLOC_BPI - this_blocks;
118 if (this_blocks == SMALLOC_BPI)
121 mask = ((1U << this_blocks) - 1) << idx;
123 if (!func(map, mask))
126 nr_blocks -= this_blocks;
134 static int mask_cmp(unsigned int *map, unsigned int mask)
136 return !(*map & mask);
139 static int mask_clear(unsigned int *map, unsigned int mask)
141 assert((*map & mask) == mask);
146 static int mask_set(unsigned int *map, unsigned int mask)
148 assert(!(*map & mask));
153 static int blocks_free(struct pool *pool, unsigned int pool_idx,
154 unsigned int idx, unsigned int nr_blocks)
156 return blocks_iter(pool, pool_idx, idx, nr_blocks, mask_cmp);
159 static void set_blocks(struct pool *pool, unsigned int pool_idx,
160 unsigned int idx, unsigned int nr_blocks)
162 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_set);
165 static void clear_blocks(struct pool *pool, unsigned int pool_idx,
166 unsigned int idx, unsigned int nr_blocks)
168 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_clear);
171 static int find_next_zero(int word, int start)
174 word >>= (start + 1);
175 return ffz(word) + start + 1;
178 static int add_pool(struct pool *pool, unsigned int alloc_size)
183 #ifdef SMALLOC_REDZONE
184 alloc_size += sizeof(unsigned int);
186 alloc_size += sizeof(struct block_hdr);
187 if (alloc_size < INITIAL_SIZE)
188 alloc_size = INITIAL_SIZE;
190 /* round up to nearest full number of blocks */
191 alloc_size = (alloc_size + SMALLOC_BPL - 1) & ~(SMALLOC_BPL - 1);
192 bitmap_blocks = alloc_size / SMALLOC_BPL;
193 alloc_size += bitmap_blocks * sizeof(unsigned int);
194 pool->mmap_size = alloc_size;
196 pool->nr_blocks = bitmap_blocks;
197 pool->free_blocks = bitmap_blocks * SMALLOC_BPB;
199 ptr = mmap(NULL, alloc_size, PROT_READ|PROT_WRITE,
200 MAP_SHARED | OS_MAP_ANON, -1, 0);
201 if (ptr == MAP_FAILED)
204 memset(ptr, 0, alloc_size);
206 pool->bitmap = (void *) ptr + (pool->nr_blocks * SMALLOC_BPL);
208 pool->lock = fio_mutex_init(1);
215 fprintf(stderr, "smalloc: failed adding pool\n");
217 munmap(pool->map, pool->mmap_size);
225 lock = fio_mutex_rw_init();
226 ret = add_pool(&mp[0], INITIAL_SIZE);
230 static void cleanup_pool(struct pool *pool)
233 * This will also remove the temporary file we used as a backing
234 * store, it was already unlinked
236 munmap(pool->map, pool->mmap_size);
239 fio_mutex_remove(pool->lock);
246 for (i = 0; i < nr_pools; i++)
247 cleanup_pool(&mp[i]);
250 fio_mutex_remove(lock);
253 #ifdef SMALLOC_REDZONE
254 static void *postred_ptr(struct block_hdr *hdr)
258 ptr = (unsigned long) hdr + hdr->size - sizeof(unsigned int);
259 ptr = (ptr + int_mask) & ~int_mask;
264 static void fill_redzone(struct block_hdr *hdr)
266 unsigned int *postred = postred_ptr(hdr);
268 hdr->prered = SMALLOC_PRE_RED;
269 *postred = SMALLOC_POST_RED;
272 static void sfree_check_redzone(struct block_hdr *hdr)
274 unsigned int *postred = postred_ptr(hdr);
276 if (hdr->prered != SMALLOC_PRE_RED) {
277 fprintf(stderr, "smalloc pre redzone destroyed!\n");
278 fprintf(stderr, " ptr=%p, prered=%x, expected %x\n",
279 hdr, hdr->prered, SMALLOC_PRE_RED);
282 if (*postred != SMALLOC_POST_RED) {
283 fprintf(stderr, "smalloc post redzone destroyed!\n");
284 fprintf(stderr, " ptr=%p, postred=%x, expected %x\n",
285 hdr, *postred, SMALLOC_POST_RED);
290 static void fill_redzone(struct block_hdr *hdr)
294 static void sfree_check_redzone(struct block_hdr *hdr)
299 static void sfree_pool(struct pool *pool, void *ptr)
301 struct block_hdr *hdr;
303 unsigned long offset;
311 assert(ptr_valid(pool, ptr));
313 sfree_check_redzone(hdr);
315 offset = ptr - pool->map;
316 i = offset / SMALLOC_BPL;
317 idx = (offset % SMALLOC_BPL) / SMALLOC_BPB;
320 clear_blocks(pool, i, idx, size_to_blocks(hdr->size));
321 if (i < pool->next_non_full)
322 pool->next_non_full = i;
323 pool->free_blocks += size_to_blocks(hdr->size);
327 void sfree(void *ptr)
329 struct pool *pool = NULL;
337 for (i = 0; i < nr_pools; i++) {
338 if (ptr_valid(&mp[i], ptr)) {
344 global_read_unlock();
347 sfree_pool(pool, ptr);
350 static void *__smalloc_pool(struct pool *pool, unsigned int size)
352 unsigned int nr_blocks;
355 unsigned int last_idx;
360 nr_blocks = size_to_blocks(size);
361 if (nr_blocks > pool->free_blocks)
364 i = pool->next_non_full;
367 while (i < pool->nr_blocks) {
370 if (pool->bitmap[i] == -1U) {
372 pool->next_non_full = i;
377 idx = find_next_zero(pool->bitmap[i], last_idx);
378 if (!blocks_free(pool, i, idx, nr_blocks)) {
380 if (idx < SMALLOC_BPI)
384 while (idx >= SMALLOC_BPI) {
391 set_blocks(pool, i, idx, nr_blocks);
392 offset = i * SMALLOC_BPL + idx * SMALLOC_BPB;
396 if (i < pool->nr_blocks) {
397 pool->free_blocks -= nr_blocks;
398 ret = pool->map + offset;
405 static void *smalloc_pool(struct pool *pool, unsigned int size)
407 unsigned int alloc_size = size + sizeof(struct block_hdr);
411 * Round to int alignment, so that the postred pointer will
412 * be naturally aligned as well.
414 #ifdef SMALLOC_REDZONE
415 alloc_size += sizeof(unsigned int);
416 alloc_size = (alloc_size + int_mask) & ~int_mask;
419 ptr = __smalloc_pool(pool, alloc_size);
421 struct block_hdr *hdr = ptr;
423 hdr->size = alloc_size;
427 memset(ptr, 0, size);
433 void *smalloc(unsigned int size)
441 for (; i < nr_pools; i++) {
442 void *ptr = smalloc_pool(&mp[i], size);
446 global_write_unlock();
455 if (nr_pools + 1 > MAX_POOLS)
459 if (add_pool(&mp[nr_pools], size))
465 global_write_unlock();
469 char *smalloc_strdup(const char *str)
473 ptr = smalloc(strlen(str) + 1);