1 // Bitmap Allocator. -*- C++ -*- 2 3 // Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 4 // Free Software Foundation, Inc. 5 // 6 // This file is part of the GNU ISO C++ Library. This library is free 7 // software; you can redistribute it and/or modify it under the 8 // terms of the GNU General Public License as published by the 9 // Free Software Foundation; either version 3, or (at your option) 10 // any later version. 11 12 // This library is distributed in the hope that it will be useful, 13 // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 // GNU General Public License for more details. 16 17 // Under Section 7 of GPL version 3, you are granted additional 18 // permissions described in the GCC Runtime Library Exception, version 19 // 3.1, as published by the Free Software Foundation. 20 21 // You should have received a copy of the GNU General Public License and 22 // a copy of the GCC Runtime Library Exception along with this program; 23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 24 // <http://www.gnu.org/licenses/>. 25 26 /** @file ext/bitmap_allocator.h 27 * This file is a GNU extension to the Standard C++ Library. 28 */ 29 30 #ifndef _BITMAP_ALLOCATOR_H 31 #define _BITMAP_ALLOCATOR_H 1 32 33 #include <utility> // For std::pair. 34 #include <bits/functexcept.h> // For __throw_bad_alloc(). 35 #include <functional> // For greater_equal, and less_equal. 36 #include <new> // For operator new. 37 #include <debug/debug.h> // _GLIBCXX_DEBUG_ASSERT 38 #include <ext/concurrence.h> 39 #include <bits/move.h> 40 41 /** @brief The constant in the expression below is the alignment 42 * required in bytes. 43 */ 44 #define _BALLOC_ALIGN_BYTES 8 45 46 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) 47 { 48 using std::size_t; 49 using std::ptrdiff_t; 50 51 namespace __detail 52 { 53 _GLIBCXX_BEGIN_NAMESPACE_VERSION 54 /** @class __mini_vector bitmap_allocator.h bitmap_allocator.h 55 * 56 * @brief __mini_vector<> is a stripped down version of the 57 * full-fledged std::vector<>. 58 * 59 * It is to be used only for built-in types or PODs. Notable 60 * differences are: 61 * 62 * @detail 63 * 1. Not all accessor functions are present. 64 * 2. Used ONLY for PODs. 65 * 3. No Allocator template argument. Uses ::operator new() to get 66 * memory, and ::operator delete() to free it. 67 * Caveat: The dtor does NOT free the memory allocated, so this a 68 * memory-leaking vector! 69 */ 70 template<typename _Tp> 71 class __mini_vector 72 { 73 __mini_vector(const __mini_vector&); 74 __mini_vector& operator=(const __mini_vector&); 75 76 public: 77 typedef _Tp value_type; 78 typedef _Tp* pointer; 79 typedef _Tp& reference; 80 typedef const _Tp& const_reference; 81 typedef size_t size_type; 82 typedef ptrdiff_t difference_type; 83 typedef pointer iterator; 84 85 private: 86 pointer _M_start; 87 pointer _M_finish; 88 pointer _M_end_of_storage; 89 90 size_type 91 _M_space_left() const throw() 92 { return _M_end_of_storage - _M_finish; } 93 94 pointer 95 allocate(size_type __n) 96 { return static_cast<pointer>(::operator new(__n * sizeof(_Tp))); } 97 98 void 99 deallocate(pointer __p, size_type) 100 { ::operator delete(__p); } 101 102 public: 103 // Members used: size(), push_back(), pop_back(), 104 // insert(iterator, const_reference), erase(iterator), 105 // begin(), end(), back(), operator[]. 106 107 __mini_vector() 108 : _M_start(0), _M_finish(0), _M_end_of_storage(0) { } 109 110 size_type 111 size() const throw() 112 { return _M_finish - _M_start; } 113 114 iterator 115 begin() const throw() 116 { return this->_M_start; } 117 118 iterator 119 end() const throw() 120 { return this->_M_finish; } 121 122 reference 123 back() const throw() 124 { return *(this->end() - 1); } 125 126 reference 127 operator[](const size_type __pos) const throw() 128 { return this->_M_start[__pos]; } 129 130 void 131 insert(iterator __pos, const_reference __x); 132 133 void 134 push_back(const_reference __x) 135 { 136 if (this->_M_space_left()) 137 { 138 *this->end() = __x; 139 ++this->_M_finish; 140 } 141 else 142 this->insert(this->end(), __x); 143 } 144 145 void 146 pop_back() throw() 147 { --this->_M_finish; } 148 149 void 150 erase(iterator __pos) throw(); 151 152 void 153 clear() throw() 154 { this->_M_finish = this->_M_start; } 155 }; 156 157 // Out of line function definitions. 158 template<typename _Tp> 159 void __mini_vector<_Tp>:: 160 insert(iterator __pos, const_reference __x) 161 { 162 if (this->_M_space_left()) 163 { 164 size_type __to_move = this->_M_finish - __pos; 165 iterator __dest = this->end(); 166 iterator __src = this->end() - 1; 167 168 ++this->_M_finish; 169 while (__to_move) 170 { 171 *__dest = *__src; 172 --__dest; --__src; --__to_move; 173 } 174 *__pos = __x; 175 } 176 else 177 { 178 size_type __new_size = this->size() ? this->size() * 2 : 1; 179 iterator __new_start = this->allocate(__new_size); 180 iterator __first = this->begin(); 181 iterator __start = __new_start; 182 while (__first != __pos) 183 { 184 *__start = *__first; 185 ++__start; ++__first; 186 } 187 *__start = __x; 188 ++__start; 189 while (__first != this->end()) 190 { 191 *__start = *__first; 192 ++__start; ++__first; 193 } 194 if (this->_M_start) 195 this->deallocate(this->_M_start, this->size()); 196 197 this->_M_start = __new_start; 198 this->_M_finish = __start; 199 this->_M_end_of_storage = this->_M_start + __new_size; 200 } 201 } 202 203 template<typename _Tp> 204 void __mini_vector<_Tp>:: 205 erase(iterator __pos) throw() 206 { 207 while (__pos + 1 != this->end()) 208 { 209 *__pos = __pos[1]; 210 ++__pos; 211 } 212 --this->_M_finish; 213 } 214 215 216 template<typename _Tp> 217 struct __mv_iter_traits 218 { 219 typedef typename _Tp::value_type value_type; 220 typedef typename _Tp::difference_type difference_type; 221 }; 222 223 template<typename _Tp> 224 struct __mv_iter_traits<_Tp*> 225 { 226 typedef _Tp value_type; 227 typedef ptrdiff_t difference_type; 228 }; 229 230 enum 231 { 232 bits_per_byte = 8, 233 bits_per_block = sizeof(size_t) * size_t(bits_per_byte) 234 }; 235 236 template<typename _ForwardIterator, typename _Tp, typename _Compare> 237 _ForwardIterator 238 __lower_bound(_ForwardIterator __first, _ForwardIterator __last, 239 const _Tp& __val, _Compare __comp) 240 { 241 typedef typename __mv_iter_traits<_ForwardIterator>::value_type 242 _ValueType; 243 typedef typename __mv_iter_traits<_ForwardIterator>::difference_type 244 _DistanceType; 245 246 _DistanceType __len = __last - __first; 247 _DistanceType __half; 248 _ForwardIterator __middle; 249 250 while (__len > 0) 251 { 252 __half = __len >> 1; 253 __middle = __first; 254 __middle += __half; 255 if (__comp(*__middle, __val)) 256 { 257 __first = __middle; 258 ++__first; 259 __len = __len - __half - 1; 260 } 261 else 262 __len = __half; 263 } 264 return __first; 265 } 266 267 /** @brief The number of Blocks pointed to by the address pair 268 * passed to the function. 269 */ 270 template<typename _AddrPair> 271 inline size_t 272 __num_blocks(_AddrPair __ap) 273 { return (__ap.second - __ap.first) + 1; } 274 275 /** @brief The number of Bit-maps pointed to by the address pair 276 * passed to the function. 277 */ 278 template<typename _AddrPair> 279 inline size_t 280 __num_bitmaps(_AddrPair __ap) 281 { return __num_blocks(__ap) / size_t(bits_per_block); } 282 283 // _Tp should be a pointer type. 284 template<typename _Tp> 285 class _Inclusive_between 286 : public std::unary_function<typename std::pair<_Tp, _Tp>, bool> 287 { 288 typedef _Tp pointer; 289 pointer _M_ptr_value; 290 typedef typename std::pair<_Tp, _Tp> _Block_pair; 291 292 public: 293 _Inclusive_between(pointer __ptr) : _M_ptr_value(__ptr) 294 { } 295 296 bool 297 operator()(_Block_pair __bp) const throw() 298 { 299 if (std::less_equal<pointer>()(_M_ptr_value, __bp.second) 300 && std::greater_equal<pointer>()(_M_ptr_value, __bp.first)) 301 return true; 302 else 303 return false; 304 } 305 }; 306 307 // Used to pass a Functor to functions by reference. 308 template<typename _Functor> 309 class _Functor_Ref 310 : public std::unary_function<typename _Functor::argument_type, 311 typename _Functor::result_type> 312 { 313 _Functor& _M_fref; 314 315 public: 316 typedef typename _Functor::argument_type argument_type; 317 typedef typename _Functor::result_type result_type; 318 319 _Functor_Ref(_Functor& __fref) : _M_fref(__fref) 320 { } 321 322 result_type 323 operator()(argument_type __arg) 324 { return _M_fref(__arg); } 325 }; 326 327 /** @class _Ffit_finder bitmap_allocator.h bitmap_allocator.h 328 * 329 * @brief The class which acts as a predicate for applying the 330 * first-fit memory allocation policy for the bitmap allocator. 331 */ 332 // _Tp should be a pointer type, and _Alloc is the Allocator for 333 // the vector. 334 template<typename _Tp> 335 class _Ffit_finder 336 : public std::unary_function<typename std::pair<_Tp, _Tp>, bool> 337 { 338 typedef typename std::pair<_Tp, _Tp> _Block_pair; 339 typedef typename __detail::__mini_vector<_Block_pair> _BPVector; 340 typedef typename _BPVector::difference_type _Counter_type; 341 342 size_t* _M_pbitmap; 343 _Counter_type _M_data_offset; 344 345 public: 346 _Ffit_finder() : _M_pbitmap(0), _M_data_offset(0) 347 { } 348 349 bool 350 operator()(_Block_pair __bp) throw() 351 { 352 // Set the _rover to the last physical location bitmap, 353 // which is the bitmap which belongs to the first free 354 // block. Thus, the bitmaps are in exact reverse order of 355 // the actual memory layout. So, we count down the bitmaps, 356 // which is the same as moving up the memory. 357 358 // If the used count stored at the start of the Bit Map headers 359 // is equal to the number of Objects that the current Block can 360 // store, then there is definitely no space for another single 361 // object, so just return false. 362 _Counter_type __diff = __detail::__num_bitmaps(__bp); 363 364 if (*(reinterpret_cast<size_t*> 365 (__bp.first) - (__diff + 1)) == __detail::__num_blocks(__bp)) 366 return false; 367 368 size_t* __rover = reinterpret_cast<size_t*>(__bp.first) - 1; 369 370 for (_Counter_type __i = 0; __i < __diff; ++__i) 371 { 372 _M_data_offset = __i; 373 if (*__rover) 374 { 375 _M_pbitmap = __rover; 376 return true; 377 } 378 --__rover; 379 } 380 return false; 381 } 382 383 size_t* 384 _M_get() const throw() 385 { return _M_pbitmap; } 386 387 _Counter_type 388 _M_offset() const throw() 389 { return _M_data_offset * size_t(bits_per_block); } 390 }; 391 392 /** @class _Bitmap_counter bitmap_allocator.h bitmap_allocator.h 393 * 394 * @brief The bitmap counter which acts as the bitmap 395 * manipulator, and manages the bit-manipulation functions and 396 * the searching and identification functions on the bit-map. 397 */ 398 // _Tp should be a pointer type. 399 template<typename _Tp> 400 class _Bitmap_counter 401 { 402 typedef typename 403 __detail::__mini_vector<typename std::pair<_Tp, _Tp> > _BPVector; 404 typedef typename _BPVector::size_type _Index_type; 405 typedef _Tp pointer; 406 407 _BPVector& _M_vbp; 408 size_t* _M_curr_bmap; 409 size_t* _M_last_bmap_in_block; 410 _Index_type _M_curr_index; 411 412 public: 413 // Use the 2nd parameter with care. Make sure that such an 414 // entry exists in the vector before passing that particular 415 // index to this ctor. 416 _Bitmap_counter(_BPVector& Rvbp, long __index = -1) : _M_vbp(Rvbp) 417 { this->_M_reset(__index); } 418 419 void 420 _M_reset(long __index = -1) throw() 421 { 422 if (__index == -1) 423 { 424 _M_curr_bmap = 0; 425 _M_curr_index = static_cast<_Index_type>(-1); 426 return; 427 } 428 429 _M_curr_index = __index; 430 _M_curr_bmap = reinterpret_cast<size_t*> 431 (_M_vbp[_M_curr_index].first) - 1; 432 433 _GLIBCXX_DEBUG_ASSERT(__index <= (long)_M_vbp.size() - 1); 434 435 _M_last_bmap_in_block = _M_curr_bmap 436 - ((_M_vbp[_M_curr_index].second 437 - _M_vbp[_M_curr_index].first + 1) 438 / size_t(bits_per_block) - 1); 439 } 440 441 // Dangerous Function! Use with extreme care. Pass to this 442 // function ONLY those values that are known to be correct, 443 // otherwise this will mess up big time. 444 void 445 _M_set_internal_bitmap(size_t* __new_internal_marker) throw() 446 { _M_curr_bmap = __new_internal_marker; } 447 448 bool 449 _M_finished() const throw() 450 { return(_M_curr_bmap == 0); } 451 452 _Bitmap_counter& 453 operator++() throw() 454 { 455 if (_M_curr_bmap == _M_last_bmap_in_block) 456 { 457 if (++_M_curr_index == _M_vbp.size()) 458 _M_curr_bmap = 0; 459 else 460 this->_M_reset(_M_curr_index); 461 } 462 else 463 --_M_curr_bmap; 464 return *this; 465 } 466 467 size_t* 468 _M_get() const throw() 469 { return _M_curr_bmap; } 470 471 pointer 472 _M_base() const throw() 473 { return _M_vbp[_M_curr_index].first; } 474 475 _Index_type 476 _M_offset() const throw() 477 { 478 return size_t(bits_per_block) 479 * ((reinterpret_cast<size_t*>(this->_M_base()) 480 - _M_curr_bmap) - 1); 481 } 482 483 _Index_type 484 _M_where() const throw() 485 { return _M_curr_index; } 486 }; 487 488 /** @brief Mark a memory address as allocated by re-setting the 489 * corresponding bit in the bit-map. 490 */ 491 inline void 492 __bit_allocate(size_t* __pbmap, size_t __pos) throw() 493 { 494 size_t __mask = 1 << __pos; 495 __mask = ~__mask; 496 *__pbmap &= __mask; 497 } 498 499 /** @brief Mark a memory address as free by setting the 500 * corresponding bit in the bit-map. 501 */ 502 inline void 503 __bit_free(size_t* __pbmap, size_t __pos) throw() 504 { 505 size_t __mask = 1 << __pos; 506 *__pbmap |= __mask; 507 } 508 509 _GLIBCXX_END_NAMESPACE_VERSION 510 } // namespace __detail 511 512 _GLIBCXX_BEGIN_NAMESPACE_VERSION 513 514 /** @brief Generic Version of the bsf instruction. 515 */ 516 inline size_t 517 _Bit_scan_forward(size_t __num) 518 { return static_cast<size_t>(__builtin_ctzl(__num)); } 519 520 /** @class free_list bitmap_allocator.h bitmap_allocator.h 521 * 522 * @brief The free list class for managing chunks of memory to be 523 * given to and returned by the bitmap_allocator. 524 */ 525 class free_list 526 { 527 public: 528 typedef size_t* value_type; 529 typedef __detail::__mini_vector<value_type> vector_type; 530 typedef vector_type::iterator iterator; 531 typedef __mutex __mutex_type; 532 533 private: 534 struct _LT_pointer_compare 535 { 536 bool 537 operator()(const size_t* __pui, 538 const size_t __cui) const throw() 539 { return *__pui < __cui; } 540 }; 541 542 #if defined __GTHREADS 543 __mutex_type& 544 _M_get_mutex() 545 { 546 static __mutex_type _S_mutex; 547 return _S_mutex; 548 } 549 #endif 550 551 vector_type& 552 _M_get_free_list() 553 { 554 static vector_type _S_free_list; 555 return _S_free_list; 556 } 557 558 /** @brief Performs validation of memory based on their size. 559 * 560 * @param __addr The pointer to the memory block to be 561 * validated. 562 * 563 * @detail Validates the memory block passed to this function and 564 * appropriately performs the action of managing the free list of 565 * blocks by adding this block to the free list or deleting this 566 * or larger blocks from the free list. 567 */ 568 void 569 _M_validate(size_t* __addr) throw() 570 { 571 vector_type& __free_list = _M_get_free_list(); 572 const vector_type::size_type __max_size = 64; 573 if (__free_list.size() >= __max_size) 574 { 575 // Ok, the threshold value has been reached. We determine 576 // which block to remove from the list of free blocks. 577 if (*__addr >= *__free_list.back()) 578 { 579 // Ok, the new block is greater than or equal to the 580 // last block in the list of free blocks. We just free 581 // the new block. 582 ::operator delete(static_cast<void*>(__addr)); 583 return; 584 } 585 else 586 { 587 // Deallocate the last block in the list of free lists, 588 // and insert the new one in its correct position. 589 ::operator delete(static_cast<void*>(__free_list.back())); 590 __free_list.pop_back(); 591 } 592 } 593 594 // Just add the block to the list of free lists unconditionally. 595 iterator __temp = __detail::__lower_bound 596 (__free_list.begin(), __free_list.end(), 597 *__addr, _LT_pointer_compare()); 598 599 // We may insert the new free list before _temp; 600 __free_list.insert(__temp, __addr); 601 } 602 603 /** @brief Decides whether the wastage of memory is acceptable for 604 * the current memory request and returns accordingly. 605 * 606 * @param __block_size The size of the block available in the free 607 * list. 608 * 609 * @param __required_size The required size of the memory block. 610 * 611 * @return true if the wastage incurred is acceptable, else returns 612 * false. 613 */ 614 bool 615 _M_should_i_give(size_t __block_size, 616 size_t __required_size) throw() 617 { 618 const size_t __max_wastage_percentage = 36; 619 if (__block_size >= __required_size && 620 (((__block_size - __required_size) * 100 / __block_size) 621 < __max_wastage_percentage)) 622 return true; 623 else 624 return false; 625 } 626 627 public: 628 /** @brief This function returns the block of memory to the 629 * internal free list. 630 * 631 * @param __addr The pointer to the memory block that was given 632 * by a call to the _M_get function. 633 */ 634 inline void 635 _M_insert(size_t* __addr) throw() 636 { 637 #if defined __GTHREADS 638 __scoped_lock __bfl_lock(_M_get_mutex()); 639 #endif 640 // Call _M_validate to decide what should be done with 641 // this particular free list. 642 this->_M_validate(reinterpret_cast<size_t*>(__addr) - 1); 643 // See discussion as to why this is 1! 644 } 645 646 /** @brief This function gets a block of memory of the specified 647 * size from the free list. 648 * 649 * @param __sz The size in bytes of the memory required. 650 * 651 * @return A pointer to the new memory block of size at least 652 * equal to that requested. 653 */ 654 size_t* 655 _M_get(size_t __sz) throw(std::bad_alloc); 656 657 /** @brief This function just clears the internal Free List, and 658 * gives back all the memory to the OS. 659 */ 660 void 661 _M_clear(); 662 }; 663 664 665 // Forward declare the class. 666 template<typename _Tp> 667 class bitmap_allocator; 668 669 // Specialize for void: 670 template<> 671 class bitmap_allocator<void> 672 { 673 public: 674 typedef void* pointer; 675 typedef const void* const_pointer; 676 677 // Reference-to-void members are impossible. 678 typedef void value_type; 679 template<typename _Tp1> 680 struct rebind 681 { 682 typedef bitmap_allocator<_Tp1> other; 683 }; 684 }; 685 686 /** 687 * @brief Bitmap Allocator, primary template. 688 * @ingroup allocators 689 */ 690 template<typename _Tp> 691 class bitmap_allocator : private free_list 692 { 693 public: 694 typedef size_t size_type; 695 typedef ptrdiff_t difference_type; 696 typedef _Tp* pointer; 697 typedef const _Tp* const_pointer; 698 typedef _Tp& reference; 699 typedef const _Tp& const_reference; 700 typedef _Tp value_type; 701 typedef free_list::__mutex_type __mutex_type; 702 703 template<typename _Tp1> 704 struct rebind 705 { 706 typedef bitmap_allocator<_Tp1> other; 707 }; 708 709 private: 710 template<size_t _BSize, size_t _AlignSize> 711 struct aligned_size 712 { 713 enum 714 { 715 modulus = _BSize % _AlignSize, 716 value = _BSize + (modulus ? _AlignSize - (modulus) : 0) 717 }; 718 }; 719 720 struct _Alloc_block 721 { 722 char __M_unused[aligned_size<sizeof(value_type), 723 _BALLOC_ALIGN_BYTES>::value]; 724 }; 725 726 727 typedef typename std::pair<_Alloc_block*, _Alloc_block*> _Block_pair; 728 729 typedef typename __detail::__mini_vector<_Block_pair> _BPVector; 730 typedef typename _BPVector::iterator _BPiter; 731 732 template<typename _Predicate> 733 static _BPiter 734 _S_find(_Predicate __p) 735 { 736 _BPiter __first = _S_mem_blocks.begin(); 737 while (__first != _S_mem_blocks.end() && !__p(*__first)) 738 ++__first; 739 return __first; 740 } 741 742 #if defined _GLIBCXX_DEBUG 743 // Complexity: O(lg(N)). Where, N is the number of block of size 744 // sizeof(value_type). 745 void 746 _S_check_for_free_blocks() throw() 747 { 748 typedef typename __detail::_Ffit_finder<_Alloc_block*> _FFF; 749 _BPiter __bpi = _S_find(_FFF()); 750 751 _GLIBCXX_DEBUG_ASSERT(__bpi == _S_mem_blocks.end()); 752 } 753 #endif 754 755 /** @brief Responsible for exponentially growing the internal 756 * memory pool. 757 * 758 * @throw std::bad_alloc. If memory can not be allocated. 759 * 760 * @detail Complexity: O(1), but internally depends upon the 761 * complexity of the function free_list::_M_get. The part where 762 * the bitmap headers are written has complexity: O(X),where X 763 * is the number of blocks of size sizeof(value_type) within 764 * the newly acquired block. Having a tight bound. 765 */ 766 void 767 _S_refill_pool() throw(std::bad_alloc) 768 { 769 #if defined _GLIBCXX_DEBUG 770 _S_check_for_free_blocks(); 771 #endif 772 773 const size_t __num_bitmaps = (_S_block_size 774 / size_t(__detail::bits_per_block)); 775 const size_t __size_to_allocate = sizeof(size_t) 776 + _S_block_size * sizeof(_Alloc_block) 777 + __num_bitmaps * sizeof(size_t); 778 779 size_t* __temp = 780 reinterpret_cast<size_t*>(this->_M_get(__size_to_allocate)); 781 *__temp = 0; 782 ++__temp; 783 784 // The Header information goes at the Beginning of the Block. 785 _Block_pair __bp = 786 std::make_pair(reinterpret_cast<_Alloc_block*> 787 (__temp + __num_bitmaps), 788 reinterpret_cast<_Alloc_block*> 789 (__temp + __num_bitmaps) 790 + _S_block_size - 1); 791 792 // Fill the Vector with this information. 793 _S_mem_blocks.push_back(__bp); 794 795 for (size_t __i = 0; __i < __num_bitmaps; ++__i) 796 __temp[__i] = ~static_cast<size_t>(0); // 1 Indicates all Free. 797 798 _S_block_size *= 2; 799 } 800 801 static _BPVector _S_mem_blocks; 802 static size_t _S_block_size; 803 static __detail::_Bitmap_counter<_Alloc_block*> _S_last_request; 804 static typename _BPVector::size_type _S_last_dealloc_index; 805 #if defined __GTHREADS 806 static __mutex_type _S_mut; 807 #endif 808 809 public: 810 811 /** @brief Allocates memory for a single object of size 812 * sizeof(_Tp). 813 * 814 * @throw std::bad_alloc. If memory can not be allocated. 815 * 816 * @detail Complexity: Worst case complexity is O(N), but that 817 * is hardly ever hit. If and when this particular case is 818 * encountered, the next few cases are guaranteed to have a 819 * worst case complexity of O(1)! That's why this function 820 * performs very well on average. You can consider this 821 * function to have a complexity referred to commonly as: 822 * Amortized Constant time. 823 */ 824 pointer 825 _M_allocate_single_object() throw(std::bad_alloc) 826 { 827 #if defined __GTHREADS 828 __scoped_lock __bit_lock(_S_mut); 829 #endif 830 831 // The algorithm is something like this: The last_request 832 // variable points to the last accessed Bit Map. When such a 833 // condition occurs, we try to find a free block in the 834 // current bitmap, or succeeding bitmaps until the last bitmap 835 // is reached. If no free block turns up, we resort to First 836 // Fit method. 837 838 // WARNING: Do not re-order the condition in the while 839 // statement below, because it relies on C++'s short-circuit 840 // evaluation. The return from _S_last_request->_M_get() will 841 // NOT be dereference able if _S_last_request->_M_finished() 842 // returns true. This would inevitably lead to a NULL pointer 843 // dereference if tinkered with. 844 while (_S_last_request._M_finished() == false 845 && (*(_S_last_request._M_get()) == 0)) 846 _S_last_request.operator++(); 847 848 if (__builtin_expect(_S_last_request._M_finished() == true, false)) 849 { 850 // Fall Back to First Fit algorithm. 851 typedef typename __detail::_Ffit_finder<_Alloc_block*> _FFF; 852 _FFF __fff; 853 _BPiter __bpi = _S_find(__detail::_Functor_Ref<_FFF>(__fff)); 854 855 if (__bpi != _S_mem_blocks.end()) 856 { 857 // Search was successful. Ok, now mark the first bit from 858 // the right as 0, meaning Allocated. This bit is obtained 859 // by calling _M_get() on __fff. 860 size_t __nz_bit = _Bit_scan_forward(*__fff._M_get()); 861 __detail::__bit_allocate(__fff._M_get(), __nz_bit); 862 863 _S_last_request._M_reset(__bpi - _S_mem_blocks.begin()); 864 865 // Now, get the address of the bit we marked as allocated. 866 pointer __ret = reinterpret_cast<pointer> 867 (__bpi->first + __fff._M_offset() + __nz_bit); 868 size_t* __puse_count = 869 reinterpret_cast<size_t*> 870 (__bpi->first) - (__detail::__num_bitmaps(*__bpi) + 1); 871 872 ++(*__puse_count); 873 return __ret; 874 } 875 else 876 { 877 // Search was unsuccessful. We Add more memory to the 878 // pool by calling _S_refill_pool(). 879 _S_refill_pool(); 880 881 // _M_Reset the _S_last_request structure to the first 882 // free block's bit map. 883 _S_last_request._M_reset(_S_mem_blocks.size() - 1); 884 885 // Now, mark that bit as allocated. 886 } 887 } 888 889 // _S_last_request holds a pointer to a valid bit map, that 890 // points to a free block in memory. 891 size_t __nz_bit = _Bit_scan_forward(*_S_last_request._M_get()); 892 __detail::__bit_allocate(_S_last_request._M_get(), __nz_bit); 893 894 pointer __ret = reinterpret_cast<pointer> 895 (_S_last_request._M_base() + _S_last_request._M_offset() + __nz_bit); 896 897 size_t* __puse_count = reinterpret_cast<size_t*> 898 (_S_mem_blocks[_S_last_request._M_where()].first) 899 - (__detail:: 900 __num_bitmaps(_S_mem_blocks[_S_last_request._M_where()]) + 1); 901 902 ++(*__puse_count); 903 return __ret; 904 } 905 906 /** @brief Deallocates memory that belongs to a single object of 907 * size sizeof(_Tp). 908 * 909 * @detail Complexity: O(lg(N)), but the worst case is not hit 910 * often! This is because containers usually deallocate memory 911 * close to each other and this case is handled in O(1) time by 912 * the deallocate function. 913 */ 914 void 915 _M_deallocate_single_object(pointer __p) throw() 916 { 917 #if defined __GTHREADS 918 __scoped_lock __bit_lock(_S_mut); 919 #endif 920 _Alloc_block* __real_p = reinterpret_cast<_Alloc_block*>(__p); 921 922 typedef typename _BPVector::iterator _Iterator; 923 typedef typename _BPVector::difference_type _Difference_type; 924 925 _Difference_type __diff; 926 long __displacement; 927 928 _GLIBCXX_DEBUG_ASSERT(_S_last_dealloc_index >= 0); 929 930 __detail::_Inclusive_between<_Alloc_block*> __ibt(__real_p); 931 if (__ibt(_S_mem_blocks[_S_last_dealloc_index])) 932 { 933 _GLIBCXX_DEBUG_ASSERT(_S_last_dealloc_index 934 <= _S_mem_blocks.size() - 1); 935 936 // Initial Assumption was correct! 937 __diff = _S_last_dealloc_index; 938 __displacement = __real_p - _S_mem_blocks[__diff].first; 939 } 940 else 941 { 942 _Iterator _iter = _S_find(__ibt); 943 944 _GLIBCXX_DEBUG_ASSERT(_iter != _S_mem_blocks.end()); 945 946 __diff = _iter - _S_mem_blocks.begin(); 947 __displacement = __real_p - _S_mem_blocks[__diff].first; 948 _S_last_dealloc_index = __diff; 949 } 950 951 // Get the position of the iterator that has been found. 952 const size_t __rotate = (__displacement 953 % size_t(__detail::bits_per_block)); 954 size_t* __bitmapC = 955 reinterpret_cast<size_t*> 956 (_S_mem_blocks[__diff].first) - 1; 957 __bitmapC -= (__displacement / size_t(__detail::bits_per_block)); 958 959 __detail::__bit_free(__bitmapC, __rotate); 960 size_t* __puse_count = reinterpret_cast<size_t*> 961 (_S_mem_blocks[__diff].first) 962 - (__detail::__num_bitmaps(_S_mem_blocks[__diff]) + 1); 963 964 _GLIBCXX_DEBUG_ASSERT(*__puse_count != 0); 965 966 --(*__puse_count); 967 968 if (__builtin_expect(*__puse_count == 0, false)) 969 { 970 _S_block_size /= 2; 971 972 // We can safely remove this block. 973 // _Block_pair __bp = _S_mem_blocks[__diff]; 974 this->_M_insert(__puse_count); 975 _S_mem_blocks.erase(_S_mem_blocks.begin() + __diff); 976 977 // Reset the _S_last_request variable to reflect the 978 // erased block. We do this to protect future requests 979 // after the last block has been removed from a particular 980 // memory Chunk, which in turn has been returned to the 981 // free list, and hence had been erased from the vector, 982 // so the size of the vector gets reduced by 1. 983 if ((_Difference_type)_S_last_request._M_where() >= __diff--) 984 _S_last_request._M_reset(__diff); 985 986 // If the Index into the vector of the region of memory 987 // that might hold the next address that will be passed to 988 // deallocated may have been invalidated due to the above 989 // erase procedure being called on the vector, hence we 990 // try to restore this invariant too. 991 if (_S_last_dealloc_index >= _S_mem_blocks.size()) 992 { 993 _S_last_dealloc_index =(__diff != -1 ? __diff : 0); 994 _GLIBCXX_DEBUG_ASSERT(_S_last_dealloc_index >= 0); 995 } 996 } 997 } 998 999 public: 1000 bitmap_allocator() throw() 1001 { } 1002 1003 bitmap_allocator(const bitmap_allocator&) 1004 { } 1005 1006 template<typename _Tp1> 1007 bitmap_allocator(const bitmap_allocator<_Tp1>&) throw() 1008 { } 1009 1010 ~bitmap_allocator() throw() 1011 { } 1012 1013 pointer 1014 allocate(size_type __n) 1015 { 1016 if (__n > this->max_size()) 1017 std::__throw_bad_alloc(); 1018 1019 if (__builtin_expect(__n == 1, true)) 1020 return this->_M_allocate_single_object(); 1021 else 1022 { 1023 const size_type __b = __n * sizeof(value_type); 1024 return reinterpret_cast<pointer>(::operator new(__b)); 1025 } 1026 } 1027 1028 pointer 1029 allocate(size_type __n, typename bitmap_allocator<void>::const_pointer) 1030 { return allocate(__n); } 1031 1032 void 1033 deallocate(pointer __p, size_type __n) throw() 1034 { 1035 if (__builtin_expect(__p != 0, true)) 1036 { 1037 if (__builtin_expect(__n == 1, true)) 1038 this->_M_deallocate_single_object(__p); 1039 else 1040 ::operator delete(__p); 1041 } 1042 } 1043 1044 pointer 1045 address(reference __r) const 1046 { return std::__addressof(__r); } 1047 1048 const_pointer 1049 address(const_reference __r) const 1050 { return std::__addressof(__r); } 1051 1052 size_type 1053 max_size() const throw() 1054 { return size_type(-1) / sizeof(value_type); } 1055 1056 void 1057 construct(pointer __p, const_reference __data) 1058 { ::new((void *)__p) value_type(__data); } 1059 1060 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 1061 template<typename... _Args> 1062 void 1063 construct(pointer __p, _Args&&... __args) 1064 { ::new((void *)__p) _Tp(std::forward<_Args>(__args)...); } 1065 #endif 1066 1067 void 1068 destroy(pointer __p) 1069 { __p->~value_type(); } 1070 }; 1071 1072 template<typename _Tp1, typename _Tp2> 1073 bool 1074 operator==(const bitmap_allocator<_Tp1>&, 1075 const bitmap_allocator<_Tp2>&) throw() 1076 { return true; } 1077 1078 template<typename _Tp1, typename _Tp2> 1079 bool 1080 operator!=(const bitmap_allocator<_Tp1>&, 1081 const bitmap_allocator<_Tp2>&) throw() 1082 { return false; } 1083 1084 // Static member definitions. 1085 template<typename _Tp> 1086 typename bitmap_allocator<_Tp>::_BPVector 1087 bitmap_allocator<_Tp>::_S_mem_blocks; 1088 1089 template<typename _Tp> 1090 size_t bitmap_allocator<_Tp>::_S_block_size = 1091 2 * size_t(__detail::bits_per_block); 1092 1093 template<typename _Tp> 1094 typename bitmap_allocator<_Tp>::_BPVector::size_type 1095 bitmap_allocator<_Tp>::_S_last_dealloc_index = 0; 1096 1097 template<typename _Tp> 1098 __detail::_Bitmap_counter 1099 <typename bitmap_allocator<_Tp>::_Alloc_block*> 1100 bitmap_allocator<_Tp>::_S_last_request(_S_mem_blocks); 1101 1102 #if defined __GTHREADS 1103 template<typename _Tp> 1104 typename bitmap_allocator<_Tp>::__mutex_type 1105 bitmap_allocator<_Tp>::_S_mut; 1106 #endif 1107 1108 _GLIBCXX_END_NAMESPACE_VERSION 1109 } // namespace __gnu_cxx 1110 1111 #endif 1112 1113