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