C++STL中的空間配置器只有一種,是同過底層的malloc和free實作的,空間配置器中有四種方法:

SGI STL中有兩種空間配置器,一級allocator是與stl一致的malloc和free的方式,二級allocator是通過記憶體池的方式實作的,
SGI STL中的vector容器的模板中用到了空間配置器,默認用的是二級allocator,該容器底層存盤物件的構造和析構是通過全域的函式模板construct和destroy實作的,這里我們著重研究allocator中的allocate和deallocate方法,

allocate:
// breaks if we make these template class members:
enum {_ALIGN = 8};
enum {_MAX_BYTES = 128};
enum {_NFREELISTS = 16}; // _MAX_BYTES/_ALIGN
/* __n must be > 0 */
//分配大小為__n的記憶體
static void* allocate(size_t __n)
{
void* __ret = 0;
if (__n > (size_t) _MAX_BYTES) {//如果需要的記憶體塊大小超過最大記憶體,則按照標準庫的方式分配記憶體
__ret = malloc_alloc::allocate(__n);//呼叫一級allocator
}
else {
_Obj* __STL_VOLATILE* __my_free_list//是一個double*的型別,指向陣列的位置,解參考之后是陣列中元素的值
= _S_free_list + _S_freelist_index(__n);//根據_S_freelist_index(__n)定位陣列中chunk塊的位置
//_S_free_list就是上圖中的陣列
// Acquire the lock here with a constructor call.
// This ensures that it is released in exit or during stack
// unwinding.
# ifndef _NOTHREADS
/*REFERENCED*/
_Lock __lock_instance;//上鎖
# endif
_Obj* __RESTRICT __result = *__my_free_list;//result是陣列中的__Obj*
if (__result == 0)//如果陣列中的元素未初始化
__ret = _S_refill(_S_round_up(__n));//構建鏈表,回傳鏈表的地址
else {//已經初始化
*__my_free_list = __result -> _M_free_list_link;//指向下一個節點
__ret = __result;//回傳下一個節點的地址
}
}
return __ret;
};
指標的加法操作注意事項:
? 指標型別占用的記憶體多大,其指標加一就會偏移多少位元組,舉個例子:char型別只占1個位元組,那么char* +1就只偏移一個位元組,指向下一個記憶體地址;int型別占4個位元組,int* +1就會偏移4個位元組,指向4個位元組后的記憶體地址,
_S_refill函式:
/* Returns an object of size __n, and optionally adds to size __n free list.*/
/* We assume that __n is properly aligned. */
/* We hold the allocation lock. */
template <bool __threads, int __inst>
void*
__default_alloc_template<__threads, __inst>::_S_refill(size_t __n)
{
int __nobjs = 20;
char* __chunk = _S_chunk_alloc(__n, __nobjs);//分配起始位置的地址
_Obj* __STL_VOLATILE* __my_free_list;
_Obj* __result;
_Obj* __current_obj;
_Obj* __next_obj;
int __i;
if (1 == __nobjs) return(__chunk);//如果數量為1,直接回傳當前塊
__my_free_list = _S_free_list + _S_freelist_index(__n);//指向陣列塊的位置,這里先以 __n=8 為例
/* Build free list in chunk */
__result = (_Obj*)__chunk;//result加一次可以跳到下個節點
*__my_free_list = __next_obj = (_Obj*)(__chunk + __n);//使陣列中的第一個元素指向第二個chunk
for (__i = 1; ; __i++) {
__current_obj = __next_obj;
__next_obj = (_Obj*)((char*)__next_obj + __n);//指向下一個節點
if (__nobjs - 1 == __i) {//到鏈表最后節點
__current_obj -> _M_free_list_link = 0;//使next節點等于nullptr表示最后一個節點
break;
} else {
__current_obj -> _M_free_list_link = __next_obj;
}
}
return(__result);//回傳鏈表首節點地址
}

_S_chunk_alloc(size_t __size, int& __nobjs):
/* We allocate memory in large chunks in order to avoid fragmenting */
/* the malloc heap too much. */
/* We assume that size is properly aligned. */
/* We hold the allocation lock. */
template <bool __threads, int __inst>
char*
__default_alloc_template<__threads, __inst>::_S_chunk_alloc(size_t __size,
int& __nobjs)
{//還是以nobjs為20,size為8來假設
char* __result;
size_t __total_bytes = __size * __nobjs;//160
size_t __bytes_left = _S_end_free - _S_start_free;//0 兩者初始化都為0 //第二次_1 __bytes_left=320
if (__bytes_left >= __total_bytes) {//第二次_2 回傳開辟的記憶體
__result = _S_start_free;
_S_start_free += __total_bytes;
return(__result);
} else if (__bytes_left >= __size) {//如果想要申請其他大小的chunk塊,可能會呼叫此方法在上一步申請的備用記憶體中查找有無可用的記憶體
__nobjs = (int)(__bytes_left/__size);
__total_bytes = __size * __nobjs;
__result = _S_start_free;
_S_start_free += __total_bytes;
return(__result);
} else {//初始化_1首先進入該分支
size_t __bytes_to_get =
2 * __total_bytes + _S_round_up(_S_heap_size >> 4);//_S_heap_size初始為0,初始化__bytes_to_get=320
// Try to make use of the left-over piece.
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;
}
_S_start_free = (char*)malloc(__bytes_to_get);//初始化_2跳到這一步
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.
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;//初始化_3 _S_heap_size=320
_S_end_free = _S_start_free + __bytes_to_get;//_S_end_free是 char*型別,開辟一塊320位元組的記憶體塊
return(_S_chunk_alloc(__size, __nobjs));//初始化_4 遞回呼叫自己
}
}
deallocate
/* __p may not be 0 */
static void deallocate(void* __p, size_t __n)
{
if (__n > (size_t) _MAX_BYTES)//如果大于_MAX_BYTES,就用標準庫的方法來
malloc_alloc::deallocate(__p, __n);
else {
_Obj* __STL_VOLATILE* __my_free_list
= _S_free_list + _S_freelist_index(__n);//取陣列中的元素
_Obj* __q = (_Obj*)__p;
// acquire lock
# ifndef _NOTHREADS
/*REFERENCED*/
_Lock __lock_instance;
# endif /* _NOTHREADS */
__q -> _M_free_list_link = *__my_free_list;
*__my_free_list = __q;//回收原來陣列Obj*指向的記憶體
// lock is released here
}
}

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/482110.html
標籤:C++
上一篇:st表
下一篇:堆
