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