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