From d628355dcde85c0266f1475ecb7fdfe04c63b388 Mon Sep 17 00:00:00 2001 From: zcy <290198252@qq.com> Date: Sun, 16 May 2021 01:51:10 +0800 Subject: [PATCH] =?UTF-8?q?ringbuffer=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- general/src/pattern/ringbuffer.hpp | 83 ++++++++++++++++++++++++------ test/src/cpp11/template.cpp | 17 +++++- 2 files changed, 84 insertions(+), 16 deletions(-) diff --git a/general/src/pattern/ringbuffer.hpp b/general/src/pattern/ringbuffer.hpp index 295bcb3..cbc3c6e 100644 --- a/general/src/pattern/ringbuffer.hpp +++ b/general/src/pattern/ringbuffer.hpp @@ -17,6 +17,7 @@ public: uint32_t CanReadCount(); uint32_t CanWriteCount(); uint32_t Size(); + uint32_t Length(); ~RingBuffer(); @@ -57,6 +58,10 @@ template uint32_t RingBuffer::Size(){ return mSize; } +template +uint32_t RingBuffer::Length(){ + return mSize - mRemain; +} template T RingBuffer::At(uint64_t pos){ @@ -84,6 +89,7 @@ int RingBuffer::Add(T *data,uint64_t len){ if(mCurrentHead == 0){ memcpy(mData,data,bytes_write*sizeof(T)); mCurrentTail = bytes_write; + return bytes_write; }else{ /// |start |...| head also tail|...|end| if(mCurrentTail + bytes_write < mSize){ @@ -94,9 +100,12 @@ int RingBuffer::Add(T *data,uint64_t len){ 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; + return bytes_write; } } } + /// |start|head|.....|tail|end| if(mCurrentTail > mCurrentHead){ uint16_t off = mCurrentTail + bytes_write; @@ -105,43 +114,87 @@ int RingBuffer::Add(T *data,uint64_t len){ sizeof(T)*(mSize - mCurrentTail)); memcpy(mData,data + sizeof(T)*(mSize - mCurrentTail), (bytes_write - (mSize - mCurrentTail))*sizeof(T)); + mRemain -= bytes_write; + mCurrentTail = off - mSize; + return bytes_write; } if(off <= mSize){ memcpy(&mData[mCurrentTail],data, - sizeof(T)*bytes_write); + sizeof(T)*bytes_write); + mRemain -= bytes_write; + if(off < mSize) + mCurrentTail += bytes_write; + else + mCurrentTail = 0; + return bytes_write; } } /// |start|...|tail|.....|head|...|end| if(mCurrentHead > mCurrentTail){ memcpy(&mData[mCurrentTail],data,sizeof(T)*(bytes_write)); + mCurrentTail += bytes_write; + mRemain -= bytes_write; + return bytes_write; } - mRemain -= bytes_write; - return bytes_write; + } template int RingBuffer::Take(T *data,uint64_t len){ - + if(data == nullptr) + return -1; + if(mRemain == mSize) + return 0; + int bytes_read = 0; + if(len > (mSize - mRemain)){ + bytes_read = mSize - mRemain; + }else{ + bytes_read = len; + } + /// |start|head|.....|tail|end| + if(mCurrentTail >= mCurrentHead){ + memcpy(data,&mData[mCurrentHead],sizeof(T)*bytes_read); + mCurrentHead += bytes_read; + mRemain = mCurrentTail - mCurrentHead; + return bytes_read; + + } + /// |start|...|tail|.....|head|...|end| + if(mCurrentHead > mCurrentTail){ + int off = mCurrentHead + bytes_read; + if(off > mSize){ + int tmp = mSize - mCurrentHead; + memcpy(data,&mData[mCurrentHead],(mSize - mCurrentHead)*sizeof(T)); + memcpy(data + (mSize - mCurrentHead)*sizeof(T),&mData[0],(bytes_read - tmp)*sizeof(T)); + mCurrentHead = off - mSize; + mRemain = mCurrentTail - mCurrentHead; + return bytes_read; + } + if(off <= mSize){ + memcpy(data,&mData[mCurrentHead],sizeof(T)*bytes_read); + if(off == mSize){ + mCurrentHead = 0; + mRemain = mCurrentTail; + return bytes_read; + } + else{ + mCurrentHead += bytes_read; + mRemain = (mSize - mCurrentHead) + mCurrentTail; + return bytes_read; + } + } + } } template uint32_t RingBuffer::CanReadCount(){ - if(mCurrentHead > mCurrentTail){ - return mSize - (mCurrentHead - mCurrentTail); - }else{ - return mCurrentTail - mCurrentHead; - } + return mSize - mRemain; } template uint32_t RingBuffer::CanWriteCount(){ - if (mCurrentTail > mCurrentHead){ - return mSize - (mCurrentTail - mCurrentHead); - } - if(mCurrentHead > mCurrentTail){ - return mCurrentHead - mCurrentTail; - } + return mRemain; } #endif diff --git a/test/src/cpp11/template.cpp b/test/src/cpp11/template.cpp index 764e18f..77f9d89 100644 --- a/test/src/cpp11/template.cpp +++ b/test/src/cpp11/template.cpp @@ -17,11 +17,26 @@ void TestRingBuffer(){ } x.SetEmpty(); - ret = x.Add(in,1024); + ret = x.Add(in,516); std::cout<<"recv "<