libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::DoubleBuffer Class Reference

双缓冲区管理类 / Double buffer manager class More...

#include <double_buffer.hpp>

Public Member Functions

 DoubleBuffer ()=default
 默认构造一个未初始化的双缓冲对象 Constructs one uninitialized double buffer object
 
 DoubleBuffer (const LibXR::RawData &raw_data)
 构造函数,使用连续内存构造两个缓冲区 Constructs a double buffer using one continuous memory block
 
void Init (const LibXR::RawData &raw_data)
 绑定连续 backing storage 并重置双缓冲状态 Binds continuous backing storage and resets double-buffer state
 
void Reset ()
 重置 active/pending 状态,但保留已绑定的 backing storage Resets active/pending state while keeping bound backing storage
 
uint8_t * ActiveBuffer () const
 获取当前正在使用的缓冲区指针 Returns the currently active buffer
 
uint8_t * PendingBuffer () const
 获取备用缓冲区的指针 Returns the pending (inactive) buffer
 
uint8_t * Buffer (int block) const
 获取指定编号缓冲区的指针 Returns the pointer of the specified block
 
size_t Size () const
 获取每个缓冲区的大小(单位:字节) Gets the size of each buffer in bytes
 
void Switch ()
 切换到备用缓冲区(若其有效) Switches to the pending buffer if it's valid
 
bool HasPending () const
 判断是否有待切换的缓冲区 Checks whether a pending buffer is ready
 
bool FillPending (const uint8_t *data, size_t len)
 向备用缓冲区写入数据(不可重入) Fills the pending buffer with data (non-reentrant)
 
bool FillActive (const uint8_t *data, size_t len)
 向当前使用的缓冲区直接写入数据 Fills the active buffer directly
 
void EnablePending ()
 手动启用 pending 状态 Manually sets the pending state to true
 
size_t GetPendingLength () const
 获取 pending 缓冲区中准备好的数据长度 Gets the size of valid data in pending buffer
 
size_t GetActiveLength () const
 获取当前活动缓冲区中准备好的数据长度 Gets the size of valid data in active buffer
 
void SetPendingLength (size_t length)
 设置备用缓冲区的数据长度 Sets the size of the pending buffer
 
void SetActiveLength (size_t length)
 设置当前活动缓冲区的数据长度 Sets the size of the active buffer
 
void FlipActiveBlock ()
 翻转当前活动缓冲区编号 Flips the current active block index
 
int ActiveBlock () const
 获取当前活动缓冲区编号 Returns the current active block index
 
void SetActiveBlock (bool block)
 设置当前活动缓冲区 Sets the active buffer
 

Private Attributes

uint8_t * buffer_ [2] = {nullptr, nullptr}
 双缓冲区指针 / Double buffer pointers
 
size_t size_ = 0
 单个缓冲区大小 / Size of each buffer
 
int active_ = 0
 当前活动缓冲区编号 / Index of active buffer
 
bool pending_valid_
 标记备用区是否准备好 / Whether pending buffer is ready
 
size_t active_len_ = 0
 当前活动缓冲区有效数据长度 / Length of active data
 
size_t pending_len_ = 0
 备用缓冲区有效数据长度 / Length of pending data
 

Detailed Description

双缓冲区管理类 / Double buffer manager class

该类用于在嵌入式场景中管理双缓冲传输结构,支持主动缓冲、备用缓冲切换与填充。 用于实现无缝 DMA 或 USB 数据流水线发送,提高吞吐效率。 This class provides double-buffer control for efficient pipelined transmission such as USB or DMA streaming.

Definition at line 21 of file double_buffer.hpp.

Constructor & Destructor Documentation

◆ DoubleBuffer()

DoubleBuffer::DoubleBuffer ( const LibXR::RawData & raw_data)
explicit

构造函数,使用连续内存构造两个缓冲区 Constructs a double buffer using one continuous memory block

Parameters
raw_data连续内存区,大小必须为两个缓冲区之和 / The raw memory to be split

Definition at line 10 of file double_buffer.cpp.

10{ Init(raw_data); }
void Init(const LibXR::RawData &raw_data)
绑定连续 backing storage 并重置双缓冲状态 Binds continuous backing storage and resets double-buffer state

Member Function Documentation

◆ ActiveBlock()

int LibXR::DoubleBuffer::ActiveBlock ( ) const
inline

获取当前活动缓冲区编号 Returns the current active block index

Definition at line 177 of file double_buffer.hpp.

177{ return active_; }
int active_
当前活动缓冲区编号 / Index of active buffer

◆ ActiveBuffer()

uint8_t * DoubleBuffer::ActiveBuffer ( ) const

获取当前正在使用的缓冲区指针 Returns the currently active buffer

