当前位置:网站首页>Utlis memory pool object pool
Utlis memory pool object pool
2022-06-30 09:40:00 【Zip-List】
utils Memory pool
Memory pools are responsible for managing multiple object pools , The object pool is responsible for managing multiple objects
Spatial discontinuity , Time allocation
Object pool
Variables of the object pool class map<mid_t, T*> Responsible for recording the memory mapping in use ,POD Object pool of type ,size It's certain
class UNIT_POOL_BASE
{
public:
virtual mid_t alloc_unit() = 0;
virtual void free_unit(mid_t mid) = 0;
virtual void* get_unit(mid_t mid) = 0;
virtual int get_cur_use_num() = 0;
public:
u16 _type; // pool type
int _num_limit; // Quantitative restriction
int _unit_size; // Single unit size
int _max_used; // How many did you use
std::string _type_name; // type name
};
template<typename T>
class UNIT_POOL : UNIT_POOL_BASE
{
public:
typedef typename std::map<mid_t, T*>::iterator IT;
UNIT_POOL()
{
_map_use.clear();
_num_limit = 0;
_max_used = 0;
_type_name = "";
}
~UNIT_POOL()
{
for (auto& itr : _map_use)
{
if (itr->second)
{
delete (itr->second); //delete Pointer will call the destructor of the object pointed to by the pointer
}
}
_map_use.clear();
}
virtual mid_t alloc_unit();
virtual void free_unit(mid_t mid);
virtual void* get_unit(mid_t mid);
virtual int get_cur_use_num();
private:
std::map<mid_t, T*> _map_use; // Memory mapping in use
};
Tips , Using the bit field, press type Distribution , Add serial number information . Object records its own type You can reverse query the information of the corresponding object pool
typedef struct unitmid_t
{
u64 type : 16;
u64 serial : 48;
} unitmid_t;
inline mid_t alloc_mid(u16 type)
{
static u64 base_id = 10000;
++base_id;
unitmid_t unit_mid;
#pragma GCC diagnostic ignored "-Wconversion"
unit_mid.type = type;
unit_mid.serial = base_id;
#pragma GCC diagnostic pop
return *(mid_t *)&unit_mid;
}
Corresponding function implementation Assigned to Get objects Release object
template<typename T>
mid_t UNIT_POOL<T>::alloc_unit()
{
if (_num_limit <= (int)_map_use.size())
{
fatal_log("type:%d type_name:%s alloc fail, cur use:%d upper limit exceeded"
, _type, _type_name.c_str(), (int)_map_use.size());
return INVALID_MID;
}
void* data = malloc(_unit_size);
assert_retval(data, INVALID_MID);
// placement new
memset((char*)data, 0x00, _unit_size);
T* ptr = new(data) T;
assert_retval(ptr != nullptr, INVALID_MID);
mid_t mid = alloc_mid(_type);
_map_use[mid] = ptr;
if (_max_used < (int)_map_use.size())
{
_max_used = (int)_map_use.size();
}
return mid;
}
#pragma GCC diagnostic ignored "-Wdelete-non-virtual-dtor"
template<typename T>
void UNIT_POOL<T>::free_unit(mid_t mid)
{
IT itr = _map_use.find(mid);
if (itr == _map_use.end())
{
return;
}
T* ptr = itr->second; // Construct... In allocated storage (“ Arrangement ”)
_map_use.erase(itr);
assert_retnone(ptr);
ptr->~T(); // free The destructor of the object is not called , To manually call
// free
free((void*)ptr);
}
#pragma GCC diagnostic warning "-Wdelete-non-virtual-dtor"
template<typename T>
void* UNIT_POOL<T>::get_unit(mid_t mid)
{
IT itr = _map_use.find(mid);
if (itr == _map_use.end())
{
return nullptr;
}
return itr->second;
}
template<typename T>
int UNIT_POOL<T>::get_cur_use_num()
{
return (int)_map_use.size();
}
Memory pool
The object pool manages the corresponding object pool according to the number
std::map<int, UNIT_POOL_BASE*> g_map_pool; // Global memory pool The address containing the object pool
Corresponding function encapsulation Assigned to Get objects Release object
/** @brief Memory pool initialization and recycling , Called when a process starts and exits */
int mempool_init();
void mempool_fini();
/** @brief Object type registration @param[in] type -- Memory object type @param[string] type_name -- Memory type name @param[in] num_limit -- Memory limit @param[UNIT_POOL_BASE] pool_fn -- Memory object management pointer @retval 0 -- success @retval <0 -- Failure */
int unitpool_init(u16 type, const char* type_name, int num_limit, int unit_size, UNIT_POOL_BASE* unit_pool);
/** @brief Applicants // Applicants , Call the allocation object of the fixed type object pool in the memory pool @param[in] type -- Memory object type @retval =INVALID_MID -- Failure @retval Other values -- success , Object's mid sign */
mid_t memunit_alloc(u16 type);
/** @brief Object release @param[in] type -- Memory object type */
void memunit_free(mid_t mid);
/** @brief Get the memory address of the object @param[in] type -- Memory object type @retval =nullptr -- Failure , The object was not found @retval !=nullptr-- success , The memory address of the object */
void* memunit_get(mid_t mid);
Concrete realization
int mempool_init()
{
g_map_pool.clear();
return 0;
}
void mempool_fini()
{
g_map_pool.clear();
}
int unitpool_init(u16 type, const char* type_name, int num_limit, int unit_size, UNIT_POOL_BASE* unit_pool)
{
assert_retval(unit_pool && type_name, BGERR_INVALID_ARG);
assert_retval(type >= 0 && type < MAX_POOL_TYPE_NUM, BGERR_INVALID_ARG);
assert_retval(num_limit > 0, BGERR_INVALID_ARG);
auto itr = g_map_pool.find(type);
if (itr != g_map_pool.end())
{
assert_retval(0, BGERR_INVALID_ARG);
}
unit_pool->_type = type;
unit_pool->_num_limit = num_limit;
unit_pool->_type_name = type_name;
unit_pool->_unit_size = unit_size;
g_map_pool[type] = unit_pool;
infor_log("unitpool init, type:%d name:%s num_limit:%d", type, type_name, num_limit);
return 0;
}
mid_t memunit_alloc(u16 type)
{
auto itr = g_map_pool.find(type);
if (itr == g_map_pool.end() || !itr->second)
{
assert_retval(0, INVALID_MID);
}
return itr->second->alloc_unit();
}
void memunit_free(mid_t mid)
{
unitmid_t *unitmid = (unitmid_t *)∣
int type = unitmid->type;
auto itr = g_map_pool.find(type);
if (itr == g_map_pool.end() || !itr->second)
{
//assert_retnone(0);
return;
}
itr->second->free_unit(mid);
}
void* memunit_get(mid_t mid)
{
unitmid_t *unitmid = (unitmid_t *)∣
int type = unitmid->type;
auto itr = g_map_pool.find(type);
if (itr == g_map_pool.end() || !itr->second)
{
//assert_retval(0, nullptr);
return nullptr;
}
return itr->second->get_unit(mid);
}
#define POOL_REGISTER(MEM_TYPE_ID, MEM_TYPE_NAME, MEM_STRUCT_TYPE, MEM_UNIT_NUM) \ do {
\ UNIT_POOL<MEM_STRUCT_TYPE>* unit_pool = new UNIT_POOL<MEM_STRUCT_TYPE>;\ if (nullptr == unit_pool)\ {
\ error_log("mem type: %s create register deal fn failed", #MEM_TYPE_ID); \ return BGERR_UNKNOWN; \ }\ int ret = bg_unitpool_init(MEM_TYPE_ID, MEM_TYPE_NAME, MEM_UNIT_NUM, sizeof(MEM_STRUCT_TYPE), (UNIT_POOL_BASE*)unit_pool);\ if (0 != ret)\ {
\ error_log("mem type: %s register deal fn failed, retval: %d", \ #MEM_TYPE_ID, BGERR_UNKNOWN); \ return ret; \ }\ }while(0)
reflection
Object pool time allocation , Not allocated on a continuous space , If the contiguous space is to be initialized, allocate all the space , For object management, you can use map perhaps free_list Record , perhaps Array records Which pieces are used
How to prevent the edge of continuous space from being damaged , Discontinuous space segment error , Continuous space is added The protection part , Transformational use dynamic_cast
边栏推荐
- DataTableToModelList实体类
- Niuke IOI weekly competition 20 popularization group (problem solving)
- 布隆过滤器
- ES6 learning road 5 symbol
- Code management related issues
- Couldn't load this key (openssh ssh-2 private key (old PEM format))
- Cftpconnection:: getfile() download FTP server files and related parameter descriptions
- Pytorch graduate warm LR installation
- Pytorch for former Torch users - Tensors
- Linear-gradient()
猜你喜欢

Idea setting automatic package Guide

Solution to the eighth training competition of 2020 Provincial Games

Flutter 0001, environment configuration

Solution to pychart's failure in importing torch package

Pit encountered by fastjason

Express - static resource request

9.JNI_ Necessary optimization design

12. problem set: process, thread and JNI architecture

CentOS MySQL installation details

Flutter 中的 ValueNotifier 和 ValueListenableBuilder
随机推荐
Pipe pipe --namedpipe and anonymouspipe
抽象类和接口
What kind of experience is it to develop a "grandson" who will call himself "Grandpa"?
桂林 稳健医疗收购桂林乳胶100%股权 填补乳胶产品线空白
Deep Learning with Pytorch- A 60 Minute Blitz
机器学习笔记 九:预测模型优化(防止欠拟合和过拟合问题发生)
Properties of string
【Ubuntu-redis安装】
thrift简单使用
JVM memory common parameter configuration set
Redis docker 主从模式与哨兵sentinel
2021-10-20
Tablet PC based ink handwriting recognition input method
QR code generation and analysis
ACM intensive training graph theory exercise 3 in the summer vacation of 2020 [problem solving]
Distributed ID
Flutter 中的 ValueNotifier 和 ValueListenableBuilder
Flutter的特别之处在哪里
POJ 1753 flip game (DFS 𞓜 bit operation)
Summary of Android knowledge points and common interview questions