ringbuiffer 修复满1024后无法take_back问题
parent
ae51fee51d
commit
298480b9f1
|
@ -24,8 +24,8 @@ public:
|
|||
protected:
|
||||
T *m_data;
|
||||
T *m_data_end;
|
||||
uint64_t m_size;
|
||||
uint32_t m_len;
|
||||
uint64_t m_size; // 容量
|
||||
uint32_t m_len; // 数据长度
|
||||
T *m_head;
|
||||
};
|
||||
|
||||
|
@ -150,16 +150,18 @@ int RingBuffer<T>::TakeBack(T *data,uint64_t len){
|
|||
T *tail = nullptr;
|
||||
if(m_len == 0)
|
||||
return 0;
|
||||
if((m_head + m_len) > m_data_end){
|
||||
std::cout<< 1;
|
||||
tail = m_head + m_len - m_size;
|
||||
|
||||
if((m_data + (m_len - 1)) > m_data_end){
|
||||
tail = m_data + (m_len - 1) - m_size;
|
||||
printf("m_data_end:%u m_head + m_len:%u m_head: %u m_len:%ld tail: %u m_size: %ld m_data:%u\r\n",m_data_end,(m_head + m_len),m_head,m_len,tail,m_size,m_data);
|
||||
}else{
|
||||
tail = m_head + m_len - 1;
|
||||
tail = m_data + m_len - 1;
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
|
||||
for(i = 0;i < len;i++) {
|
||||
// std::cout<<"take" <<i<<std::endl;
|
||||
data[len - 1 - i] = (*tail) ;
|
||||
if((tail == m_head)) {
|
||||
i++;
|
||||
|
|
|
@ -15,7 +15,7 @@ link_directories("../../../obj/")
|
|||
link_directories("./third/gtest/lib")
|
||||
|
||||
link_libraries(generallib)
|
||||
link_libraries(jsoncpp)
|
||||
# link_libraries(jsoncpp)
|
||||
|
||||
include_directories("./third/jsoncpp/include/json")
|
||||
include_directories("../../../obj/inc/")
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
#include <vector>
|
||||
#include "pattern/ringbuffer.hpp"
|
||||
#include <tuple>
|
||||
#include <math.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
@ -24,84 +25,106 @@ void clearBuffer(int *dat,int len){
|
|||
}
|
||||
void TestRingBuffer(){
|
||||
int in[1024];
|
||||
int out[1024];
|
||||
|
||||
for(int i = 0;i < 1024;i ++){
|
||||
in[i] = i;
|
||||
}
|
||||
RingBuffer<int> x(1024);
|
||||
int ret = x.Add(in,512);
|
||||
std::cout<<"add "<<ret<<" x.Length():" << x.Length() << " "<<std::endl;
|
||||
for(uint32_t i = 0;i < x.Length();i++){
|
||||
std::cout<<x.At(i) <<" ";
|
||||
}
|
||||
std::cout<<"\r\n clear x----------------------------------"<<std::endl;
|
||||
x.SetEmpty();
|
||||
std::cout<<"\r\n ----------------------------------"<<std::endl;
|
||||
std::cout<<"add data :"<<std::endl;
|
||||
for(uint32_t i = 0;i < 516;i++){
|
||||
std::cout<<in[i] <<" ";
|
||||
}
|
||||
ret = x.Add(in,516);
|
||||
std::cout<<"after add "<<ret << " length: "<< x.Length()<<"\r\n"<<std::endl;
|
||||
for(uint32_t i = 0;i < x.Length();i++){
|
||||
std::cout<<x.At(i) <<" ";
|
||||
}
|
||||
std::cout<<"\r\n----------------------------------"<<std::endl;
|
||||
clearBuffer(in,1024);
|
||||
ret = x.Add(in,20);
|
||||
std::cout<<"add "<<ret<<"\r\n"<<std::endl;
|
||||
for(uint32_t i = 0;i < x.Length();i++){
|
||||
std::cout<<x.At(i) <<" ";
|
||||
}
|
||||
std::cout<<"\r\n----------------------------------"<<std::endl;
|
||||
clearBuffer(in,1024);
|
||||
|
||||
std::cout<<"before take x: "<< x.Length()<<std::endl;
|
||||
for(uint32_t i = 0;i < x.Length();i++){
|
||||
std::cout<<x.At(i) <<" ";
|
||||
}
|
||||
std::cout<<"\r\n----------------------------------"<<std::endl;
|
||||
ret = x.TakeBack(in,20);
|
||||
std::cout<<"take "<<ret<<"\r\n"<<std::endl;
|
||||
for(uint32_t i = 0;i < ret;i++){
|
||||
std::cout<<in[i] <<" ";
|
||||
}
|
||||
std::cout<<"\r\n----------------------------------"<<std::endl;
|
||||
std::cout<<"after take x: "<<x.Length()<<"\r\n"<<std::endl;
|
||||
for(uint32_t i = 0;i < x.Length();i++){
|
||||
std::cout<<x.At(i) <<" ";
|
||||
}
|
||||
clearBuffer(in,1024);
|
||||
std::cout<<"\r\n----------------------------------"<<std::endl;
|
||||
ret = x.TakeBack(in,20);
|
||||
std::cout<<"take "<<ret<<"\r\n"<<std::endl;
|
||||
for(uint32_t i = 0;i < ret;i++){
|
||||
std::cout<<in[i] <<" ";
|
||||
}
|
||||
std::cout<<"\r\n----------------------------------"<<std::endl;
|
||||
std::cout<<"after take x: "<<x.Length()<<"\r\n"<<std::endl;
|
||||
std::cout<<"----------------------------------"<<"\r\n";
|
||||
for(int z = 0;z < x.Length();z++){
|
||||
std::cout<<x.At(z) <<" ";
|
||||
}
|
||||
clearBuffer(in,1024);
|
||||
ret = x.TakeFront(in,496);
|
||||
std::cout<<"\r\ntake from front "<<ret<<"\r\n"<<std::endl;
|
||||
for(uint32_t i = 0;i < ret;i++){
|
||||
std::cout<<in[i] <<" ";
|
||||
}
|
||||
std::cout<<"\r\nafter take x: "<<x.Length()<<"\r\n"<<std::endl;
|
||||
std::cout<<"----------------------------------"<<"\r\n";
|
||||
for(int z = 0;z < x.Length();z++){
|
||||
std::cout<<x.At(z) <<" ";
|
||||
for(int i = 0;i < 10;i++){
|
||||
std::cout<<std::endl<<"----------------------------------"<<"\r\n";
|
||||
int ran = rand()%1024;
|
||||
int ret = x.Add(in,ran);
|
||||
std::cout<<"add "<<ran<<" x.Length():" << x.Length() << " "<<std::endl;
|
||||
for(uint32_t i = 0;i < x.Length();i++){
|
||||
std::cout<<x.At(i) <<" ";
|
||||
}
|
||||
std::cout<<"----------------------------------"<<"\r\n";
|
||||
|
||||
int ran2 = rand()%1024;
|
||||
x.TakeBack(out,ran2);
|
||||
std::cout<<"take "<<ran2<<" x.Length():" << x.Length() << " "<<std::endl;
|
||||
for(uint32_t i = 0;i < x.Length();i++){
|
||||
std::cout<<x.At(i) <<" ";
|
||||
}
|
||||
std::cout<<std::endl<<"----------------------------------"<<"\r\n";
|
||||
|
||||
}
|
||||
|
||||
// int ret = x.Add(in,512);
|
||||
// std::cout<<"add "<<ret<<" x.Length():" << x.Length() << " "<<std::endl;
|
||||
// for(uint32_t i = 0;i < x.Length();i++){
|
||||
// std::cout<<x.At(i) <<" ";
|
||||
// }
|
||||
// std::cout<<"\r\n clear x----------------------------------"<<std::endl;
|
||||
// x.SetEmpty();
|
||||
// std::cout<<"\r\n ----------------------------------"<<std::endl;
|
||||
// std::cout<<"add data :"<<std::endl;
|
||||
// for(uint32_t i = 0;i < 516;i++){
|
||||
// std::cout<<in[i] <<" ";
|
||||
// }
|
||||
// ret = x.Add(in,516);
|
||||
// std::cout<<"after add "<<ret << " length: "<< x.Length()<<"\r\n"<<std::endl;
|
||||
// for(uint32_t i = 0;i < x.Length();i++){
|
||||
// std::cout<<x.At(i) <<" ";
|
||||
// }
|
||||
// std::cout<<"\r\n----------------------------------"<<std::endl;
|
||||
// clearBuffer(in,1024);
|
||||
// ret = x.Add(in,20);
|
||||
// std::cout<<"add "<<ret<<"\r\n"<<std::endl;
|
||||
// for(uint32_t i = 0;i < x.Length();i++){
|
||||
// std::cout<<x.At(i) <<" ";
|
||||
// }
|
||||
// std::cout<<"\r\n----------------------------------"<<std::endl;
|
||||
// clearBuffer(in,1024);
|
||||
|
||||
// std::cout<<"before take x: "<< x.Length()<<std::endl;
|
||||
// for(uint32_t i = 0;i < x.Length();i++){
|
||||
// std::cout<<x.At(i) <<" ";
|
||||
// }
|
||||
// std::cout<<"\r\n----------------------------------"<<std::endl;
|
||||
// ret = x.TakeBack(in,20);
|
||||
// std::cout<<"take "<<ret<<"\r\n"<<std::endl;
|
||||
// for(uint32_t i = 0;i < ret;i++){
|
||||
// std::cout<<in[i] <<" ";
|
||||
// }
|
||||
// std::cout<<"\r\n----------------------------------"<<std::endl;
|
||||
// std::cout<<"after take x: "<<x.Length()<<"\r\n"<<std::endl;
|
||||
// for(uint32_t i = 0;i < x.Length();i++){
|
||||
// std::cout<<x.At(i) <<" ";
|
||||
// }
|
||||
// clearBuffer(in,1024);
|
||||
// std::cout<<"\r\n----------------------------------"<<std::endl;
|
||||
// ret = x.TakeBack(in,20);
|
||||
// std::cout<<"take "<<ret<<"\r\n"<<std::endl;
|
||||
// for(uint32_t i = 0;i < ret;i++){
|
||||
// std::cout<<in[i] <<" ";
|
||||
// }
|
||||
// std::cout<<"\r\n----------------------------------"<<std::endl;
|
||||
// std::cout<<"after take x: "<<x.Length()<<"\r\n"<<std::endl;
|
||||
// std::cout<<"----------------------------------"<<"\r\n";
|
||||
// for(int z = 0;z < x.Length();z++){
|
||||
// std::cout<<x.At(z) <<" ";
|
||||
// }
|
||||
// clearBuffer(in,1024);
|
||||
// ret = x.TakeFront(in,496);
|
||||
// std::cout<<"\r\ntake from front "<<ret<<"\r\n"<<std::endl;
|
||||
// for(uint32_t i = 0;i < ret;i++){
|
||||
// std::cout<<in[i] <<" ";
|
||||
// }
|
||||
// std::cout<<"\r\nafter take x: "<<x.Length()<<"\r\n"<<std::endl;
|
||||
// std::cout<<"----------------------------------"<<"\r\n";
|
||||
// for(int z = 0;z < x.Length();z++){
|
||||
// std::cout<<x.At(z) <<" ";
|
||||
// }
|
||||
fflush(stdout);
|
||||
getchar();
|
||||
}
|
||||
|
||||
int main() {
|
||||
auto x = ReturnMoreData();
|
||||
std::cout<<"x first is "<<std::get<0>(x) << std::get<1>(x)<<std::endl;
|
||||
std::cout<<"main"<<std::endl;
|
||||
|
||||
// auto x = ReturnMoreData();
|
||||
// std::cout<<"x first is "<<std::get<0>(x) << std::get<1>(x)<<std::endl;
|
||||
// std::cout<<"main"<<std::endl;
|
||||
TestRingBuffer();
|
||||
}
|
Loading…
Reference in New Issue