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 1048576 /* new pool size */
20 #define MAX_POOLS 32 /* maximum number of pools to setup */
22 unsigned int smalloc_pool_size = INITIAL_SIZE;
25 #define MAX_SIZE 8 * smalloc_pool_size
26 static unsigned int resize_error;
30 struct fio_mutex *lock; /* protects this pool */
31 void *map; /* map of blocks */
32 void *last; /* next free block hint */
33 unsigned int size; /* size of pool */
34 unsigned int room; /* size left in pool */
35 unsigned int largest_block; /* largest block free */
36 unsigned int free_since_compact; /* sfree() since compact() */
37 int fd; /* memory backing fd */
38 char file[PATH_MAX]; /* filename for fd */
41 static struct pool mp[MAX_POOLS];
42 static unsigned int nr_pools;
43 static unsigned int last_pool;
44 static struct fio_mutex *lock;
50 static inline void pool_lock(struct pool *pool)
53 fio_mutex_down(pool->lock);
56 static inline void pool_unlock(struct pool *pool)
59 fio_mutex_up(pool->lock);
62 static inline void global_read_lock(void)
65 fio_mutex_down_read(lock);
68 static inline void global_read_unlock(void)
71 fio_mutex_up_read(lock);
74 static inline void global_write_lock(void)
77 fio_mutex_down_write(lock);
80 static inline void global_write_unlock(void)
83 fio_mutex_up_write(lock);
86 #define hdr_free(hdr) ((hdr)->size & 0x80000000)
87 #define hdr_size(hdr) ((hdr)->size & ~0x80000000)
88 #define hdr_mark_free(hdr) ((hdr)->size |= 0x80000000)
90 static inline int ptr_valid(struct pool *pool, void *ptr)
92 return (ptr >= pool->map) && (ptr < pool->map + pool->size);
95 static inline int __hdr_valid(struct pool *pool, struct mem_hdr *hdr,
98 return ptr_valid(pool, hdr) && ptr_valid(pool, (void *) hdr + size - 1);
101 static inline int hdr_valid(struct pool *pool, struct mem_hdr *hdr)
103 return __hdr_valid(pool, hdr, hdr_size(hdr));
106 static inline int region_free(struct mem_hdr *hdr)
108 return hdr_free(hdr) || (!hdr_free(hdr) && !hdr_size(hdr));
111 static inline struct mem_hdr *__hdr_nxt(struct pool *pool, struct mem_hdr *hdr,
114 struct mem_hdr *nxt = (void *) hdr + size + sizeof(*hdr);
116 if (__hdr_valid(pool, nxt, size))
122 static inline struct mem_hdr *hdr_nxt(struct pool *pool, struct mem_hdr *hdr)
124 return __hdr_nxt(pool, hdr, hdr_size(hdr));
127 static void merge(struct pool *pool, struct mem_hdr *hdr, struct mem_hdr *nxt)
129 unsigned int hfree = hdr_free(hdr);
130 unsigned int nfree = hdr_free(nxt);
132 hdr->size = hdr_size(hdr) + hdr_size(nxt) + sizeof(*nxt);
140 if (pool->last == nxt)
144 static int combine(struct pool *pool, struct mem_hdr *prv, struct mem_hdr *hdr)
146 if (prv && hdr_free(prv) && hdr_free(hdr)) {
147 merge(pool, prv, hdr);
154 static int compact_pool(struct pool *pool)
156 struct mem_hdr *hdr = pool->map, *nxt;
157 unsigned int compacted = 0;
159 if (pool->free_since_compact < 50)
163 nxt = hdr_nxt(pool, hdr);
166 if (hdr_free(nxt) && hdr_free(hdr)) {
167 merge(pool, hdr, nxt);
171 hdr = hdr_nxt(pool, hdr);
174 pool->free_since_compact = 0;
178 static int resize_pool(struct pool *pool)
181 unsigned int new_size = pool->size << 1;
182 struct mem_hdr *hdr, *last_hdr;
185 if (new_size >= MAX_SIZE || resize_error)
188 if (ftruncate(pool->fd, new_size) < 0)
191 ptr = mremap(pool->map, pool->size, new_size, 0);
192 if (ptr == MAP_FAILED)
199 } while ((hdr = hdr_nxt(hdr)) != NULL);
201 if (hdr_free(last_hdr)) {
202 last_hdr->size = hdr_size(last_hdr) + new_size - pool_size;
203 hdr_mark_free(last_hdr);
207 nxt = (void *) last_hdr + hdr_size(last_hdr) + sizeof(*hdr);
208 nxt->size = new_size - pool_size - sizeof(*hdr);
212 pool_room += new_size - pool_size;
213 pool_size = new_size;
223 static int add_pool(struct pool *pool, unsigned int alloc_size)
229 strcpy(pool->file, "/tmp/.fio_smalloc.XXXXXX");
230 fd = mkstemp(pool->file);
234 if (alloc_size > smalloc_pool_size)
235 pool->size = alloc_size;
237 pool->size = smalloc_pool_size;
239 if (ftruncate(fd, pool->size) < 0)
242 ptr = mmap(NULL, pool->size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
243 if (ptr == MAP_FAILED)
246 memset(ptr, 0, pool->size);
247 pool->map = pool->last = ptr;
250 pool->lock = fio_mutex_init(1);
258 pool->room = hdr->size = pool->size - sizeof(*hdr);
259 pool->largest_block = pool->room;
263 global_write_unlock();
267 munmap(pool->map, pool->size);
280 lock = fio_mutex_rw_init();
282 ret = add_pool(&mp[0], INITIAL_SIZE);
286 static void cleanup_pool(struct pool *pool)
290 munmap(pool->map, pool->size);
293 fio_mutex_remove(pool->lock);
300 for (i = 0; i < nr_pools; i++)
301 cleanup_pool(&mp[i]);
304 fio_mutex_remove(lock);
307 static void sfree_pool(struct pool *pool, void *ptr)
309 struct mem_hdr *hdr, *nxt;
314 assert(ptr_valid(pool, ptr));
317 hdr = ptr - sizeof(*hdr);
318 assert(!hdr_free(hdr));
320 pool->room -= hdr_size(hdr);
322 nxt = hdr_nxt(pool, hdr);
323 if (nxt && hdr_free(nxt))
324 merge(pool, hdr, nxt);
326 if (hdr_size(hdr) > pool->largest_block)
327 pool->largest_block = hdr_size(hdr);
329 pool->free_since_compact++;
333 void sfree(void *ptr)
335 struct pool *pool = NULL;
340 for (i = 0; i < nr_pools; i++) {
341 if (ptr_valid(&mp[i], ptr)) {
347 global_read_unlock();
350 sfree_pool(pool, ptr);
353 static void *smalloc_pool(struct pool *pool, unsigned int size)
355 struct mem_hdr *hdr, *prv;
360 * slight chance of race with sfree() here, but acceptable
362 if (!size || size > pool->room + sizeof(*hdr) ||
363 ((size > pool->largest_block) && pool->largest_block))
371 if (combine(pool, prv, hdr))
374 if (hdr_free(hdr) && hdr_size(hdr) >= size)
378 } while ((hdr = hdr_nxt(pool, hdr)) != NULL);
384 * more room, adjust next header if any
386 if (hdr_size(hdr) - size >= 2 * sizeof(*hdr)) {
387 struct mem_hdr *nxt = __hdr_nxt(pool, hdr, size);
390 nxt->size = hdr_size(hdr) - size - sizeof(*hdr);
391 if (hdr_size(hdr) == pool->largest_block)
392 pool->largest_block = hdr_size(nxt);
395 size = hdr_size(hdr);
397 size = hdr_size(hdr);
399 if (size == hdr_size(hdr) && size == pool->largest_block)
400 pool->largest_block = 0;
403 * also clears free bit
406 pool->last = hdr_nxt(pool, hdr);
408 pool->last = pool->map;
412 ret = (void *) hdr + sizeof(*hdr);
413 memset(ret, 0, size);
417 * if we fail to allocate, first compact the entries that we missed.
418 * if that also fails, increase the size of the pool
421 if (did_restart <= 1) {
422 if (!compact_pool(pool)) {
423 pool->last = pool->map;
428 if (did_restart <= 2) {
429 if (!resize_pool(pool)) {
430 pool->last = pool->map;
438 void *smalloc(unsigned int size)
446 for (; i < nr_pools; i++) {
447 void *ptr = smalloc_pool(&mp[i], size);
451 global_read_unlock();
460 if (nr_pools + 1 >= MAX_POOLS)
464 global_read_unlock();
465 if (add_pool(&mp[nr_pools], size))
471 global_read_unlock();
476 char *smalloc_strdup(const char *str)
480 ptr = smalloc(strlen(str) + 1);