当前位置:网站首页>Ncnn allocator memory allocator
Ncnn allocator memory allocator
2022-07-19 03:09:00 【HySmiley】
1、Allocator class
Statement :
class NCNN_EXPORT Allocator
{
public:
virtual ~Allocator();
virtual void* fastMalloc(size_t size) = 0;
virtual void fastFree(void* ptr) = 0;
};
Allocator class , There are three virtual functions inside : Two pure virtual functions , A virtual destructor .
Pure virtual function : signify Allocator Is an abstract class , It cannot be instantiated , Only through its subclasses and non pure virtual functions with the same name . If the subclass does not implement the base class , Then the subclass is also an abstract class .
Virtual functions act on : The base class pointer points to an instance of a subclass , So as to call the members of the subclass .
fastMalloc Function request memory
fastFree Function to free memory
2、 Derived class PoolAllocator Memory pool
Add a lock before the thread operates the resource
PoolAllocator Class declaration
class PoolAllocatorPrivate;
class NCNN_EXPORT PoolAllocator : public Allocator
{
public:
PoolAllocator();
~PoolAllocator();
// ratio range 0 ~ 1
// default cr = 0.75
void set_size_compare_ratio(float scr);
// release all budgets immediately
void clear();
// Base class virtual function inheritance
virtual void* fastMalloc(size_t size);
virtual void fastFree(void* ptr);
private:
// Constructors :
PoolAllocator(const PoolAllocator&);
// Assignment constructors
PoolAllocator& operator=(const PoolAllocator&);
private:
// Constant pointer
PoolAllocatorPrivate* const d;
};
PoolAllocatorPrivate Class declaration
class PoolAllocatorPrivate
{
public:
// The mutex - The budget
Mutex budgets_lock;
// The mutex - payment
Mutex payouts_lock;
// Size proportion
unsigned int size_compare_ratio; // 0~256
// The budget
std::list<std::pair<size_t, void*> > budgets;
// payment
std::list<std::pair<size_t, void*> > payouts;
};Function body implementation :
PoolAllocator Constructor initialization
PoolAllocator::PoolAllocator()
: Allocator(), d(new PoolAllocatorPrivate)
{
d->size_compare_ratio = 192; // 0.75f * 256
}
PoolAllocator::PoolAllocator(const PoolAllocator&)
: d(0)
{
}
PoolAllocator& PoolAllocator::operator=(const PoolAllocator&)
{
return *this;
}Destructor - Pointer release
PoolAllocator::~PoolAllocator()
{
clear();
if (!d->payouts.empty())
{
NCNN_LOGE("FATAL ERROR! pool allocator destroyed too early");
#if NCNN_STDIO
std::list<std::pair<size_t, void*> >::iterator it = d->payouts.begin();
for (; it != d->payouts.end(); ++it)
{
void* ptr = it->second;
NCNN_LOGE("%p still in use", ptr);
}
#endif
}
delete d;
}Unlock
void PoolAllocator::clear()
{
d->budgets_lock.lock();
std::list<std::pair<size_t, void*> >::iterator it = d->budgets.begin();
for (; it != d->budgets.end(); ++it)
{
void* ptr = it->second;
ncnn::fastFree(ptr);
}
d->budgets.clear();
d->budgets_lock.unlock();
}Memory request and release
// Quickly apply for memory
void* PoolAllocator::fastMalloc(size_t size)
{
// Lock
d->budgets_lock.lock();
// find free budget
std::list<std::pair<size_t, void*> >::iterator it = d->budgets.begin();
for (; it != d->budgets.end(); ++it)
{
size_t bs = it->first;
// size_compare_ratio ~ 100%
if (bs >= size && ((bs * d->size_compare_ratio) >> 8) <= size)
{
void* ptr = it->second;
d->budgets.erase(it);
d->budgets_lock.unlock();
d->payouts_lock.lock();
d->payouts.push_back(std::make_pair(bs, ptr));
d->payouts_lock.unlock();
return ptr;
}
}
d->budgets_lock.unlock();
// new
void* ptr = ncnn::fastMalloc(size);
d->payouts_lock.lock();
d->payouts.push_back(std::make_pair(size, ptr));
d->payouts_lock.unlock();
return ptr;
}
// Quickly release memory
void PoolAllocator::fastFree(void* ptr)
{
d->payouts_lock.lock();
// return to budgets
std::list<std::pair<size_t, void*> >::iterator it = d->payouts.begin();
for (; it != d->payouts.end(); ++it)
{
if (it->second == ptr)
{
size_t size = it->first;
d->payouts.erase(it);
d->payouts_lock.unlock();
d->budgets_lock.lock();
d->budgets.push_back(std::make_pair(size, ptr));
d->budgets_lock.unlock();
return;
}
}
d->payouts_lock.unlock();
NCNN_LOGE("FATAL ERROR! pool allocator get wild %p", ptr);
ncnn::fastFree(ptr);
}3、 Derived class UnlockedPoolAllocator
Unlock after the thread operates the resource
class UnlockedPoolAllocatorPrivate;
class NCNN_EXPORT UnlockedPoolAllocator : public Allocator
{
public:
UnlockedPoolAllocator();
~UnlockedPoolAllocator();
// ratio range 0 ~ 1
// default cr = 0.75
void set_size_compare_ratio(float scr);
// release all budgets immediately
void clear();
virtual void* fastMalloc(size_t size);
virtual void fastFree(void* ptr);
private:
UnlockedPoolAllocator(const UnlockedPoolAllocator&);
UnlockedPoolAllocator& operator=(const UnlockedPoolAllocator&);
private:
UnlockedPoolAllocatorPrivate* const d;
};
UnlockedPoolAllocatorPrivate Class declaration
class UnlockedPoolAllocatorPrivate
{
public:
unsigned int size_compare_ratio; // 0~256
std::list<std::pair<size_t, void*> > budgets;
std::list<std::pair<size_t, void*> > payouts;
};边栏推荐
- [single chip microcomputer simulation] (10) instruction system - multiplication instruction and division of arithmetic operation instruction
- 内置键盘连续444
- Systick timer basic learning and hand tearing code
- 2002 - Can‘t connect to server on ‘127.0.0.1‘ (36)
- GraphQL初识
- ncnn paramdict&modelbin
- [MCU simulation] (V) addressing mode - immediate addressing and register indirect addressing
- 【单片机仿真】(十三)指令系统逻辑运算指令 — 移位指令
- Fiddler grabbing
- Need to slow down a little
猜你喜欢

