位運算 與& 或| 異或^ 左移<< 右移>> \(x<<y=x·2^{y}\) \(x>>y=\frac{x}{2^{y}}\) \(2a+1=(a<<1)|1\) \(a\)%\(2=a\)&\(1\) st表 當st表合併的複雜度為$O(1)$時,st表構建的複雜度為$O(nlogn)$,查詢 ...
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
}
}