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