当前位置:网站首页>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 !
};
边栏推荐
- Kubernetes 资源拓扑感知调度优化
- String string is converted to jsonarray and parsed
- C primer plus学习笔记 —— 3、字符的IO(输入/输出)
- Serial port application program based on gd32
- Project practice 6: distributed transaction Seata
- Unity——Mathf. Similarities and differences between atan and atan2
- Three basic backup methods of mongodb
- Why don't I recommend going to sap training institution for training?
- The king of Internet of things protocol: mqtt
- 数据库范式和主码的选择
猜你喜欢

On the escape of inequality value

黑客用机器学习发动攻击的九种方法

Muke 11. User authentication and authorization of microservices

Disruptor local thread queue_ Use transprocessor processor and workpool to compare consumption - Notes on inter thread communication 005

郭明錤:苹果 AR / MR 头显是其有史以来设计最复杂的产品,将于 2023 年 1 月发布

Minimum spanning tree, shortest path, topology sorting, critical path

Pinda general permission system (day 1~day 2)

Wechat applet uniapp left slide delete with Delete Icon

回溯思路详解

阿里云个人镜像仓库日常基本使用
随机推荐
Mr. Sun's version of JDBC (21:34:25, June 12, 2022)
問題解决:虛擬機無法複制粘貼文件
Three basic backup methods of mongodb
开发者调查:Rust/PostgreSQL 最受喜爱,PHP 薪水偏低
Button how to dynamically set drawablebottom (setcomposunddrawables is invalid)
Uni app uses canvas to draw QR code
网上办理中金财富开户安全吗?
好物推荐:移动端开发安全工具
飞天+CIPU体为元宇宙带来更大想象空间
Introduction to single chip microcomputer one-on-one learning strategy, independent development program immediately after reading
0基础学c语言(3)
Unit test of boot
Arduino uno + DS1302 uses 31 byte static RAM to store data and print through serial port
460million zongzi were sold in half a year. How big is the "imagination space" of time-honored brands?
MySQL stored procedure
回溯思路详解
Detailed explanation of stored procedures in MySQL
Super VRT
黑客用机器学习发动攻击的九种方法
MySQL - subquery usage