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