LevelDB源码分析5-Arena.md
2016-11-16 16:45
351 查看
Arena为leveldb的内存管理单元,避免频繁的new/delete,减少内存申请和释放带来的开销。
其他的成员变量
这样就不用每次申请一个小的空间时都malloc。
整体类
class Arena { public: Arena(); ~Arena(); // Return a pointer to a newly allocated memory block of "bytes" bytes. char* Allocate(size_t bytes); // Allocate memory with the normal alignment guarantees provided by malloc //yanke:保证字节对齐 char* AllocateAligned(size_t bytes); // Returns an estimate of the total memory usage of data allocated // by the arena (including space allocated but not yet used for user // allocations). size_t MemoryUsage() const { return blocks_memory_ + blocks_.capacity() * sizeof(char*); } private: char* AllocateFallback(size_t bytes); char* AllocateNewBlock(size_t block_bytes); // Allocation state char* alloc_ptr_; //指向当前内存块剩余空间的首地址 size_t alloc_bytes_remaining_;//剩余的内存数 // Array of new[] allocated memory blocks std::vector<char*> blocks_; //blocks_ // Bytes of memory in blocks allocated so far size_t blocks_memory_; //分配的总内存 // No copying allowed //yanke:只声明不实现,防止编译器隐式拷贝 Arena(const Arena&); void operator=(const Arena&); };
具体分析
成员变量
其中的blocks_是一个char*的vector,这个vector中每个元素都是指向一块内存(大小为4096,4K)其他的成员变量
char* alloc_ptr_;//指向当前内存块剩余空间的首地址 size_t alloc_bytes_remaining_;//剩余的内存数 std::vector<char*> blocks_; //blocks_ size_t blocks_memory_; //分配的总内存
构造函数和析构函数
//构造函数:分配内存为0 Arena::Arena() { blocks_memory_ = 0; alloc_ptr_ = NULL; // First allocation will allocate a block alloc_bytes_remaining_ = 0; } //析构函数:释放blocks_中每一个元素指向的内存 Arena::~Arena() { for (size_t i = 0; i < blocks_.size(); i++) { delete[] blocks_[i]; } }
内存分配
Arena维护一个char*的vector,每次需要内存的时候就向操作系统申请4K的空间,
这样就不用每次申请一个小的空间时都malloc。
//内存分配函数: //1、如果分配的内存小于等于alloc_bytes_remaining_内存,那么直接分配就行了 //2、否则调用AllocateFallback函数 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) { //如果比kBlockSize / 4还要多,直接给这个内存单独分配一个内存。 //防止内存碎片 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. //处理剩余空间的方法:直接放弃掉,开辟一个新的4K的空间 alloc_ptr_ = AllocateNewBlock(kBlockSize); //向操作系统申请4K空间 alloc_bytes_remaining_ = kBlockSize; //然后这就是当前的块剩余的空间4K char* result = alloc_ptr_; //这是新的空间 alloc_ptr_ += bytes; alloc_bytes_remaining_ -= bytes; //新的剩余空间为kBlockSize-bytes return result; //返回 } //分配新的内存块函数:需求是block_bytes char* Arena::AllocateNewBlock(size_t block_bytes) { char* result = new char[block_bytes]; //分配内存,block_bytes blocks_memory_ += block_bytes;//总的内存增加 blocks_.push_back(result);//添加到内存指针数组中 return result; }
内存使用量
包含放弃的小于1K的空间size_t MemoryUsage() const { return blocks_memory_ + blocks_.capacity() * sizeof(char*); }
内存对齐分配
char* Arena::AllocateAligned(size_t bytes) { //根据void*的大小对齐 const int align = sizeof(void*); // We'll align to pointer size //align应该为2的幂,如果不为2的幂,说明出错了,检测到了如此地步。 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); //因此内存对齐后需要的字节为bytes+slop size_t needed = bytes + slop; char* result; //如果需求小于剩余的bytes,那么直接分配 if (needed <= alloc_bytes_remaining_) { result = alloc_ptr_ + slop; alloc_ptr_ += needed; alloc_bytes_remaining_ -= needed; } else { // 否则调用AllocateFallback函数 result = AllocateFallback(bytes); } assert((reinterpret_cast<uintptr_t>(result) & (align-1)) == 0); return result; }
相关文章推荐
- levelDB源码分析-Arena
- LevelDB源码分析3-Slice.md
- LevelDB源码分析之内存管理类arena
- LevelDB源码分析之三:arena
- Leveldb源码分析--15
- leveldb源码分析 之 入门使用
- Leveldb源码分析--2
- levelDB源码分析-Memtable
- Leveldb源码分析--20
- Leveldb源码分析--11
- [Leveldb]源码分析之二 Cache模块的实现
- Leveldb源码分析--14
- levelDB源码分析-提纲
- Leveldb源码分析--18
- Leveldb源码分析--19
- Leveldb源码分析--13
- levelDB源码分析-Skiplist
- Leveldb源码分析--12
- leveldb源码分析 之 入门使用
- Leveldb源码分析--17