Fix bug in parser that causes a crash with a bad option
[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
JA
226
227 pool->fd = fd;
228
d24c33a4
JA
229 nr_pools++;
230 return 0;
231out_unlink:
ec996e9c 232 fprintf(stderr, "smalloc: failed adding pool\n");
d24c33a4 233 if (pool->map)
ec996e9c 234 munmap(pool->map, pool->mmap_size);
d24c33a4
JA
235 unlink(pool->file);
236out_close:
237 if (fd >= 0)
238 close(fd);
239 return 1;
240}
241
242void sinit(void)
243{
4d4e80f2 244 int ret;
d24c33a4 245
6548f47f
JA
246#ifdef MP_SAFE
247 lock = fio_mutex_rw_init();
248#endif
adf57099 249 ret = add_pool(&mp[0], INITIAL_SIZE);
d24c33a4
JA
250 assert(!ret);
251}
252
253static void cleanup_pool(struct pool *pool)
254{
255 unlink(pool->file);
256 close(pool->fd);
ec996e9c 257 munmap(pool->map, pool->mmap_size);
6548f47f
JA
258
259 if (pool->lock)
260 fio_mutex_remove(pool->lock);
d24c33a4
JA
261}
262
263void scleanup(void)
264{
265 unsigned int i;
266
267 for (i = 0; i < nr_pools; i++)
268 cleanup_pool(&mp[i]);
269
6548f47f
JA
270 if (lock)
271 fio_mutex_remove(lock);
d24c33a4
JA
272}
273
89da54e8 274#ifdef SMALLOC_REDZONE
ec996e9c 275static void fill_redzone(struct block_hdr *hdr)
55f6491d 276{
ec996e9c 277 unsigned int *postred = (void *) hdr + hdr->size - sizeof(unsigned int);
55f6491d 278
ec996e9c
JA
279 hdr->prered = SMALLOC_PRE_RED;
280 *postred = SMALLOC_POST_RED;
ec996e9c 281}
55f6491d 282
ec996e9c
JA
283static void sfree_check_redzone(struct block_hdr *hdr)
284{
ec996e9c
JA
285 unsigned int *postred = (void *) hdr + hdr->size - sizeof(unsigned int);
286
287 if (hdr->prered != SMALLOC_PRE_RED) {
55f6491d
JA
288 fprintf(stderr, "smalloc pre redzone destroyed!\n");
289 fprintf(stderr, " ptr=%p, prered=%x, expected %x\n",
ec996e9c 290 hdr, hdr->prered, SMALLOC_PRE_RED);
55f6491d
JA
291 assert(0);
292 }
293 if (*postred != SMALLOC_POST_RED) {
294 fprintf(stderr, "smalloc post redzone destroyed!\n");
295 fprintf(stderr, " ptr=%p, postred=%x, expected %x\n",
ec996e9c 296 hdr, *postred, SMALLOC_POST_RED);
55f6491d
JA
297 assert(0);
298 }
89da54e8
JA
299}
300#else
301static void fill_redzone(struct block_hdr *hdr)
302{
55f6491d
JA
303}
304
89da54e8
JA
305static void sfree_check_redzone(struct block_hdr *hdr)
306{
307}
308#endif
309
d24c33a4
JA
310static void sfree_pool(struct pool *pool, void *ptr)
311{
ec996e9c 312 struct block_hdr *hdr;
179446e0 313 unsigned int i, idx;
ec996e9c 314 unsigned long offset;
d24c33a4
JA
315
316 if (!ptr)
317 return;
318
ec996e9c
JA
319 ptr -= sizeof(*hdr);
320 hdr = ptr;
55f6491d 321
d24c33a4
JA
322 assert(ptr_valid(pool, ptr));
323
ec996e9c 324 sfree_check_redzone(hdr);
d24c33a4 325
ec996e9c
JA
326 offset = ptr - pool->map;
327 i = offset / SMALLOC_BPL;
328 idx = (offset % SMALLOC_BPL) / SMALLOC_BPB;
d24c33a4 329
ec996e9c 330 pool_lock(pool);
dcb69098 331 clear_blocks(pool, i, idx, size_to_blocks(hdr->size));
ec996e9c
JA
332 if (i < pool->next_non_full)
333 pool->next_non_full = i;
179446e0 334 pool->free_blocks += size_to_blocks(hdr->size);
d24c33a4
JA
335 pool_unlock(pool);
336}
337
338void sfree(void *ptr)
339{
340 struct pool *pool = NULL;
341 unsigned int i;
342
8e5732e5
JA
343 if (!ptr)
344 return;
345
65864cf7 346 global_read_lock();
d24c33a4
JA
347
348 for (i = 0; i < nr_pools; i++) {
349 if (ptr_valid(&mp[i], ptr)) {
350 pool = &mp[i];
351 break;
352 }
353 }
354
65864cf7 355 global_read_unlock();
d24c33a4
JA
356
357 assert(pool);
358 sfree_pool(pool, ptr);
359}
360
55f6491d 361static void *__smalloc_pool(struct pool *pool, unsigned int size)
d24c33a4 362{
ec996e9c
JA
363 unsigned int nr_blocks;
364 unsigned int i;
365 unsigned int offset;
366 unsigned int last_idx;
367 void *ret = NULL;
d24c33a4 368
d24c33a4 369 pool_lock(pool);
179446e0
JA
370
371 nr_blocks = size_to_blocks(size);
ec996e9c 372 if (nr_blocks > pool->free_blocks)
8e5732e5 373 goto fail;
5ec10eaa 374
ec996e9c
JA
375 i = pool->next_non_full;
376 last_idx = 0;
377 offset = -1U;
378 while (i < pool->nr_blocks) {
379 unsigned int idx;
d24c33a4 380
ec996e9c
JA
381 if (pool->bitmap[i] == -1U) {
382 i++;
383 pool->next_non_full = i;
384 last_idx = 0;
385 continue;
386 }
d24c33a4 387
ec996e9c 388 idx = find_next_zero(pool->bitmap[i], last_idx);
dcb69098 389 if (!blocks_free(pool, i, idx, nr_blocks)) {
ec996e9c
JA
390 idx += nr_blocks;
391 if (idx < SMALLOC_BPI)
392 last_idx = idx;
393 else {
394 last_idx = 0;
395 while (idx >= SMALLOC_BPI) {
396 i++;
397 idx -= SMALLOC_BPI;
398 }
399 }
400 continue;
d24c33a4 401 }
dcb69098 402 set_blocks(pool, i, idx, nr_blocks);
ec996e9c
JA
403 offset = i * SMALLOC_BPL + idx * SMALLOC_BPB;
404 break;
405 }
406
407 if (i < pool->nr_blocks) {
408 pool->free_blocks -= nr_blocks;
409 ret = pool->map + offset;
d24c33a4 410 }
ec996e9c 411fail:
d24c33a4 412 pool_unlock(pool);
ec996e9c 413 return ret;
d24c33a4
JA
414}
415
55f6491d
JA
416static void *smalloc_pool(struct pool *pool, unsigned int size)
417{
89da54e8 418 unsigned int alloc_size = size + sizeof(struct block_hdr);
55f6491d
JA
419 void *ptr;
420
ec996e9c
JA
421#ifdef SMALLOC_REDZONE
422 alloc_size += sizeof(unsigned int);
423#endif
424
425 ptr = __smalloc_pool(pool, alloc_size);
89da54e8
JA
426 if (ptr) {
427 struct block_hdr *hdr = ptr;
55f6491d 428
89da54e8
JA
429 hdr->size = alloc_size;
430 fill_redzone(hdr);
55f6491d 431
89da54e8
JA
432 ptr += sizeof(*hdr);
433 memset(ptr, 0, size);
434 }
ec996e9c 435
55f6491d 436 return ptr;
55f6491d
JA
437}
438
d24c33a4
JA
439void *smalloc(unsigned int size)
440{
441 unsigned int i;
442
d1271dc1 443 global_write_lock();
d24c33a4
JA
444 i = last_pool;
445
446 do {
447 for (; i < nr_pools; i++) {
448 void *ptr = smalloc_pool(&mp[i], size);
449
450 if (ptr) {
451 last_pool = i;
d1271dc1 452 global_write_unlock();
d24c33a4
JA
453 return ptr;
454 }
455 }
456 if (last_pool) {
457 last_pool = 0;
458 continue;
459 }
460
ec996e9c 461 if (nr_pools + 1 > MAX_POOLS)
d24c33a4
JA
462 break;
463 else {
464 i = nr_pools;
adf57099 465 if (add_pool(&mp[nr_pools], size))
65864cf7 466 goto out;
d24c33a4
JA
467 }
468 } while (1);
469
65864cf7 470out:
d1271dc1 471 global_write_unlock();
d24c33a4
JA
472 return NULL;
473}
474
475char *smalloc_strdup(const char *str)
476{
477 char *ptr;
478
479 ptr = smalloc(strlen(str) + 1);
480 strcpy(ptr, str);
481 return ptr;
482}