forked from shajen/rtl-sdr-scanner-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
shajen
committed
Jan 6, 2023
1 parent
a6f7b1a
commit d1e4da8
Showing
5 changed files
with
200 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,70 @@ | ||
#include "ring_buffer.h" | ||
|
||
#include <logger.h> | ||
|
||
#include <cstring> | ||
|
||
constexpr auto PRINT_DEBUG_INTERVAL = 100; | ||
|
||
RingBuffer::RingBuffer(uint32_t bufferSize) : m_bufferSize(bufferSize), m_writePosition(0), m_readPosition(0), m_pushDataSize(0), m_popDataSize(0), m_buffer(bufferSize) { | ||
Logger::info("RingBuffer", "init, buffer size: {}", m_bufferSize); | ||
} | ||
|
||
void RingBuffer::clear() { | ||
m_writePosition = 0; | ||
m_readPosition = 0; | ||
} | ||
|
||
uint32_t RingBuffer::availableDataSize() const { | ||
if (m_readPosition <= m_writePosition) { | ||
return m_writePosition - m_readPosition; | ||
} else { | ||
return m_bufferSize - (m_readPosition - m_writePosition); | ||
} | ||
} | ||
|
||
uint32_t RingBuffer::availableSpaceSize() const { return m_bufferSize - availableDataSize(); } | ||
|
||
void RingBuffer::push(uint8_t *data, uint32_t size) { | ||
m_pushDataSize = size; | ||
|
||
bool overflow = false; | ||
if (availableSpaceSize() < size) { | ||
Logger::warn("RingBuffer", "overflow"); | ||
overflow = true; | ||
} | ||
if (m_writePosition size < m_bufferSize) { | ||
memcpy(m_buffer.data() m_writePosition, data, size); | ||
m_writePosition = size; | ||
} else { | ||
const auto endSize = m_bufferSize - m_writePosition; | ||
memcpy(m_buffer.data() m_writePosition, data, endSize); | ||
memcpy(m_buffer.data(), data endSize, size - endSize); | ||
m_writePosition = (m_writePosition size) % m_bufferSize; | ||
} | ||
if (overflow) { | ||
m_readPosition = m_writePosition.load(); | ||
} | ||
} | ||
|
||
std::vector<uint8_t> RingBuffer::pop(uint32_t size) { | ||
m_popDataSize = size; | ||
m_popCount ; | ||
|
||
if (m_popCount % PRINT_DEBUG_INTERVAL == 0) { | ||
const auto ratio = static_cast<float>(m_popDataSize) / static_cast<float>(m_pushDataSize); | ||
Logger::info("RingBuffer", "pop/push: {}/{} ({:.2f})", m_popDataSize, m_pushDataSize, ratio); | ||
} | ||
|
||
std::vector<uint8_t> data(size); | ||
if (m_readPosition size < m_bufferSize) { | ||
memcpy(data.data(), m_buffer.data() m_readPosition, size); | ||
m_readPosition = size; | ||
} else { | ||
const auto endSize = m_bufferSize - m_readPosition; | ||
memcpy(data.data(), m_buffer.data() m_readPosition, endSize); | ||
memcpy(data.data() endSize, m_buffer.data(), size - endSize); | ||
m_readPosition = (m_readPosition size) % m_bufferSize; | ||
} | ||
return data; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,25 @@ | ||
#pragma once | ||
|
||
#include <atomic> | ||
#include <cstdint> | ||
#include <vector> | ||
|
||
class RingBuffer { | ||
public: | ||
RingBuffer(uint32_t bufferSize); | ||
|
||
void clear(); | ||
uint32_t availableDataSize() const; | ||
uint32_t availableSpaceSize() const; | ||
void push(uint8_t* data, uint32_t size); | ||
std::vector<uint8_t> pop(uint32_t size); | ||
|
||
private: | ||
const uint32_t m_bufferSize; | ||
std::atomic_uint32_t m_writePosition; | ||
std::atomic_uint32_t m_readPosition; | ||
uint64_t m_pushDataSize; | ||
uint64_t m_popDataSize; | ||
std::vector<uint8_t> m_buffer; | ||
uint32_t m_popCount; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,65 @@ | ||
#include <gtest/gtest.h> | ||
#include <ring_buffer.h> | ||
|
||
TEST(RingBufferTest, Empty) { | ||
RingBuffer buffer(100); | ||
std::vector<uint8_t> tmp(20); | ||
|
||
EXPECT_EQ(buffer.availableDataSize(), 0); | ||
EXPECT_EQ(buffer.availableSpaceSize(), 100); | ||
|
||
buffer.push(tmp.data(), tmp.size()); | ||
EXPECT_EQ(buffer.availableDataSize(), 20); | ||
EXPECT_EQ(buffer.availableSpaceSize(), 80); | ||
|
||
buffer.clear(); | ||
EXPECT_EQ(buffer.availableDataSize(), 0); | ||
EXPECT_EQ(buffer.availableSpaceSize(), 100); | ||
} | ||
|
||
TEST(RingBufferTest, Overflow) { | ||
constexpr auto SIZE = 31; | ||
constexpr auto ITERATION = 83; | ||
|
||
RingBuffer buffer(ITERATION * SIZE - 1); | ||
std::vector<uint8_t> tmp(SIZE); | ||
for (int i = 1; i < ITERATION; i) { | ||
buffer.push(tmp.data(), tmp.size()); | ||
EXPECT_EQ(buffer.availableDataSize(), i * SIZE); | ||
} | ||
for (int i = 0; i < ITERATION / 2; i) { | ||
buffer.pop(SIZE); | ||
buffer.push(tmp.data(), tmp.size()); | ||
} | ||
EXPECT_EQ(buffer.availableSpaceSize(), SIZE - 1); | ||
buffer.push(tmp.data(), tmp.size()); | ||
EXPECT_EQ(buffer.availableDataSize(), 0); | ||
} | ||
|
||
TEST(RingBufferTest, Round) { | ||
std::vector<uint8_t> push; | ||
std::vector<uint8_t> poped; | ||
|
||
constexpr auto PUSH_SIZE = 1229; | ||
constexpr auto POP_SIZE = 1231; | ||
constexpr auto BUFFER_SIZE = 1277 * 10; | ||
|
||
for (int i = 0; i < 100 * PUSH_SIZE * POP_SIZE; i) { | ||
push.push_back(rand()); | ||
} | ||
|
||
RingBuffer buffer(BUFFER_SIZE); | ||
uint32_t pushed = 0; | ||
while (poped.size() < push.size()) { | ||
while (buffer.availableDataSize() < POP_SIZE && pushed < push.size()) { | ||
buffer.push(push.data() pushed, PUSH_SIZE); | ||
pushed = PUSH_SIZE; | ||
} | ||
while (POP_SIZE <= buffer.availableDataSize()) { | ||
const auto tmp = buffer.pop(POP_SIZE); | ||
std::copy(tmp.begin(), tmp.end(), std::back_inserter(poped)); | ||
} | ||
} | ||
|
||
EXPECT_EQ(push, poped); | ||
} |