LevelDB源码分析之三:arena

来源:互联网 发布:淘宝被扣几分会关店 编辑:程序博客网 时间:2024/06/06 08:55

一.原理

        arena是LevelDB内部实现的内存池。

        我们知道,对于一个高性能的服务器端程序来说,内存的使用非常重要。C++提供了new/delete来管理内存的申请和释放,但是对于小对象来说,直接使用new/delete代价比较大,要付出额外的空间和时间,性价比不高。另外,我们也要避免多次的申请和释放引起的内存碎片。一旦碎片到达一定程度,即使剩余内存总量够用,但由于缺乏足够的连续空闲空间,导致内存不够用的假象。
        C++ STL为了避免内存碎片,实现一个复杂的内存池,LevelDB中则没有那么复杂,只是实现了一个"一次性"内存池arena。在leveldb里面,并不是所有的地方都使用了这个内存池,主要是memtable使用,主要是用于临时存放用户的更新数据,由于更新的数据可能很小,所以这里使用内存池就很合适。

        为了避免小对象的频繁分配,需要减少对new的调用,最简单的做法就是申请大块的内存,多次分给客户。LevelDB用一个vector<char *>来保存所有的内存分配记录,默认每次申请4k的内存块,记录下当前内存块剩余指针和剩余内存字节数,每当有新的申请,如果当前剩余的字节能满足需要,则直接返回给用户。如果不能,对于超过1k的请求,直接new一个指定大小的内存块并返回,小于1K的请求,则申请一个新的4k内存块,从中分配一部分给用户。当内存池对象析构时,分配的内存均被释放,保证了内存不会泄漏。


二.头文件

class Arena { public:  Arena();  ~Arena();  // Return a pointer to a newly allocated memory block of "bytes" bytes.  // 分配bytes大小的内存块,返回指向该内存块的指针  char* Allocate(size_t bytes);  // Allocate memory with the normal alignment guarantees provided by malloc  // 基于malloc的字节对齐内存分配  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).  // 返回整个内存池使用内存的总大小(不精确),这里只计算了已分配内存块的总大小和  // 存储各个内存块指针所用的空间。并未计算alloc_ptr_和alloc_bytes_remaining_  // 等数据成员的大小。  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  // 当前内存块(block)偏移量指针,也就是未使用内存的首地址  char* alloc_ptr_;  // 表示当前内存块(block)中未使用的空间大小  size_t alloc_bytes_remaining_;  // Array of new[] allocated memory blocks  // 用来存储每一次向系统请求分配的内存块的指针  std::vector<char*> blocks_;  // Bytes of memory in blocks allocated so far  // 迄今为止分配的内存块的总大小  size_t blocks_memory_;  // No copying allowed  Arena(const Arena&);  void operator=(const Arena&);};

三.源文件

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).  // 如果允许分配0字节的内存,那么返回值在语义上会比较难以理解,因此这里禁止bytes=0  // 如果需求的内存小于当前内存块中剩余的内存,那么直接从当前内存快中获取  assert(bytes > 0);  if (bytes <= alloc_bytes_remaining_) {    char* result = alloc_ptr_;    alloc_ptr_ += bytes;    alloc_bytes_remaining_ -= bytes;    return result;  }  // 因为alloc_bytes_remaining_初始为0,因此第一次调用Allocate实际上直接调用的是AllocateFallback  // 如果需求的内存大于内存块中剩余的内存,也会调用AllocateFallback  return AllocateFallback(bytes);}
char* Arena::AllocateFallback(size_t bytes) {  // 如果需求的内存大于内存块中剩余的内存,而且大于1K,则给这内存单独分配一块bytes大小的内存。  // 这样可以避免浪费过多的空间(因为如果bytes大于1K也从4K的内存块去取用,那么如果当前内存块中刚好剩余  // 1K,只能再新建一个4K的内存块,并且取用bytes。此时新建的内存块是当前内存块,后续操作都是基于当前内  // 存块的,那么原内存块中的1K空间就浪费了)  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;  }  // 如果需求的内存大于内存块中剩余的内存,而且小于1K,则重新分配一个内存块,默认大小4K,  // 原内存块中剩余的内存浪费掉(这样虽然也会浪费,但是浪费的空间小于1K)。并在新内存块  // 中取用bytes大小的内存。  // 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;}
// 提供了字节对齐内存分配,一般情况是4字节或8个字节对齐分配,// 对齐内存的好处简单的说就是加速内存访问。// 首先获取一个指针的大小const int align = sizeof(void*),// 很明显,在32位系统下是4 ,64位系统下是8 ,为了表述方便,我们假设是32位系统,即align = 4,// 然后将我们使用的char * 指针地址转换为一个无符号整型(reinterpret_cast<uintptr_t>(result):// It is an unsigned int that is guaranteed to be the same size as a pointer.),通过与操作来// 获取size_t current_mod = reinterpret_cast<uintptr_t>(alloc_ptr_) & (align-1);当前指针模4// 的值,有了这个值以后,我们就容易知道,还差 slop = align - current_mod多个字节,内存才是对齐的,// 所以有了result = alloc_ptr + slop。那么获取bytes大小的内存,实际上需要的大小为needed = bytes + slop。char* Arena::AllocateAligned(size_t bytes) {  const int align = sizeof(void*);    // We'll align to pointer size  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;}

关于内存对齐可参考:内存对齐到底是怎么回事?

// 分配新的内存块char* Arena::AllocateNewBlock(size_t block_bytes) {  char* result = new char[block_bytes];  blocks_memory_ += block_bytes;  blocks_.push_back(result);  return result;}
// 释放整个内存池所占内存Arena::~Arena() {  for (size_t i = 0; i < blocks_.size(); i++) {    delete[] blocks_[i];  }}


参考链接:http://www.cnblogs.com/shenzhaohai1989/p/3904808.html





原创粉丝点击