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