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