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 #undef ENABLE_RESIZE /* define to enable pool resizing */
17 #define MP_SAFE /* define to made allocator thread safe */
19 #define INITIAL_SIZE 65536 /* new pool size */
20 #define MAX_POOLS 32 /* maximum number of pools to setup */
23 #define MAX_SIZE 8 * INITIAL_SIZE
24 static unsigned int resize_error;
28 struct fio_mutex *lock; /* protects this pool */
29 void *map; /* map of blocks */
30 void *last; /* next free block hint */
31 unsigned int size; /* size of pool */
32 unsigned int room; /* size left in pool */
33 unsigned int largest_block; /* largest block free */
34 unsigned int free_since_compact; /* sfree() since compact() */
35 int fd; /* memory backing fd */
36 char file[PATH_MAX]; /* filename for fd */
39 static struct pool mp[MAX_POOLS];
40 static unsigned int nr_pools;
41 static unsigned int last_pool;
42 static struct fio_mutex *lock;
48 static inline void pool_lock(struct pool *pool)
51 fio_mutex_down(pool->lock);
54 static inline void pool_unlock(struct pool *pool)
57 fio_mutex_up(pool->lock);
60 static inline void global_lock(void)
66 static inline void global_unlock(void)
72 #define hdr_free(hdr) ((hdr)->size & 0x80000000)
73 #define hdr_size(hdr) ((hdr)->size & ~0x80000000)
74 #define hdr_mark_free(hdr) ((hdr)->size |= 0x80000000)
76 static inline int ptr_valid(struct pool *pool, void *ptr)
78 return (ptr >= pool->map) && (ptr < pool->map + pool->size);
81 static inline int __hdr_valid(struct pool *pool, struct mem_hdr *hdr,
84 return ptr_valid(pool, hdr) && ptr_valid(pool, (void *) hdr + size - 1);
87 static inline int hdr_valid(struct pool *pool, struct mem_hdr *hdr)
89 return __hdr_valid(pool, hdr, hdr_size(hdr));
92 static inline int region_free(struct mem_hdr *hdr)
94 return hdr_free(hdr) || (!hdr_free(hdr) && !hdr_size(hdr));
97 static inline struct mem_hdr *__hdr_nxt(struct pool *pool, struct mem_hdr *hdr,
100 struct mem_hdr *nxt = (void *) hdr + size + sizeof(*hdr);
102 if (__hdr_valid(pool, nxt, size))
108 static inline struct mem_hdr *hdr_nxt(struct pool *pool, struct mem_hdr *hdr)
110 return __hdr_nxt(pool, hdr, hdr_size(hdr));
113 static void merge(struct pool *pool, struct mem_hdr *hdr, struct mem_hdr *nxt)
115 unsigned int hfree = hdr_free(hdr);
116 unsigned int nfree = hdr_free(nxt);
118 hdr->size = hdr_size(hdr) + hdr_size(nxt) + sizeof(*nxt);
126 if (pool->last == nxt)
130 static int combine(struct pool *pool, struct mem_hdr *prv, struct mem_hdr *hdr)
132 if (prv && hdr_free(prv) && hdr_free(hdr)) {
133 merge(pool, prv, hdr);
140 static int compact_pool(struct pool *pool)
142 struct mem_hdr *hdr = pool->map, *nxt;
143 unsigned int compacted = 0;
145 if (pool->free_since_compact < 50)
149 nxt = hdr_nxt(pool, hdr);
152 if (hdr_free(nxt) && hdr_free(hdr)) {
153 merge(pool, hdr, nxt);
157 hdr = hdr_nxt(pool, hdr);
160 pool->free_since_compact = 0;
164 static int resize_pool(struct pool *pool)
167 unsigned int new_size = pool->size << 1;
168 struct mem_hdr *hdr, *last_hdr;
171 if (new_size >= MAX_SIZE || resize_error)
174 if (ftruncate(pool->fd, new_size) < 0)
177 ptr = mremap(pool->map, pool->size, new_size, 0);
178 if (ptr == MAP_FAILED)
185 } while ((hdr = hdr_nxt(hdr)) != NULL);
187 if (hdr_free(last_hdr)) {
188 last_hdr->size = hdr_size(last_hdr) + new_size - pool_size;
189 hdr_mark_free(last_hdr);
193 nxt = (void *) last_hdr + hdr_size(last_hdr) + sizeof(*hdr);
194 nxt->size = new_size - pool_size - sizeof(*hdr);
198 pool_room += new_size - pool_size;
199 pool_size = new_size;
209 static int add_pool(struct pool *pool)
215 strcpy(pool->file, "/tmp/.fio_smalloc.XXXXXX");
216 fd = mkstemp(pool->file);
220 pool->size = INITIAL_SIZE;
221 if (ftruncate(fd, pool->size) < 0)
224 ptr = mmap(NULL, pool->size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
225 if (ptr == MAP_FAILED)
228 memset(ptr, 0, pool->size);
229 pool->map = pool->last = ptr;
232 pool->lock = fio_mutex_init(1);
240 pool->room = hdr->size = pool->size - sizeof(*hdr);
241 pool->largest_block = pool->room;
247 munmap(pool->map, pool->size);
257 int ret = add_pool(&mp[0]);
260 lock = fio_mutex_init(1);
265 static void cleanup_pool(struct pool *pool)
269 munmap(pool->map, pool->size);
272 fio_mutex_remove(pool->lock);
279 for (i = 0; i < nr_pools; i++)
280 cleanup_pool(&mp[i]);
283 fio_mutex_remove(lock);
286 static void sfree_pool(struct pool *pool, void *ptr)
288 struct mem_hdr *hdr, *nxt;
293 assert(ptr_valid(pool, ptr));
296 hdr = ptr - sizeof(*hdr);
297 assert(!hdr_free(hdr));
299 pool->room -= hdr_size(hdr);
301 nxt = hdr_nxt(pool, hdr);
302 if (nxt && hdr_free(nxt))
303 merge(pool, hdr, nxt);
305 if (hdr_size(hdr) > pool->largest_block)
306 pool->largest_block = hdr_size(hdr);
308 pool->free_since_compact++;
312 void sfree(void *ptr)
314 struct pool *pool = NULL;
319 for (i = 0; i < nr_pools; i++) {
320 if (ptr_valid(&mp[i], ptr)) {
329 sfree_pool(pool, ptr);
332 static void *smalloc_pool(struct pool *pool, unsigned int size)
334 struct mem_hdr *hdr, *prv;
339 * slight chance of race with sfree() here, but acceptable
341 if (!size || size > pool->room + sizeof(*hdr) ||
342 ((size > pool->largest_block) && pool->largest_block))
350 if (combine(pool, prv, hdr))
353 if (hdr_free(hdr) && hdr_size(hdr) >= size)
357 } while ((hdr = hdr_nxt(pool, hdr)) != NULL);
363 * more room, adjust next header if any
365 if (hdr_size(hdr) - size >= 2 * sizeof(*hdr)) {
366 struct mem_hdr *nxt = __hdr_nxt(pool, hdr, size);
369 nxt->size = hdr_size(hdr) - size - sizeof(*hdr);
370 if (hdr_size(hdr) == pool->largest_block)
371 pool->largest_block = hdr_size(nxt);
374 size = hdr_size(hdr);
376 size = hdr_size(hdr);
378 if (size == hdr_size(hdr) && size == pool->largest_block)
379 pool->largest_block = 0;
382 * also clears free bit
385 pool->last = hdr_nxt(pool, hdr);
387 pool->last = pool->map;
391 ret = (void *) hdr + sizeof(*hdr);
392 memset(ret, 0, size);
396 * if we fail to allocate, first compact the entries that we missed.
397 * if that also fails, increase the size of the pool
400 if (did_restart <= 1) {
401 if (!compact_pool(pool)) {
402 pool->last = pool->map;
407 if (did_restart <= 2) {
408 if (!resize_pool(pool)) {
409 pool->last = pool->map;
417 void *smalloc(unsigned int size)
425 for (; i < nr_pools; i++) {
426 void *ptr = smalloc_pool(&mp[i], size);
439 if (nr_pools + 1 >= MAX_POOLS)
443 if (add_pool(&mp[nr_pools]))
452 char *smalloc_strdup(const char *str)
456 ptr = smalloc(strlen(str) + 1);