no message
parent
eecb67b88a
commit
29c15e4016
|
@ -2,10 +2,7 @@
|
||||||
#define __RINGBUFFER__
|
#define __RINGBUFFER__
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
/**
|
|
||||||
* author : caiyuzheng
|
|
||||||
* function: a ringbuffer class
|
|
||||||
*/
|
|
||||||
#include<stdint.h>
|
#include<stdint.h>
|
||||||
template <typename T> class RingBuffer{
|
template <typename T> class RingBuffer{
|
||||||
public:
|
public:
|
||||||
|
@ -40,7 +37,8 @@ RingBuffer<T>::RingBuffer(uint64_t size) :
|
||||||
mData(nullptr),
|
mData(nullptr),
|
||||||
mSize(0),
|
mSize(0),
|
||||||
mCurrentTail(0),
|
mCurrentTail(0),
|
||||||
mCurrentHead(0) {
|
mCurrentHead(0)
|
||||||
|
{
|
||||||
mSize = size;
|
mSize = size;
|
||||||
mData = new T[size];
|
mData = new T[size];
|
||||||
mRemain = mSize;
|
mRemain = mSize;
|
||||||
|
@ -125,18 +123,20 @@ int RingBuffer<T>::Add(T *data,uint64_t len){
|
||||||
if(mCurrentHead == 0){
|
if(mCurrentHead == 0){
|
||||||
memcpy(mData,data,bytes_write*sizeof(T));
|
memcpy(mData,data,bytes_write*sizeof(T));
|
||||||
mCurrentTail = bytes_write;
|
mCurrentTail = bytes_write;
|
||||||
|
mRemain -= bytes_write;
|
||||||
return bytes_write;
|
return bytes_write;
|
||||||
}else{
|
}else{
|
||||||
/// |start |...| head also tail|...|end|
|
/// |start |...| head also tail|...|end|
|
||||||
if(mCurrentTail + bytes_write < mSize){
|
if(mCurrentTail + bytes_write < mSize){
|
||||||
memcpy(&mData[mCurrentTail],data,(bytes_write)*sizeof(T));
|
memcpy(&mData[mCurrentTail],data,(bytes_write)*sizeof(T));
|
||||||
mCurrentTail += bytes_write;
|
mCurrentTail += bytes_write;
|
||||||
|
mRemain -= bytes_write;
|
||||||
}else{
|
}else{
|
||||||
memcpy(&mData[mCurrentTail],data,(mSize - mCurrentTail)*sizeof(T));
|
memcpy(&mData[mCurrentTail],data,(mSize - mCurrentTail)*sizeof(T));
|
||||||
std::cout<<"\r\n"<<(mCurrentTail + bytes_write)%mSize<<"\r\n";
|
std::cout<<"\r\n"<<(mCurrentTail + bytes_write)%mSize<<"\r\n";
|
||||||
memcpy(&mData[0],&data[mSize - mCurrentTail],(mCurrentTail + bytes_write)%mSize*sizeof(T));
|
memcpy(&mData[0],&data[mSize - mCurrentTail],(mCurrentTail + bytes_write)%mSize*sizeof(T));
|
||||||
mCurrentTail = (mCurrentTail + bytes_write)%mSize;
|
mCurrentTail = (mCurrentTail + bytes_write)%mSize;
|
||||||
this->mRemain -= bytes_write;
|
mRemain -= bytes_write;
|
||||||
return bytes_write;
|
return bytes_write;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -172,7 +172,6 @@ int RingBuffer<T>::Add(T *data,uint64_t len){
|
||||||
mRemain -= bytes_write;
|
mRemain -= bytes_write;
|
||||||
return bytes_write;
|
return bytes_write;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
|
|
@ -18,13 +18,12 @@ void TestRingBuffer(){
|
||||||
}
|
}
|
||||||
RingBuffer<int> x(1024);
|
RingBuffer<int> x(1024);
|
||||||
int ret = x.Add(in,512);
|
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++){
|
for(uint32_t i = 0;i < x.Length();i++){
|
||||||
printf("%d ",x.At(i));
|
printf("%d ",x.At(i));
|
||||||
}
|
}
|
||||||
std::cout<<"\r\n----------------------------------"<<std::endl;
|
std::cout<<"\r\n----------------------------------"<<std::endl;
|
||||||
x.SetEmpty();
|
x.SetEmpty();
|
||||||
|
|
||||||
ret = x.Add(in,516);
|
ret = x.Add(in,516);
|
||||||
std::cout<<"add "<<ret<<"\r\n"<<std::endl;
|
std::cout<<"add "<<ret<<"\r\n"<<std::endl;
|
||||||
for(uint32_t i = 0;i < x.Length();i++){
|
for(uint32_t i = 0;i < x.Length();i++){
|
||||||
|
|
Loading…
Reference in New Issue