arena.h
#ifndef ARENA_H #define ARENA_H #include <stdio.h> #include <vector> #include <stdint.h> #include <assert.h> //内存池类 //析构函数中自动释放内存 class Arena { public: Arena(); ~Arena(); //内存申请函数 //@bytes 需要申请的内存大小 //返回指向内存的指针 char* Allocate(size_t bytes); //内存使用情况函数 size_t MemoryUsage() const { return memory_usage_; } size_t MemoryRemain() { return alloc_bytes_remaining_; } private: //申请内存函数,当内存池中剩余内存少于申请内存时使用 //@bytes 需要申请的内存大小 //返回指向内存的指针 char* AllocateFallback(size_t bytes); //申请块内存函数,直接申请一块新的内存块 //@block_bytes 需要申请的块内存大小 //返回指向内存的指针 char* AllocateNewBlock(size_t block_bytes); char* alloc_ptr_; //指向内存的指针 size_t alloc_bytes_remaining_; //剩余可用内存大小 std::vector<char*> blocks_; //内存池 size_t memory_usage_; //总共申请的内存大小 Arena(const Arena&); void operator=(const Arena&); }; inline char* Arena::Allocate(size_t bytes) { assert(bytes > 0); if(bytes <= alloc_bytes_remaining_) { char* result = alloc_ptr_; alloc_ptr_ += bytes; alloc_bytes_remaining_ -= bytes; return result; } return AllocateFallback(bytes); } #endif // ARENA_H
arena.cpp
#include "arena.h" #include <assert.h> //固定块内存大小 static const int kBlockSize = 4096; Arena::Arena() : memory_usage_(0) { alloc_ptr_ = NULL; alloc_bytes_remaining_ = 0; } Arena::~Arena() { for(size_t i = 0; i < blocks_.size(); i++) delete[] blocks_[i]; } char* Arena::AllocateFallback(size_t bytes) { if(bytes > kBlockSize/4) { char* result = AllocateNewBlock(bytes); return result; } alloc_ptr_ = AllocateNewBlock(kBlockSize); alloc_bytes_remaining_ = kBlockSize; char* result = alloc_ptr_; alloc_ptr_ += bytes; alloc_bytes_remaining_ -= bytes; return result; } char* Arena::AllocateNewBlock(size_t block_bytes) { char* result = new char[block_bytes]; blocks_.push_back(result); memory_usage_ = MemoryUsage() + block_bytes + sizeof(char*); return result; }
原文地址:http://blog.51cto.com/12876518/2097528