一. 內存池的優勢
1. 直接使用系統調用的弊端
- 調用malloc/new,系統需要根據“最先匹配”、“最優匹配”或其他算法在內存空閑塊表中查找一塊空閑內存,調用free/delete,系統可能需要合并空閑內存塊,這些會產生額外開銷
- 頻繁使用時會產生大量內存碎片,從而降低程序運行效率(對于內存碎片問題上這篇博文有著不一樣的看法,有一定道理,可以參考一下_)
- 容易造成內存泄漏
2. 內存池的優點
- 比malloc/free進行內存申請/釋放的方式快(向內存池請求,而不是直接向操作系統請求)
- 不會產生或很少產生堆碎片
- 可避免內存泄漏
二. Arena內存池的實現
1. Arena類的成員變量
// Allocation state
char* alloc_ptr_;
size_t alloc_bytes_remaining_;
// Array of new[] allocated memory blocks
std::vector<char*> blocks_;
// Total memory usage of the arena.
port::AtomicPointer memory_usage_;
Arena類的成員變量還是相對比較簡單的,alloc_ptr_
表示當前內存塊(block)偏移量指針,也就是未使用內存的首地址。 alloc_bytes_remaining_
表示當前塊所未使用的空間大小。blocks_
是一個vector
用來存儲每一次向系統請求的分配的內存指針。 memory_usage_
則是用來記錄Arena
類內存使用情況的。
2. Arena類的成員函數
a. 構造函數
Arena::Arena() : memory_usage_(0) {
alloc_ptr_ = NULL; // First allocation will allocate a block
alloc_bytes_remaining_ = 0;
}
非常簡單的一個構造函數,負責初始化一些變量,注意剛開始是沒有立刻分配內存的
b. 析構函數
Arena::~Arena() {
for (size_t i = 0; i < blocks_.size(); i++) {
delete[] blocks_[i];
}
}
析構函數負責將從系統中申請的內存返還給系統。
c. Allocate() && AllocateFallback() && AllocateNewBlock()
inline char* Arena::Allocate(size_t bytes) {
// The semantics of what to return are a bit messy if we allow
// 0-byte allocations, so we disallow them here (we don't need
// them for our internal use).
assert(bytes > 0);
if (bytes <= alloc_bytes_remaining_) {
char* result = alloc_ptr_;
alloc_ptr_ += bytes;
alloc_bytes_remaining_ -= bytes;
return result;
}
return AllocateFallback(bytes);
}
char* Arena::AllocateFallback(size_t bytes) {
if (bytes > kBlockSize / 4) {
// Object is more than a quarter of our block size. Allocate it separately
// to avoid wasting too much space in leftover bytes.
char* result = AllocateNewBlock(bytes);
return result;
}
// We waste the remaining space in the current block.
alloc_ptr_ = AllocateNewBlock(kBlockSize);
alloc_bytes_remaining_ = kBlockSize;
char* result = alloc_ptr_;
alloc_ptr_ += bytes;
alloc_bytes_remaining_ -= bytes;
return result;
}
char* Arena::AllocateNewBlock(size_t block_bytes) {
char* result = new char[block_bytes];
blocks_.push_back(result);
memory_usage_.NoBarrier_Store(
reinterpret_cast<void*>(MemoryUsage() + block_bytes + sizeof(char*)));
return result;
}
Allocate
是Arena向外界提供的接口,該函數會調用AllocateFallback() && AllocateNewBlock() 這兩個私有函數。
- 如果需求的內存小于剩余的內存,那么直接從內存池中獲取
- 如果需求的內存大于剩余的內存,而且大于1K,則給這內存單獨分配一塊bytes(函數參數)大小的內存
- 如果需求的內存大于剩余的內存,而且小于4096/4,則重新分配一個內存塊,默認大小4096,用于存儲數據。原有的剩余空間浪費掉。
d. AllocateAligned()
char* Arena::AllocateAligned(size_t bytes) {
const int align = (sizeof(void*) > 8) ? sizeof(void*) : 8;
assert((align & (align-1)) == 0); // Pointer size should be a power of 2
size_t current_mod = reinterpret_cast<uintptr_t>(alloc_ptr_) & (align-1);
size_t slop = (current_mod == 0 ? 0 : align - current_mod);
size_t needed = bytes + slop;
char* result;
if (needed <= alloc_bytes_remaining_) {
result = alloc_ptr_ + slop;
alloc_ptr_ += needed;
alloc_bytes_remaining_ -= needed;
} else {
// AllocateFallback always returned aligned memory
result = AllocateFallback(bytes);
}
assert((reinterpret_cast<uintptr_t>(result) & (align-1)) == 0);
return result;
}
Arena
還提供了字節對齊內存分配,一般情況是8個字節對齊分配。對齊內存的好處簡單的說就是加速內存訪問。具體實現源碼已經寫的非常詳細了,稍微有點疑惑的地方也都有了注釋。
e. MemoryUsage()
size_t MemoryUsage() const {
return reinterpret_cast<uintptr_t>(memory_usage_.NoBarrier_Load());
}
Arena
最后一個對外接口是返回這個內存池分配總的內存大小。
icecity96 liuhiter@gmail.com