当前位置:网站首页>Tar source code analysis 4
Tar source code analysis 4
2022-07-04 06:37:00 【Tao song remains the same】
Basics buffer The implementation of the , It's also very simple. , Take a general look at :
#include "util/tc_buffer.h"
#include <iostream>
#include <algorithm>
#include <limits>
#include <cassert>
inline static std::size_t RoundUp2Power(std::size_t size)
{
if (size == 0)
return 0;
std::size_t roundUp = 1;
while (roundUp < size)
roundUp *= 2;
return roundUp;
}
namespace tars
{
const std::size_t TC_Buffer::kMaxBufferSize = std::numeric_limits<std::size_t>::max() / 2;
const std::size_t TC_Buffer::kDefaultSize = 128;
std::size_t TC_Buffer::PushData(const void* data, std::size_t size)
{
if (!data || size == 0)
return 0;
if (ReadableSize() + size >= kMaxBufferSize)
return 0; // overflow
AssureSpace(size);
::memcpy(&_buffer[_writePos], data, size);
Produce(size);
return size;
}
std::size_t TC_Buffer::PopData(void* buf, std::size_t size)
{
const std::size_t dataSize = ReadableSize();
if (!buf ||
size == 0 ||
dataSize == 0)
return 0;
if (size > dataSize)
size = dataSize; // truncate
::memcpy(buf, &_buffer[_readPos], size);
Consume(size);
return size;
}
void TC_Buffer::PeekData(void*& buf, std::size_t& size)
{
buf = ReadAddr();
size = ReadableSize();
}
void TC_Buffer::Consume(std::size_t bytes)
{
assert (_readPos + bytes <= _writePos);
_readPos += bytes;
if (IsEmpty())
Clear();
}
void TC_Buffer::AssureSpace(std::size_t needsize)
{
if (WritableSize() >= needsize)
return;
const size_t dataSize = ReadableSize();
const size_t oldCap = _capacity;
while (WritableSize() + _readPos < needsize)
{
if (_capacity < kDefaultSize)
{
_capacity = kDefaultSize;
}
else if (_capacity <= kMaxBufferSize)
{
const size_t newCapcity = RoundUp2Power(_capacity);
if (_capacity < newCapcity)
_capacity = newCapcity;
else
_capacity = 2 * newCapcity;
}
else
{
assert(false);
}
}
if (oldCap < _capacity)
{
char* tmp(new char[_capacity]);
if (dataSize != 0)
memcpy(&tmp[0], &_buffer[_readPos], dataSize);
ResetBuffer(tmp);
}
else
{
assert (_readPos > 0);
::memmove(&_buffer[0], &_buffer[_readPos], dataSize);
}
_readPos = 0;
_writePos = dataSize;
assert (needsize <= WritableSize());
}
void TC_Buffer::Shrink()
{
if (IsEmpty())
{
Clear();
_capacity = 0;
ResetBuffer();
return;
}
if (_capacity <= kDefaultSize)
return;
std::size_t oldCap = _capacity;
std::size_t dataSize = ReadableSize();
if (dataSize * 100 > oldCap * _highWaterPercent)
return;
std::size_t newCap = RoundUp2Power(dataSize);
char* tmp(new char[newCap]);
memcpy(&tmp[0], &_buffer[_readPos], dataSize);
ResetBuffer(tmp);
_capacity = newCap;
_readPos = 0;
_writePos = dataSize;
}
void TC_Buffer::Clear()
{
_readPos = _writePos = 0;
}
void TC_Buffer::Swap(TC_Buffer& buf)
{
std::swap(_readPos, buf._readPos);
std::swap(_writePos, buf._writePos);
std::swap(_capacity, buf._capacity);
std::swap(_buffer, buf._buffer);
}
void TC_Buffer::ResetBuffer(void* ptr)
{
delete[] _buffer;
_buffer = reinterpret_cast<char*>(ptr);
}
void TC_Buffer::SetHighWaterPercent(size_t percents)
{
if (percents < 10 || percents >= 100)
return;
_highWaterPercent = percents;
}
} // end namespace tars
边栏推荐
- uniapp 自定义环境变量
- How does apscheduler set tasks not to be concurrent (that is, execute the next task after the first one)?
- The width of the picture in rich text used by wechat applet exceeds the problem
- Notes and notes
- 手动对list进行分页(参数list ,当前页,页面大小)
- 27-31. Dependency transitivity, principle
- Learning multi-level structural information for small organ segmentation
- 《ClickHouse原理解析与应用实践》读书笔记(4)
- 【MySQL】数据库视图的介绍、作用、创建、查看、删除和修改(附练习题)
- Functions in C language (detailed explanation)
猜你喜欢
Overview of convolutional neural network structure optimization
【问题记录】03 连接MySQL数据库提示:1040 Too many connections
QT get random color value and set label background color code
Tree DP
C réaliser des jeux de serpents gourmands
17-18. Dependency scope and life cycle plug-ins
[March 3, 2019] MAC starts redis
4G wireless all network solar hydrological equipment power monitoring system bms110
Arcpy uses the updatelayer function to change the symbol system of the layer
Learning multi-level structural information for small organ segmentation
随机推荐
Download kicad on Alibaba cloud image station
Inputstream/outputstream (input and output of file)
Sort list tool class, which can sort strings
MySQL information_ Schema database
Bicolor case
Layoutmanager layout manager: flowlayout, borderlayout, GridLayout, gridbaglayout, CardLayout, BoxLayout
Appium foundation - appium installation (II)
27-31. Dependency transitivity, principle
7. Agency mode
Summary of leetcode BFS question brushing
Dimension and format of data
Software keywords and process information intercepted by Golden Shield video player
tars源码分析之2
What is the "relative dilemma" in cognitive fallacy?
Mysql 45讲学习笔记(七)行锁
[March 3, 2019] MAC starts redis
[untitled]
Tsinghua University product: penalty gradient norm improves generalization of deep learning model
内卷怎么破?
Invalid bound statement (not found): com. example. mapper. TblUserRecordMapper. login