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>
15 #include "arch/arch.h"
17 #define SMALLOC_REDZONE /* define to detect memory corruption */
19 #define SMALLOC_BPB 32 /* block size, bytes-per-bit in bitmap */
20 #define SMALLOC_BPI (sizeof(unsigned int) * 8)
21 #define SMALLOC_BPL (SMALLOC_BPB * SMALLOC_BPI)
23 #define INITIAL_SIZE 1024*1024 /* new pool size */
24 #define MAX_POOLS 128 /* 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;
32 struct fio_mutex *lock; /* protects this pool */
33 void *map; /* map of blocks */
34 unsigned int *bitmap; /* blocks free/busy map */
35 unsigned int free_blocks; /* free blocks */
36 unsigned int nr_blocks; /* total blocks */
37 unsigned int next_non_full;
38 int fd; /* memory backing fd */
39 unsigned int mmap_size;
44 #ifdef SMALLOC_REDZONE
49 static struct pool mp[MAX_POOLS];
50 static unsigned int nr_pools;
51 static unsigned int last_pool;
52 static struct fio_mutex *lock;
54 static inline void pool_lock(struct pool *pool)
56 fio_mutex_down(pool->lock);
59 static inline void pool_unlock(struct pool *pool)
61 fio_mutex_up(pool->lock);
64 static inline void global_read_lock(void)
66 fio_mutex_down_read(lock);
69 static inline void global_read_unlock(void)
71 fio_mutex_up_read(lock);
74 static inline void global_write_lock(void)
76 fio_mutex_down_write(lock);
79 static inline void global_write_unlock(void)
81 fio_mutex_up_write(lock);
84 static inline int ptr_valid(struct pool *pool, void *ptr)
86 unsigned int pool_size = pool->nr_blocks * SMALLOC_BPL;
88 return (ptr >= pool->map) && (ptr < pool->map + pool_size);
91 static inline unsigned int size_to_blocks(unsigned int size)
93 return (size + SMALLOC_BPB - 1) / SMALLOC_BPB;
96 static int blocks_iter(struct pool *pool, unsigned int pool_idx,
97 unsigned int idx, unsigned int nr_blocks,
98 int (*func)(unsigned int *map, unsigned int mask))
102 unsigned int this_blocks, mask;
105 if (pool_idx >= pool->nr_blocks)
108 map = &pool->bitmap[pool_idx];
110 this_blocks = nr_blocks;
111 if (this_blocks + idx > SMALLOC_BPI) {
112 this_blocks = SMALLOC_BPI - idx;
113 idx = SMALLOC_BPI - this_blocks;
116 if (this_blocks == SMALLOC_BPI)
119 mask = ((1U << this_blocks) - 1) << idx;
121 if (!func(map, mask))
124 nr_blocks -= this_blocks;
132 static int mask_cmp(unsigned int *map, unsigned int mask)
134 return !(*map & mask);
137 static int mask_clear(unsigned int *map, unsigned int mask)
139 assert((*map & mask) == mask);
144 static int mask_set(unsigned int *map, unsigned int mask)
146 assert(!(*map & mask));
151 static int blocks_free(struct pool *pool, unsigned int pool_idx,
152 unsigned int idx, unsigned int nr_blocks)
154 return blocks_iter(pool, pool_idx, idx, nr_blocks, mask_cmp);
157 static void set_blocks(struct pool *pool, unsigned int pool_idx,
158 unsigned int idx, unsigned int nr_blocks)
160 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_set);
163 static void clear_blocks(struct pool *pool, unsigned int pool_idx,
164 unsigned int idx, unsigned int nr_blocks)
166 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_clear);
169 static int find_next_zero(int word, int start)
172 word >>= (start + 1);
173 return ffz(word) + start + 1;
176 static int add_pool(struct pool *pool, unsigned int alloc_size)
178 int fd, bitmap_blocks;
179 char file[] = "/tmp/.fio_smalloc.XXXXXX";
186 #ifdef SMALLOC_REDZONE
187 alloc_size += sizeof(unsigned int);
189 alloc_size += sizeof(struct block_hdr);
190 if (alloc_size < INITIAL_SIZE)
191 alloc_size = INITIAL_SIZE;
193 /* round up to nearest full number of blocks */
194 alloc_size = (alloc_size + SMALLOC_BPL - 1) & ~(SMALLOC_BPL - 1);
195 bitmap_blocks = alloc_size / SMALLOC_BPL;
196 alloc_size += bitmap_blocks * sizeof(unsigned int);
197 pool->mmap_size = alloc_size;
199 pool->nr_blocks = bitmap_blocks;
200 pool->free_blocks = bitmap_blocks * SMALLOC_BPB;
202 if (ftruncate(fd, alloc_size) < 0)
205 ptr = mmap(NULL, alloc_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
206 if (ptr == MAP_FAILED)
209 memset(ptr, 0, alloc_size);
211 pool->bitmap = (void *) ptr + (pool->nr_blocks * SMALLOC_BPL);
213 pool->lock = fio_mutex_init(1);
218 * Unlink pool file now. It wont get deleted until the fd is closed,
219 * which happens both for cleanup or unexpected quit. This way we
220 * don't leave temp files around in case of a crash.
228 fprintf(stderr, "smalloc: failed adding pool\n");
230 munmap(pool->map, pool->mmap_size);
241 lock = fio_mutex_rw_init();
242 ret = add_pool(&mp[0], INITIAL_SIZE);
246 static void cleanup_pool(struct pool *pool)
249 * This will also remove the temporary file we used as a backing
250 * store, it was already unlinked
253 munmap(pool->map, pool->mmap_size);
256 fio_mutex_remove(pool->lock);
263 for (i = 0; i < nr_pools; i++)
264 cleanup_pool(&mp[i]);
267 fio_mutex_remove(lock);
270 #ifdef SMALLOC_REDZONE
271 static void fill_redzone(struct block_hdr *hdr)
273 unsigned int *postred = (void *) hdr + hdr->size - sizeof(unsigned int);
275 hdr->prered = SMALLOC_PRE_RED;
276 *postred = SMALLOC_POST_RED;
279 static void sfree_check_redzone(struct block_hdr *hdr)
281 unsigned int *postred = (void *) hdr + hdr->size - sizeof(unsigned int);
283 if (hdr->prered != SMALLOC_PRE_RED) {
284 fprintf(stderr, "smalloc pre redzone destroyed!\n");
285 fprintf(stderr, " ptr=%p, prered=%x, expected %x\n",
286 hdr, hdr->prered, SMALLOC_PRE_RED);
289 if (*postred != SMALLOC_POST_RED) {
290 fprintf(stderr, "smalloc post redzone destroyed!\n");
291 fprintf(stderr, " ptr=%p, postred=%x, expected %x\n",
292 hdr, *postred, SMALLOC_POST_RED);
297 static void fill_redzone(struct block_hdr *hdr)
301 static void sfree_check_redzone(struct block_hdr *hdr)
306 static void sfree_pool(struct pool *pool, void *ptr)
308 struct block_hdr *hdr;
310 unsigned long offset;
318 assert(ptr_valid(pool, ptr));
320 sfree_check_redzone(hdr);
322 offset = ptr - pool->map;
323 i = offset / SMALLOC_BPL;
324 idx = (offset % SMALLOC_BPL) / SMALLOC_BPB;
327 clear_blocks(pool, i, idx, size_to_blocks(hdr->size));
328 if (i < pool->next_non_full)
329 pool->next_non_full = i;
330 pool->free_blocks += size_to_blocks(hdr->size);
334 void sfree(void *ptr)
336 struct pool *pool = NULL;
344 for (i = 0; i < nr_pools; i++) {
345 if (ptr_valid(&mp[i], ptr)) {
351 global_read_unlock();
354 sfree_pool(pool, ptr);
357 static void *__smalloc_pool(struct pool *pool, unsigned int size)
359 unsigned int nr_blocks;
362 unsigned int last_idx;
367 nr_blocks = size_to_blocks(size);
368 if (nr_blocks > pool->free_blocks)
371 i = pool->next_non_full;
374 while (i < pool->nr_blocks) {
377 if (pool->bitmap[i] == -1U) {
379 pool->next_non_full = i;
384 idx = find_next_zero(pool->bitmap[i], last_idx);
385 if (!blocks_free(pool, i, idx, nr_blocks)) {
387 if (idx < SMALLOC_BPI)
391 while (idx >= SMALLOC_BPI) {
398 set_blocks(pool, i, idx, nr_blocks);
399 offset = i * SMALLOC_BPL + idx * SMALLOC_BPB;
403 if (i < pool->nr_blocks) {
404 pool->free_blocks -= nr_blocks;
405 ret = pool->map + offset;
412 static void *smalloc_pool(struct pool *pool, unsigned int size)
414 unsigned int alloc_size = size + sizeof(struct block_hdr);
417 #ifdef SMALLOC_REDZONE
418 alloc_size += sizeof(unsigned int);
421 ptr = __smalloc_pool(pool, alloc_size);
423 struct block_hdr *hdr = ptr;
425 hdr->size = alloc_size;
429 memset(ptr, 0, size);
435 void *smalloc(unsigned int size)
443 for (; i < nr_pools; i++) {
444 void *ptr = smalloc_pool(&mp[i], size);
448 global_write_unlock();
457 if (nr_pools + 1 > MAX_POOLS)
461 if (add_pool(&mp[nr_pools], size))
467 global_write_unlock();
471 char *smalloc_strdup(const char *str)
475 ptr = smalloc(strlen(str) + 1);