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