__) && (defined(__i386__) || defined(__x86_64__))) 151 int x1, x2; 152 asm ("bsf %0,%0\n" "jnz 1f\n" "bsf %1,%0\n" "jz 1f\n" "addl $32,%0\n" 153 "1:": "=&q" (x1), "=&q" (x2):"1" ((int) (map >> 32)), 154 "0" ((int) map)); 155 idx = x1; 156 #else 157 static const index_type lsb_64_table[64] = 158 { 159 63, 30, 3, 32, 59, 14, 11, 33, 160 60, 24, 50, 9, 55, 19, 21, 34, 161 61, 29, 2, 53, 51, 23, 41, 18, 162 56, 28, 1, 43, 46, 27, 0, 35, 163 62, 31, 58, 4, 5, 49, 54, 6, 164 15, 52, 12, 40, 7, 42, 45, 16, 165 25, 57, 48, 13, 10, 39, 8, 44, 166 20, 47, 38, 22, 17, 37, 36, 26 167 }; 168 unsigned long folded; 169 map ^= map - 1; 170 folded = (int) map ^ (map >> 32); 171 idx = lsb_64_table[folded * 0x78291ACF >> 26]; 172 #endif 173 return idx; 174 } 175 176 inline typename bitmap_fixed_size_allocator::object* 177 bitmap_fixed_size_allocator::get_fblk(chunk* cptr) 178 { 179 index_type fbit = find_fbit(cptr->map); 180 mark_bit(cptr->map, fbit); 181 object* bptr = (object*)((char*)(cptr + 1U) + fbit * blk_sz); 182 bptr->idx = fbit; 183 return bptr + 1U; 184 } 185 186 inline typename bitmap_fixed_size_allocator::object* 187 bitmap_fixed_size_allocator::chunk_alloc() 188 { 189 chk_hd = (chunk*)std::malloc((sizeof(size_type) << 3U) * blk_sz + sizeof(chunk)); 190 if(0 == chk_hd) 191 return 0; 192 chk_hd->map = FREE; 193 return get_fblk(chk_hd); 194 } 195 196 inline void bitmap_fixed_size_allocator::clear() 197 { 198 if(0 != chk_hd && FREE == chk_hd->map) 199 { 200 std::free(chk_hd); 201 chk_hd = 0; 202 } 203 } 204 205 206 /// only wrappers below. 207 208 template 209 class single_object_allocator; 210 211 /// single_object_allocator specialization. 212 213 template<> 214 class single_object_allocator 215 { 216 public: 217 typedef unsigned long size_type; 218 typedef long difference_type; 219 typedef void* pointer; 220 typedef const void* const_pointer; 221 typedef void value_type; 222 223 template 224 struct rebind { typedef single_object_allocator other; }; 225 }; 226 227 template 228 class single_object_allocator 229 { 230 static bitmap_fixed_size_allocator bm; 231 public: 232 typedef T value_type; 233 typedef T* pointer; 234 typedef const T* const_pointer; 235 typedef T& reference; 236 typedef const T& const_reference; 237 typedef unsigned long size_type; 238 typedef long difference_type; 239 240 template 241 struct rebind { typedef single_object_allocator other; }; 242 243 single_object_allocator() {} 244 245 single_object_allocator(const single_object_allocator&) {} 246 247 template 248 single_object_allocator(const single_object_allocator&) {} 249 250 ~single_object_allocator() throw() {} 251 252 pointer allocate(size_type n) // n仅仅用于校验而已,几乎没有任何价值,稍微修改下代码,可以去掉它 253 { 254 if(n == sizeof(T)) 255 { 256 guard g(bm); 257 return static_cast(bm.allocate()); 258 } 259 else 260 throw "memory corruption"; 261 } 262 263 void deallocate(pointer p, size_type n) // n 仅用于校验而已,几乎没有什么价值,可以稍微修改下,去掉该参数 264 { 265 guard g(bm); 266 if(n == sizeof(T)) 267 bm.deallocate(p); 268 else 269 { 270 bm.clear(); 271 bm.~bitmap_fixed_size_allocator(); 272 throw "memory corruption"; 273 |