Returns
指向活动缓冲区的指针 / Pointer to the active buffer

Definition at line 47 of file double_buffer.cpp.

47{ return buffer_[active_]; }
uint8_t * buffer_[2]
双缓冲区指针 / Double buffer pointers

◆ Buffer()

uint8_t * DoubleBuffer::Buffer ( int block) const

获取指定编号缓冲区的指针 Returns the pointer of the specified block

Parameters
block缓冲区编号,只允许 0 或 1 / Buffer block index, only 0 or 1
Returns
指向指定缓冲区的指针 / Pointer to the requested buffer block

Definition at line 57 of file double_buffer.cpp.

58{
59 ASSERT((block == 0) || (block == 1));
60 return buffer_[block];
61}

◆ EnablePending()

void DoubleBuffer::EnablePending ( )

手动启用 pending 状态 Manually sets the pending state to true

通常与 FillActive() 配合使用,表示下次发送使用 FillActive 写入的缓冲。

Definition at line 115 of file double_buffer.cpp.

115{ pending_valid_ = true; }
bool pending_valid_
标记备用区是否准备好 / Whether pending buffer is ready

◆ FillActive()

bool DoubleBuffer::FillActive ( const uint8_t * data,
size_t len )

向当前使用的缓冲区直接写入数据 Fills the active buffer directly

Parameters
data数据源指针 / Source data pointer
len数据长度 / Length to write
Returns
成功返回 true,失败(超出大小)返回 false True on success, false if length exceeds buffer size

Definition at line 102 of file double_buffer.cpp.

103{
104 if (len > size_)
105 {
106 return false;
107 }
108
110 return true;
111}
size_t size_
单个缓冲区大小 / Size of each buffer
uint8_t * ActiveBuffer() const
获取当前正在使用的缓冲区指针 Returns the currently active buffer
static void FastCopy(void *dst, const void *src, size_t size)
快速内存拷贝 / Fast memory copy

◆ FillPending()

bool DoubleBuffer::FillPending ( const uint8_t * data,
size_t len )

向备用缓冲区写入数据(不可重入) Fills the pending buffer with data (non-reentrant)

Parameters
data数据源指针 / Pointer to the source data
len数据长度(字节) / Data length in bytes
Returns
写入成功返回 true,失败(如已占用或溢出)返回 false Returns true on success, false if buffer already pending or overflow

Definition at line 86 of file double_buffer.cpp.

87{
88 if (pending_valid_ || len > size_)
89 {
90 return false;
91 }
92
94 pending_len_ = len;
95 pending_valid_ = true;
96 return true;
97}
size_t pending_len_
备用缓冲区有效数据长度 / Length of pending data
uint8_t * PendingBuffer() const
获取备用缓冲区的指针 Returns the pending (inactive) buffer

◆ FlipActiveBlock()

void LibXR::DoubleBuffer::FlipActiveBlock ( )
inline

翻转当前活动缓冲区编号 Flips the current active block index

Definition at line 171 of file double_buffer.hpp.

171{ active_ ^= 1; }

◆ GetActiveLength()

size_t LibXR::DoubleBuffer::GetActiveLength ( ) const
inline

获取当前活动缓冲区中准备好的数据长度 Gets the size of valid data in active buffer

Returns
准备好的长度 / Valid active buffer data length

Definition at line 149 of file double_buffer.hpp.

149{ return active_len_; }
size_t active_len_
当前活动缓冲区有效数据长度 / Length of active data

◆ GetPendingLength()

size_t DoubleBuffer::GetPendingLength ( ) const

获取 pending 缓冲区中准备好的数据长度 Gets the size of valid data in pending buffer

Returns
准备好的长度 / Valid pending buffer data length

Definition at line 120 of file double_buffer.cpp.

121{
122 return pending_valid_ ? pending_len_ : 0;
123}

◆ HasPending()

bool DoubleBuffer::HasPending ( ) const

判断是否有待切换的缓冲区 Checks whether a pending buffer is ready

Returns
若有数据待切换返回 true,否则返回 false / True if pending buffer is valid

Definition at line 81 of file double_buffer.cpp.

81{ return pending_valid_; }

◆ Init()

void DoubleBuffer::Init ( const LibXR::RawData & raw_data)

绑定连续 backing storage 并重置双缓冲状态 Binds continuous backing storage and resets double-buffer state

Parameters
raw_data连续内存区,大小必须满足双缓冲对半切分约束;空双缓冲允许 传入 nullptr + 0
raw_dataContinuous memory block satisfying the half-split contract; an empty double buffer may be initialized with nullptr + 0

Definition at line 25 of file double_buffer.cpp.

