测试代码添加ringbuffer
parent
b4665a782e
commit
22ad0c7c81
|
@ -5,13 +5,13 @@ using namespace std;
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class RingBuffer{
|
class RingBuffer{
|
||||||
public:
|
public:
|
||||||
typedef enum Error{
|
enum Error{
|
||||||
Error = -10,
|
Error = -10,
|
||||||
ERROR_FULL = -11,
|
ERROR_FULL = -11,
|
||||||
ERROR_NO_ENOUGH_WRITE = -12,
|
ERROR_NO_ENOUGH_WRITE = -12,
|
||||||
};
|
};
|
||||||
RingBuffer(uint16_t len){
|
RingBuffer(uint16_t len) {
|
||||||
mBuffer = new[len];
|
mBuffer = new T[len];
|
||||||
mHead = mTail = mBuffer;
|
mHead = mTail = mBuffer;
|
||||||
if (mBuffer == nullptr){
|
if (mBuffer == nullptr){
|
||||||
this->mLen = 0;
|
this->mLen = 0;
|
||||||
|
@ -35,7 +35,7 @@ public:
|
||||||
{
|
{
|
||||||
int copy_sz = min(count, this->ReadableCnt());
|
int copy_sz = min(count, this->ReadableCnt());
|
||||||
memcpy(data, mHead, copy_sz * sizeof(T));
|
memcpy(data, mHead, copy_sz * sizeof(T));
|
||||||
rb->rb_head += copy_sz * sizeof(T);
|
mHead += copy_sz * sizeof(T);
|
||||||
return copy_sz;
|
return copy_sz;
|
||||||
}
|
}
|
||||||
else // 头部大于尾部
|
else // 头部大于尾部
|
||||||
|
@ -43,8 +43,8 @@ public:
|
||||||
if (count < mLen -(mHead - mBuffer)) // (rb->rb_head - rb->rb_buff) 是从头部到数组物理结束的长度,如果小于说明不需要读取数组头部的,只需要copy一次
|
if (count < mLen -(mHead - mBuffer)) // (rb->rb_head - rb->rb_buff) 是从头部到数组物理结束的长度,如果小于说明不需要读取数组头部的,只需要copy一次
|
||||||
{
|
{
|
||||||
int copy_sz = count*sizeof(T);
|
int copy_sz = count*sizeof(T);
|
||||||
memcpy(data, rb->rb_head, copy_sz*sizeof(T));
|
memcpy(data, mHead, copy_sz*sizeof(T));
|
||||||
rb->rb_head += copy_sz;
|
mHead += copy_sz;
|
||||||
return copy_sz;
|
return copy_sz;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -59,14 +59,14 @@ public:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
int16_t Write(const T *in,uint16_t len){
|
int16_t Write(const T *in,uint16_t len){
|
||||||
if (len >= WriteableCnt(rb))
|
if (len >= WriteableCnt())
|
||||||
return ERROR_NO_ENOUGH_WRITE;
|
return ERROR_NO_ENOUGH_WRITE;
|
||||||
if (mHead <= mTail) // 头部小于尾部
|
if (mHead <= mTail) // 头部小于尾部
|
||||||
{
|
{
|
||||||
int tail_avail_sz = mLen - (mTail - this->mBuffer);
|
int tail_avail_sz = mLen - (mTail - this->mBuffer);
|
||||||
if (len <= tail_avail_sz) // 是否需要循环到0开始写
|
if (len <= tail_avail_sz) // 是否需要循环到0开始写
|
||||||
{
|
{
|
||||||
memcpy(rb->rb_tail, in, len*sizeof(T));
|
memcpy(mTail, in, len*sizeof(T));
|
||||||
mTail += len;
|
mTail += len;
|
||||||
if (mTail == mBuffer + mLen) // 刚好相等的情况
|
if (mTail == mBuffer + mLen) // 刚好相等的情况
|
||||||
mTail = mBuffer;
|
mTail = mBuffer;
|
||||||
|
@ -75,8 +75,8 @@ public:
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
memcpy(mTail, in, tail_avail_sz*sizeof(T));
|
memcpy(mTail, in, tail_avail_sz*sizeof(T));
|
||||||
mTail = rb->rb_buff;
|
mTail = mBuffer;
|
||||||
return tail_avail_sz + Write(rb, (char*)in + tail_avail_sz, (len - tail_avail_sz)*sizeof(T));
|
return tail_avail_sz + Write( (T*)in + tail_avail_sz* sizeof(T), (len - tail_avail_sz));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
Loading…
Reference in New Issue