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