当前位置:网站首页>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边栏推荐
- Appium基础 — APPium安装(二)
- Shopping malls, storerooms, flat display, user-defined maps can also be played like this!
- tars源码分析之9
- 2022年,或许是未来10年经济最好的一年,2022年你毕业了吗?毕业后是怎么计划的?
- What is the "relative dilemma" in cognitive fallacy?
- R statistical mapping - random forest classification analysis and species abundance difference test combination diagram
- 金盾视频播放器拦截的软件关键词和进程信息
- Average two numbers
- Functions in C language (detailed explanation)
- 树形dp
猜你喜欢

Detailed explanation of common APIs for component and container containers: frame, panel, scrollpane

List of top ten professional skills required for data science work

C realize Snake games

2022 where to find enterprise e-mail and which is the security of enterprise e-mail system?

【MySQL】数据库视图的介绍、作用、创建、查看、删除和修改(附练习题)

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

2022 Xinjiang's latest eight members (Safety Officer) simulated examination questions and answers
![[problem record] 03 connect to MySQL database prompt: 1040 too many connections](/img/bb/4d8d202cf5c6e556bc860a734e5934.png)
[problem record] 03 connect to MySQL database prompt: 1040 too many connections
![[Android reverse] function interception (CPU cache mechanism | CPU cache mechanism causes function interception failure)](/img/7e/02bb01480257cd56537914a7247733.jpg)
[Android reverse] function interception (CPU cache mechanism | CPU cache mechanism causes function interception failure)

Fundamentals of SQL database operation
随机推荐
Tree DP
雲原生——上雲必讀之SSH篇(常用於遠程登錄雲服務器)
双色球案例
Invalid revision: 3.18.1-g262b901-dirty
2022.7.2-----leetcode.871
R statistical mapping - random forest classification analysis and species abundance difference test combination diagram
11. Dimitt's law
STC8H开发(十二): I2C驱动AT24C08,AT24C32系列EEPROM存储
ABAP:OOALV实现增删改查功能
微信小程序使用rich-text中图片宽度超出问题
Json Web token - jwt vs. Traditional session login Authentication
Common JS tool Libraries
How to avoid JVM memory leakage?
Displaying currency in Indian numbering format
Background and current situation of domestic CDN acceleration
ADC voltage calculation of STM32 single chip microcomputer
Operator < <> > fool test case
buuctf-pwn write-ups (8)
Fast power (template)
[MySQL] introduction, function, creation, view, deletion and modification of database view (with exercises)