当前位置:网站首页>Fixed length memory pool
Fixed length memory pool
2022-06-26 20:25:00 【FIappy Brid】
Write a fixed length memory pool !
#pragma once
#include <iostream>
using std::cout;
using std::endl;
// Fixed length memory pool !
// template<size_t N>
// class ObjectPool {};
/* The left value and Right value The left value : stay '=' Sinister , In fact, the memory contents of variables should be overwritten ! Right value : stay '=' Dexter , In fact, the contents of the memory where the variable is located should be taken out ! */
template<class T>
class ObjectPool {
public:
T* New() {
T* obj = nullptr;
if (_freeList) {
// The memory released is still there ! Delete the header !
void* next = *(void**)_freeList; // The contents of the pointer are given to next The pointer
obj = _freeList;
freeList = next;
new (obj)T;
return obj;
}
if (residualSize < sizeof(T)) {
// There is not enough memory left !
residualSize = 128 * 1024;
_memory = malloc(residualSize); // Apply for another piece !
if (_memory == nullptr) {
// Memory request failed !
throw std::bad_alloc(); // Throw exceptions , To terminate the program !
}
}
obj = (T*)_memory; // Cut a piece of memory for him !
// Ensure that the requested memory can be organized in the form of a linked list !
size_t obj_size = sizeof(T) > sizeof(void*) ? sizeof(T) : sizeof(void*);
_memory += obj_size;
residualSize -= obj_size();
new (obj)T; // Initialize memory !
return obj;
}
void Delete(T* obj) {
// Free memory !
obj->~T(); // Show call object destructors !
// Take a pointer size space from the released memory to store the address of the next node !
*(void**)obj = _freeList; // take obj Before the memory pointed to ( Save pointer length ) Bytes of memory do next Pointer memory !
_freeList = obj;
}
private:
char* _memory = nullptr; // The first address of the requested memory !
void* _freeList = nullptr; // Free memory chain header pointer !
size_t residualSize = 0; // Remaining memory size !
};
边栏推荐
- Super VRT
- Unity - URP get camera stack
- JWT operation tool class sharing
- BOM and DOM operations
- Muke 11. User authentication and authorization of microservices
- Six necessary threat tracking tools for threat hunters
- Database SQL statement writing
- 剑指 Offer II 091. 粉刷房子
- Serial port application program based on gd32
- MySQL - subquery usage
猜你喜欢

清华大学就光刻机发声,ASML立马加紧向中国出口光刻机

Wechat applet uniapp left slide delete with Delete Icon

分布式ID生成系统
Mongodb implements creating and deleting databases, creating and deleting tables (sets), and adding, deleting, modifying, and querying data

The successfully resolved idea cannot use the log normally after referencing Lombok's @slf4j

飞天+CIPU体为元宇宙带来更大想象空间

mysql的充值问题

论数据库的传统与未来之争之溯源溯本----AWS系列专栏

抖音实战~首页视频~下拉刷新

Introduction to single chip microcomputer one-on-one learning strategy, independent development program immediately after reading
随机推荐
抖音实战~分享模块~短视频下载(保存到相册)
The king of Internet of things protocol: mqtt
超分之VRT
JWT操作工具类分享
好物推荐:移动端开发安全工具
Review of watermelon book (VII): Bayesian classifier (manual push + code demo)
Tiktok practice ~ homepage video ~ pull-down refresh
WebView load pdf
Successfully solved the problem of garbled microservice @value obtaining configuration file
Tiktok practice ~ search page ~ scan QR code
mysql的充值问题
MongoDB实现创建删除数据库、创建删除表(集合)、数据增删改查
数据库SQL语句撰写
[serialization] how to master the core technology of opengauss database? Secret 5: master database security (6)
Can I open an account online? Is it safe?
分布式ID生成系统
Nftgamefi chain game system development detailed solution - chain game system development principle analysis
IDEA 报错:Process terminated【已解决】
Daily basic use of alicloud personal image warehouse
Summary of several common UML diagrams