blob: 3b512bcf63cdee4f57b6377684d366546fb0e55c [file] [log] [blame]
Jens Axboed24c33a2008-03-01 18:27:36 +01001/*
2 * simple memory allocator, backed by mmap() so that it hands out memory
3 * that can be shared across processes and threads
4 */
5#include <sys/mman.h>
6#include <stdio.h>
7#include <stdlib.h>
8#include <assert.h>
9#include <string.h>
10#include <unistd.h>
11#include <sys/types.h>
12#include <limits.h>
13
14#include "mutex.h"
15
Jens Axboe1f37f632008-05-27 11:40:37 +020016#define MP_SAFE /* define to make thread safe */
Jens Axboe55f64912008-05-26 09:37:21 +020017#define SMALLOC_REDZONE /* define to detect memory corruption */
Jens Axboed24c33a2008-03-01 18:27:36 +010018
Jens Axboeec996e92008-05-27 11:39:39 +020019#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)
22
23#define INITIAL_SIZE 1024*1024 /* new pool size */
Jens Axboec5dda9e2008-05-23 10:10:54 +020024#define MAX_POOLS 4 /* maximum number of pools to setup */
Jens Axboed24c33a2008-03-01 18:27:36 +010025
Jens Axboe55f64912008-05-26 09:37:21 +020026#define SMALLOC_PRE_RED 0xdeadbeefU
27#define SMALLOC_POST_RED 0x5aa55aa5U
Jens Axboe55f64912008-05-26 09:37:21 +020028
Jens Axboe2b386d22008-03-26 10:32:57 +010029unsigned int smalloc_pool_size = INITIAL_SIZE;
30
Jens Axboed24c33a2008-03-01 18:27:36 +010031struct pool {
32 struct fio_mutex *lock; /* protects this pool */
33 void *map; /* map of blocks */
Jens Axboeec996e92008-05-27 11:39:39 +020034 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;
Jens Axboed24c33a2008-03-01 18:27:36 +010038 int fd; /* memory backing fd */
39 char file[PATH_MAX]; /* filename for fd */
Jens Axboeec996e92008-05-27 11:39:39 +020040 unsigned int mmap_size;
41};
42
43struct block_hdr {
44 unsigned int size;
45#ifdef SMALLOC_REDZONE
46 unsigned int prered;
47#endif
Jens Axboed24c33a2008-03-01 18:27:36 +010048};
49
50static struct pool mp[MAX_POOLS];
51static unsigned int nr_pools;
52static unsigned int last_pool;
53static struct fio_mutex *lock;
54
Jens Axboed24c33a2008-03-01 18:27:36 +010055static inline void pool_lock(struct pool *pool)
56{
57 if (pool->lock)
58 fio_mutex_down(pool->lock);
59}
60
61static inline void pool_unlock(struct pool *pool)
62{
63 if (pool->lock)
64 fio_mutex_up(pool->lock);
65}
66
Jens Axboe65864cf2008-03-03 10:36:36 +010067static inline void global_read_lock(void)
Jens Axboed24c33a2008-03-01 18:27:36 +010068{
69 if (lock)
Jens Axboe65864cf2008-03-03 10:36:36 +010070 fio_mutex_down_read(lock);
Jens Axboed24c33a2008-03-01 18:27:36 +010071}
72
Jens Axboe65864cf2008-03-03 10:36:36 +010073static inline void global_read_unlock(void)
Jens Axboed24c33a2008-03-01 18:27:36 +010074{
75 if (lock)
Jens Axboe65864cf2008-03-03 10:36:36 +010076 fio_mutex_up_read(lock);
77}
78
79static inline void global_write_lock(void)
80{
81 if (lock)
82 fio_mutex_down_write(lock);
83}
84
85static inline void global_write_unlock(void)
86{
87 if (lock)
88 fio_mutex_up_write(lock);
Jens Axboed24c33a2008-03-01 18:27:36 +010089}
90
Jens Axboed24c33a2008-03-01 18:27:36 +010091static inline int ptr_valid(struct pool *pool, void *ptr)
92{
Jens Axboeec996e92008-05-27 11:39:39 +020093 unsigned int pool_size = pool->nr_blocks * SMALLOC_BPL;
94
95 return (ptr >= pool->map) && (ptr < pool->map + pool_size);
Jens Axboed24c33a2008-03-01 18:27:36 +010096}
97
Jens Axboeec996e92008-05-27 11:39:39 +020098static int blocks_iter(unsigned int *map, unsigned int idx,
99 unsigned int nr_blocks,
100 int (*func)(unsigned int *map, unsigned int mask))
Jens Axboed24c33a2008-03-01 18:27:36 +0100101{
Jens Axboeec996e92008-05-27 11:39:39 +0200102 while (nr_blocks) {
103 unsigned int this_blocks, mask;
Jens Axboed24c33a2008-03-01 18:27:36 +0100104
Jens Axboeec996e92008-05-27 11:39:39 +0200105 this_blocks = nr_blocks;
106 if (this_blocks + idx > SMALLOC_BPI) {
107 this_blocks = SMALLOC_BPI - idx;
108 idx = SMALLOC_BPI - this_blocks;
Jens Axboed24c33a2008-03-01 18:27:36 +0100109 }
Jens Axboeec996e92008-05-27 11:39:39 +0200110
111 if (this_blocks == SMALLOC_BPI)
112 mask = -1U;
113 else
114 mask = ((1U << this_blocks) - 1) << idx;
115
116 if (!func(map, mask))
117 return 0;
118
119 nr_blocks -= this_blocks;
120 idx = 0;
121 map++;
Jens Axboed24c33a2008-03-01 18:27:36 +0100122 }
123
Jens Axboeec996e92008-05-27 11:39:39 +0200124 return 1;
Jens Axboeec996e92008-05-27 11:39:39 +0200125}
126
127static int mask_cmp(unsigned int *map, unsigned int mask)
128{
129 return !(*map & mask);
130}
131
132static int mask_clear(unsigned int *map, unsigned int mask)
133{
134 *map &= ~mask;
135 return 1;
136}
137
138static int mask_set(unsigned int *map, unsigned int mask)
139{
140 *map |= mask;
141 return 1;
142}
143
144static int blocks_free(unsigned int *map, unsigned int idx,
145 unsigned int nr_blocks)
146{
147 return blocks_iter(map, idx, nr_blocks, mask_cmp);
148}
149
150static void set_blocks(unsigned int *map, unsigned int idx,
151 unsigned int nr_blocks)
152{
153 blocks_iter(map, idx, nr_blocks, mask_set);
154}
155
156static void clear_blocks(unsigned int *map, unsigned int idx,
157 unsigned int nr_blocks)
158{
159 blocks_iter(map, idx, nr_blocks, mask_clear);
160}
161
162static inline int __ffs(int word)
163{
164 int r = 0;
165
166 if (!(word & 0xffff)) {
167 word >>= 16;
168 r += 16;
169 }
170 if (!(word & 0xff)) {
171 word >>= 8;
172 r += 8;
173 }
174 if (!(word & 0xf)) {
175 word >>= 4;
176 r += 4;
177 }
178 if (!(word & 3)) {
179 word >>= 2;
180 r += 2;
181 }
182 if (!(word & 1)) {
183 word >>= 1;
184 r += 1;
185 }
186
187 return r;
188}
189
190static int find_next_zero(int word, int start)
191{
192 assert(word != -1U);
193 word >>= (start + 1);
194 return __ffs(~word) + start + 1;
Jens Axboed24c33a2008-03-01 18:27:36 +0100195}
196
Jens Axboeadf57092008-04-16 19:43:17 +0200197static int add_pool(struct pool *pool, unsigned int alloc_size)
Jens Axboed24c33a2008-03-01 18:27:36 +0100198{
Jens Axboed24c33a2008-03-01 18:27:36 +0100199 void *ptr;
Jens Axboeec996e92008-05-27 11:39:39 +0200200 int fd, bitmap_blocks;
201
Jens Axboed24c33a2008-03-01 18:27:36 +0100202 strcpy(pool->file, "/tmp/.fio_smalloc.XXXXXX");
203 fd = mkstemp(pool->file);
204 if (fd < 0)
205 goto out_close;
206
Jens Axboe55f64912008-05-26 09:37:21 +0200207#ifdef SMALLOC_REDZONE
Jens Axboeec996e92008-05-27 11:39:39 +0200208 alloc_size += sizeof(unsigned int);
Jens Axboe55f64912008-05-26 09:37:21 +0200209#endif
Jens Axboeec996e92008-05-27 11:39:39 +0200210 alloc_size += sizeof(struct block_hdr);
211 if (alloc_size < INITIAL_SIZE)
212 alloc_size = INITIAL_SIZE;
Jens Axboeadf57092008-04-16 19:43:17 +0200213
Jens Axboeec996e92008-05-27 11:39:39 +0200214 /* round up to nearest full number of blocks */
215 alloc_size = (alloc_size + SMALLOC_BPL - 1) & ~(SMALLOC_BPL - 1);
216 bitmap_blocks = alloc_size / SMALLOC_BPL;
217 alloc_size += bitmap_blocks * sizeof(unsigned int);
218 pool->mmap_size = alloc_size;
219
220 pool->nr_blocks = bitmap_blocks;
221 pool->free_blocks = bitmap_blocks * SMALLOC_BPB;
222
223 if (ftruncate(fd, alloc_size) < 0)
Jens Axboed24c33a2008-03-01 18:27:36 +0100224 goto out_unlink;
225
Jens Axboeec996e92008-05-27 11:39:39 +0200226 ptr = mmap(NULL, alloc_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
Jens Axboed24c33a2008-03-01 18:27:36 +0100227 if (ptr == MAP_FAILED)
228 goto out_unlink;
229
Jens Axboeec996e92008-05-27 11:39:39 +0200230 memset(ptr, 0, alloc_size);
231 pool->map = ptr;
232 pool->bitmap = (void *) ptr + (pool->nr_blocks * SMALLOC_BPL);
Jens Axboed24c33a2008-03-01 18:27:36 +0100233
234#ifdef MP_SAFE
235 pool->lock = fio_mutex_init(1);
236 if (!pool->lock)
237 goto out_unlink;
238#endif
239
240 pool->fd = fd;
241
Jens Axboe65864cf2008-03-03 10:36:36 +0100242 global_write_lock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100243 nr_pools++;
Jens Axboe65864cf2008-03-03 10:36:36 +0100244 global_write_unlock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100245 return 0;
246out_unlink:
Jens Axboeec996e92008-05-27 11:39:39 +0200247 fprintf(stderr, "smalloc: failed adding pool\n");
Jens Axboed24c33a2008-03-01 18:27:36 +0100248 if (pool->map)
Jens Axboeec996e92008-05-27 11:39:39 +0200249 munmap(pool->map, pool->mmap_size);
Jens Axboed24c33a2008-03-01 18:27:36 +0100250 unlink(pool->file);
251out_close:
252 if (fd >= 0)
253 close(fd);
254 return 1;
255}
256
257void sinit(void)
258{
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100259 int ret;
Jens Axboed24c33a2008-03-01 18:27:36 +0100260
261#ifdef MP_SAFE
Jens Axboe9c5b5292008-03-03 11:08:19 +0100262 lock = fio_mutex_rw_init();
Jens Axboed24c33a2008-03-01 18:27:36 +0100263#endif
Jens Axboeadf57092008-04-16 19:43:17 +0200264 ret = add_pool(&mp[0], INITIAL_SIZE);
Jens Axboed24c33a2008-03-01 18:27:36 +0100265 assert(!ret);
266}
267
268static void cleanup_pool(struct pool *pool)
269{
270 unlink(pool->file);
271 close(pool->fd);
Jens Axboeec996e92008-05-27 11:39:39 +0200272 munmap(pool->map, pool->mmap_size);
Jens Axboed24c33a2008-03-01 18:27:36 +0100273
274 if (pool->lock)
275 fio_mutex_remove(pool->lock);
276}
277
278void scleanup(void)
279{
280 unsigned int i;
281
282 for (i = 0; i < nr_pools; i++)
283 cleanup_pool(&mp[i]);
284
285 if (lock)
286 fio_mutex_remove(lock);
287}
288
Jens Axboeec996e92008-05-27 11:39:39 +0200289static void fill_redzone(struct block_hdr *hdr)
Jens Axboe55f64912008-05-26 09:37:21 +0200290{
291#ifdef SMALLOC_REDZONE
Jens Axboeec996e92008-05-27 11:39:39 +0200292 unsigned int *postred = (void *) hdr + hdr->size - sizeof(unsigned int);
Jens Axboe55f64912008-05-26 09:37:21 +0200293
Jens Axboeec996e92008-05-27 11:39:39 +0200294 hdr->prered = SMALLOC_PRE_RED;
295 *postred = SMALLOC_POST_RED;
296#endif
297}
Jens Axboe55f64912008-05-26 09:37:21 +0200298
Jens Axboeec996e92008-05-27 11:39:39 +0200299static void sfree_check_redzone(struct block_hdr *hdr)
300{
301#ifdef SMALLOC_REDZONE
302 unsigned int *postred = (void *) hdr + hdr->size - sizeof(unsigned int);
303
304 if (hdr->prered != SMALLOC_PRE_RED) {
Jens Axboe55f64912008-05-26 09:37:21 +0200305 fprintf(stderr, "smalloc pre redzone destroyed!\n");
306 fprintf(stderr, " ptr=%p, prered=%x, expected %x\n",
Jens Axboeec996e92008-05-27 11:39:39 +0200307 hdr, hdr->prered, SMALLOC_PRE_RED);
Jens Axboe55f64912008-05-26 09:37:21 +0200308 assert(0);
309 }
310 if (*postred != SMALLOC_POST_RED) {
311 fprintf(stderr, "smalloc post redzone destroyed!\n");
312 fprintf(stderr, " ptr=%p, postred=%x, expected %x\n",
Jens Axboeec996e92008-05-27 11:39:39 +0200313 hdr, *postred, SMALLOC_POST_RED);
Jens Axboe55f64912008-05-26 09:37:21 +0200314 assert(0);
315 }
316#endif
317}
318
Jens Axboed24c33a2008-03-01 18:27:36 +0100319static void sfree_pool(struct pool *pool, void *ptr)
320{
Jens Axboeec996e92008-05-27 11:39:39 +0200321 struct block_hdr *hdr;
Jens Axboe179446e2008-05-27 14:11:56 +0200322 unsigned int i, idx;
Jens Axboeec996e92008-05-27 11:39:39 +0200323 unsigned long offset;
Jens Axboed24c33a2008-03-01 18:27:36 +0100324
325 if (!ptr)
326 return;
327
Jens Axboeec996e92008-05-27 11:39:39 +0200328 ptr -= sizeof(*hdr);
329 hdr = ptr;
Jens Axboe55f64912008-05-26 09:37:21 +0200330
Jens Axboed24c33a2008-03-01 18:27:36 +0100331 assert(ptr_valid(pool, ptr));
332
Jens Axboeec996e92008-05-27 11:39:39 +0200333 sfree_check_redzone(hdr);
334
335 offset = ptr - pool->map;
336 i = offset / SMALLOC_BPL;
337 idx = (offset % SMALLOC_BPL) / SMALLOC_BPB;
338
Jens Axboed24c33a2008-03-01 18:27:36 +0100339 pool_lock(pool);
Jens Axboe179446e2008-05-27 14:11:56 +0200340 clear_blocks(&pool->bitmap[i], idx, size_to_blocks(hdr->size));
Jens Axboeec996e92008-05-27 11:39:39 +0200341 if (i < pool->next_non_full)
342 pool->next_non_full = i;
Jens Axboe179446e2008-05-27 14:11:56 +0200343 pool->free_blocks += size_to_blocks(hdr->size);
Jens Axboed24c33a2008-03-01 18:27:36 +0100344 pool_unlock(pool);
345}
346
347void sfree(void *ptr)
348{
349 struct pool *pool = NULL;
350 unsigned int i;
351
Jens Axboe8e5732e2008-04-16 19:51:46 +0200352 if (!ptr)
353 return;
354
Jens Axboe65864cf2008-03-03 10:36:36 +0100355 global_read_lock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100356
357 for (i = 0; i < nr_pools; i++) {
358 if (ptr_valid(&mp[i], ptr)) {
359 pool = &mp[i];
360 break;
361 }
362 }
363
Jens Axboe65864cf2008-03-03 10:36:36 +0100364 global_read_unlock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100365
366 assert(pool);
367 sfree_pool(pool, ptr);
368}
369
Jens Axboe179446e2008-05-27 14:11:56 +0200370static inline unsigned int size_to_blocks(unsigned int size)
371{
372 return (size + SMALLOC_BPB - 1) / SMALLOC_BPB;
373}
374
Jens Axboe55f64912008-05-26 09:37:21 +0200375static void *__smalloc_pool(struct pool *pool, unsigned int size)
Jens Axboed24c33a2008-03-01 18:27:36 +0100376{
Jens Axboeec996e92008-05-27 11:39:39 +0200377 unsigned int nr_blocks;
378 unsigned int i;
379 unsigned int offset;
380 unsigned int last_idx;
381 void *ret = NULL;
Jens Axboed24c33a2008-03-01 18:27:36 +0100382
Jens Axboed24c33a2008-03-01 18:27:36 +0100383 pool_lock(pool);
Jens Axboe179446e2008-05-27 14:11:56 +0200384
385 nr_blocks = size_to_blocks(size);
Jens Axboeec996e92008-05-27 11:39:39 +0200386 if (nr_blocks > pool->free_blocks)
Jens Axboed24c33a2008-03-01 18:27:36 +0100387 goto fail;
388
Jens Axboeec996e92008-05-27 11:39:39 +0200389 i = pool->next_non_full;
390 last_idx = 0;
391 offset = -1U;
392 while (i < pool->nr_blocks) {
393 unsigned int idx;
Jens Axboed24c33a2008-03-01 18:27:36 +0100394
Jens Axboeec996e92008-05-27 11:39:39 +0200395 if (pool->bitmap[i] == -1U) {
396 i++;
397 pool->next_non_full = i;
398 last_idx = 0;
399 continue;
Jens Axboed24c33a2008-03-01 18:27:36 +0100400 }
Jens Axboeec996e92008-05-27 11:39:39 +0200401
402 idx = find_next_zero(pool->bitmap[i], last_idx);
403 if (!blocks_free(&pool->bitmap[i], idx, nr_blocks)) {
404 idx += nr_blocks;
405 if (idx < SMALLOC_BPI)
406 last_idx = idx;
407 else {
408 last_idx = 0;
409 while (idx >= SMALLOC_BPI) {
410 i++;
411 idx -= SMALLOC_BPI;
412 }
413 }
414 continue;
415 }
416 set_blocks(&pool->bitmap[i], idx, nr_blocks);
417 offset = i * SMALLOC_BPL + idx * SMALLOC_BPB;
418 break;
Jens Axboed24c33a2008-03-01 18:27:36 +0100419 }
Jens Axboeec996e92008-05-27 11:39:39 +0200420
421 if (i < pool->nr_blocks) {
422 pool->free_blocks -= nr_blocks;
423 ret = pool->map + offset;
424 }
425fail:
Jens Axboed24c33a2008-03-01 18:27:36 +0100426 pool_unlock(pool);
Jens Axboeec996e92008-05-27 11:39:39 +0200427 return ret;
Jens Axboed24c33a2008-03-01 18:27:36 +0100428}
429
Jens Axboe55f64912008-05-26 09:37:21 +0200430static void *smalloc_pool(struct pool *pool, unsigned int size)
431{
Jens Axboeec996e92008-05-27 11:39:39 +0200432 struct block_hdr *hdr;
433 unsigned int alloc_size;
Jens Axboe55f64912008-05-26 09:37:21 +0200434 void *ptr;
435
Jens Axboeec996e92008-05-27 11:39:39 +0200436 alloc_size = size + sizeof(*hdr);
437#ifdef SMALLOC_REDZONE
438 alloc_size += sizeof(unsigned int);
Jens Axboe55f64912008-05-26 09:37:21 +0200439#endif
Jens Axboeec996e92008-05-27 11:39:39 +0200440
441 ptr = __smalloc_pool(pool, alloc_size);
442 if (!ptr) {
443 printf("failed allocating %u\n", alloc_size);
444 return NULL;
445 }
446
447 hdr = ptr;
448 hdr->size = alloc_size;
449 ptr += sizeof(*hdr);
450
451 fill_redzone(hdr);
452
453 memset(ptr, 0, size);
454 return ptr;
Jens Axboe55f64912008-05-26 09:37:21 +0200455}
456
Jens Axboed24c33a2008-03-01 18:27:36 +0100457void *smalloc(unsigned int size)
458{
459 unsigned int i;
460
Jens Axboe65864cf2008-03-03 10:36:36 +0100461 global_read_lock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100462 i = last_pool;
463
464 do {
465 for (; i < nr_pools; i++) {
466 void *ptr = smalloc_pool(&mp[i], size);
467
468 if (ptr) {
469 last_pool = i;
Jens Axboe65864cf2008-03-03 10:36:36 +0100470 global_read_unlock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100471 return ptr;
472 }
473 }
474 if (last_pool) {
475 last_pool = 0;
476 continue;
477 }
478
Jens Axboeec996e92008-05-27 11:39:39 +0200479 if (nr_pools + 1 > MAX_POOLS)
Jens Axboed24c33a2008-03-01 18:27:36 +0100480 break;
481 else {
482 i = nr_pools;
Jens Axboe65864cf2008-03-03 10:36:36 +0100483 global_read_unlock();
Jens Axboeadf57092008-04-16 19:43:17 +0200484 if (add_pool(&mp[nr_pools], size))
Jens Axboe65864cf2008-03-03 10:36:36 +0100485 goto out;
486 global_read_lock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100487 }
488 } while (1);
489
Jens Axboe65864cf2008-03-03 10:36:36 +0100490 global_read_unlock();
491out:
Jens Axboed24c33a2008-03-01 18:27:36 +0100492 return NULL;
493}
494
495char *smalloc_strdup(const char *str)
496{
497 char *ptr;
498
499 ptr = smalloc(strlen(str) + 1);
500 strcpy(ptr, str);
501 return ptr;
502}