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>
14 #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 4 /* 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_spinlock *lock;
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 char file[PATH_MAX]; /* filename for fd */
40 unsigned int mmap_size;
45 #ifdef SMALLOC_REDZONE
50 static struct pool mp[MAX_POOLS];
51 static unsigned int nr_pools;
52 static unsigned int last_pool;
53 static struct fio_spinlock *slock;
55 static inline void pool_lock(struct pool *pool)
57 fio_spin_lock(pool->lock);
60 static inline void pool_unlock(struct pool *pool)
62 fio_spin_unlock(pool->lock);
65 static inline void global_read_lock(void)
70 static inline void global_read_unlock(void)
72 fio_spin_unlock(slock);
75 static inline void global_write_lock(void)
80 static inline void global_write_unlock(void)
82 fio_spin_unlock(slock);
85 static inline int ptr_valid(struct pool *pool, void *ptr)
87 unsigned int pool_size = pool->nr_blocks * SMALLOC_BPL;
89 return (ptr >= pool->map) && (ptr < pool->map + pool_size);
92 static inline unsigned int size_to_blocks(unsigned int size)
94 return (size + SMALLOC_BPB - 1) / SMALLOC_BPB;
97 static int blocks_iter(struct pool *pool, unsigned int pool_idx,
98 unsigned int idx, unsigned int nr_blocks,
99 int (*func)(unsigned int *map, unsigned int mask))
103 unsigned int this_blocks, mask;
106 if (pool_idx >= pool->nr_blocks)
109 map = &pool->bitmap[pool_idx];
111 this_blocks = nr_blocks;
112 if (this_blocks + idx > SMALLOC_BPI) {
113 this_blocks = SMALLOC_BPI - idx;
114 idx = SMALLOC_BPI - this_blocks;
117 if (this_blocks == SMALLOC_BPI)
120 mask = ((1U << this_blocks) - 1) << idx;
122 if (!func(map, mask))
125 nr_blocks -= this_blocks;
133 static int mask_cmp(unsigned int *map, unsigned int mask)
135 return !(*map & mask);
138 static int mask_clear(unsigned int *map, unsigned int mask)
140 assert((*map & mask) == mask);
145 static int mask_set(unsigned int *map, unsigned int mask)
147 assert(!(*map & mask));
152 static int blocks_free(struct pool *pool, unsigned int pool_idx,
153 unsigned int idx, unsigned int nr_blocks)
155 return blocks_iter(pool, pool_idx, idx, nr_blocks, mask_cmp);
158 static void set_blocks(struct pool *pool, unsigned int pool_idx,
159 unsigned int idx, unsigned int nr_blocks)
161 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_set);
164 static void clear_blocks(struct pool *pool, unsigned int pool_idx,
165 unsigned int idx, unsigned int nr_blocks)
167 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_clear);
170 static int find_next_zero(int word, int start)
173 word >>= (start + 1);
174 return ffz(word) + start + 1;
177 static int add_pool(struct pool *pool, unsigned int alloc_size)
180 int fd, bitmap_blocks;
182 strcpy(pool->file, "/tmp/.fio_smalloc.XXXXXX");
183 fd = mkstemp(pool->file);
187 #ifdef SMALLOC_REDZONE
188 alloc_size += sizeof(unsigned int);
190 alloc_size += sizeof(struct block_hdr);
191 if (alloc_size < INITIAL_SIZE)
192 alloc_size = INITIAL_SIZE;
194 /* round up to nearest full number of blocks */
195 alloc_size = (alloc_size + SMALLOC_BPL - 1) & ~(SMALLOC_BPL - 1);
196 bitmap_blocks = alloc_size / SMALLOC_BPL;
197 alloc_size += bitmap_blocks * sizeof(unsigned int);
198 pool->mmap_size = alloc_size;
200 pool->nr_blocks = bitmap_blocks;
201 pool->free_blocks = bitmap_blocks * SMALLOC_BPB;
203 if (ftruncate(fd, alloc_size) < 0)
206 ptr = mmap(NULL, alloc_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
207 if (ptr == MAP_FAILED)
210 memset(ptr, 0, alloc_size);
212 pool->bitmap = (void *) ptr + (pool->nr_blocks * SMALLOC_BPL);
214 pool->lock = fio_spinlock_init();
222 global_write_unlock();
225 fprintf(stderr, "smalloc: failed adding pool\n");
227 munmap(pool->map, pool->mmap_size);
239 slock = fio_spinlock_init();
241 ret = add_pool(&mp[0], INITIAL_SIZE);
245 static void cleanup_pool(struct pool *pool)
249 munmap(pool->map, pool->mmap_size);
250 fio_spinlock_remove(pool->lock);
257 for (i = 0; i < nr_pools; i++)
258 cleanup_pool(&mp[i]);
260 fio_spinlock_remove(slock);
263 #ifdef SMALLOC_REDZONE
264 static void fill_redzone(struct block_hdr *hdr)
266 unsigned int *postred = (void *) hdr + hdr->size - sizeof(unsigned int);
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 = (void *) hdr + hdr->size - sizeof(unsigned int);
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);
410 #ifdef SMALLOC_REDZONE
411 alloc_size += sizeof(unsigned int);
414 ptr = __smalloc_pool(pool, alloc_size);
416 struct block_hdr *hdr = ptr;
418 hdr->size = alloc_size;
422 memset(ptr, 0, size);
428 void *smalloc(unsigned int size)
436 for (; i < nr_pools; i++) {
437 void *ptr = smalloc_pool(&mp[i], size);
441 global_read_unlock();
450 if (nr_pools + 1 > MAX_POOLS)
454 global_read_unlock();
455 if (add_pool(&mp[nr_pools], size))
461 global_read_unlock();
466 char *smalloc_strdup(const char *str)
470 ptr = smalloc(strlen(str) + 1);