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>
17 #define MP_SAFE /* define to make thread safe */
18 #define SMALLOC_REDZONE /* define to detect memory corruption */
20 #define SMALLOC_BPB 32 /* block size, bytes-per-bit in bitmap */
21 #define SMALLOC_BPI (sizeof(unsigned int) * 8)
22 #define SMALLOC_BPL (SMALLOC_BPB * SMALLOC_BPI)
24 #define INITIAL_SIZE 1024*1024 /* new pool size */
25 #define MAX_POOLS 4 /* maximum number of pools to setup */
27 #define SMALLOC_PRE_RED 0xdeadbeefU
28 #define SMALLOC_POST_RED 0x5aa55aa5U
30 unsigned int smalloc_pool_size = INITIAL_SIZE;
33 struct fio_mutex *lock; /* protects this pool */
34 void *map; /* map of blocks */
35 unsigned int *bitmap; /* blocks free/busy map */
36 unsigned int free_blocks; /* free blocks */
37 unsigned int nr_blocks; /* total blocks */
38 unsigned int next_non_full;
39 int fd; /* memory backing fd */
40 char file[PATH_MAX]; /* filename for fd */
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)
59 fio_mutex_down(pool->lock);
62 static inline void pool_unlock(struct pool *pool)
65 fio_mutex_up(pool->lock);
68 static inline void global_read_lock(void)
71 fio_mutex_down_read(lock);
74 static inline void global_read_unlock(void)
77 fio_mutex_up_read(lock);
80 static inline void global_write_lock(void)
83 fio_mutex_down_write(lock);
86 static inline void global_write_unlock(void)
89 fio_mutex_up_write(lock);
92 static inline int ptr_valid(struct pool *pool, void *ptr)
94 unsigned int pool_size = pool->nr_blocks * SMALLOC_BPL;
96 return (ptr >= pool->map) && (ptr < pool->map + pool_size);
99 static inline unsigned int size_to_blocks(unsigned int size)
101 return (size + SMALLOC_BPB - 1) / SMALLOC_BPB;
104 static int blocks_iter(struct pool *pool, unsigned int pool_idx,
105 unsigned int idx, unsigned int nr_blocks,
106 int (*func)(unsigned int *map, unsigned int mask))
110 unsigned int this_blocks, mask;
113 if (pool_idx >= pool->nr_blocks)
116 map = &pool->bitmap[pool_idx];
118 this_blocks = nr_blocks;
119 if (this_blocks + idx > SMALLOC_BPI) {
120 this_blocks = SMALLOC_BPI - idx;
121 idx = SMALLOC_BPI - this_blocks;
124 if (this_blocks == SMALLOC_BPI)
127 mask = ((1U << this_blocks) - 1) << idx;
129 if (!func(map, mask))
132 nr_blocks -= this_blocks;
140 static int mask_cmp(unsigned int *map, unsigned int mask)
142 return !(*map & mask);
145 static int mask_clear(unsigned int *map, unsigned int mask)
147 assert((*map & mask) == mask);
152 static int mask_set(unsigned int *map, unsigned int mask)
154 assert(!(*map & mask));
159 static int blocks_free(struct pool *pool, unsigned int pool_idx,
160 unsigned int idx, unsigned int nr_blocks)
162 return blocks_iter(pool, pool_idx, idx, nr_blocks, mask_cmp);
165 static void set_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_set);
171 static void clear_blocks(struct pool *pool, unsigned int pool_idx,
172 unsigned int idx, unsigned int nr_blocks)
174 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_clear);
177 static int find_next_zero(int word, int start)
180 word >>= (start + 1);
181 return __ffs(~word) + start + 1;
184 static int add_pool(struct pool *pool, unsigned int alloc_size)
187 int fd, bitmap_blocks;
189 strcpy(pool->file, "/tmp/.fio_smalloc.XXXXXX");
190 fd = mkstemp(pool->file);
194 #ifdef SMALLOC_REDZONE
195 alloc_size += sizeof(unsigned int);
197 alloc_size += sizeof(struct block_hdr);
198 if (alloc_size < INITIAL_SIZE)
199 alloc_size = INITIAL_SIZE;
201 /* round up to nearest full number of blocks */
202 alloc_size = (alloc_size + SMALLOC_BPL - 1) & ~(SMALLOC_BPL - 1);
203 bitmap_blocks = alloc_size / SMALLOC_BPL;
204 alloc_size += bitmap_blocks * sizeof(unsigned int);
205 pool->mmap_size = alloc_size;
207 pool->nr_blocks = bitmap_blocks;
208 pool->free_blocks = bitmap_blocks * SMALLOC_BPB;
210 if (ftruncate(fd, alloc_size) < 0)
213 ptr = mmap(NULL, alloc_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
214 if (ptr == MAP_FAILED)
217 memset(ptr, 0, alloc_size);
219 pool->bitmap = (void *) ptr + (pool->nr_blocks * SMALLOC_BPL);
222 pool->lock = fio_mutex_init(1);
231 global_write_unlock();
234 fprintf(stderr, "smalloc: failed adding pool\n");
236 munmap(pool->map, pool->mmap_size);
249 lock = fio_mutex_rw_init();
251 ret = add_pool(&mp[0], INITIAL_SIZE);
255 static void cleanup_pool(struct pool *pool)
259 munmap(pool->map, pool->mmap_size);
262 fio_mutex_remove(pool->lock);
269 for (i = 0; i < nr_pools; i++)
270 cleanup_pool(&mp[i]);
273 fio_mutex_remove(lock);
276 #ifdef SMALLOC_REDZONE
277 static void fill_redzone(struct block_hdr *hdr)
279 unsigned int *postred = (void *) hdr + hdr->size - sizeof(unsigned int);
281 hdr->prered = SMALLOC_PRE_RED;
282 *postred = SMALLOC_POST_RED;
285 static void sfree_check_redzone(struct block_hdr *hdr)
287 unsigned int *postred = (void *) hdr + hdr->size - sizeof(unsigned int);
289 if (hdr->prered != SMALLOC_PRE_RED) {
290 fprintf(stderr, "smalloc pre redzone destroyed!\n");
291 fprintf(stderr, " ptr=%p, prered=%x, expected %x\n",
292 hdr, hdr->prered, SMALLOC_PRE_RED);
295 if (*postred != SMALLOC_POST_RED) {
296 fprintf(stderr, "smalloc post redzone destroyed!\n");
297 fprintf(stderr, " ptr=%p, postred=%x, expected %x\n",
298 hdr, *postred, SMALLOC_POST_RED);
303 static void fill_redzone(struct block_hdr *hdr)
307 static void sfree_check_redzone(struct block_hdr *hdr)
312 static void sfree_pool(struct pool *pool, void *ptr)
314 struct block_hdr *hdr;
316 unsigned long offset;
324 assert(ptr_valid(pool, ptr));
326 sfree_check_redzone(hdr);
328 offset = ptr - pool->map;
329 i = offset / SMALLOC_BPL;
330 idx = (offset % SMALLOC_BPL) / SMALLOC_BPB;
333 clear_blocks(pool, i, idx, size_to_blocks(hdr->size));
334 if (i < pool->next_non_full)
335 pool->next_non_full = i;
336 pool->free_blocks += size_to_blocks(hdr->size);
340 void sfree(void *ptr)
342 struct pool *pool = NULL;
350 for (i = 0; i < nr_pools; i++) {
351 if (ptr_valid(&mp[i], ptr)) {
357 global_read_unlock();
360 sfree_pool(pool, ptr);
363 static void *__smalloc_pool(struct pool *pool, unsigned int size)
365 unsigned int nr_blocks;
368 unsigned int last_idx;
373 nr_blocks = size_to_blocks(size);
374 if (nr_blocks > pool->free_blocks)
377 i = pool->next_non_full;
380 while (i < pool->nr_blocks) {
383 if (pool->bitmap[i] == -1U) {
385 pool->next_non_full = i;
390 idx = find_next_zero(pool->bitmap[i], last_idx);
391 if (!blocks_free(pool, i, idx, nr_blocks)) {
393 if (idx < SMALLOC_BPI)
397 while (idx >= SMALLOC_BPI) {
404 set_blocks(pool, i, idx, nr_blocks);
405 offset = i * SMALLOC_BPL + idx * SMALLOC_BPB;
409 if (i < pool->nr_blocks) {
410 pool->free_blocks -= nr_blocks;
411 ret = pool->map + offset;
418 static void *smalloc_pool(struct pool *pool, unsigned int size)
420 unsigned int alloc_size = size + sizeof(struct block_hdr);
423 #ifdef SMALLOC_REDZONE
424 alloc_size += sizeof(unsigned int);
427 ptr = __smalloc_pool(pool, alloc_size);
429 struct block_hdr *hdr = ptr;
431 hdr->size = alloc_size;
435 memset(ptr, 0, size);
441 void *smalloc(unsigned int size)
449 for (; i < nr_pools; i++) {
450 void *ptr = smalloc_pool(&mp[i], size);
454 global_read_unlock();
463 if (nr_pools + 1 > MAX_POOLS)
467 global_read_unlock();
468 if (add_pool(&mp[nr_pools], size))
474 global_read_unlock();
479 char *smalloc_strdup(const char *str)
483 ptr = smalloc(strlen(str) + 1);