Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 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> |
Bruce Cran | e43606c | 2012-02-20 09:34:24 +0100 | [diff] [blame] | 11 | #include <inttypes.h> |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 12 | #include <sys/types.h> |
| 13 | #include <limits.h> |
Greg Edwards | 3a8600b | 2010-06-25 09:24:19 -0600 | [diff] [blame] | 14 | #include <fcntl.h> |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 15 | |
Jens Axboe | 6548f47 | 2008-06-13 08:38:31 +0200 | [diff] [blame] | 16 | #include "mutex.h" |
Jens Axboe | b3268b9 | 2008-06-02 09:59:32 +0200 | [diff] [blame] | 17 | #include "arch/arch.h" |
Greg Edwards | 3a8600b | 2010-06-25 09:24:19 -0600 | [diff] [blame] | 18 | #include "os/os.h" |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 19 | |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 20 | #define SMALLOC_REDZONE /* define to detect memory corruption */ |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 21 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 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 | |
Jens Axboe | 5e01298 | 2009-07-31 11:23:19 +0200 | [diff] [blame] | 26 | #define INITIAL_SIZE 8192*1024 /* new pool size */ |
Jens Axboe | 6885768 | 2008-07-31 19:50:54 +0200 | [diff] [blame] | 27 | #define MAX_POOLS 128 /* maximum number of pools to setup */ |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 28 | |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 29 | #define SMALLOC_PRE_RED 0xdeadbeefU |
| 30 | #define SMALLOC_POST_RED 0x5aa55aa5U |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 31 | |
Jens Axboe | 2b386d2 | 2008-03-26 10:32:57 +0100 | [diff] [blame] | 32 | unsigned int smalloc_pool_size = INITIAL_SIZE; |
Jens Axboe | 122426d | 2009-07-07 23:03:06 +0200 | [diff] [blame] | 33 | const int int_mask = sizeof(int) - 1; |
Jens Axboe | 2b386d2 | 2008-03-26 10:32:57 +0100 | [diff] [blame] | 34 | |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 35 | struct pool { |
Jens Axboe | 6548f47 | 2008-06-13 08:38:31 +0200 | [diff] [blame] | 36 | struct fio_mutex *lock; /* protects this pool */ |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 37 | void *map; /* map of blocks */ |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 38 | unsigned int *bitmap; /* blocks free/busy map */ |
Jens Axboe | a3ebe7e | 2012-11-02 16:47:03 +0100 | [diff] [blame] | 39 | size_t free_blocks; /* free blocks */ |
| 40 | size_t nr_blocks; /* total blocks */ |
| 41 | size_t next_non_full; |
| 42 | size_t mmap_size; |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | struct block_hdr { |
Jens Axboe | a3ebe7e | 2012-11-02 16:47:03 +0100 | [diff] [blame] | 46 | size_t size; |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 47 | #ifdef SMALLOC_REDZONE |
| 48 | unsigned int prered; |
| 49 | #endif |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | static struct pool mp[MAX_POOLS]; |
| 53 | static unsigned int nr_pools; |
| 54 | static unsigned int last_pool; |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 55 | static struct fio_rwlock *lock; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 56 | |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 57 | static inline void pool_lock(struct pool *pool) |
| 58 | { |
Jens Axboe | 2e3e31e | 2009-01-06 11:30:08 +0100 | [diff] [blame] | 59 | fio_mutex_down(pool->lock); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | static inline void pool_unlock(struct pool *pool) |
| 63 | { |
Jens Axboe | 2e3e31e | 2009-01-06 11:30:08 +0100 | [diff] [blame] | 64 | fio_mutex_up(pool->lock); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 65 | } |
| 66 | |
Jens Axboe | 65864cf | 2008-03-03 10:36:36 +0100 | [diff] [blame] | 67 | static inline void global_read_lock(void) |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 68 | { |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 69 | fio_rwlock_read(lock); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 70 | } |
| 71 | |
Jens Axboe | 65864cf | 2008-03-03 10:36:36 +0100 | [diff] [blame] | 72 | static inline void global_read_unlock(void) |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 73 | { |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 74 | fio_rwlock_unlock(lock); |
Jens Axboe | 65864cf | 2008-03-03 10:36:36 +0100 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | static inline void global_write_lock(void) |
| 78 | { |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 79 | fio_rwlock_write(lock); |
Jens Axboe | 65864cf | 2008-03-03 10:36:36 +0100 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | static inline void global_write_unlock(void) |
| 83 | { |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 84 | fio_rwlock_unlock(lock); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 85 | } |
| 86 | |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 87 | static inline int ptr_valid(struct pool *pool, void *ptr) |
| 88 | { |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 89 | unsigned int pool_size = pool->nr_blocks * SMALLOC_BPL; |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 90 | |
| 91 | return (ptr >= pool->map) && (ptr < pool->map + pool_size); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 92 | } |
| 93 | |
Jens Axboe | a3ebe7e | 2012-11-02 16:47:03 +0100 | [diff] [blame] | 94 | static inline size_t size_to_blocks(size_t size) |
Jens Axboe | 808e9ea | 2008-05-27 14:12:45 +0200 | [diff] [blame] | 95 | { |
| 96 | return (size + SMALLOC_BPB - 1) / SMALLOC_BPB; |
| 97 | } |
| 98 | |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 99 | static int blocks_iter(struct pool *pool, unsigned int pool_idx, |
Jens Axboe | a3ebe7e | 2012-11-02 16:47:03 +0100 | [diff] [blame] | 100 | unsigned int idx, size_t nr_blocks, |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 101 | int (*func)(unsigned int *map, unsigned int mask)) |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 102 | { |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 103 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 104 | while (nr_blocks) { |
| 105 | unsigned int this_blocks, mask; |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 106 | unsigned int *map; |
| 107 | |
| 108 | if (pool_idx >= pool->nr_blocks) |
| 109 | return 0; |
| 110 | |
| 111 | map = &pool->bitmap[pool_idx]; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 112 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 113 | this_blocks = nr_blocks; |
| 114 | if (this_blocks + idx > SMALLOC_BPI) { |
| 115 | this_blocks = SMALLOC_BPI - idx; |
| 116 | idx = SMALLOC_BPI - this_blocks; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 117 | } |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 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; |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 129 | pool_idx++; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 130 | } |
| 131 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 132 | return 1; |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | static int mask_cmp(unsigned int *map, unsigned int mask) |
| 136 | { |
| 137 | return !(*map & mask); |
| 138 | } |
| 139 | |
| 140 | static int mask_clear(unsigned int *map, unsigned int mask) |
| 141 | { |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 142 | assert((*map & mask) == mask); |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 143 | *map &= ~mask; |
| 144 | return 1; |
| 145 | } |
| 146 | |
| 147 | static int mask_set(unsigned int *map, unsigned int mask) |
| 148 | { |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 149 | assert(!(*map & mask)); |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 150 | *map |= mask; |
| 151 | return 1; |
| 152 | } |
| 153 | |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 154 | static int blocks_free(struct pool *pool, unsigned int pool_idx, |
Jens Axboe | a3ebe7e | 2012-11-02 16:47:03 +0100 | [diff] [blame] | 155 | unsigned int idx, size_t nr_blocks) |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 156 | { |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 157 | return blocks_iter(pool, pool_idx, idx, nr_blocks, mask_cmp); |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 158 | } |
| 159 | |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 160 | static void set_blocks(struct pool *pool, unsigned int pool_idx, |
Jens Axboe | a3ebe7e | 2012-11-02 16:47:03 +0100 | [diff] [blame] | 161 | unsigned int idx, size_t nr_blocks) |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 162 | { |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 163 | blocks_iter(pool, pool_idx, idx, nr_blocks, mask_set); |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 164 | } |
| 165 | |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 166 | static void clear_blocks(struct pool *pool, unsigned int pool_idx, |
Jens Axboe | a3ebe7e | 2012-11-02 16:47:03 +0100 | [diff] [blame] | 167 | unsigned int idx, size_t nr_blocks) |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 168 | { |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 169 | blocks_iter(pool, pool_idx, idx, nr_blocks, mask_clear); |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 170 | } |
| 171 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 172 | static int find_next_zero(int word, int start) |
| 173 | { |
| 174 | assert(word != -1U); |
Jiri Horky | 271067a | 2011-08-15 15:18:03 +0200 | [diff] [blame] | 175 | word >>= start; |
| 176 | return ffz(word) + start; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 177 | } |
| 178 | |
Jens Axboe | adf5709 | 2008-04-16 19:43:17 +0200 | [diff] [blame] | 179 | static int add_pool(struct pool *pool, unsigned int alloc_size) |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 180 | { |
Jens Axboe | 8d5844e | 2011-06-29 09:50:08 +0200 | [diff] [blame] | 181 | int bitmap_blocks; |
Jens Axboe | b8a6582 | 2008-12-04 14:33:41 +0100 | [diff] [blame] | 182 | void *ptr; |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 183 | |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 184 | #ifdef SMALLOC_REDZONE |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 185 | alloc_size += sizeof(unsigned int); |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 186 | #endif |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 187 | alloc_size += sizeof(struct block_hdr); |
| 188 | if (alloc_size < INITIAL_SIZE) |
| 189 | alloc_size = INITIAL_SIZE; |
Jens Axboe | adf5709 | 2008-04-16 19:43:17 +0200 | [diff] [blame] | 190 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 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; |
Jens Axboe | 0b9d69e | 2009-09-11 22:29:54 +0200 | [diff] [blame] | 196 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 197 | pool->nr_blocks = bitmap_blocks; |
| 198 | pool->free_blocks = bitmap_blocks * SMALLOC_BPB; |
| 199 | |
Jens Axboe | 8d5844e | 2011-06-29 09:50:08 +0200 | [diff] [blame] | 200 | ptr = mmap(NULL, alloc_size, PROT_READ|PROT_WRITE, |
| 201 | MAP_SHARED | OS_MAP_ANON, -1, 0); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 202 | if (ptr == MAP_FAILED) |
Jens Axboe | 8d5844e | 2011-06-29 09:50:08 +0200 | [diff] [blame] | 203 | goto out_fail; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 204 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 205 | memset(ptr, 0, alloc_size); |
| 206 | pool->map = ptr; |
| 207 | pool->bitmap = (void *) ptr + (pool->nr_blocks * SMALLOC_BPL); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 208 | |
Jens Axboe | 521da52 | 2012-08-02 11:21:36 +0200 | [diff] [blame] | 209 | pool->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 210 | if (!pool->lock) |
Jens Axboe | 8d5844e | 2011-06-29 09:50:08 +0200 | [diff] [blame] | 211 | goto out_fail; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 212 | |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 213 | nr_pools++; |
| 214 | return 0; |
Jens Axboe | 8d5844e | 2011-06-29 09:50:08 +0200 | [diff] [blame] | 215 | out_fail: |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 216 | fprintf(stderr, "smalloc: failed adding pool\n"); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 217 | if (pool->map) |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 218 | munmap(pool->map, pool->mmap_size); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 219 | return 1; |
| 220 | } |
| 221 | |
| 222 | void sinit(void) |
| 223 | { |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 224 | int ret; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 225 | |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 226 | lock = fio_rwlock_init(); |
Jens Axboe | adf5709 | 2008-04-16 19:43:17 +0200 | [diff] [blame] | 227 | ret = add_pool(&mp[0], INITIAL_SIZE); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 228 | assert(!ret); |
| 229 | } |
| 230 | |
| 231 | static void cleanup_pool(struct pool *pool) |
| 232 | { |
Jens Axboe | 443bb11 | 2008-12-04 14:30:13 +0100 | [diff] [blame] | 233 | /* |
| 234 | * This will also remove the temporary file we used as a backing |
| 235 | * store, it was already unlinked |
| 236 | */ |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 237 | munmap(pool->map, pool->mmap_size); |
Jens Axboe | 6548f47 | 2008-06-13 08:38:31 +0200 | [diff] [blame] | 238 | |
| 239 | if (pool->lock) |
| 240 | fio_mutex_remove(pool->lock); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 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 | |
Jens Axboe | 6548f47 | 2008-06-13 08:38:31 +0200 | [diff] [blame] | 250 | if (lock) |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 251 | fio_rwlock_remove(lock); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 252 | } |
| 253 | |
Jens Axboe | 89da54e | 2008-05-27 20:49:29 +0200 | [diff] [blame] | 254 | #ifdef SMALLOC_REDZONE |
Jens Axboe | cf98708 | 2009-07-07 17:43:11 +0200 | [diff] [blame] | 255 | static void *postred_ptr(struct block_hdr *hdr) |
| 256 | { |
Bruce Cran | e43606c | 2012-02-20 09:34:24 +0100 | [diff] [blame] | 257 | uintptr_t ptr; |
Jens Axboe | cf98708 | 2009-07-07 17:43:11 +0200 | [diff] [blame] | 258 | |
Bruce Cran | e43606c | 2012-02-20 09:34:24 +0100 | [diff] [blame] | 259 | ptr = (uintptr_t) hdr + hdr->size - sizeof(unsigned int); |
Jens Axboe | cf98708 | 2009-07-07 17:43:11 +0200 | [diff] [blame] | 260 | ptr = (ptr + int_mask) & ~int_mask; |
| 261 | |
| 262 | return (void *) ptr; |
| 263 | } |
| 264 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 265 | static void fill_redzone(struct block_hdr *hdr) |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 266 | { |
Jens Axboe | cf98708 | 2009-07-07 17:43:11 +0200 | [diff] [blame] | 267 | unsigned int *postred = postred_ptr(hdr); |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 268 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 269 | hdr->prered = SMALLOC_PRE_RED; |
| 270 | *postred = SMALLOC_POST_RED; |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 271 | } |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 272 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 273 | static void sfree_check_redzone(struct block_hdr *hdr) |
| 274 | { |
Jens Axboe | cf98708 | 2009-07-07 17:43:11 +0200 | [diff] [blame] | 275 | unsigned int *postred = postred_ptr(hdr); |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 276 | |
| 277 | if (hdr->prered != SMALLOC_PRE_RED) { |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 278 | fprintf(stderr, "smalloc pre redzone destroyed!\n"); |
| 279 | fprintf(stderr, " ptr=%p, prered=%x, expected %x\n", |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 280 | hdr, hdr->prered, SMALLOC_PRE_RED); |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 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", |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 286 | hdr, *postred, SMALLOC_POST_RED); |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 287 | assert(0); |
| 288 | } |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 289 | } |
Jens Axboe | 89da54e | 2008-05-27 20:49:29 +0200 | [diff] [blame] | 290 | #else |
| 291 | static void fill_redzone(struct block_hdr *hdr) |
| 292 | { |
| 293 | } |
| 294 | |
| 295 | static void sfree_check_redzone(struct block_hdr *hdr) |
| 296 | { |
| 297 | } |
| 298 | #endif |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 299 | |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 300 | static void sfree_pool(struct pool *pool, void *ptr) |
| 301 | { |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 302 | struct block_hdr *hdr; |
Jens Axboe | 179446e | 2008-05-27 14:11:56 +0200 | [diff] [blame] | 303 | unsigned int i, idx; |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 304 | unsigned long offset; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 305 | |
| 306 | if (!ptr) |
| 307 | return; |
| 308 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 309 | ptr -= sizeof(*hdr); |
| 310 | hdr = ptr; |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 311 | |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 312 | assert(ptr_valid(pool, ptr)); |
| 313 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 314 | sfree_check_redzone(hdr); |
| 315 | |
| 316 | offset = ptr - pool->map; |
| 317 | i = offset / SMALLOC_BPL; |
| 318 | idx = (offset % SMALLOC_BPL) / SMALLOC_BPB; |
| 319 | |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 320 | pool_lock(pool); |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 321 | clear_blocks(pool, i, idx, size_to_blocks(hdr->size)); |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 322 | if (i < pool->next_non_full) |
| 323 | pool->next_non_full = i; |
Jens Axboe | 179446e | 2008-05-27 14:11:56 +0200 | [diff] [blame] | 324 | pool->free_blocks += size_to_blocks(hdr->size); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 325 | pool_unlock(pool); |
| 326 | } |
| 327 | |
| 328 | void sfree(void *ptr) |
| 329 | { |
| 330 | struct pool *pool = NULL; |
| 331 | unsigned int i; |
| 332 | |
Jens Axboe | 8e5732e | 2008-04-16 19:51:46 +0200 | [diff] [blame] | 333 | if (!ptr) |
| 334 | return; |
| 335 | |
Jens Axboe | 65864cf | 2008-03-03 10:36:36 +0100 | [diff] [blame] | 336 | global_read_lock(); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 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 | |
Jens Axboe | 65864cf | 2008-03-03 10:36:36 +0100 | [diff] [blame] | 345 | global_read_unlock(); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 346 | |
| 347 | assert(pool); |
| 348 | sfree_pool(pool, ptr); |
| 349 | } |
| 350 | |
Jens Axboe | a3ebe7e | 2012-11-02 16:47:03 +0100 | [diff] [blame] | 351 | static void *__smalloc_pool(struct pool *pool, size_t size) |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 352 | { |
Jens Axboe | a3ebe7e | 2012-11-02 16:47:03 +0100 | [diff] [blame] | 353 | size_t nr_blocks; |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 354 | unsigned int i; |
| 355 | unsigned int offset; |
| 356 | unsigned int last_idx; |
| 357 | void *ret = NULL; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 358 | |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 359 | pool_lock(pool); |
Jens Axboe | 179446e | 2008-05-27 14:11:56 +0200 | [diff] [blame] | 360 | |
| 361 | nr_blocks = size_to_blocks(size); |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 362 | if (nr_blocks > pool->free_blocks) |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 363 | goto fail; |
| 364 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 365 | i = pool->next_non_full; |
| 366 | last_idx = 0; |
| 367 | offset = -1U; |
| 368 | while (i < pool->nr_blocks) { |
| 369 | unsigned int idx; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 370 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 371 | if (pool->bitmap[i] == -1U) { |
| 372 | i++; |
| 373 | pool->next_non_full = i; |
| 374 | last_idx = 0; |
| 375 | continue; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 376 | } |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 377 | |
| 378 | idx = find_next_zero(pool->bitmap[i], last_idx); |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 379 | if (!blocks_free(pool, i, idx, nr_blocks)) { |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 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; |
| 391 | } |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 392 | set_blocks(pool, i, idx, nr_blocks); |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 393 | offset = i * SMALLOC_BPL + idx * SMALLOC_BPB; |
| 394 | break; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 395 | } |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 396 | |
| 397 | if (i < pool->nr_blocks) { |
| 398 | pool->free_blocks -= nr_blocks; |
| 399 | ret = pool->map + offset; |
| 400 | } |
| 401 | fail: |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 402 | pool_unlock(pool); |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 403 | return ret; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 404 | } |
| 405 | |
Jens Axboe | a3ebe7e | 2012-11-02 16:47:03 +0100 | [diff] [blame] | 406 | static void *smalloc_pool(struct pool *pool, size_t size) |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 407 | { |
Jens Axboe | a3ebe7e | 2012-11-02 16:47:03 +0100 | [diff] [blame] | 408 | size_t alloc_size = size + sizeof(struct block_hdr); |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 409 | void *ptr; |
| 410 | |
Jens Axboe | cf98708 | 2009-07-07 17:43:11 +0200 | [diff] [blame] | 411 | /* |
Jens Axboe | 122426d | 2009-07-07 23:03:06 +0200 | [diff] [blame] | 412 | * Round to int alignment, so that the postred pointer will |
| 413 | * be naturally aligned as well. |
Jens Axboe | cf98708 | 2009-07-07 17:43:11 +0200 | [diff] [blame] | 414 | */ |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 415 | #ifdef SMALLOC_REDZONE |
Jens Axboe | 122426d | 2009-07-07 23:03:06 +0200 | [diff] [blame] | 416 | alloc_size += sizeof(unsigned int); |
| 417 | alloc_size = (alloc_size + int_mask) & ~int_mask; |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 418 | #endif |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 419 | |
| 420 | ptr = __smalloc_pool(pool, alloc_size); |
Jens Axboe | 89da54e | 2008-05-27 20:49:29 +0200 | [diff] [blame] | 421 | if (ptr) { |
| 422 | struct block_hdr *hdr = ptr; |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 423 | |
Jens Axboe | 89da54e | 2008-05-27 20:49:29 +0200 | [diff] [blame] | 424 | hdr->size = alloc_size; |
| 425 | fill_redzone(hdr); |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 426 | |
Jens Axboe | 89da54e | 2008-05-27 20:49:29 +0200 | [diff] [blame] | 427 | ptr += sizeof(*hdr); |
| 428 | memset(ptr, 0, size); |
| 429 | } |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 430 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 431 | return ptr; |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 432 | } |
| 433 | |
Jens Axboe | 7982aa7 | 2012-11-02 16:35:18 +0100 | [diff] [blame] | 434 | void *smalloc(size_t size) |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 435 | { |
| 436 | unsigned int i; |
| 437 | |
Jens Axboe | 7982aa7 | 2012-11-02 16:35:18 +0100 | [diff] [blame] | 438 | if (size != (unsigned int) size) |
| 439 | return NULL; |
| 440 | |
Shaozhi Shawn Ye | d1271dc | 2008-09-10 09:02:51 +0200 | [diff] [blame] | 441 | global_write_lock(); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 442 | i = last_pool; |
| 443 | |
| 444 | do { |
| 445 | for (; i < nr_pools; i++) { |
| 446 | void *ptr = smalloc_pool(&mp[i], size); |
| 447 | |
| 448 | if (ptr) { |
| 449 | last_pool = i; |
Shaozhi Shawn Ye | d1271dc | 2008-09-10 09:02:51 +0200 | [diff] [blame] | 450 | global_write_unlock(); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 451 | return ptr; |
| 452 | } |
| 453 | } |
| 454 | if (last_pool) { |
| 455 | last_pool = 0; |
| 456 | continue; |
| 457 | } |
| 458 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 459 | if (nr_pools + 1 > MAX_POOLS) |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 460 | break; |
| 461 | else { |
| 462 | i = nr_pools; |
Jens Axboe | adf5709 | 2008-04-16 19:43:17 +0200 | [diff] [blame] | 463 | if (add_pool(&mp[nr_pools], size)) |
Jens Axboe | 65864cf | 2008-03-03 10:36:36 +0100 | [diff] [blame] | 464 | goto out; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 465 | } |
| 466 | } while (1); |
| 467 | |
Jens Axboe | 65864cf | 2008-03-03 10:36:36 +0100 | [diff] [blame] | 468 | out: |
Shaozhi Shawn Ye | d1271dc | 2008-09-10 09:02:51 +0200 | [diff] [blame] | 469 | global_write_unlock(); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 470 | return NULL; |
| 471 | } |
| 472 | |
| 473 | char *smalloc_strdup(const char *str) |
| 474 | { |
| 475 | char *ptr; |
| 476 | |
| 477 | ptr = smalloc(strlen(str) + 1); |
| 478 | strcpy(ptr, str); |
| 479 | return ptr; |
| 480 | } |