no message

master
zcy 2021-12-20 16:28:09 +08:00
parent eecb67b88a
commit 29c15e4016
2 changed files with 7 additions and 9 deletions

View File

@ -2,10 +2,7 @@
#define __RINGBUFFER__
#include <memory>
/**
* author : caiyuzheng
* function: a ringbuffer class
*/
#include<stdint.h>
template <typename T> class RingBuffer{
public:
@ -40,7 +37,8 @@ RingBuffer<T>::RingBuffer(uint64_t size) :
mData(nullptr),
mSize(0),
mCurrentTail(0),
mCurrentHead(0) {
mCurrentHead(0)
{
mSize = size;
mData = new T[size];
mRemain = mSize;
@ -125,18 +123,20 @@ int RingBuffer<T>::Add(T *data,uint64_t len){
if(mCurrentHead == 0){
memcpy(mData,data,bytes_write*sizeof(T));
mCurrentTail = bytes_write;
mRemain -= bytes_write;
return bytes_write;
}else{
/// |start |...| head also tail|...|end|
if(mCurrentTail + bytes_write < mSize){
memcpy(&mData[mCurrentTail],data,(bytes_write)*sizeof(T));
mCurrentTail += bytes_write;
mRemain -= bytes_write;
}else{
memcpy(&mData[mCurrentTail],data,(mSize - mCurrentTail)*sizeof(T));
std::cout<<"\r\n"<<(mCurrentTail + bytes_write)%mSize<<"\r\n";
memcpy(&mData[0],&data[mSize - mCurrentTail],(mCurrentTail + bytes_write)%mSize*sizeof(T));
mCurrentTail = (mCurrentTail + bytes_write)%mSize;
this->mRemain -= bytes_write;
mRemain -= bytes_write;
return bytes_write;
}
}
@ -172,7 +172,6 @@ int RingBuffer<T>::Add(T *data,uint64_t len){
mRemain -= bytes_write;
return bytes_write;
}
}
template<typename T>

View File

@ -18,13 +18,12 @@ void TestRingBuffer(){
}
RingBuffer<int> x(1024);
int ret = x.Add(in,512);
std::cout<<"add "<<ret<<"\r\n"<<std::endl;
std::cout<<"add "<<ret<<" x.Length():" << x.Length() << " "<<std::endl;
for(uint32_t i = 0;i < x.Length();i++){
printf("%d ",x.At(i));
}
std::cout<<"\r\n----------------------------------"<<std::endl;
x.SetEmpty();
ret = x.Add(in,516);
std::cout<<"add "<<ret<<"\r\n"<<std::endl;
for(uint32_t i = 0;i < x.Length();i++){