SGI STL第2段空間配置器メモリプール

5940 ワード

メモリプールからコントロールを取ってfree listに使用します.chunk_です.allocの仕事:
template <bool __threads, int __inst>
char*
__default_alloc_template<__threads __inst="">::
chunk_alloc(size_t __size, int& __nobjs)//          
{
    char* __result;
    size_t __total_bytes = __size * __nobjs;//       
    size_t __bytes_left = _S_end_free - _S_start_free; //     
    //            
    if (__bytes_left >= __total_bytes) { 
        __result = _S_start_free; //       __result
        _S_start_free += __total_bytes;//  _S_start_free  
        return(__result); //           
    //          size            
    } else if (__bytes_left >= __size) { 
    //              left/size  
        __nobjs = (int)(__bytes_left/__size);//       
        __total_bytes = __size * __nobjs;
        __result = _S_start_free;
        _S_start_free += __total_bytes; //  _S_start_free  
        return(__result); //     
    //           
    } else {
        //               
        size_t __bytes_to_get = 
      2 * __total_bytes + _S_round_up(_S_heap_size >> 4);
        // Try to make use of the left-over piece.
        //              free list 
        if (__bytes_left > 0) {
          //           
          _Obj* __STL_VOLATILE* __my_free_list = //    
                        _S_free_list + _S_freelist_index(__bytes_left);
          //                                 
          ((_Obj*)_S_start_free) -> _M_free_list_link = *__my_free_list;
          *__my_free_list = (_Obj*)_S_start_free;
        }
        //  malloc          
        _S_start_free = (char*)malloc(__bytes_to_get); //       
        //  malloc    ,    free list    
        if (0 == _S_start_free) {
            size_t __i;
            _Obj* __STL_VOLATILE* __my_free_list;
            _Obj* __p;
            // Try to make do with what we have.  That can't
            // hurt.  We do not try smaller requests, since that tends
            // to result in disaster on multi-process machines.
            //   free list  
            for (__i = __size;__i <= (size_t) _MAX_BYTES;
                 __i += (size_t) _ALIGN) {
                __my_free_list = _S_free_list + _S_freelist_index(__i);
                __p = *__my_free_list;
                if (0 != __p) {//  ,          
                    *__my_free_list = __p -> _M_free_list_link;
                    _S_start_free = (char*)__p;
                    _S_end_free = _S_start_free + __i;
                    return(_S_chunk_alloc(__size, __nobjs));//       
                    // Any leftover piece will eventually make it to the
                    // right free list.
                }
            }
            //      
            _S_end_free = 0;    // In case of exception.
            //               ,        ,      
            _S_start_free =(char*)malloc_alloc::allocate(__bytes_to_get);
            // This should either throw an
            // exception or remedy the situation.  Thus we assume it
            // succeeded.
        }
        //        
        _S_heap_size += __bytes_to_get;//      
        _S_end_free = _S_start_free + __bytes_to_get;//        
        return(_S_chunk_alloc(__size, __nobjs)); //    
    }
}
     chunk_alloc    _S _end _free - _S _start _free               。
         ,     20    free _list. 
         20   ,              ,      20        。  ,nobjs                 ;
                   ,      malloc heap     ,                 。            。
    heap          ,chunk_alloc  “          “ free lists。          ,            。