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