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