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