smalloc: unlink pool file in add_pool()
[fio.git] / smalloc.c
CommitLineData
d24c33a4
JA
1/*
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
6548f47f 14#include "mutex.h"
b3268b92 15#include "arch/arch.h"
d24c33a4 16
6548f47f 17#define MP_SAFE /* define to make thread safe */
55f6491d 18#define SMALLOC_REDZONE /* define to detect memory corruption */
d24c33a4 19
ec996e9c
JA
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)
23
24#define INITIAL_SIZE 1024*1024 /* new pool size */
68857686 25#define MAX_POOLS 128 /* maximum number of pools to setup */
d24c33a4 26
55f6491d
JA
27#define SMALLOC_PRE_RED 0xdeadbeefU
28#define SMALLOC_POST_RED 0x5aa55aa5U
55f6491d 29
2b386d25
JA
30unsigned int smalloc_pool_size = INITIAL_SIZE;
31
d24c33a4 32struct pool {
6548f47f 33 struct fio_mutex *lock; /* protects this pool */
d24c33a4 34 void *map; /* map of blocks */
ec996e9c
JA
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;
d24c33a4
JA
39 int fd; /* memory backing fd */
40 char file[PATH_MAX]; /* filename for fd */
ec996e9c
JA
41 unsigned int mmap_size;
42};
43
44struct block_hdr {
45 unsigned int size;
46#ifdef SMALLOC_REDZONE
47 unsigned int prered;
48#endif
d24c33a4
JA
49};
50
51static struct pool mp[MAX_POOLS];
52static unsigned int nr_pools;
53static unsigned int last_pool;
6548f47f 54static struct fio_mutex *lock;
d24c33a4 55
d24c33a4
JA
56static inline void pool_lock(struct pool *pool)
57{
6548f47f
JA
58 if (pool->lock)
59 fio_mutex_down(pool->lock);
d24c33a4
JA
60}
61
62static inline void pool_unlock(struct pool *pool)
63{
6548f47f
JA
64 if (pool->lock)
65 fio_mutex_up(pool->lock);
d24c33a4
JA
66}
67
65864cf7 68static inline void global_read_lock(void)
d24c33a4 69{
6548f47f
JA
70 if (lock)
71 fio_mutex_down_read(lock);
d24c33a4
JA
72}
73
65864cf7 74static inline void global_read_unlock(void)
d24c33a4 75{
6548f47f
JA
76 if (lock)
77 fio_mutex_up_read(lock);
65864cf7
JA
78}
79
80static inline void global_write_lock(void)
81{
6548f47f
JA
82 if (lock)
83 fio_mutex_down_write(lock);
65864cf7
JA
84}
85
86static inline void global_write_unlock(void)
87{
6548f47f
JA
88 if (lock)
89 fio_mutex_up_write(lock);
d24c33a4
JA
90}
91
d24c33a4
JA
92static inline int ptr_valid(struct pool *pool, void *ptr)
93{
dcb69098 94 unsigned int pool_size = pool->nr_blocks * SMALLOC_BPL;
ec996e9c
JA
95
96 return (ptr >= pool->map) && (ptr < pool->map + pool_size);
d24c33a4
JA
97}
98
808e9ea8
JA
99static inline unsigned int size_to_blocks(unsigned int size)
100{
101 return (size + SMALLOC_BPB - 1) / SMALLOC_BPB;
102}
103
dcb69098
JA
104static int blocks_iter(struct pool *pool, unsigned int pool_idx,
105 unsigned int idx, unsigned int nr_blocks,
ec996e9c 106 int (*func)(unsigned int *map, unsigned int mask))
d24c33a4 107{
dcb69098 108
ec996e9c
JA
109 while (nr_blocks) {
110 unsigned int this_blocks, mask;
dcb69098
JA
111 unsigned int *map;
112
113 if (pool_idx >= pool->nr_blocks)
114 return 0;
115
116 map = &pool->bitmap[pool_idx];
ec996e9c
JA
117
118 this_blocks = nr_blocks;
119 if (this_blocks + idx > SMALLOC_BPI) {
120 this_blocks = SMALLOC_BPI - idx;
121 idx = SMALLOC_BPI - this_blocks;
122 }
123
124 if (this_blocks == SMALLOC_BPI)
125 mask = -1U;
126 else
127 mask = ((1U << this_blocks) - 1) << idx;
128
129 if (!func(map, mask))
130 return 0;
131
132 nr_blocks -= this_blocks;
133 idx = 0;
dcb69098 134 pool_idx++;
ec996e9c
JA
135 }
136
137 return 1;
d24c33a4
JA
138}
139
ec996e9c 140static int mask_cmp(unsigned int *map, unsigned int mask)
d24c33a4 141{
ec996e9c 142 return !(*map & mask);
d24c33a4
JA
143}
144
ec996e9c 145static int mask_clear(unsigned int *map, unsigned int mask)
d24c33a4 146{
dcb69098 147 assert((*map & mask) == mask);
ec996e9c
JA
148 *map &= ~mask;
149 return 1;
d24c33a4
JA
150}
151
ec996e9c 152static int mask_set(unsigned int *map, unsigned int mask)
d24c33a4 153{
dcb69098 154 assert(!(*map & mask));
ec996e9c
JA
155 *map |= mask;
156 return 1;
d24c33a4
JA
157}
158
dcb69098
JA
159static int blocks_free(struct pool *pool, unsigned int pool_idx,
160 unsigned int idx, unsigned int nr_blocks)
d24c33a4 161{
dcb69098 162 return blocks_iter(pool, pool_idx, idx, nr_blocks, mask_cmp);
d24c33a4
JA
163}
164
dcb69098
JA
165static void set_blocks(struct pool *pool, unsigned int pool_idx,
166 unsigned int idx, unsigned int nr_blocks)
d24c33a4 167{
dcb69098 168 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_set);
d24c33a4
JA
169}
170
dcb69098
JA
171static void clear_blocks(struct pool *pool, unsigned int pool_idx,
172 unsigned int idx, unsigned int nr_blocks)
d24c33a4 173{
dcb69098 174 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_clear);
d24c33a4
JA
175}
176
ec996e9c
JA
177static int find_next_zero(int word, int start)
178{
179 assert(word != -1U);
180 word >>= (start + 1);
b3268b92 181 return ffz(word) + start + 1;
d24c33a4
JA
182}
183
adf57099 184static int add_pool(struct pool *pool, unsigned int alloc_size)
d24c33a4 185{
d24c33a4 186 void *ptr;
ec996e9c
JA
187 int fd, bitmap_blocks;
188
d24c33a4
JA
189 strcpy(pool->file, "/tmp/.fio_smalloc.XXXXXX");
190 fd = mkstemp(pool->file);
191 if (fd < 0)
192 goto out_close;
193
55f6491d 194#ifdef SMALLOC_REDZONE
ec996e9c 195 alloc_size += sizeof(unsigned int);
55f6491d 196#endif
ec996e9c
JA
197 alloc_size += sizeof(struct block_hdr);
198 if (alloc_size < INITIAL_SIZE)
199 alloc_size = INITIAL_SIZE;
200
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;
55f6491d 206
ec996e9c
JA
207 pool->nr_blocks = bitmap_blocks;
208 pool->free_blocks = bitmap_blocks * SMALLOC_BPB;
adf57099 209
ec996e9c 210 if (ftruncate(fd, alloc_size) < 0)
d24c33a4
JA
211 goto out_unlink;
212
ec996e9c 213 ptr = mmap(NULL, alloc_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
d24c33a4
JA
214 if (ptr == MAP_FAILED)
215 goto out_unlink;
216
ec996e9c
JA
217 memset(ptr, 0, alloc_size);
218 pool->map = ptr;
219 pool->bitmap = (void *) ptr + (pool->nr_blocks * SMALLOC_BPL);
d24c33a4 220
6548f47f
JA
221#ifdef MP_SAFE
222 pool->lock = fio_mutex_init(1);
d24c33a4
JA
223 if (!pool->lock)
224 goto out_unlink;
6548f47f 225#endif
d24c33a4 226
443bb114
JA
227 /*
228 * Unlink pool file now. It wont get deleted until the fd is closed,
229 * which happens both for cleanup or unexpected quit. This way we
230 * don't leave temp files around in case of a crash.
231 */
d24c33a4 232 pool->fd = fd;
443bb114 233 unlink(pool->file);
d24c33a4 234
d24c33a4
JA
235 nr_pools++;
236 return 0;
237out_unlink:
ec996e9c 238 fprintf(stderr, "smalloc: failed adding pool\n");
d24c33a4 239 if (pool->map)
ec996e9c 240 munmap(pool->map, pool->mmap_size);
d24c33a4
JA
241 unlink(pool->file);
242out_close:
243 if (fd >= 0)
244 close(fd);
245 return 1;
246}
247
248void sinit(void)
249{
4d4e80f2 250 int ret;
d24c33a4 251
6548f47f
JA
252#ifdef MP_SAFE
253 lock = fio_mutex_rw_init();
254#endif
adf57099 255 ret = add_pool(&mp[0], INITIAL_SIZE);
d24c33a4
JA
256 assert(!ret);
257}
258
259static void cleanup_pool(struct pool *pool)
260{
443bb114
JA
261 /*
262 * This will also remove the temporary file we used as a backing
263 * store, it was already unlinked
264 */
d24c33a4 265 close(pool->fd);
ec996e9c 266 munmap(pool->map, pool->mmap_size);
6548f47f
JA
267
268 if (pool->lock)
269 fio_mutex_remove(pool->lock);
d24c33a4
JA
270}
271
272void scleanup(void)
273{
274 unsigned int i;
275
276 for (i = 0; i < nr_pools; i++)
277 cleanup_pool(&mp[i]);
278
6548f47f
JA
279 if (lock)
280 fio_mutex_remove(lock);
d24c33a4
JA
281}
282
89da54e8 283#ifdef SMALLOC_REDZONE
ec996e9c 284static void fill_redzone(struct block_hdr *hdr)
55f6491d 285{
ec996e9c 286 unsigned int *postred = (void *) hdr + hdr->size - sizeof(unsigned int);
55f6491d 287
ec996e9c
JA
288 hdr->prered = SMALLOC_PRE_RED;
289 *postred = SMALLOC_POST_RED;
ec996e9c 290}
55f6491d 291
ec996e9c
JA
292static void sfree_check_redzone(struct block_hdr *hdr)
293{
ec996e9c
JA
294 unsigned int *postred = (void *) hdr + hdr->size - sizeof(unsigned int);
295
296 if (hdr->prered != SMALLOC_PRE_RED) {
55f6491d
JA
297 fprintf(stderr, "smalloc pre redzone destroyed!\n");
298 fprintf(stderr, " ptr=%p, prered=%x, expected %x\n",
ec996e9c 299 hdr, hdr->prered, SMALLOC_PRE_RED);
55f6491d
JA
300 assert(0);
301 }
302 if (*postred != SMALLOC_POST_RED) {
303 fprintf(stderr, "smalloc post redzone destroyed!\n");
304 fprintf(stderr, " ptr=%p, postred=%x, expected %x\n",
ec996e9c 305 hdr, *postred, SMALLOC_POST_RED);
55f6491d
JA
306 assert(0);
307 }
89da54e8
JA
308}
309#else
310static void fill_redzone(struct block_hdr *hdr)
311{
55f6491d
JA
312}
313
89da54e8
JA
314static void sfree_check_redzone(struct block_hdr *hdr)
315{
316}
317#endif
318
d24c33a4
JA
319static void sfree_pool(struct pool *pool, void *ptr)
320{
ec996e9c 321 struct block_hdr *hdr;
179446e0 322 unsigned int i, idx;
ec996e9c 323 unsigned long offset;
d24c33a4
JA
324
325 if (!ptr)
326 return;
327
ec996e9c
JA
328 ptr -= sizeof(*hdr);
329 hdr = ptr;
55f6491d 330
d24c33a4
JA
331 assert(ptr_valid(pool, ptr));
332
ec996e9c 333 sfree_check_redzone(hdr);
d24c33a4 334
ec996e9c
JA
335 offset = ptr - pool->map;
336 i = offset / SMALLOC_BPL;
337 idx = (offset % SMALLOC_BPL) / SMALLOC_BPB;
d24c33a4 338
ec996e9c 339 pool_lock(pool);
dcb69098 340 clear_blocks(pool, i, idx, size_to_blocks(hdr->size));
ec996e9c
JA
341 if (i < pool->next_non_full)
342 pool->next_non_full = i;
179446e0 343 pool->free_blocks += size_to_blocks(hdr->size);
d24c33a4
JA
344 pool_unlock(pool);
345}
346
347void sfree(void *ptr)
348{
349 struct pool *pool = NULL;
350 unsigned int i;
351
8e5732e5
JA
352 if (!ptr)
353 return;
354
65864cf7 355 global_read_lock();
d24c33a4
JA
356
357 for (i = 0; i < nr_pools; i++) {
358 if (ptr_valid(&mp[i], ptr)) {
359 pool = &mp[i];
360 break;
361 }
362 }
363
65864cf7 364 global_read_unlock();
d24c33a4
JA
365
366 assert(pool);
367 sfree_pool(pool, ptr);
368}
369
55f6491d 370static void *__smalloc_pool(struct pool *pool, unsigned int size)
d24c33a4 371{
ec996e9c
JA
372 unsigned int nr_blocks;
373 unsigned int i;
374 unsigned int offset;
375 unsigned int last_idx;
376 void *ret = NULL;
d24c33a4 377
d24c33a4 378 pool_lock(pool);
179446e0
JA
379
380 nr_blocks = size_to_blocks(size);
ec996e9c 381 if (nr_blocks > pool->free_blocks)
8e5732e5 382 goto fail;
5ec10eaa 383
ec996e9c
JA
384 i = pool->next_non_full;
385 last_idx = 0;
386 offset = -1U;
387 while (i < pool->nr_blocks) {
388 unsigned int idx;
d24c33a4 389
ec996e9c
JA
390 if (pool->bitmap[i] == -1U) {
391 i++;
392 pool->next_non_full = i;
393 last_idx = 0;
394 continue;
395 }
d24c33a4 396
ec996e9c 397 idx = find_next_zero(pool->bitmap[i], last_idx);
dcb69098 398 if (!blocks_free(pool, i, idx, nr_blocks)) {
ec996e9c
JA
399 idx += nr_blocks;
400 if (idx < SMALLOC_BPI)
401 last_idx = idx;
402 else {
403 last_idx = 0;
404 while (idx >= SMALLOC_BPI) {
405 i++;
406 idx -= SMALLOC_BPI;
407 }
408 }
409 continue;
d24c33a4 410 }
dcb69098 411 set_blocks(pool, i, idx, nr_blocks);
ec996e9c
JA
412 offset = i * SMALLOC_BPL + idx * SMALLOC_BPB;
413 break;
414 }
415
416 if (i < pool->nr_blocks) {
417 pool->free_blocks -= nr_blocks;
418 ret = pool->map + offset;
d24c33a4 419 }
ec996e9c 420fail:
d24c33a4 421 pool_unlock(pool);
ec996e9c 422 return ret;
d24c33a4
JA
423}
424
55f6491d
JA
425static void *smalloc_pool(struct pool *pool, unsigned int size)
426{
89da54e8 427 unsigned int alloc_size = size + sizeof(struct block_hdr);
55f6491d
JA
428 void *ptr;
429
ec996e9c
JA
430#ifdef SMALLOC_REDZONE
431 alloc_size += sizeof(unsigned int);
432#endif
433
434 ptr = __smalloc_pool(pool, alloc_size);
89da54e8
JA
435 if (ptr) {
436 struct block_hdr *hdr = ptr;
55f6491d 437
89da54e8
JA
438 hdr->size = alloc_size;
439 fill_redzone(hdr);
55f6491d 440
89da54e8
JA
441 ptr += sizeof(*hdr);
442 memset(ptr, 0, size);
443 }
ec996e9c 444
55f6491d 445 return ptr;
55f6491d
JA
446}
447
d24c33a4
JA
448void *smalloc(unsigned int size)
449{
450 unsigned int i;
451
d1271dc1 452 global_write_lock();
d24c33a4
JA
453 i = last_pool;
454
455 do {
456 for (; i < nr_pools; i++) {
457 void *ptr = smalloc_pool(&mp[i], size);
458
459 if (ptr) {
460 last_pool = i;
d1271dc1 461 global_write_unlock();
d24c33a4
JA
462 return ptr;
463 }
464 }
465 if (last_pool) {
466 last_pool = 0;
467 continue;
468 }
469
ec996e9c 470 if (nr_pools + 1 > MAX_POOLS)
d24c33a4
JA
471 break;
472 else {
473 i = nr_pools;
adf57099 474 if (add_pool(&mp[nr_pools], size))
65864cf7 475 goto out;
d24c33a4
JA
476 }
477 } while (1);
478
65864cf7 479out:
d1271dc1 480 global_write_unlock();
d24c33a4
JA
481 return NULL;
482}
483
484char *smalloc_strdup(const char *str)
485{
486 char *ptr;
487
488 ptr = smalloc(strlen(str) + 1);
489 strcpy(ptr, str);
490 return ptr;
491}