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