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