ringbuffer添加read
parent
fae73f8668
commit
b4665a782e
|
@ -30,11 +30,8 @@
|
|||
</component>
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="0facce0d-c642-4d80-b2fb-daf5f3e68dff" name="Default Changelist" comment="">
|
||||
<change afterPath="$PROJECT_DIR$/general/src/algorithm/ringbuffer.hpp" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/test/CMakeLists.txt" beforeDir="false" afterPath="$PROJECT_DIR$/test/src/tcptest/CMakeLists.txt" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/test/src/heapsort/main.c" beforeDir="false" afterPath="$PROJECT_DIR$/test/src/heapsort/main.c" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/test/src/tcpclient_test.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/test/src/tcptest/tcpclient_test.cpp" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/general/src/algorithm/ringbuffer.hpp" beforeDir="false" afterPath="$PROJECT_DIR$/general/src/algorithm/ringbuffer.hpp" afterDir="false" />
|
||||
</list>
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
|
@ -149,7 +146,7 @@
|
|||
<workItem from="1588731711427" duration="7486000" />
|
||||
<workItem from="1588756178008" duration="3364000" />
|
||||
<workItem from="1588988345219" duration="6326000" />
|
||||
<workItem from="1589364212338" duration="1825000" />
|
||||
<workItem from="1589364212338" duration="2663000" />
|
||||
</task>
|
||||
<servers />
|
||||
</component>
|
||||
|
|
|
@ -27,7 +27,38 @@ public:
|
|||
uint16_t WriteableCnt(){
|
||||
return mLen - this->ReadableCnt();
|
||||
}
|
||||
Error Write(const T *in,uint16_t len){
|
||||
int16_t Read(T *data, uint16_t count)
|
||||
{
|
||||
//assert(rb != NULL);
|
||||
//assert(data != NULL);
|
||||
if (mHead< mTail) // 头部小于尾部,正常情况下
|
||||
{
|
||||
int copy_sz = min(count, this->ReadableCnt());
|
||||
memcpy(data, mHead, copy_sz * sizeof(T));
|
||||
rb->rb_head += copy_sz * sizeof(T);
|
||||
return copy_sz;
|
||||
}
|
||||
else // 头部大于尾部
|
||||
{
|
||||
if (count < mLen -(mHead - mBuffer)) // (rb->rb_head - rb->rb_buff) 是从头部到数组物理结束的长度,如果小于说明不需要读取数组头部的,只需要copy一次
|
||||
{
|
||||
int copy_sz = count*sizeof(T);
|
||||
memcpy(data, rb->rb_head, copy_sz*sizeof(T));
|
||||
rb->rb_head += copy_sz;
|
||||
return copy_sz;
|
||||
}
|
||||
else
|
||||
{
|
||||
//反之需要copy两次,因为数组本身不是有环的
|
||||
int copy_sz = mLen - (mHead - mBuffer);
|
||||
memcpy(data, mHead, copy_sz * sizeof(T));
|
||||
mHead = mBuffer;
|
||||
copy_sz += Read( (char*)data + copy_sz*sizeof(T), count - copy_sz);
|
||||
return copy_sz;
|
||||
}
|
||||
}
|
||||
}
|
||||
int16_t Write(const T *in,uint16_t len){
|
||||
if (len >= WriteableCnt(rb))
|
||||
return ERROR_NO_ENOUGH_WRITE;
|
||||
if (mHead <= mTail) // 头部小于尾部
|
||||
|
@ -35,7 +66,7 @@ public:
|
|||
int tail_avail_sz = mLen - (mTail - this->mBuffer);
|
||||
if (len <= tail_avail_sz) // 是否需要循环到0开始写
|
||||
{
|
||||
memcpy(rb->rb_tail, in, len);
|
||||
memcpy(rb->rb_tail, in, len*sizeof(T));
|
||||
mTail += len;
|
||||
if (mTail == mBuffer + mLen) // 刚好相等的情况
|
||||
mTail = mBuffer;
|
||||
|
@ -43,14 +74,14 @@ public:
|
|||
}
|
||||
else
|
||||
{
|
||||
memcpy(mTail, in, tail_avail_sz);
|
||||
memcpy(mTail, in, tail_avail_sz*sizeof(T));
|
||||
mTail = rb->rb_buff;
|
||||
return tail_avail_sz + Write(rb, (char*)in + tail_avail_sz, len - tail_avail_sz);
|
||||
return tail_avail_sz + Write(rb, (char*)in + tail_avail_sz, (len - tail_avail_sz)*sizeof(T));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
memcpy(mTail, in, len); // 头部大于尾部
|
||||
memcpy(mTail, in, len*sizeof(T)); // 头部大于尾部
|
||||
mTail += len;
|
||||
return count;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue