c++ 11 简约版内存池实现

c++ 11 简约版内存池实现

使用c++11的智能指针实现一个简约版的内存池

.h文件
//
// Created by 李文龙 on 2019/12/1.
//

#ifndef THREAD_TOOL_TEST_MEMORY_POOL_H
#define THREAD_TOOL_TEST_MEMORY_POOL_H

#include "common.h"
#include <list>
#include <map>
#include <mutex>
#include <memory>
#include <string>

NS_ILONG_BEGIN

template <class T>
class MemoryPool{
public:
    constexpr static int kMaxBufferCount = 30;
    MemoryPool(int maxCount = kMaxBufferCount, const std::string &tag = "");
    virtual ~MemoryPool();
    std::shared_ptr<T> getObject();
    void clearAllObjects();
    int getFreeCount();
    int getUsedCount();
private:
    std::list<std::shared_ptr<T>> buffers_;
    std::mutex mutex_;
    int max_buffer_count_;

    std::string tag_;
};

NS_ILONG_END

#endif //THREAD_TOOL_TEST_MEMORY_POOL_H
.cpp文件
//
// Created by 李文龙 on 2019/12/1.
//

#include "memory_pool.h"

NS_ILONG_BEGIN

template <class T>
MemoryPool<T>::MemoryPool(int maxCount, const std::string &tag)
:max_buffer_count_{maxCount},
tag_{tag}{
    buffers_.clear();
}

template <class T>
MemoryPool<T>::~MemoryPool() {
    clearAllObjects();
}

template <class T>
std::shared_ptr<T> MemoryPool<T>::getObject(){
    std::lock_guard<std::mutex> guard(mutex_);
    int used_count = buffers_.size();
    if(used_count > max_buffer_count_){
        std::shared_ptr<T> data;
        return data;
    }
    for (const std::shared_ptr<T> &buffer : buffers_) {
        if (buffer.use_count() == 1){
            return buffer;
        }
    }
    std::shared_ptr<T> data = std::make_shared<T>();
    buffers_.push_back(data);
    return data;
}

template <class T>
void MemoryPool<T>::clearAllObjects(){
    std::lock_guard<std::mutex> guard(mutex_);
    buffers_.clear();
}

template <class T>
int MemoryPool<T>::getFreeCount(){
    std::lock_guard<std::mutex> guard(mutex_);
    int freeCount = 0;
    for (const std::shared_ptr<T> &buffer : buffers_) {
        if (buffer.use_count() == 1){
            freeCount++;
        }
    }
    return freeCount;
}

template <class T>
int MemoryPool<T>::getUsedCount(){
    std::lock_guard<std::mutex> guard(mutex_);
    int usedCount = 0;
    for (const std::shared_ptr<T> &buffer : buffers_) {
        if (buffer.use_count() > 1){
            usedCount++;
        }
    }
    return usedCount;
}

NS_ILONG_END
测试程序
#include "memory_pool.cpp"

class DataBuffer{
public:
    DataBuffer(){

    }
};
int main() {
   MemoryPool<DataBuffer> memoryPool;
   for(int i = 0; i < 10; i++){
        auto data = memoryPool.GetObject();
    }
    int count = memoryPool.GetFreeCount();
    ELOG_DEBUG("Memory Pool Count: %d", count);
    auto data = memoryPool.GetObject();
    count = memoryPool.GetFreeCount();
    ELOG_DEBUG("Memory Pool Count: %d", count);
    memory_pool_.ClearAllObjects();
    count = memoryPool.GetFreeCount();
    ELOG_DEBUG("Memory Pool Count: %d", count);
}
注意由于模版类是写在.h和.cpp文件中,所以使用是要 #include “memory_pool.cpp”,否则编译会报错

源码地址

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值