smalloc: fix compile error
[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
14#include "mutex.h"
15
1f37f636 16#define MP_SAFE /* define to make thread safe */
55f6491d 17#define SMALLOC_REDZONE /* define to detect memory corruption */
d24c33a4 18
ec996e9c
JA
19#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 */
c5dda9e3 24#define MAX_POOLS 4 /* maximum number of pools to setup */
d24c33a4 25
55f6491d
JA
26#define SMALLOC_PRE_RED 0xdeadbeefU
27#define SMALLOC_POST_RED 0x5aa55aa5U
55f6491d 28
2b386d25
JA
29unsigned int smalloc_pool_size = INITIAL_SIZE;
30
d24c33a4
JA
31struct pool {
32 struct fio_mutex *lock; /* protects this pool */
33 void *map; /* map of blocks */
ec996e9c
JA
34 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;
d24c33a4
JA
38 int fd; /* memory backing fd */
39 char file[PATH_MAX]; /* filename for fd */
ec996e9c
JA
40 unsigned int mmap_size;
41};
42
43struct block_hdr {
44 unsigned int size;
45#ifdef SMALLOC_REDZONE
46 unsigned int prered;
47#endif
d24c33a4
JA
48};
49
50static struct pool mp[MAX_POOLS];
51static unsigned int nr_pools;
52static unsigned int last_pool;
53static struct fio_mutex *lock;
54
d24c33a4
JA
55static 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
65864cf7 67static inline void global_read_lock(void)
d24c33a4
JA
68{
69 if (lock)
65864cf7 70 fio_mutex_down_read(lock);
d24c33a4
JA
71}
72
65864cf7 73static inline void global_read_unlock(void)
d24c33a4
JA
74{
75 if (lock)
65864cf7
JA
76 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);
d24c33a4
JA
89}
90
d24c33a4
JA
91static inline int ptr_valid(struct pool *pool, void *ptr)
92{
ec996e9c
JA
93 unsigned int pool_size = pool->nr_blocks * SMALLOC_BPL;
94
95 return (ptr >= pool->map) && (ptr < pool->map + pool_size);
d24c33a4
JA
96}
97
808e9ea8
JA
98static inline unsigned int size_to_blocks(unsigned int size)
99{
100 return (size + SMALLOC_BPB - 1) / SMALLOC_BPB;
101}
102
ec996e9c
JA
103static int blocks_iter(unsigned int *map, unsigned int idx,
104 unsigned int nr_blocks,
105 int (*func)(unsigned int *map, unsigned int mask))
d24c33a4 106{
ec996e9c
JA
107 while (nr_blocks) {
108 unsigned int this_blocks, mask;
109
110 this_blocks = nr_blocks;
111 if (this_blocks + idx > SMALLOC_BPI) {
112 this_blocks = SMALLOC_BPI - idx;
113 idx = SMALLOC_BPI - this_blocks;
114 }
115
116 if (this_blocks == SMALLOC_BPI)
117 mask = -1U;
118 else
119 mask = ((1U << this_blocks) - 1) << idx;
120
121 if (!func(map, mask))
122 return 0;
123
124 nr_blocks -= this_blocks;
125 idx = 0;
126 map++;
127 }
128
129 return 1;
d24c33a4
JA
130}
131
ec996e9c 132static int mask_cmp(unsigned int *map, unsigned int mask)
d24c33a4 133{
ec996e9c 134 return !(*map & mask);
d24c33a4
JA
135}
136
ec996e9c 137static int mask_clear(unsigned int *map, unsigned int mask)
d24c33a4 138{
ec996e9c
JA
139 *map &= ~mask;
140 return 1;
d24c33a4
JA
141}
142
ec996e9c 143static int mask_set(unsigned int *map, unsigned int mask)
d24c33a4 144{
ec996e9c
JA
145 *map |= mask;
146 return 1;
d24c33a4
JA
147}
148
ec996e9c
JA
149static int blocks_free(unsigned int *map, unsigned int idx,
150 unsigned int nr_blocks)
d24c33a4 151{
ec996e9c 152 return blocks_iter(map, idx, nr_blocks, mask_cmp);
d24c33a4
JA
153}
154
ec996e9c
JA
155static void set_blocks(unsigned int *map, unsigned int idx,
156 unsigned int nr_blocks)
d24c33a4 157{
ec996e9c 158 blocks_iter(map, idx, nr_blocks, mask_set);
d24c33a4
JA
159}
160
ec996e9c
JA
161static void clear_blocks(unsigned int *map, unsigned int idx,
162 unsigned int nr_blocks)
d24c33a4 163{
ec996e9c 164 blocks_iter(map, idx, nr_blocks, mask_clear);
d24c33a4
JA
165}
166
ec996e9c 167static inline int __ffs(int word)
d24c33a4 168{
ec996e9c 169 int r = 0;
d24c33a4 170
ec996e9c
JA
171 if (!(word & 0xffff)) {
172 word >>= 16;
173 r += 16;
174 }
175 if (!(word & 0xff)) {
176 word >>= 8;
177 r += 8;
178 }
179 if (!(word & 0xf)) {
180 word >>= 4;
181 r += 4;
182 }
183 if (!(word & 3)) {
184 word >>= 2;
185 r += 2;
186 }
187 if (!(word & 1)) {
188 word >>= 1;
189 r += 1;
d24c33a4
JA
190 }
191
ec996e9c
JA
192 return r;
193}
194
195static int find_next_zero(int word, int start)
196{
197 assert(word != -1U);
198 word >>= (start + 1);
199 return __ffs(~word) + start + 1;
d24c33a4
JA
200}
201
adf57099 202static int add_pool(struct pool *pool, unsigned int alloc_size)
d24c33a4 203{
d24c33a4 204 void *ptr;
ec996e9c
JA
205 int fd, bitmap_blocks;
206
d24c33a4
JA
207 strcpy(pool->file, "/tmp/.fio_smalloc.XXXXXX");
208 fd = mkstemp(pool->file);
209 if (fd < 0)
210 goto out_close;
211
55f6491d 212#ifdef SMALLOC_REDZONE
ec996e9c 213 alloc_size += sizeof(unsigned int);
55f6491d 214#endif
ec996e9c
JA
215 alloc_size += sizeof(struct block_hdr);
216 if (alloc_size < INITIAL_SIZE)
217 alloc_size = INITIAL_SIZE;
218
219 /* round up to nearest full number of blocks */
220 alloc_size = (alloc_size + SMALLOC_BPL - 1) & ~(SMALLOC_BPL - 1);
221 bitmap_blocks = alloc_size / SMALLOC_BPL;
222 alloc_size += bitmap_blocks * sizeof(unsigned int);
223 pool->mmap_size = alloc_size;
55f6491d 224
ec996e9c
JA
225 pool->nr_blocks = bitmap_blocks;
226 pool->free_blocks = bitmap_blocks * SMALLOC_BPB;
adf57099 227
ec996e9c 228 if (ftruncate(fd, alloc_size) < 0)
d24c33a4
JA
229 goto out_unlink;
230
ec996e9c 231 ptr = mmap(NULL, alloc_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
d24c33a4
JA
232 if (ptr == MAP_FAILED)
233 goto out_unlink;
234
ec996e9c
JA
235 memset(ptr, 0, alloc_size);
236 pool->map = ptr;
237 pool->bitmap = (void *) ptr + (pool->nr_blocks * SMALLOC_BPL);
d24c33a4
JA
238
239#ifdef MP_SAFE
240 pool->lock = fio_mutex_init(1);
241 if (!pool->lock)
242 goto out_unlink;
243#endif
244
245 pool->fd = fd;
246
65864cf7 247 global_write_lock();
d24c33a4 248 nr_pools++;
65864cf7 249 global_write_unlock();
d24c33a4
JA
250 return 0;
251out_unlink:
ec996e9c 252 fprintf(stderr, "smalloc: failed adding pool\n");
d24c33a4 253 if (pool->map)
ec996e9c 254 munmap(pool->map, pool->mmap_size);
d24c33a4
JA
255 unlink(pool->file);
256out_close:
257 if (fd >= 0)
258 close(fd);
259 return 1;
260}
261
262void sinit(void)
263{
4d4e80f2 264 int ret;
d24c33a4
JA
265
266#ifdef MP_SAFE
9c5b5290 267 lock = fio_mutex_rw_init();
d24c33a4 268#endif
adf57099 269 ret = add_pool(&mp[0], INITIAL_SIZE);
d24c33a4
JA
270 assert(!ret);
271}
272
273static void cleanup_pool(struct pool *pool)
274{
275 unlink(pool->file);
276 close(pool->fd);
ec996e9c 277 munmap(pool->map, pool->mmap_size);
d24c33a4
JA
278
279 if (pool->lock)
280 fio_mutex_remove(pool->lock);
281}
282
283void scleanup(void)
284{
285 unsigned int i;
286
287 for (i = 0; i < nr_pools; i++)
288 cleanup_pool(&mp[i]);
289
290 if (lock)
291 fio_mutex_remove(lock);
292}
293
ec996e9c 294static void fill_redzone(struct block_hdr *hdr)
55f6491d
JA
295{
296#ifdef SMALLOC_REDZONE
ec996e9c 297 unsigned int *postred = (void *) hdr + hdr->size - sizeof(unsigned int);
55f6491d 298
ec996e9c
JA
299 hdr->prered = SMALLOC_PRE_RED;
300 *postred = SMALLOC_POST_RED;
301#endif
302}
55f6491d 303
ec996e9c
JA
304static void sfree_check_redzone(struct block_hdr *hdr)
305{
306#ifdef SMALLOC_REDZONE
307 unsigned int *postred = (void *) hdr + hdr->size - sizeof(unsigned int);
308
309 if (hdr->prered != SMALLOC_PRE_RED) {
55f6491d
JA
310 fprintf(stderr, "smalloc pre redzone destroyed!\n");
311 fprintf(stderr, " ptr=%p, prered=%x, expected %x\n",
ec996e9c 312 hdr, hdr->prered, SMALLOC_PRE_RED);
55f6491d
JA
313 assert(0);
314 }
315 if (*postred != SMALLOC_POST_RED) {
316 fprintf(stderr, "smalloc post redzone destroyed!\n");
317 fprintf(stderr, " ptr=%p, postred=%x, expected %x\n",
ec996e9c 318 hdr, *postred, SMALLOC_POST_RED);
55f6491d
JA
319 assert(0);
320 }
321#endif
322}
323
d24c33a4
JA
324static void sfree_pool(struct pool *pool, void *ptr)
325{
ec996e9c 326 struct block_hdr *hdr;
179446e0 327 unsigned int i, idx;
ec996e9c 328 unsigned long offset;
d24c33a4
JA
329
330 if (!ptr)
331 return;
332
ec996e9c
JA
333 ptr -= sizeof(*hdr);
334 hdr = ptr;
55f6491d 335
d24c33a4
JA
336 assert(ptr_valid(pool, ptr));
337
ec996e9c 338 sfree_check_redzone(hdr);
d24c33a4 339
ec996e9c
JA
340 offset = ptr - pool->map;
341 i = offset / SMALLOC_BPL;
342 idx = (offset % SMALLOC_BPL) / SMALLOC_BPB;
d24c33a4 343
ec996e9c 344 pool_lock(pool);
179446e0 345 clear_blocks(&pool->bitmap[i], idx, size_to_blocks(hdr->size));
ec996e9c
JA
346 if (i < pool->next_non_full)
347 pool->next_non_full = i;
179446e0 348 pool->free_blocks += size_to_blocks(hdr->size);
d24c33a4
JA
349 pool_unlock(pool);
350}
351
352void sfree(void *ptr)
353{
354 struct pool *pool = NULL;
355 unsigned int i;
356
8e5732e5
JA
357 if (!ptr)
358 return;
359
65864cf7 360 global_read_lock();
d24c33a4
JA
361
362 for (i = 0; i < nr_pools; i++) {
363 if (ptr_valid(&mp[i], ptr)) {
364 pool = &mp[i];
365 break;
366 }
367 }
368
65864cf7 369 global_read_unlock();
d24c33a4
JA
370
371 assert(pool);
372 sfree_pool(pool, ptr);
373}
374
55f6491d 375static void *__smalloc_pool(struct pool *pool, unsigned int size)
d24c33a4 376{
ec996e9c
JA
377 unsigned int nr_blocks;
378 unsigned int i;
379 unsigned int offset;
380 unsigned int last_idx;
381 void *ret = NULL;
d24c33a4 382
d24c33a4 383 pool_lock(pool);
179446e0
JA
384
385 nr_blocks = size_to_blocks(size);
ec996e9c 386 if (nr_blocks > pool->free_blocks)
8e5732e5 387 goto fail;
5ec10eaa 388
ec996e9c
JA
389 i = pool->next_non_full;
390 last_idx = 0;
391 offset = -1U;
392 while (i < pool->nr_blocks) {
393 unsigned int idx;
d24c33a4 394
ec996e9c
JA
395 if (pool->bitmap[i] == -1U) {
396 i++;
397 pool->next_non_full = i;
398 last_idx = 0;
399 continue;
400 }
d24c33a4 401
ec996e9c
JA
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;
d24c33a4 415 }
ec996e9c
JA
416 set_blocks(&pool->bitmap[i], idx, nr_blocks);
417 offset = i * SMALLOC_BPL + idx * SMALLOC_BPB;
418 break;
419 }
420
421 if (i < pool->nr_blocks) {
422 pool->free_blocks -= nr_blocks;
423 ret = pool->map + offset;
d24c33a4 424 }
ec996e9c 425fail:
d24c33a4 426 pool_unlock(pool);
ec996e9c 427 return ret;
d24c33a4
JA
428}
429
55f6491d
JA
430static void *smalloc_pool(struct pool *pool, unsigned int size)
431{
ec996e9c
JA
432 struct block_hdr *hdr;
433 unsigned int alloc_size;
55f6491d
JA
434 void *ptr;
435
ec996e9c
JA
436 alloc_size = size + sizeof(*hdr);
437#ifdef SMALLOC_REDZONE
438 alloc_size += sizeof(unsigned int);
439#endif
440
441 ptr = __smalloc_pool(pool, alloc_size);
442 if (!ptr) {
443 printf("failed allocating %u\n", alloc_size);
55f6491d 444 return NULL;
ec996e9c 445 }
55f6491d 446
ec996e9c
JA
447 hdr = ptr;
448 hdr->size = alloc_size;
449 ptr += sizeof(*hdr);
55f6491d 450
ec996e9c
JA
451 fill_redzone(hdr);
452
453 memset(ptr, 0, size);
55f6491d 454 return ptr;
55f6491d
JA
455}
456
d24c33a4
JA
457void *smalloc(unsigned int size)
458{
459 unsigned int i;
460
65864cf7 461 global_read_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;
65864cf7 470 global_read_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;
65864cf7 483 global_read_unlock();
adf57099 484 if (add_pool(&mp[nr_pools], size))
65864cf7
JA
485 goto out;
486 global_read_lock();
d24c33a4
JA
487 }
488 } while (1);
489
65864cf7
JA
490 global_read_unlock();
491out:
d24c33a4
JA
492 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}