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