当前位置: 首页 > news >正文

【CPP】固定大小内存池

程序运行时,通常会频繁地进行内存的分配和释放操作。传统的内存分配方式(如使用new和delete运算符)可能会导致内存碎片的产生,并且每次分配和释放内存都有一定的时间开销。内存池通过在程序启动时一次性分配一大块内存或一次性分配多个对象备用,然后在需要时从使用这些内存。当程序使用完内存后,将其归还给内存池,而不是直接释放回操作系统,以此来提高内存分配的效率并减少内存碎片。

#ifndef MEMORY_POOL_HPP_
#define MEMORY_POOL_HPP_#include <condition_variable>
#include <list>
#include <memory>
#include <queue>
#include <type_traits>
#include <utility>#if __cplusplus < 201103L
#error "This program requires C++11 or a later version to compile."
#elif __cplusplus < 201703L
#include <mutex>
#else
#include <shared_mutex>
#endiftemplate <typename ElementType>
class MemoryPool final {template <typename T, typename = void>struct HasReset : std::false_type {};template <typename T>struct HasReset<T, std::void_t<decltype(std::declval<T>().Reset())>> : std::true_type {};static_assert(HasReset<ElementType>::value, "T must has Reset() function");#if __cplusplus < 201703Lusing mutex       = std::mutex;using lock_guard  = std::lock_guard<std::mutex>;using unique_lock = std::lock_guard<std::mutex>;
#elseusing mutex       = std::shared_mutex;using lock_guard  = std::lock_guard<std::shared_mutex>;using unique_lock = std::lock_guard<std::shared_mutex>;
#endifpublic:using SharedPtr = std::shared_ptr<MemoryPool>;using UniquePtr = std::unique_ptr<MemoryPool>;private:explicit MemoryPool(const std::size_t capacity) : capacity(capacity) {}public:MemoryPool(const MemoryPool &)            = delete;MemoryPool(MemoryPool &&)                 = delete;MemoryPool &operator=(const MemoryPool &) = delete;MemoryPool &operator=(MemoryPool &&)      = delete;~MemoryPool() {lock_guard lck(mu);for (auto *item : this->itemAll) {delete item;}this->itemAll.clear();while (!this->itemAvailable.empty()) {this->itemAvailable.pop();}}template <typename... Args>static std::shared_ptr<MemoryPool> Create(std::size_t capacity, Args &&...args) {auto *poolPtr = new MemoryPool(capacity);for (int32_t i = 0; i < poolPtr->capacity; i++) {auto *itemPtr = new ElementType(std::forward<Args>(args)...);poolPtr->itemAll.push_back(itemPtr);poolPtr->itemAvailable.push(itemPtr);}return std::shared_ptr<MemoryPool>(poolPtr);}bool GetSharedPtr(std::shared_ptr<ElementType> &sharedItemPtr, const int32_t timeout = 0) {unique_lock lck(mu);if (timeout > 0) {this->cv.wait_for(lck, std::chrono::milliseconds(timeout), [this] { return !this->itemAvailable.empty(); });} else {this->cv.wait(lck, [this] { return !this->itemAvailable.empty(); });}if (this->itemAvailable.empty()) {sharedItemPtr = nullptr;return false;}auto *itemPtr = this->itemAvailable.front();this->itemAvailable.pop();sharedItemPtr = std::shared_ptr<ElementType>(itemPtr, [this](ElementType *elementPtr) {elementPtr->Reset();this->itemAvailable.push(elementPtr);this->cv.notify_all();});return true;}private:mutex                     mu;std::condition_variable   cv;std::list<ElementType *>  itemAll;std::queue<ElementType *> itemAvailable;std::size_t               capacity;
};
#endif

相关文章:

  • 蓝牙 6.0 发布,解锁无线科技新可能
  • 【TeamFlow】4.3.2 细化时间单位
  • ISO15189认证有什么要求?ISO15189认证流程
  • 15.三数之和(LeetCode)java
  • 数据集 | 柑橘果目标检测数据集
  • 云原生--CNCF-1-云原生计算基金会介绍(云原生生态的发展目标和未来)
  • Oracle--SQL事务操作与管理流程
  • Vue2-样式相关
  • 【高并发内存池】从零到一的项目之高并发内存池整体框架设计及thread cache设计
  • 基于TCP的协议
  • 深度学习--卷积神经网络保存最优模型
  • mcp 客户端sse远程调用服务端与本地大模型集成实例
  • Python 基础
  • ABAQUS多晶体材料断裂模型
  • 百度搜索 API 相比于爬虫的效率提升、价格及如何注册使用
  • Kubernetes Docker 部署达梦8数据库
  • 【EasyPan】文件上传、文件秒传、文件转码、文件合并、异步转码、视频切割分析
  • MySQL索引知识点(笔记)
  • 《大模型+Agent 企业应用实践》的大纲
  • 网络基础概念(下)
  • 研讨会|中国古代石刻与历史研究的多重图景
  • 牛市早报|外汇局:4月以来外汇市场交易保持平稳,跨境资金延续净流入
  • 裁员15%、撤销132个机构,美国务院将全面重组
  • 广汽全域赋能,领程皮卡概念车重磅登陆上海车展
  • 关税飓风下的跨境电商人:已度过焦虑期,和中国做生意是美国最优解
  • 中国与柬埔寨签署产供链经济合作谅解备忘录