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