码迷,mamicode.com
首页 > 数据库 > 详细

leveldb登山之路——arena

时间:2018-04-01 20:46:43      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:leveldb   内存池   arena   

        一、预备知识——内存池

        内存池是为了使内存分配的效率得到提升而采用的一种方法,并且很少产生堆碎片,可以避免内存泄漏。

        简单来说,就是每次申请的内存都放入一个容器当中,每次需要申请的内存先看是否可以从内存池中直接分配,如果不够,那么先申请一块新的内存放入内存池中,然后再进行分配。最后释放的时候,只需要释放这个容器管理的内存即可。


        二、源码分析

        1. arena.h

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
  char* AllocateAligned(size_t bytes);

  // Returns an estimate of the total memory usage of data allocated
  // by the arena.
  size_t MemoryUsage() const {
    return reinterpret_cast<uintptr_t>(memory_usage_.NoBarrier_Load());
  }

 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_;            //整个内存池

  // Total memory usage of the arena.
  port::AtomicPointer memory_usage_;        //内存总的使用情况,即申请了多大的内存

  // No copying allowed
  Arena(const Arena&);
  void operator=(const Arena&);
};


        整个Arena类中,只有三个接口函数可以调用,他们分别是Allocate,申请内存;AllocateAligned 申请字节对齐的内存;MemoryUsage, 内存使用情况,三个函数。下面分别对三个函数进行说明


        1.1  Allocate函数

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);
}

        如果申请的内存大小不比当前内存池中可用内存的大时,那么就将直接从当前内存池中获得内存,否则,就需要重新申请内存。

过程如下:

技术分享图片

              上图是初始内存池

技术分享图片


        上图是bytes<alloc_bytes_remaining_时,申请内存的情况。

        当bytes>alloc_bytes_remaining_时,这时就调用了AllocateFallback函数。


        1.2    AllocateFallback函数

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;
}

        从代码可以看出,当bytes 比 BlockSize/4, 即大于1024时,那么就直接重新申请需要大小的内存。但是,如果申请的内存小于1024时,这时候,就是直接申请一块4096的新内存,然后将可用的内存大小设置为4096,然后再进行指针的偏移和可用内存的减小。

这里有两个特别需要注意的地方:

        1)    大于1024时申请的内存是在之前申请的内存后面,如果下一次申请的内存小于alloc_bytes_remaining_,那么还是用当前内存池当中的可用内存。

技术分享图片

        2)    当申请的内存处于大于alloc_bytes_remaining_,并且小于1024时,那么这个时候就会重新申请一块4096大小的内存,并且将alloc_bytes_emaining_的大小更新为4096。这里意味着浪费了一块小于1024大小的内存,在查询资料以后发现,作者浪费就浪费了,只要性能Ok就行了。因此,如果是自己使用的时候,这里属于可以优化的部分。

        技术分享图片

技术分享图片

技术分享图片





        1.3 AllocateNewBlock

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;
}

        申请一块新的内存,并将这块内存放入内存池容器中,然后更新内存使用情况。


        1.4  AllocateAligned函数

char* Arena::AllocateAligned(size_t bytes) {
  //判断一个数是不是2的指数次幂的奇淫巧技
  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;
}

        这个函数是用来申请内存对齐的函数。

  




leveldb登山之路——arena

标签:leveldb   内存池   arena   

原文地址:http://blog.51cto.com/12876518/2093599

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!