diff --git a/.vscode/settings.json b/.vscode/settings.json index 9f6bb22..1facd46 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -10,7 +10,8 @@ "thread": "cpp", "vector": "cpp", "xmemory": "cpp", - "xtree": "cpp" + "xtree": "cpp", + "ostream": "cpp" }, "cmake.buildDirectory": "${workspaceFolder}/build", "cmake.mingwSearchDirs": [ diff --git a/general/src/pattern/ringbuffer.hpp b/general/src/pattern/ringbuffer.hpp index bf9ef4a..9c74f97 100644 --- a/general/src/pattern/ringbuffer.hpp +++ b/general/src/pattern/ringbuffer.hpp @@ -1,13 +1,13 @@ #ifndef __RINGBUFFER__ #define __RINGBUFFER__ #include - - #include +#include + template class RingBuffer{ public: T At(uint64_t pos); - RingBuffer(uint64_t size); + RingBuffer(uint64_t size,bool ts); int Add(T *data,uint64_t len); int TakeBack(T *data,uint64_t len); int TakeFront(T *data,uint64_t len); @@ -22,15 +22,17 @@ public: ~RingBuffer(); protected: + bool m_threadsafe; T *m_data; T *m_data_end; uint64_t m_size; // 容量 uint32_t m_len; // 数据长度 - T *m_head; + T *m_head; // 数组头 + std::mutex m_mutex; // 互斥锁 }; template -RingBuffer::RingBuffer(uint64_t size) : +RingBuffer::RingBuffer(uint64_t size,bool ts) : m_data(nullptr), m_size(0), m_head(0) @@ -40,6 +42,7 @@ RingBuffer::RingBuffer(uint64_t size) : m_data_end = &m_data[size - 1]; m_head = m_data; m_len = 0; + m_threadsafe = ts; } template @@ -103,6 +106,8 @@ T RingBuffer::At(uint64_t pos){ if(p > m_data_end){ p = p - m_size; } + // printf("\r\n at %u: %d\r\n",p,*p); + return *p; } @@ -113,12 +118,20 @@ int RingBuffer::Add(T *data,uint64_t len){ } if(m_len == m_size) return 0; + int left = m_size - m_len; + if(len > left){ + len = left; + } + T *tail = nullptr; + // like 1023 0 1 2 or 0 1 2 3 if((m_head + m_len) > m_data_end){ tail = m_head + m_len - m_size; }else{ tail = m_head + m_len; } + printf("head %u len %u tail %u\r\n",m_head,len,tail); + int i = 0; for(i = 0;i < len;i++){ T* tmp = tail; @@ -129,12 +142,13 @@ int RingBuffer::Add(T *data,uint64_t len){ tmp = m_data; } if(tmp == m_head) { - (*tail) = data[i]; + printf("break %u %u\r\n",m_head,tmp); i++; break; } (*tail) = data[i]; tail = tmp; + // printf("add %u:%d\r\n",tail,data[i]); } m_len += i; return i; @@ -151,17 +165,16 @@ int RingBuffer::TakeBack(T *data,uint64_t len){ if(m_len == 0) return 0; - if((m_data + (m_len - 1)) > m_data_end){ - tail = m_data + (m_len - 1) - m_size; + if((m_head + (m_len - 1)) > m_data_end){ + tail = m_head + (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_data + m_len - 1; + tail = m_head + m_len - 1; } int i = 0; for(i = 0;i < len;i++) { - // std::cout<<"take" <::TakeFront(T *data,uint64_t len){ T *tail = nullptr; if(m_len == 0) return 0; - if((m_head + m_len) > m_data_end){ - tail = m_head + m_len - m_size; - }else{ - tail = m_head + m_len - 1; + if(len > m_len){ + len = m_len; } + int i = 0; for(i = 0;i < len;i++) { data[i] = (*m_head); diff --git a/test/src/cpp11/CMakeLists.txt b/test/src/cpp11/CMakeLists.txt index 2633bcc..b71abd6 100644 --- a/test/src/cpp11/CMakeLists.txt +++ b/test/src/cpp11/CMakeLists.txt @@ -21,6 +21,7 @@ include_directories("./third/jsoncpp/include/json") include_directories("../../../obj/inc/") include_directories("./third/gtest/include") +add_executable(ringbuffer ringbuffer_test.cpp ) add_executable(cpp11 cpp11_test.cpp ) add_executable(gtest gtest.cpp ) add_executable(thread_test thread_usage.cpp threadpool.cpp) diff --git a/test/src/cpp11/template.cpp b/test/src/cpp11/template.cpp index b0dbc9f..45ac575 100644 --- a/test/src/cpp11/template.cpp +++ b/test/src/cpp11/template.cpp @@ -23,6 +23,7 @@ void clearBuffer(int *dat,int len){ dat[i] = 0; } } + void TestRingBuffer(){ int in[1024]; int out[1024];