当前位置:网站首页>tars源码分析之4
tars源码分析之4
2022-07-04 06:33:00 【涛歌依旧】
基础buffer的实现,也很简单,大致看看:
#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边栏推荐
- ABCD four sequential execution methods, extended application
- Invalid bound statement (not found): com. example. mapper. TblUserRecordMapper. login
- What is the "relative dilemma" in cognitive fallacy?
- leetcode 310. Minimum Height Trees
- 实用的小工具指令
- Background and current situation of domestic CDN acceleration
- ORICO ORICO outdoor power experience, lightweight and portable, the most convenient office charging station
- Appium基础 — APPium安装(二)
- R统计绘图-随机森林分类分析及物种丰度差异检验组合图
- [MySQL] introduction, function, creation, view, deletion and modification of database view (with exercises)
猜你喜欢

How to avoid JVM memory leakage?

Detectron: train your own data set -- convert your own data format to coco format

Practical gadget instructions

After the festival, a large number of people change careers. Is it still time to be 30? Listen to the experience of the past people
![[number theory] fast power (Euler power)](/img/1e/5d032c8f2e43f553b4543d28ea2a2d.jpg)
[number theory] fast power (Euler power)

Notes and notes

如何避免 JVM 内存泄漏?

How to use multithreading to export excel under massive data? Source code attached!

C language - Blue Bridge Cup - Snake filling

Arcpy uses the updatelayer function to change the symbol system of the layer
随机推荐
Practical gadget instructions
如何实现视频平台会员多账号登录
[backpack DP] backpack problem
C语言练习题(递归)
Vant --- detailed explanation and use of list component in vant
Stc8h development (XII): I2C drive AT24C08, at24c32 series EEPROM storage
Which water in the environment needs water quality monitoring
Learn about the Internet of things protocol WiFi ZigBee Bluetooth, etc. --- WiFi and WiFi protocols start from WiFi. What do we need to know about WiFi protocol itself?
Fast power (template)
QT qtablewidget table column top requirements ideas and codes
CORS is not intended to protect API endpoints - nikofischer
Stc8h development (XII): I2C drive AT24C08, at24c32 series EEPROM storage
MySQL information_ Schema database
2022.7.2-----leetcode.871
Learning multi-level structural information for small organ segmentation
Mysql 45讲学习笔记(十四)count(*)
C实现贪吃蛇小游戏
P26-P34 third_ template
C réaliser des jeux de serpents gourmands
手动对list进行分页(参数list ,当前页,页面大小)