1. Introduction, analysis and implementation of asynctool framework

2. Actual use of asynctool framework

ubuntu清除cuda缓存

MySQL storage engine details

All dates in Oracle query time period

Redis' simple dynamic string SDS

C language foundation day4 array

Code demonstration of fcos face detection model in openvino

yolov6 学习初篇

Understand JVM garbage collection in one article
随机推荐
[single chip microcomputer simulation] (10) instruction system - multiplication instruction and division of arithmetic operation instruction
【单片机仿真】(九)指令系统 — 算术运算指令 之 ADD、ADDC、SUBB、INC、DEC、DA
[MCU simulation] (VI) addressing mode - index addressing and relative addressing
We should increase revenue and reduce expenditure
04_ Service registration Eureka
【单片机仿真】(十九)介绍汇编、汇编指令、伪指令
【回归预测】基于粒子滤波实现锂离子电池寿命预测附matlab代码
[MCU simulation] (XIV) instruction system bit operation instructions - bit data transmission instructions MOV, bit variable modification instructions
C language foundation day4 array
自动装配 & 集合注入
First knowledge of JPA (ORM idea, basic operation of JPA)
【单片机仿真】(二十)ORG — 设置起始地址
mysql复制表
无线用的鉴权代码
[MCU simulation] (XVIII) control transfer instructions - empty operation instructions
快照:数据快照(数据兜底方式)
What happens when you get stuck compiling and installing MySQL database in Linux system?
【单片机仿真】(十六)控制转移类指令 — 无条件转移指令、条件转移指令
MySQL replication table
Graphql first acquaintance