ballocator_doc.txt

来自「xml大全 可读写调用率很高 xml大全 可读写调用率很高」· 文本 代码 · 共 375 行 · 第 1/2 页

TXT
375
字号
			BITMAPPED ALLOCATOR			===================2004-03-11  Dhruv Matani  <dhruvbird@HotPOP.com>---------------------------------------------------------------------As this name suggests, this allocator uses a bit-map to keep track ofthe used and unused memory locations for it's book-keeping purposes.This allocator will make use of 1 single bit to keep track of whetherit has been allocated or not. A bit 1 indicates free, while 0indicates allocated. This has been done so that you can easily check acollection of bits for a free block. This kind of Bitmapped strategyworks best for single object allocations, and with the STL typeparameterized allocators, we do not need to choose any size for theblock which will be represented by a single bit. This will be the sizeof the parameter around which the allocator has beenparameterized. Thus, close to optimal performance will result. Hence,this should be used for node based containers which call the allocatefunction with an argument of 1.The bitmapped allocator's internal pool is exponentiallygrowing. Meaning that internally, the blocks acquired from the FreeList Store will double every time the bitmapped allocator runs out ofmemory.--------------------------------------------------------------------The macro __GTHREADS decides whether to use Mutex Protection aroundevery allocation/deallocation.  The state of the macro is picked upautomatically from the gthr abstration layer.----------------------------------------------------------------------What is the Free List Store?----------------------------The Free List Store (referred to as FLS for the remaining part of thisdocument) is the Global memory pool that is shared by all instances ofthe bitmapped allocator instantiated for any type. This maintains asorted order of all free memory blocks given back to it by thebitmapped allocator, and is also responsible for giving memory to thebitmapped allocator when it asks for more.Internally, there is a Free List threshold which indicates the Maximumnumber of free lists that the FLS can hold internally(cache). Currently, this value is set at 64. So, if there are morethan 64 free lists coming in, then some of them will be given back tothe OS using operator delete so that at any given time the Free List'ssize does not exceed 64 entries. This is done because a Binary Searchis used to locate an entry in a free list when a request for memorycomes along. Thus, the run-time complexity of the search would go upgiven an increasing size, for 64 entries however, lg(64) == 6comparisons are enough to locate the correct free list if it exists.Suppose the free list size has reached it's threshold, then thelargest block from among those in the list and the new block will beselected and given back to the OS. This is done because it reducesexternal fragmentation, and allows the OS to use the larger blockslater in an orderly fashion, possibly merging them later. Also, onsome systems, large blocks are obtained via calls to mmap, so givingthem back to free system resources becomes most important.The function _S_should_i_give decides the policy that determineswhether the current block of memory should be given to the allocatorfor the request that it has made. That's because we may not alwayshave exact fits for the memory size that the allocator requests. We dothis mainly to prevent external fragmentation at the cost of a littleinternal fragmentation. Now, the value of this internal fragmentationhas to be decided by this function. I can see 3 possibilities rightnow. Please add more as and when you find better strategies.1. Equal size check. Return true only when the 2 blocks are of equal   size.2. Difference Threshold: Return true only when the _block_size is   greater than or equal to the _required_size, and if the _BS is >   _RS by a difference of less than some THRESHOLD value, then return   true, else return false.  3. Percentage Threshold. Return true only when the _block_size is   greater than or equal to the _required_size, and if the _BS is >   _RS by a percentage of less than some THRESHOLD value, then return   true, else return false.Currently, (3) is being used with a value of 36% Maximum wastage perSuper Block.--------------------------------------------------------------------1) What is a super block? Why is it needed?   A super block is the block of memory acquired from the FLS from   which the bitmap allocator carves out memory for single objects and   satisfies the user's requests. These super blocks come in sizes that   are powers of 2 and multiples of 32 (_Bits_Per_Block). Yes both at   the same time! That's because the next super block acquired will be   2 times the previous one, and also all super blocks have to be   multiples of the _Bits_Per_Block value.2) How does it interact with the free list store?   The super block is contained in the FLS, and the FLS is responsible   for getting / returning Super Bocks to and from the OS using   operator new as defined by the C++ standard.---------------------------------------------------------------------How does the allocate function Work?------------------------------------The allocate function is specialized for single object allocationONLY. Thus, ONLY if n == 1, will the bitmap_allocator's specializedalgorithm be used. Otherwise, the request is satisfied directly bycalling operator new.Suppose n == 1, then the allocator does the following:1. Checks to see whether the a free block exists somewhere in a region   of memory close to the last satisfied request. If so, then that   block is marked as allocated in the bit map and given to the   user. If not, then (2) is executed.2. Is there a free block anywhere after the current block right upto   the end of the memory that we have? If so, that block is found, and   the same procedure is applied as above, and returned to the   user. If not, then (3) is executed.3. Is there any block in whatever region of memory that we own free?   This is done by checking (a) The use count for each super block,   and if that fails then (b) The individual bit-maps for each super   block. Note: Here we are never touching any of the memory that the   user will be given, and we are confining all memory accesses to a   small region of memory! This helps reduce cache misses. If this   succeeds then we apply the same procedure on that bit-map as (1),   and return that block of memory to the user. However, if this   process fails, then we resort to (4).4. This process involves Refilling the internal exponentially growing   memory pool. The said effect is achieved by calling _S_refill_pool   which does the following:	 (a). Gets more memory from the Global Free List of the	      Required size.	 (b). Adjusts the size for the next call to itself.	 (c). Writes the appropriate headers in the bit-maps.	 (d). Sets the use count for that super-block just allocated	      to 0 (zero).	 (e). All of the above accounts to maintaining the basic	      invariant for the allocator. If the invariant is	      maintained, we are sure that all is well.   Now, the same process is applied on the newly acquired free blocks,   which are dispatched accordingly.Thus, you can clearly see that the allocate function is nothing but acombination of the next-fit and first-fit algorithm optimized ONLY forsingle object allocations.-------------------------------------------------------------------------How does the deallocate function work?--------------------------------------The deallocate function again is specialized for single objects ONLY.For all n belonging to > 1, the operator delete is called withoutfurther ado, and the deallocate function returns.However for n == 1, a series of steps are performed:1. We first need to locate that super-block which holds the memory   location given to us by the user. For that purpose, we maintain a   static variable _S_last_dealloc_index, which holds the index into   the vector of block pairs which indicates the index of the last   super-block from which memory was freed. We use this strategy in   the hope that the user will deallocate memory in a region close to   what he/she deallocated the last time around. If the check for   belongs_to succeeds, then we determine the bit-map for the given   pointer, and locate the index into that bit-map, and mark that bit   as free by setting it.2. If the _S_last_dealloc_index does not point to the memory block   that we're looking for, then we do a linear search on the block   stored in the vector of Block Pairs. This vector in code is called   _S_mem_blocks. When the corresponding super-block is found, we   apply the same procedure as we did for (1) to mark the block as   free in the bit-map.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?