levelDB源码分析-Arena

       Arena在leveldb中它是一个内存池,它所作的工作十分简单,申请内存时,将申请到的内存块放入std::vector blocks_中,在Arena的生命周期结束后,统一释放掉所有申请到的内存,内部结构如下图所示。




       Arena定义为:

    class Arena {
        public:
        Arena();       	// 构造函数
        ~Arena();		// 析构函数

        // Return a pointer to a newly allocated memory block of "bytes" bytes.
        char* Allocate(size_t bytes);				// 分配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 (including space allocated but not yet used for user
        // allocations).
        size_t MemoryUsage() const {				// 返回已使用的内存容量(分配的内存容量+vector指针内存)
            return blocks_memory_ + blocks_.capacity() * sizeof(char*);
        }

        private:
        char* AllocateFallback(size_t bytes);
        char* AllocateNewBlock(size_t block_bytes);

        // Allocation state
        char* alloc_ptr_;						// 每分配一个Block,记录当前可用的offset指针
        size_t alloc_bytes_remaining_;				// 每分配一个Block,记录当前可用的bytes大小

        // Array of new[] allocated memory blocks
        std::vector<char*> blocks_;					// 每次分配的内存都放入vector中

        // Bytes of memory in blocks allocated so far
        size_t blocks_memory_;					// 当前已经分配的内存容量

        // No copying allowed
        Arena(const Arena&);						// 将拷贝构造函数及赋值构造函数设置为private,表示不运行此2个操作
        void operator=(const Arena&);
        };

        实现如下
   static const int kBlockSize = 4096;       	// 定义每个Block的大小为 4K bytes

    Arena::Arena() {					// 构造函数,初始化
        blocks_memory_ = 0;
        alloc_ptr_ = NULL;  // First allocation will allocate a block
        alloc_bytes_remaining_ = 0;
    }

    Arena::~Arena() {					// 析构函数,释放分配的内存
        for (size_t i = 0; i < blocks_.size(); i++) {
            delete[] blocks_[i];
        }
    }

    // 如果bytes超过1K,则直接调用malloc分配内存;
    // 否则,重新分配一个Block,再返回bytes大小的内存
    char* Arena::AllocateFallback(size_t bytes) {
        if (bytes > kBlockSize / 4) {			// 当请求的内存超过 1K时,直接分配,以免造成每次Block剩余内存不能利用,产生碎片
            // 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); 	// 直接分配bytes内存
            return result;
        }
	  // 每个Block剩余的bytes不够时,产生浪费
        // We waste the remaining space in the current block.
        alloc_ptr_ = AllocateNewBlock(kBlockSize); 	// 分配blocksize大小的内存
        alloc_bytes_remaining_ = kBlockSize;

        char* result = alloc_ptr_;				
        alloc_ptr_ += bytes;					// 指针偏移
        alloc_bytes_remaining_ -= bytes;			// 剩余bytes大小
        return result;
    }

    // 分配bytes大小的内存空间,返回分配的内存的指针(在头文件中,为了方便介绍,放入这里)
    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_) { 			// 当前block剩余空间可用
            char* result = alloc_ptr_;				// 返回当前可用内存offset指针
            alloc_ptr_ += bytes;		    			// 向后偏移
            alloc_bytes_remaining_ -= bytes;			// 当前剩余bytes大小
            return result;
        }
        return AllocateFallback(bytes);
    }
    // 分配bytes大小的内存空间,起始地址内存对齐(void*)
    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;
    }

    // 分配一个Block的内存,并放入vector中
    char* Arena::AllocateNewBlock(size_t block_bytes) {
        char* result = new char[block_bytes];
        blocks_memory_ += block_bytes;
        blocks_.push_back(result);
        return result;
    }
 

       从上面可以看到主要提供了两个申请函数:其中一个直接分配内存,另一个可以申请对齐的内存空间。Arena没有直接调用delete/free函数,而是在Arena的析构函数中统一释放所有的内存。

        Arena实现的是粗粒度的内存池,每个Block内都可能产生剩余部分内存不能用的问题,且不存在中间释放内存和提供内存复用机制,不适用于在全局使用,且容易造成系统内存碎片。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用,你提到了pip show jsonpath的安装成功。根据这个引用,我可以确认jsonpath已经成功安装在你的Python环境中。 至于你提到的"windows python LevelDB安装 metadata-generation-failed",我没有找到相关的引用来回答这个问题。但是我可以给你一些建议来解决这个问题。 首先,确保你已经正确地安装了LevelDB以及与之相关的依赖包。你可以尝试使用pip install命令来安装LevelDB。如果出现metadata generation failed的错误,可能是由于网络问题或者软件包源的问题导致的。你可以尝试使用其他的软件包源或者使用代理来解决这个问题。 另外,你还可以尝试使用conda来安装LevelDB,它是一个流行的Python包管理器,可以管理Python包及其依赖项。你可以在Anaconda官方网站上找到更多关于conda的信息和安装指南。 最后,如果上述方法都没有解决你的问题,我建议你检查一下你的操作系统和Python版本是否与LevelDB兼容。有时候,特定的软件包可能只能在特定的操作系统或者Python版本上正常工作。 希望这些建议对你有帮助,如果你还有其他问题,请随时提问。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [python38安装jsonpath失败问题解决](https://blog.csdn.net/qq_27371025/article/details/125855179)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [The Log: What every software engineer should know about real-time data's unifying abstraction](https://blog.csdn.net/iloveu8780/article/details/80097101)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值