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 | |
Jens Axboe | 6548f47 | 2008-06-13 08:38:31 +0200 | [diff] [blame] | 14 | #include "mutex.h" |
Jens Axboe | b3268b9 | 2008-06-02 09:59:32 +0200 | [diff] [blame] | 15 | #include "arch/arch.h" |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 16 | |
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 | 6885768 | 2008-07-31 19:50:54 +0200 | [diff] [blame] | 24 | #define MAX_POOLS 128 /* 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 { |
Jens Axboe | 6548f47 | 2008-06-13 08:38:31 +0200 | [diff] [blame] | 32 | struct fio_mutex *lock; /* protects this pool */ |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 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 */ |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 39 | unsigned int mmap_size; |
| 40 | }; |
| 41 | |
| 42 | struct block_hdr { |
| 43 | unsigned int size; |
| 44 | #ifdef SMALLOC_REDZONE |
| 45 | unsigned int prered; |
| 46 | #endif |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | static struct pool mp[MAX_POOLS]; |
| 50 | static unsigned int nr_pools; |
| 51 | static unsigned int last_pool; |
Jens Axboe | 6548f47 | 2008-06-13 08:38:31 +0200 | [diff] [blame] | 52 | static struct fio_mutex *lock; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 53 | |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 54 | static inline void pool_lock(struct pool *pool) |
| 55 | { |
Jens Axboe | 2e3e31e | 2009-01-06 11:30:08 +0100 | [diff] [blame] | 56 | fio_mutex_down(pool->lock); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | static inline void pool_unlock(struct pool *pool) |
| 60 | { |
Jens Axboe | 2e3e31e | 2009-01-06 11:30:08 +0100 | [diff] [blame] | 61 | fio_mutex_up(pool->lock); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 62 | } |
| 63 | |
Jens Axboe | 65864cf | 2008-03-03 10:36:36 +0100 | [diff] [blame] | 64 | static inline void global_read_lock(void) |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 65 | { |
Jens Axboe | 2e3e31e | 2009-01-06 11:30:08 +0100 | [diff] [blame] | 66 | fio_mutex_down_read(lock); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 67 | } |
| 68 | |
Jens Axboe | 65864cf | 2008-03-03 10:36:36 +0100 | [diff] [blame] | 69 | static inline void global_read_unlock(void) |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 70 | { |
Jens Axboe | 2e3e31e | 2009-01-06 11:30:08 +0100 | [diff] [blame] | 71 | fio_mutex_up_read(lock); |
Jens Axboe | 65864cf | 2008-03-03 10:36:36 +0100 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | static inline void global_write_lock(void) |
| 75 | { |
Jens Axboe | 2e3e31e | 2009-01-06 11:30:08 +0100 | [diff] [blame] | 76 | fio_mutex_down_write(lock); |
Jens Axboe | 65864cf | 2008-03-03 10:36:36 +0100 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static inline void global_write_unlock(void) |
| 80 | { |
Jens Axboe | 2e3e31e | 2009-01-06 11:30:08 +0100 | [diff] [blame] | 81 | fio_mutex_up_write(lock); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 82 | } |
| 83 | |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 84 | static inline int ptr_valid(struct pool *pool, void *ptr) |
| 85 | { |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 86 | unsigned int pool_size = pool->nr_blocks * SMALLOC_BPL; |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 87 | |
| 88 | return (ptr >= pool->map) && (ptr < pool->map + pool_size); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 89 | } |
| 90 | |
Jens Axboe | 808e9ea | 2008-05-27 14:12:45 +0200 | [diff] [blame] | 91 | static inline unsigned int size_to_blocks(unsigned int size) |
| 92 | { |
| 93 | return (size + SMALLOC_BPB - 1) / SMALLOC_BPB; |
| 94 | } |
| 95 | |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 96 | static int blocks_iter(struct pool *pool, unsigned int pool_idx, |
| 97 | unsigned int idx, unsigned int nr_blocks, |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 98 | int (*func)(unsigned int *map, unsigned int mask)) |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 99 | { |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 100 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 101 | while (nr_blocks) { |
| 102 | unsigned int this_blocks, mask; |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 103 | unsigned int *map; |
| 104 | |
| 105 | if (pool_idx >= pool->nr_blocks) |
| 106 | return 0; |
| 107 | |
| 108 | map = &pool->bitmap[pool_idx]; |
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; |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 126 | pool_idx++; |
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 | { |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 139 | assert((*map & mask) == mask); |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 140 | *map &= ~mask; |
| 141 | return 1; |
| 142 | } |
| 143 | |
| 144 | static int mask_set(unsigned int *map, unsigned int mask) |
| 145 | { |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 146 | assert(!(*map & mask)); |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 147 | *map |= mask; |
| 148 | return 1; |
| 149 | } |
| 150 | |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 151 | static int blocks_free(struct pool *pool, unsigned int pool_idx, |
| 152 | unsigned int idx, unsigned int nr_blocks) |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 153 | { |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 154 | return blocks_iter(pool, pool_idx, idx, nr_blocks, mask_cmp); |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 155 | } |
| 156 | |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 157 | static void set_blocks(struct pool *pool, unsigned int pool_idx, |
| 158 | unsigned int idx, unsigned int nr_blocks) |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 159 | { |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 160 | blocks_iter(pool, pool_idx, idx, nr_blocks, mask_set); |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 161 | } |
| 162 | |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 163 | static void clear_blocks(struct pool *pool, unsigned int pool_idx, |
| 164 | unsigned int idx, unsigned int nr_blocks) |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 165 | { |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 166 | blocks_iter(pool, pool_idx, idx, nr_blocks, mask_clear); |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 167 | } |
| 168 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 169 | static int find_next_zero(int word, int start) |
| 170 | { |
| 171 | assert(word != -1U); |
| 172 | word >>= (start + 1); |
Jens Axboe | b3268b9 | 2008-06-02 09:59:32 +0200 | [diff] [blame] | 173 | return ffz(word) + start + 1; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 174 | } |
| 175 | |
Jens Axboe | adf5709 | 2008-04-16 19:43:17 +0200 | [diff] [blame] | 176 | static int add_pool(struct pool *pool, unsigned int alloc_size) |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 177 | { |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 178 | int fd, bitmap_blocks; |
Jens Axboe | b8a6582 | 2008-12-04 14:33:41 +0100 | [diff] [blame] | 179 | char file[] = "/tmp/.fio_smalloc.XXXXXX"; |
| 180 | void *ptr; |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 181 | |
Jens Axboe | b8a6582 | 2008-12-04 14:33:41 +0100 | [diff] [blame] | 182 | fd = mkstemp(file); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 183 | if (fd < 0) |
| 184 | goto out_close; |
| 185 | |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 186 | #ifdef SMALLOC_REDZONE |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 187 | alloc_size += sizeof(unsigned int); |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 188 | #endif |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 189 | alloc_size += sizeof(struct block_hdr); |
| 190 | if (alloc_size < INITIAL_SIZE) |
| 191 | alloc_size = INITIAL_SIZE; |
Jens Axboe | adf5709 | 2008-04-16 19:43:17 +0200 | [diff] [blame] | 192 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 193 | /* round up to nearest full number of blocks */ |
| 194 | alloc_size = (alloc_size + SMALLOC_BPL - 1) & ~(SMALLOC_BPL - 1); |
| 195 | bitmap_blocks = alloc_size / SMALLOC_BPL; |
| 196 | alloc_size += bitmap_blocks * sizeof(unsigned int); |
| 197 | pool->mmap_size = alloc_size; |
| 198 | |
| 199 | pool->nr_blocks = bitmap_blocks; |
| 200 | pool->free_blocks = bitmap_blocks * SMALLOC_BPB; |
| 201 | |
| 202 | if (ftruncate(fd, alloc_size) < 0) |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 203 | goto out_unlink; |
| 204 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 205 | 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] | 206 | if (ptr == MAP_FAILED) |
| 207 | goto out_unlink; |
| 208 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 209 | memset(ptr, 0, alloc_size); |
| 210 | pool->map = ptr; |
| 211 | pool->bitmap = (void *) ptr + (pool->nr_blocks * SMALLOC_BPL); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 212 | |
Jens Axboe | 6548f47 | 2008-06-13 08:38:31 +0200 | [diff] [blame] | 213 | pool->lock = fio_mutex_init(1); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 214 | if (!pool->lock) |
| 215 | goto out_unlink; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 216 | |
Jens Axboe | 443bb11 | 2008-12-04 14:30:13 +0100 | [diff] [blame] | 217 | /* |
| 218 | * Unlink pool file now. It wont get deleted until the fd is closed, |
| 219 | * which happens both for cleanup or unexpected quit. This way we |
| 220 | * don't leave temp files around in case of a crash. |
| 221 | */ |
Jens Axboe | b8a6582 | 2008-12-04 14:33:41 +0100 | [diff] [blame] | 222 | unlink(file); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 223 | pool->fd = fd; |
| 224 | |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 225 | nr_pools++; |
| 226 | return 0; |
| 227 | out_unlink: |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 228 | fprintf(stderr, "smalloc: failed adding pool\n"); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 229 | if (pool->map) |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 230 | munmap(pool->map, pool->mmap_size); |
Jens Axboe | b8a6582 | 2008-12-04 14:33:41 +0100 | [diff] [blame] | 231 | unlink(file); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 232 | out_close: |
Jens Axboe | b8a6582 | 2008-12-04 14:33:41 +0100 | [diff] [blame] | 233 | close(fd); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 234 | return 1; |
| 235 | } |
| 236 | |
| 237 | void sinit(void) |
| 238 | { |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 239 | int ret; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 240 | |
Jens Axboe | 6548f47 | 2008-06-13 08:38:31 +0200 | [diff] [blame] | 241 | lock = fio_mutex_rw_init(); |
Jens Axboe | adf5709 | 2008-04-16 19:43:17 +0200 | [diff] [blame] | 242 | ret = add_pool(&mp[0], INITIAL_SIZE); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 243 | assert(!ret); |
| 244 | } |
| 245 | |
| 246 | static void cleanup_pool(struct pool *pool) |
| 247 | { |
Jens Axboe | 443bb11 | 2008-12-04 14:30:13 +0100 | [diff] [blame] | 248 | /* |
| 249 | * This will also remove the temporary file we used as a backing |
| 250 | * store, it was already unlinked |
| 251 | */ |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 252 | close(pool->fd); |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 253 | munmap(pool->map, pool->mmap_size); |
Jens Axboe | 6548f47 | 2008-06-13 08:38:31 +0200 | [diff] [blame] | 254 | |
| 255 | if (pool->lock) |
| 256 | fio_mutex_remove(pool->lock); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | void scleanup(void) |
| 260 | { |
| 261 | unsigned int i; |
| 262 | |
| 263 | for (i = 0; i < nr_pools; i++) |
| 264 | cleanup_pool(&mp[i]); |
| 265 | |
Jens Axboe | 6548f47 | 2008-06-13 08:38:31 +0200 | [diff] [blame] | 266 | if (lock) |
| 267 | fio_mutex_remove(lock); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 268 | } |
| 269 | |
Jens Axboe | 89da54e | 2008-05-27 20:49:29 +0200 | [diff] [blame] | 270 | #ifdef SMALLOC_REDZONE |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 271 | static void fill_redzone(struct block_hdr *hdr) |
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 | unsigned int *postred = (void *) hdr + hdr->size - sizeof(unsigned int); |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 274 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 275 | hdr->prered = SMALLOC_PRE_RED; |
| 276 | *postred = SMALLOC_POST_RED; |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 277 | } |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 278 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 279 | static void sfree_check_redzone(struct block_hdr *hdr) |
| 280 | { |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 281 | unsigned int *postred = (void *) hdr + hdr->size - sizeof(unsigned int); |
| 282 | |
| 283 | if (hdr->prered != SMALLOC_PRE_RED) { |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 284 | fprintf(stderr, "smalloc pre redzone destroyed!\n"); |
| 285 | fprintf(stderr, " ptr=%p, prered=%x, expected %x\n", |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 286 | hdr, hdr->prered, SMALLOC_PRE_RED); |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 287 | assert(0); |
| 288 | } |
| 289 | if (*postred != SMALLOC_POST_RED) { |
| 290 | fprintf(stderr, "smalloc post redzone destroyed!\n"); |
| 291 | fprintf(stderr, " ptr=%p, postred=%x, expected %x\n", |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 292 | hdr, *postred, SMALLOC_POST_RED); |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 293 | assert(0); |
| 294 | } |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 295 | } |
Jens Axboe | 89da54e | 2008-05-27 20:49:29 +0200 | [diff] [blame] | 296 | #else |
| 297 | static void fill_redzone(struct block_hdr *hdr) |
| 298 | { |
| 299 | } |
| 300 | |
| 301 | static void sfree_check_redzone(struct block_hdr *hdr) |
| 302 | { |
| 303 | } |
| 304 | #endif |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 305 | |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 306 | static void sfree_pool(struct pool *pool, void *ptr) |
| 307 | { |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 308 | struct block_hdr *hdr; |
Jens Axboe | 179446e | 2008-05-27 14:11:56 +0200 | [diff] [blame] | 309 | unsigned int i, idx; |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 310 | unsigned long offset; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 311 | |
| 312 | if (!ptr) |
| 313 | return; |
| 314 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 315 | ptr -= sizeof(*hdr); |
| 316 | hdr = ptr; |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 317 | |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 318 | assert(ptr_valid(pool, ptr)); |
| 319 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 320 | sfree_check_redzone(hdr); |
| 321 | |
| 322 | offset = ptr - pool->map; |
| 323 | i = offset / SMALLOC_BPL; |
| 324 | idx = (offset % SMALLOC_BPL) / SMALLOC_BPB; |
| 325 | |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 326 | pool_lock(pool); |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 327 | clear_blocks(pool, i, idx, size_to_blocks(hdr->size)); |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 328 | if (i < pool->next_non_full) |
| 329 | pool->next_non_full = i; |
Jens Axboe | 179446e | 2008-05-27 14:11:56 +0200 | [diff] [blame] | 330 | pool->free_blocks += size_to_blocks(hdr->size); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 331 | pool_unlock(pool); |
| 332 | } |
| 333 | |
| 334 | void sfree(void *ptr) |
| 335 | { |
| 336 | struct pool *pool = NULL; |
| 337 | unsigned int i; |
| 338 | |
Jens Axboe | 8e5732e | 2008-04-16 19:51:46 +0200 | [diff] [blame] | 339 | if (!ptr) |
| 340 | return; |
| 341 | |
Jens Axboe | 65864cf | 2008-03-03 10:36:36 +0100 | [diff] [blame] | 342 | global_read_lock(); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 343 | |
| 344 | for (i = 0; i < nr_pools; i++) { |
| 345 | if (ptr_valid(&mp[i], ptr)) { |
| 346 | pool = &mp[i]; |
| 347 | break; |
| 348 | } |
| 349 | } |
| 350 | |
Jens Axboe | 65864cf | 2008-03-03 10:36:36 +0100 | [diff] [blame] | 351 | global_read_unlock(); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 352 | |
| 353 | assert(pool); |
| 354 | sfree_pool(pool, ptr); |
| 355 | } |
| 356 | |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 357 | static void *__smalloc_pool(struct pool *pool, unsigned int size) |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 358 | { |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 359 | unsigned int nr_blocks; |
| 360 | unsigned int i; |
| 361 | unsigned int offset; |
| 362 | unsigned int last_idx; |
| 363 | void *ret = NULL; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 364 | |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 365 | pool_lock(pool); |
Jens Axboe | 179446e | 2008-05-27 14:11:56 +0200 | [diff] [blame] | 366 | |
| 367 | nr_blocks = size_to_blocks(size); |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 368 | if (nr_blocks > pool->free_blocks) |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 369 | goto fail; |
| 370 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 371 | i = pool->next_non_full; |
| 372 | last_idx = 0; |
| 373 | offset = -1U; |
| 374 | while (i < pool->nr_blocks) { |
| 375 | unsigned int idx; |
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 | if (pool->bitmap[i] == -1U) { |
| 378 | i++; |
| 379 | pool->next_non_full = i; |
| 380 | last_idx = 0; |
| 381 | continue; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 382 | } |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 383 | |
| 384 | idx = find_next_zero(pool->bitmap[i], last_idx); |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 385 | if (!blocks_free(pool, i, idx, nr_blocks)) { |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 386 | idx += nr_blocks; |
| 387 | if (idx < SMALLOC_BPI) |
| 388 | last_idx = idx; |
| 389 | else { |
| 390 | last_idx = 0; |
| 391 | while (idx >= SMALLOC_BPI) { |
| 392 | i++; |
| 393 | idx -= SMALLOC_BPI; |
| 394 | } |
| 395 | } |
| 396 | continue; |
| 397 | } |
Jens Axboe | dcb6909 | 2008-05-27 20:35:18 +0200 | [diff] [blame] | 398 | set_blocks(pool, i, idx, nr_blocks); |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 399 | offset = i * SMALLOC_BPL + idx * SMALLOC_BPB; |
| 400 | break; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 401 | } |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 402 | |
| 403 | if (i < pool->nr_blocks) { |
| 404 | pool->free_blocks -= nr_blocks; |
| 405 | ret = pool->map + offset; |
| 406 | } |
| 407 | fail: |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 408 | pool_unlock(pool); |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 409 | return ret; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 410 | } |
| 411 | |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 412 | static void *smalloc_pool(struct pool *pool, unsigned int size) |
| 413 | { |
Jens Axboe | 89da54e | 2008-05-27 20:49:29 +0200 | [diff] [blame] | 414 | unsigned int alloc_size = size + sizeof(struct block_hdr); |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 415 | void *ptr; |
| 416 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 417 | #ifdef SMALLOC_REDZONE |
| 418 | alloc_size += sizeof(unsigned int); |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 419 | #endif |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 420 | |
| 421 | ptr = __smalloc_pool(pool, alloc_size); |
Jens Axboe | 89da54e | 2008-05-27 20:49:29 +0200 | [diff] [blame] | 422 | if (ptr) { |
| 423 | struct block_hdr *hdr = ptr; |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 424 | |
Jens Axboe | 89da54e | 2008-05-27 20:49:29 +0200 | [diff] [blame] | 425 | hdr->size = alloc_size; |
| 426 | fill_redzone(hdr); |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 427 | |
Jens Axboe | 89da54e | 2008-05-27 20:49:29 +0200 | [diff] [blame] | 428 | ptr += sizeof(*hdr); |
| 429 | memset(ptr, 0, size); |
| 430 | } |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 431 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 432 | return ptr; |
Jens Axboe | 55f6491 | 2008-05-26 09:37:21 +0200 | [diff] [blame] | 433 | } |
| 434 | |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 435 | void *smalloc(unsigned int size) |
| 436 | { |
| 437 | unsigned int i; |
| 438 | |
Shaozhi Shawn Ye | d1271dc | 2008-09-10 09:02:51 +0200 | [diff] [blame] | 439 | global_write_lock(); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 440 | i = last_pool; |
| 441 | |
| 442 | do { |
| 443 | for (; i < nr_pools; i++) { |
| 444 | void *ptr = smalloc_pool(&mp[i], size); |
| 445 | |
| 446 | if (ptr) { |
| 447 | last_pool = i; |
Shaozhi Shawn Ye | d1271dc | 2008-09-10 09:02:51 +0200 | [diff] [blame] | 448 | global_write_unlock(); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 449 | return ptr; |
| 450 | } |
| 451 | } |
| 452 | if (last_pool) { |
| 453 | last_pool = 0; |
| 454 | continue; |
| 455 | } |
| 456 | |
Jens Axboe | ec996e9 | 2008-05-27 11:39:39 +0200 | [diff] [blame] | 457 | if (nr_pools + 1 > MAX_POOLS) |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 458 | break; |
| 459 | else { |
| 460 | i = nr_pools; |
Jens Axboe | adf5709 | 2008-04-16 19:43:17 +0200 | [diff] [blame] | 461 | if (add_pool(&mp[nr_pools], size)) |
Jens Axboe | 65864cf | 2008-03-03 10:36:36 +0100 | [diff] [blame] | 462 | goto out; |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 463 | } |
| 464 | } while (1); |
| 465 | |
Jens Axboe | 65864cf | 2008-03-03 10:36:36 +0100 | [diff] [blame] | 466 | out: |
Shaozhi Shawn Ye | d1271dc | 2008-09-10 09:02:51 +0200 | [diff] [blame] | 467 | global_write_unlock(); |
Jens Axboe | d24c33a | 2008-03-01 18:27:36 +0100 | [diff] [blame] | 468 | return NULL; |
| 469 | } |
| 470 | |
| 471 | char *smalloc_strdup(const char *str) |
| 472 | { |
| 473 | char *ptr; |
| 474 | |
| 475 | ptr = smalloc(strlen(str) + 1); |
| 476 | strcpy(ptr, str); |
| 477 | return ptr; |
| 478 | } |