当前位置:网站首页>Tar source code analysis Part 3
Tar source code analysis Part 3
2022-07-04 06:37:00 【Tao song remains the same】
buffer pool The implementation of the , It's also very classic :
#include "util/tc_buffer_pool.h"
#include <cassert>
#include <sstream>
#include <iomanip>
inline static std::size_t RoundUp2Power(std::size_t size)
{
if (size == 0)
return 0;
size_t roundUp = 1;
while (roundUp < size)
roundUp *= 2;
return roundUp;
}
namespace tars
{
TC_Slice::TC_Slice(void* d, size_t dl, size_t l) :
data(d),
dataLen(dl),
len(l)
{
}
TC_BufferPool::TC_BufferPool(size_t minBlock, size_t maxBlock) :
_minBlock(RoundUp2Power(minBlock)),
_maxBlock(RoundUp2Power(maxBlock)),
_maxBytes(1024 * 1024), // pool The allocated memory cannot exceed it , Otherwise directly new and delete, Avoid using too much memory
_totalBytes(0) // pool Memory in
{
/*
* minBlock It means that we should pool Responsible for the minimum memory allocation , Rounding up , such as 20 byte , Rounding off 32
* maxBlock It means that we should pool The maximum memory allocation for which you are responsible , Rounding up , such as 1000 byte , Rounding off 1024
* listCount It's calculation buffer Number of linked lists , such as minBlock yes 32,maxBlock yes 64, Then only two linked lists are needed
*/
size_t listCount = 0;
size_t testVal = _minBlock;
while (testVal <= _maxBlock)
{
testVal *= 2;
++ listCount;
}
assert (listCount > 0);
_buffers.resize(listCount);
}
TC_BufferPool::~TC_BufferPool()
{
std::vector<BufferList>::iterator it(_buffers.begin());
for (; it != _buffers.end(); ++ it)
{
BufferList& blist = *it;
BufferList::iterator bit(blist.begin());
for ( ; bit != blist.end(); ++ bit)
{
//delete[] (*bit);
delete[] reinterpret_cast<char*>(*bit);
}
}
}
TC_Slice TC_BufferPool::Allocate(size_t size)
{
TC_Slice s;
size = RoundUp2Power(size);
if (size == 0)
return s;
if (size < _minBlock || size > _maxBlock)
{
// Not to return pool management , direct new
s.data = new char[size];
s.len = size;
}
else
{
// Positioning to specific buffer Linked list
BufferList& blist = _GetBufferList(size);
s = _Allocate(size, blist);
}
return s;
}
void TC_BufferPool::Deallocate(TC_Slice s)
{
if (s.len < _minBlock || s.len > _maxBlock)
{
// Not to return pool management , direct delete
delete[] reinterpret_cast<char*>(s.data);
}
else if (_totalBytes >= _maxBytes)
{
// Too much memory , Don't give it back pool
delete[] reinterpret_cast<char*>(s.data);
}
else
{
// give back pool
BufferList& blist = _GetBufferList(s.len);
blist.push_back(s.data);
_totalBytes += s.len;
}
}
void TC_BufferPool::SetMaxBytes(size_t bytes)
{
_maxBytes = bytes;
}
size_t TC_BufferPool::GetMaxBytes() const
{
return _maxBytes;
}
std::string TC_BufferPool::DebugPrint() const
{
std::ostringstream oss;
oss << "\n===============================================================\n";
oss << "============ BucketCount " << std::setiosflags(std::ios::left) << std::setw(4) << _buffers.size() << " ================================" << std::endl;
oss << "============ PoolBytes " << std::setw(10) << _totalBytes << " ============================" << std::endl;
int bucket = 0;
size_t size = _minBlock;
std::vector<BufferList>::const_iterator it(_buffers.begin());
for (; it != _buffers.end(); ++ it)
{
const BufferList& blist = *it;
oss << "== Bucket " << std::setw(3) << bucket
<< ": BlockSize " << std::setw(8) << size
<< " Remain blocks " << std::setw(6) << blist.size()
<< " ======== \n";
++ bucket;
size *= 2;
}
return oss.str();
}
TC_Slice TC_BufferPool::_Allocate(size_t size, BufferList& blist)
{
assert ((size & (size - 1)) == 0);
TC_Slice s;
s.len = size;
if (blist.empty())
{
s.data = new char[size];
}
else
{
// Take it directly from the linked list buffer
s.data = *blist.begin();
blist.pop_front();
_totalBytes -= s.len;
}
return s;
}
TC_BufferPool::BufferList& TC_BufferPool::_GetBufferList(size_t s)
{
const BufferList& blist = const_cast<const TC_BufferPool& >(*this)._GetBufferList(s);
return const_cast<BufferList& >(blist);
}
const TC_BufferPool::BufferList& TC_BufferPool::_GetBufferList(size_t s) const
{
assert ((s & (s - 1)) == 0);
assert (s >= _minBlock && s <= _maxBlock);
size_t index = _buffers.size();
size_t testVal = s;
while (testVal <= _maxBlock)
{
testVal *= 2;
index --;
}
return _buffers[index];
}
} // end namespace tars
边栏推荐
- InputStream/OutputStream(文件的输入输出)
- 2022年,或许是未来10年经济最好的一年,2022年你毕业了吗?毕业后是怎么计划的?
- 4G wireless all network solar hydrological equipment power monitoring system bms110
- Arcpy uses the updatelayer function to change the symbol system of the layer
- Dimension and format of data
- How does apscheduler set tasks not to be concurrent (that is, execute the next task after the first one)?
- Learning multi-level structural information for small organ segmentation
- 8. Factory method
- 7. Agency mode
- lightroom 导入图片灰色/黑色矩形 多显示器
猜你喜欢

【问题记录】03 连接MySQL数据库提示:1040 Too many connections

Learning multi-level structural information for small organ segmentation

C language exercises (recursion)

《ClickHouse原理解析与应用实践》读书笔记(4)

Learning multi-level structural information for small organ segmentation

JSON web token -- comparison between JWT and traditional session login authentication

颈椎、脚气

Mysql 45讲学习笔记(七)行锁

实用的小工具指令

Abap:ooalv realizes the function of adding, deleting, modifying and checking
随机推荐
tars源码分析之2
Mysql 45讲学习笔记(七)行锁
Error CVC complex type 2.4. a: Invalid content beginning with element 'base extension' was found. Should start with one of '{layoutlib}'.
[backpack DP] backpack problem
JSON Web Token----JWT和傳統session登錄認證對比
How to realize multi account login of video platform members
《ClickHouse原理解析与应用实践》读书笔记(4)
Stc8h development (XII): I2C drive AT24C08, at24c32 series EEPROM storage
Vant --- detailed explanation and use of list component in vant
How does apscheduler set tasks not to be concurrent (that is, execute the next task after the first one)?
云原生——上云必读之SSH篇(常用于远程登录云服务器)
STC8H开发(十二): I2C驱动AT24C08,AT24C32系列EEPROM存储
Wechat applet scroll view component scrollable view area
R统计绘图-随机森林分类分析及物种丰度差异检验组合图
C realize Snake games
树形dp
How to avoid JVM memory leakage?
2022 where to find enterprise e-mail and which is the security of enterprise e-mail system?
JS execution mechanism
Cloud native - SSH article that must be read on the cloud (commonly used for remote login to ECS)