26{
27 constexpr size_t ALIGN = alignof(size_t);
28 const bool EMPTY_STORAGE = (raw_data.addr_ == nullptr) && (raw_data.size_ == 0U);
29
30 ASSERT(EMPTY_STORAGE || (raw_data.addr_ != nullptr));
31
32 if (!EMPTY_STORAGE)
33 {
34 ASSERT(raw_data.size_ > 0U);
35 ASSERT((reinterpret_cast<uintptr_t>(raw_data.addr_) % ALIGN) == 0U);
36 ASSERT((raw_data.size_ % (2U * ALIGN)) == 0U);
37 }
38
39 size_ = raw_data.size_ / 2U;
40 buffer_[0] = static_cast<uint8_t*>(raw_data.addr_);
41 buffer_[1] = EMPTY_STORAGE ? nullptr : (static_cast<uint8_t*>(raw_data.addr_) + size_);
42 Reset();
43}
void Reset()
重置 active/pending 状态,但保留已绑定的 backing storage Resets active/pending state while keeping bound backing ...
size_t size_
数据字节数 / Data size in bytes
void * addr_
数据起始地址 / Data start address

◆ PendingBuffer()

uint8_t * DoubleBuffer::PendingBuffer ( ) const

获取备用缓冲区的指针 Returns the pending (inactive) buffer

Returns
指向备用缓冲区的指针 / Pointer to the pending buffer

Definition at line 52 of file double_buffer.cpp.

52{ return buffer_[1 - active_]; }

◆ Reset()

void DoubleBuffer::Reset ( )

重置 active/pending 状态,但保留已绑定的 backing storage Resets active/pending state while keeping bound backing storage

Definition at line 14 of file double_buffer.cpp.

15{
16 active_ = 0;
17 pending_valid_ = false;
18 active_len_ = 0;
19 pending_len_ = 0;
20}

◆ SetActiveBlock()

void LibXR::DoubleBuffer::SetActiveBlock ( bool block)
inline

设置当前活动缓冲区 Sets the active buffer

Parameters
blocktrue 表示使用第二个缓冲区,false 表示使用第一个缓冲区

Definition at line 185 of file double_buffer.hpp.

185{ active_ = block ? 1 : 0; }

◆ SetActiveLength()

void LibXR::DoubleBuffer::SetActiveLength ( size_t length)
inline

设置当前活动缓冲区的数据长度 Sets the size of the active buffer

Parameters
length数据长度(字节) / Data length in bytes

Definition at line 165 of file double_buffer.hpp.

165{ active_len_ = length; }

◆ SetPendingLength()

void LibXR::DoubleBuffer::SetPendingLength ( size_t length)
inline

设置备用缓冲区的数据长度 Sets the size of the pending buffer

Parameters
length数据长度(字节) / Data length in bytes

Definition at line 157 of file double_buffer.hpp.

157{ pending_len_ = length; }

◆ Size()

size_t DoubleBuffer::Size ( ) const

获取每个缓冲区的大小(单位:字节) Gets the size of each buffer in bytes

Returns
缓冲区大小 / Size of each buffer

Definition at line 65 of file double_buffer.cpp.

65{ return size_; }

◆ Switch()

void DoubleBuffer::Switch ( )

切换到备用缓冲区(若其有效) Switches to the pending buffer if it's valid

Note
如果 pending 缓冲区未准备好,则不执行切换。 No switch is performed if no pending data is ready.

Definition at line 70 of file double_buffer.cpp.

71{
73 {
74 active_ ^= 1;
75 pending_valid_ = false;
76 }
77}

Field Documentation

◆ active_

int LibXR::DoubleBuffer::active_ = 0
private

当前活动缓冲区编号 / Index of active buffer

Definition at line 190 of file double_buffer.hpp.

◆ active_len_

size_t LibXR::DoubleBuffer::active_len_ = 0
private

当前活动缓冲区有效数据长度 / Length of active data

Definition at line 193 of file double_buffer.hpp.

◆ buffer_

uint8_t* LibXR::DoubleBuffer::buffer_[2] = {nullptr, nullptr}
private

双缓冲区指针 / Double buffer pointers

Definition at line 188 of file double_buffer.hpp.

188{nullptr, nullptr};

◆ pending_len_

size_t LibXR::DoubleBuffer::pending_len_ = 0
private

备用缓冲区有效数据长度 / Length of pending data

Definition at line 194 of file double_buffer.hpp.

◆ pending_valid_

bool LibXR::DoubleBuffer::pending_valid_
private
Initial value:
=
false

标记备用区是否准备好 / Whether pending buffer is ready

Definition at line 191 of file double_buffer.hpp.

◆ size_

size_t LibXR::DoubleBuffer::size_ = 0
private

单个缓冲区大小 / Size of each buffer

Definition at line 189 of file double_buffer.hpp.


The documentation for this class was generated from the following files: