ringbuiffer 修复满1024后无法take_back问题

master
SK-20220809YISZ\Administrator 2023-05-19 17:48:59 +08:00
parent ae51fee51d
commit 298480b9f1
3 changed files with 100 additions and 75 deletions

View File

@ -24,8 +24,8 @@ public:
protected: protected:
T *m_data; T *m_data;
T *m_data_end; T *m_data_end;
uint64_t m_size; uint64_t m_size; // 容量
uint32_t m_len; uint32_t m_len; // 数据长度
T *m_head; T *m_head;
}; };
@ -150,16 +150,18 @@ int RingBuffer<T>::TakeBack(T *data,uint64_t len){
T *tail = nullptr; T *tail = nullptr;
if(m_len == 0) if(m_len == 0)
return 0; return 0;
if((m_head + m_len) > m_data_end){
std::cout<< 1; if((m_data + (m_len - 1)) > m_data_end){
tail = m_head + m_len - m_size; 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{ }else{
tail = m_head + m_len - 1; tail = m_data + m_len - 1;
} }
int i = 0; int i = 0;
for(i = 0;i < len;i++) { for(i = 0;i < len;i++) {
// std::cout<<"take" <<i<<std::endl;
data[len - 1 - i] = (*tail) ; data[len - 1 - i] = (*tail) ;
if((tail == m_head)) { if((tail == m_head)) {
i++; i++;

View File

@ -15,7 +15,7 @@ link_directories("../../../obj/")
link_directories("./third/gtest/lib") link_directories("./third/gtest/lib")
link_libraries(generallib) link_libraries(generallib)
link_libraries(jsoncpp) # link_libraries(jsoncpp)
include_directories("./third/jsoncpp/include/json") include_directories("./third/jsoncpp/include/json")
include_directories("../../../obj/inc/") include_directories("../../../obj/inc/")

View File

@ -10,6 +10,7 @@
#include <vector> #include <vector>
#include "pattern/ringbuffer.hpp" #include "pattern/ringbuffer.hpp"
#include <tuple> #include <tuple>
#include <math.h>
using namespace std; using namespace std;
@ -24,84 +25,106 @@ void clearBuffer(int *dat,int len){
} }
void TestRingBuffer(){ void TestRingBuffer(){
int in[1024]; int in[1024];
int out[1024];
for(int i = 0;i < 1024;i ++){ for(int i = 0;i < 1024;i ++){
in[i] = i; in[i] = i;
} }
RingBuffer<int> x(1024); 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(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++){ for(uint32_t i = 0;i < x.Length();i++){
std::cout<<x.At(i) <<" "; std::cout<<x.At(i) <<" ";
} }
std::cout<<"\r\n----------------------------------"<<std::endl; std::cout<<"----------------------------------"<<"\r\n";
ret = x.TakeBack(in,20);
std::cout<<"take "<<ret<<"\r\n"<<std::endl; int ran2 = rand()%1024;
for(uint32_t i = 0;i < ret;i++){ x.TakeBack(out,ran2);
std::cout<<in[i] <<" "; std::cout<<"take "<<ran2<<" x.Length():" << x.Length() << " "<<std::endl;
}
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++){ for(uint32_t i = 0;i < x.Length();i++){
std::cout<<x.At(i) <<" "; std::cout<<x.At(i) <<" ";
} }
clearBuffer(in,1024); std::cout<<std::endl<<"----------------------------------"<<"\r\n";
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) <<" ";
} }
// 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); fflush(stdout);
getchar();
} }
int main() { int main() {
auto x = ReturnMoreData();
std::cout<<"x first is "<<std::get<0>(x) << std::get<1>(x)<<std::endl; // auto x = ReturnMoreData();
std::cout<<"main"<<std::endl; // std::cout<<"x first is "<<std::get<0>(x) << std::get<1>(x)<<std::endl;
// std::cout<<"main"<<std::endl;
TestRingBuffer(); TestRingBuffer();
} }