From 29c15e4016805f222989171f6baa7b735abb2e0b Mon Sep 17 00:00:00 2001 From: zcy <290198252@qq.com> Date: Mon, 20 Dec 2021 16:28:09 +0800 Subject: [PATCH] no message --- general/src/pattern/ringbuffer.hpp | 13 ++++++------- test/src/cpp11/template.cpp | 3 +-- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/general/src/pattern/ringbuffer.hpp b/general/src/pattern/ringbuffer.hpp index 1e508b4..1f4043c 100644 --- a/general/src/pattern/ringbuffer.hpp +++ b/general/src/pattern/ringbuffer.hpp @@ -2,10 +2,7 @@ #define __RINGBUFFER__ #include -/** - * author : caiyuzheng - * function: a ringbuffer class -*/ + #include template class RingBuffer{ public: @@ -40,7 +37,8 @@ RingBuffer::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::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::Add(T *data,uint64_t len){ mRemain -= bytes_write; return bytes_write; } - } template diff --git a/test/src/cpp11/template.cpp b/test/src/cpp11/template.cpp index 54a4aaf..0b2c600 100644 --- a/test/src/cpp11/template.cpp +++ b/test/src/cpp11/template.cpp @@ -18,13 +18,12 @@ void TestRingBuffer(){ } RingBuffer x(1024); int ret = x.Add(in,512); - std::cout<<"add "<