Fix spelling error in engines/rdma.c
[fio.git] / smalloc.c
... / ...
CommitLineData
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 <inttypes.h>
12#include <sys/types.h>
13#include <limits.h>
14#include <fcntl.h>
15
16#include "mutex.h"
17#include "arch/arch.h"
18#include "os/os.h"
19#include "smalloc.h"
20#include "log.h"
21
22#define SMALLOC_REDZONE /* define to detect memory corruption */
23
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
28#define INITIAL_SIZE 16*1024*1024 /* new pool size */
29#define MAX_POOLS 8 /* maximum number of pools to setup */
30
31#define SMALLOC_PRE_RED 0xdeadbeefU
32#define SMALLOC_POST_RED 0x5aa55aa5U
33
34unsigned int smalloc_pool_size = INITIAL_SIZE;
35#ifdef SMALLOC_REDZONE
36static const int int_mask = sizeof(int) - 1;
37#endif
38
39struct pool {
40 struct fio_mutex *lock; /* protects this pool */
41 void *map; /* map of blocks */
42 unsigned int *bitmap; /* blocks free/busy map */
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;
47};
48
49struct block_hdr {
50 size_t size;
51#ifdef SMALLOC_REDZONE
52 unsigned int prered;
53#endif
54};
55
56static struct pool mp[MAX_POOLS];
57static unsigned int nr_pools;
58static unsigned int last_pool;
59static struct fio_rwlock *lock;
60
61static inline void pool_lock(struct pool *pool)
62{
63 fio_mutex_down(pool->lock);
64}
65
66static inline void pool_unlock(struct pool *pool)
67{
68 fio_mutex_up(pool->lock);
69}
70
71static inline void global_read_lock(void)
72{
73 fio_rwlock_read(lock);
74}
75
76static inline void global_read_unlock(void)
77{
78 fio_rwlock_unlock(lock);
79}
80
81static inline void global_write_lock(void)
82{
83 fio_rwlock_write(lock);
84}
85
86static inline void global_write_unlock(void)
87{
88 fio_rwlock_unlock(lock);
89}
90
91static inline int ptr_valid(struct pool *pool, void *ptr)
92{
93 unsigned int pool_size = pool->nr_blocks * SMALLOC_BPL;
94
95 return (ptr >= pool->map) && (ptr < pool->map + pool_size);
96}
97
98static inline size_t size_to_blocks(size_t size)
99{
100 return (size + SMALLOC_BPB - 1) / SMALLOC_BPB;
101}
102
103static int blocks_iter(struct pool *pool, unsigned int pool_idx,
104 unsigned int idx, size_t nr_blocks,
105 int (*func)(unsigned int *map, unsigned int mask))
106{
107
108 while (nr_blocks) {
109 unsigned int this_blocks, mask;
110 unsigned int *map;
111
112 if (pool_idx >= pool->nr_blocks)
113 return 0;
114
115 map = &pool->bitmap[pool_idx];
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;
133 pool_idx++;
134 }
135
136 return 1;
137}
138
139static int mask_cmp(unsigned int *map, unsigned int mask)
140{
141 return !(*map & mask);
142}
143
144static int mask_clear(unsigned int *map, unsigned int mask)
145{
146 assert((*map & mask) == mask);
147 *map &= ~mask;
148 return 1;
149}
150
151static int mask_set(unsigned int *map, unsigned int mask)
152{
153 assert(!(*map & mask));
154 *map |= mask;
155 return 1;
156}
157
158static int blocks_free(struct pool *pool, unsigned int pool_idx,
159 unsigned int idx, size_t nr_blocks)
160{
161 return blocks_iter(pool, pool_idx, idx, nr_blocks, mask_cmp);
162}
163
164static void set_blocks(struct pool *pool, unsigned int pool_idx,
165 unsigned int idx, size_t nr_blocks)
166{
167 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_set);
168}
169
170static void clear_blocks(struct pool *pool, unsigned int pool_idx,
171 unsigned int idx, size_t nr_blocks)
172{
173 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_clear);
174}
175
176static int find_next_zero(int word, int start)
177{
178 assert(word != -1U);
179 word >>= start;
180 return ffz(word) + start;
181}
182
183static int add_pool(struct pool *pool, unsigned int alloc_size)
184{
185 int bitmap_blocks;
186 int mmap_flags;
187 void *ptr;
188
189#ifdef SMALLOC_REDZONE
190 alloc_size += sizeof(unsigned int);
191#endif
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;
201
202 pool->nr_blocks = bitmap_blocks;
203 pool->free_blocks = bitmap_blocks * SMALLOC_BPB;
204
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
213 if (ptr == MAP_FAILED)
214 goto out_fail;
215
216 memset(ptr, 0, alloc_size);
217 pool->map = ptr;
218 pool->bitmap = (void *) ptr + (pool->nr_blocks * SMALLOC_BPL);
219
220 pool->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
221 if (!pool->lock)
222 goto out_fail;
223
224 nr_pools++;
225 return 0;
226out_fail:
227 log_err("smalloc: failed adding pool\n");
228 if (pool->map)
229 munmap(pool->map, pool->mmap_size);
230 return 1;
231}
232
233void sinit(void)
234{
235 int i, ret;
236
237 lock = fio_rwlock_init();
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);
250}
251
252static void cleanup_pool(struct pool *pool)
253{
254 /*
255 * This will also remove the temporary file we used as a backing
256 * store, it was already unlinked
257 */
258 munmap(pool->map, pool->mmap_size);
259
260 if (pool->lock)
261 fio_mutex_remove(pool->lock);
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
271 if (lock)
272 fio_rwlock_remove(lock);
273}
274
275#ifdef SMALLOC_REDZONE
276static void *postred_ptr(struct block_hdr *hdr)
277{
278 uintptr_t ptr;
279
280 ptr = (uintptr_t) hdr + hdr->size - sizeof(unsigned int);
281 ptr = (ptr + int_mask) & ~int_mask;
282
283 return (void *) ptr;
284}
285
286static void fill_redzone(struct block_hdr *hdr)
287{
288 unsigned int *postred = postred_ptr(hdr);
289
290 hdr->prered = SMALLOC_PRE_RED;
291 *postred = SMALLOC_POST_RED;
292}
293
294static void sfree_check_redzone(struct block_hdr *hdr)
295{
296 unsigned int *postred = postred_ptr(hdr);
297
298 if (hdr->prered != SMALLOC_PRE_RED) {
299 log_err("smalloc pre redzone destroyed!\n"
300 " ptr=%p, prered=%x, expected %x\n",
301 hdr, hdr->prered, SMALLOC_PRE_RED);
302 assert(0);
303 }
304 if (*postred != SMALLOC_POST_RED) {
305 log_err("smalloc post redzone destroyed!\n"
306 " ptr=%p, postred=%x, expected %x\n",
307 hdr, *postred, SMALLOC_POST_RED);
308 assert(0);
309 }
310}
311#else
312static void fill_redzone(struct block_hdr *hdr)
313{
314}
315
316static void sfree_check_redzone(struct block_hdr *hdr)
317{
318}
319#endif
320
321static void sfree_pool(struct pool *pool, void *ptr)
322{
323 struct block_hdr *hdr;
324 unsigned int i, idx;
325 unsigned long offset;
326
327 if (!ptr)
328 return;
329
330 ptr -= sizeof(*hdr);
331 hdr = ptr;
332
333 assert(ptr_valid(pool, ptr));
334
335 sfree_check_redzone(hdr);
336
337 offset = ptr - pool->map;
338 i = offset / SMALLOC_BPL;
339 idx = (offset % SMALLOC_BPL) / SMALLOC_BPB;
340
341 pool_lock(pool);
342 clear_blocks(pool, i, idx, size_to_blocks(hdr->size));
343 if (i < pool->next_non_full)
344 pool->next_non_full = i;
345 pool->free_blocks += size_to_blocks(hdr->size);
346 pool_unlock(pool);
347}
348
349void sfree(void *ptr)
350{
351 struct pool *pool = NULL;
352 unsigned int i;
353
354 if (!ptr)
355 return;
356
357 global_read_lock();
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
366 global_read_unlock();
367
368 if (pool) {
369 sfree_pool(pool, ptr);
370 return;
371 }
372
373 log_err("smalloc: ptr %p not from smalloc pool\n", ptr);
374}
375
376static void *__smalloc_pool(struct pool *pool, size_t size)
377{
378 size_t nr_blocks;
379 unsigned int i;
380 unsigned int offset;
381 unsigned int last_idx;
382 void *ret = NULL;
383
384 pool_lock(pool);
385
386 nr_blocks = size_to_blocks(size);
387 if (nr_blocks > pool->free_blocks)
388 goto fail;
389
390 i = pool->next_non_full;
391 last_idx = 0;
392 offset = -1U;
393 while (i < pool->nr_blocks) {
394 unsigned int idx;
395
396 if (pool->bitmap[i] == -1U) {
397 i++;
398 pool->next_non_full = i;
399 last_idx = 0;
400 continue;
401 }
402
403 idx = find_next_zero(pool->bitmap[i], last_idx);
404 if (!blocks_free(pool, i, idx, nr_blocks)) {
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;
416 }
417 set_blocks(pool, i, idx, nr_blocks);
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;
425 }
426fail:
427 pool_unlock(pool);
428 return ret;
429}
430
431static void *smalloc_pool(struct pool *pool, size_t size)
432{
433 size_t alloc_size = size + sizeof(struct block_hdr);
434 void *ptr;
435
436 /*
437 * Round to int alignment, so that the postred pointer will
438 * be naturally aligned as well.
439 */
440#ifdef SMALLOC_REDZONE
441 alloc_size += sizeof(unsigned int);
442 alloc_size = (alloc_size + int_mask) & ~int_mask;
443#endif
444
445 ptr = __smalloc_pool(pool, alloc_size);
446 if (ptr) {
447 struct block_hdr *hdr = ptr;
448
449 hdr->size = alloc_size;
450 fill_redzone(hdr);
451
452 ptr += sizeof(*hdr);
453 memset(ptr, 0, size);
454 }
455
456 return ptr;
457}
458
459void *smalloc(size_t size)
460{
461 unsigned int i, end_pool;
462
463 if (size != (unsigned int) size)
464 return NULL;
465
466 global_write_lock();
467 i = last_pool;
468 end_pool = nr_pools;
469
470 do {
471 for (; i < end_pool; i++) {
472 void *ptr = smalloc_pool(&mp[i], size);
473
474 if (ptr) {
475 last_pool = i;
476 global_write_unlock();
477 return ptr;
478 }
479 }
480 if (last_pool) {
481 end_pool = last_pool;
482 last_pool = i = 0;
483 continue;
484 }
485
486 break;
487 } while (1);
488
489 global_write_unlock();
490 return NULL;
491}
492
493void *scalloc(size_t nmemb, size_t size)
494{
495 return smalloc(nmemb * size);
496}
497
498char *smalloc_strdup(const char *str)
499{
500 char *ptr = NULL;
501
502 ptr = smalloc(strlen(str) + 1);
503 if (ptr)
504 strcpy(ptr, str);
505 return ptr;
506}