leveldb之arena

定义

class Arena {
    public:
    Arena();

    Arena(const Arena&) = delete;
    Arena& operator=(const Arena&) = delete;

    ~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 memory_usage_.load(std::memory_order_relaxed);
    }

    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.
    //
    // TODO(costan): This member is accessed via atomics, but the others are
    //               accessed without any locking. Is this OK?
    std::atomic<size_t> memory_usage_; // 已经使用的内存
};

整个arena结构如图所示:
在这里插入图片描述

内存申请、分配

通过Allocate、AllocateFallback分配内存,AllocateNewBlock申请内存

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

AllocateFallback

如果小于1KB,就分配一块4KB大小的内存;

在这里插入图片描述

如果大于1KB,就按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;
}

AllocateNewBlock

动态分配空间并将指针保存到blocks_中,注意计算已使用的空间大小时要加上指针的大小。

char* Arena::AllocateNewBlock(size_t block_bytes) {
    char* result = new char[block_bytes];
    blocks_.push_back(result);
    memory_usage_.fetch_add(block_bytes + sizeof(char*),
                            std::memory_order_relaxed);
    return result;
}

AllocateAligned

按照字节对齐分配内存,对齐上一次的内存。

char* Arena::AllocateAligned(size_t bytes) {
  // 对其字节数最小是8
  const int align = (sizeof(void*) > 8) ? sizeof(void*) : 8;
  // align & (align - 1)检查是否为2的幂次
  static_assert((align & (align - 1)) == 0,
                "Pointer size should be a power of 2");
  // 当align = 8的时候,A & 7 = A % 8
  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;
}

公式:a & 7 = a % 8
假设a = 10,那么用二进制表示为1010,8为1000。
10 % 8取余可以理解为一直减8,直到值小于8为止,由于2^n的特殊性, 8(1000)有3个0,那么最后剩下的就是10的后三位010。
10 & 7,也相当于取后三位010。
总结:假设y = 2 ^ n,那么x & (y - 1) = x % y, 值为x的二进制后n位

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值