c和c++的循环buffer

自已项目上一直用的循环buffer的源码,可用于中断。

c文件

#include "buffer.h"

#if C_CODE  //c code

void DataBufferInit(DataBuffer *intance)
{
    if(NULL == intance){
        return;
    }

    intance->last_recv_time = 0;
    intance->first = 0;
    intance->last = 0;
    intance->full = 0;
}

uint16_t BufferAvailable(DataBuffer *intance)
{
    if(NULL == intance){
        return 0;
    }

    uint16_t len = 0;
    if(intance->last > intance->first)
    {
        len = intance->last - intance->first;
    }else if(intance->last < intance->first)
    {
        len = (BUFFER_DAT_LEN - intance->first) + intance->last;
    }else if(intance->last == intance->first)
    {
        len = 0;
    }

    return len;
}

uint8_t BufferRead(DataBuffer *intance)
{
    if(NULL == intance){
        return 0;
    }

    uint8_t dat = 0;
    if( BufferAvailable(intance) )
    {
        dat = intance->dat[intance->first++];
        intance->first %= BUFFER_DAT_LEN;
    }

    return dat;
}

void BufferWrite(DataBuffer *intance ,uint8_t dat)
{
    if(NULL == intance){
        return;
    }

    if(BufferAvailable(intance) < BUFFER_DAT_LEN - 1)
    {
        intance->dat[intance->last++] = dat;
        intance->last %= BUFFER_DAT_LEN;
    }
    UpdateTime(intance ,curr_time_ms());
}

//有问题,没有判断剩余长度
void BufferWriteMulti(DataBuffer *intance ,uint8_t *dat , uint16_t len)
{
    if(NULL == intance || NULL == dat){
        return;
    }

    if(len >= BUFFER_DAT_LEN)
    {
        return;
    }

    for(uint16_t i = 0 ; i < len ; i++)
    {
        if(BufferAvailable(intance) < BUFFER_DAT_LEN)
        {
            intance->dat[intance->last++]=*dat++;
            intance->last%=BUFFER_DAT_LEN;
        }
    }
}

void BufferClear(DataBuffer *intance)
{
    if(NULL == intance){
        return;
    }

    intance->first = intance->last;
}

void UpdateTime(DataBuffer *intance ,uint64_t time_)
{
    if(NULL == intance){
        return;
    }

    intance->last_recv_time = time_;
}
#else //c++ code
uint32_t curr_time_ms();

uint16_t DataBuffer::BufferAvailable()
{
    uint16_t len = 0;
    if(intance.last > intance.first)
    {
        len = intance.last - intance.first;
    }else if(intance.last < intance.first)
    {
        len = (BUFFER_DAT_LEN - intance.first) + intance.last;
    }else if(intance.last == intance.first)
    {
        len = 0;
    }

    return len;
}

uint8_t DataBuffer::BufferRead()
{
    uint8_t dat = 0;
    if( BufferAvailable() )
    {
        dat = intance.dat[intance.first++];
        intance.first %= BUFFER_DAT_LEN;
    }

    return dat;
}

void DataBuffer::BufferWrite(uint8_t dat)
{
    if(BufferAvailable() < BUFFER_DAT_LEN - 1)
    {
        intance.dat[intance.last++] = dat;
        intance.last %= BUFFER_DAT_LEN;
    }
    UpdateTime(curr_time_ms());
}

//有问题,没有判断剩余长度
void DataBuffer::BufferWriteMulti(uint8_t *dat , uint16_t len)
{
    uint16_t i = 0;
    if(len >= BUFFER_DAT_LEN)
    {
        return;
    }

    for( ; i < len ; i++)
    {
        if(BufferAvailable() < BUFFER_DAT_LEN)
        {
            intance.dat[intance.last++]=*dat++;
            intance.last%=BUFFER_DAT_LEN;
        }
    }
}

void DataBuffer::BufferClear()
{
    intance.first = 0;
    intance.last = 0;
}

void DataBuffer::UpdateTime(uint64_t time_)
{
    intance.last_recv_time = time_;
}
#endif


h文件


#ifndef _BUFFER_H_
#define _BUFFER_H_
//循环buffer,可以用于中断,当缓冲区满了以后不会再写进buffer(不会冲掉之前的数据)

#include "main.h"

#define BUFFER_DAT_LEN	64
#define curr_time_ms GetSysTime
#define CPP_CODE 0
#define C_CODE !CPP_CODE

#if C_CODE //c code
typedef struct buffer_struct
{
    uint16_t last;   //写数据的位置
    uint16_t first;  //读数据的位置
    uint16_t dat[BUFFER_DAT_LEN];
    uint64_t last_recv_time;
    bool full;
}DataBuffer;

void DataBufferInit(DataBuffer *);
uint16_t BufferAvailable(DataBuffer *); //计算buffer长度不能用标志位,避免临界区
uint8_t BufferRead(DataBuffer *);
void BufferWrite(DataBuffer * ,uint8_t dat);
void BufferClear(DataBuffer *);
void BufferWriteMulti(DataBuffer * ,uint8_t *dat , uint16_t len);
void UpdateTime(DataBuffer *intance ,uint64_t time_);
#else //c++ code
class DataBuffer
{
    public:

    typedef struct buffer_struct
    {
        uint16_t last;   //写数据的位置
        uint16_t first;  //读数据的位置
        uint16_t dat[BUFFER_DAT_LEN];
        uint64_t last_recv_time;
        bool full;
        buffer_struct()
        {
            last_recv_time = 0;
            first = 0;
            last = 0;
            full = 0;
        }
    }BufferStruct;

    BufferStruct intance;

    uint16_t BufferAvailable(); //计算buffer长度不能用标志位,避免临界区
    uint8_t BufferRead();
    void BufferWrite(uint8_t dat);
    void BufferClear();
    void BufferWriteMulti(uint8_t *dat , uint16_t len);
    void UpdateTime(uint64_t time_);
};

#endif

#endif

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我可以帮你实现char buffer的两个队列循环。对于这个问题,我们可以使用两个循环队列来实现缓存的读写操作。 以下是一个简单的实现示例: ```c++ #include <iostream> #define MAX_SIZE 10 using namespace std; class CharBuffer { private: char data[MAX_SIZE]; int front1, rear1, front2, rear2; public: CharBuffer() { front1 = rear1 = front2 = rear2 = -1; } bool isEmpty1() { return front1 == -1 && rear1 == -1; } bool isFull1() { return (rear1+1)%MAX_SIZE == front1; } bool isEmpty2() { return front2 == -1 && rear2 == -1; } bool isFull2() { return (rear2+1)%MAX_SIZE == front2; } void enqueue1(char c) { if (isEmpty1()) { front1 = rear1 = 0; } else if (isFull1()) { cout << "Queue 1 is full!" << endl; return; } else { rear1 = (rear1+1) % MAX_SIZE; } data[rear1] = c; } char dequeue1() { if (isEmpty1()) { cout << "Queue 1 is empty!" << endl; return '\0'; } char c = data[front1]; if (front1 == rear1) { front1 = rear1 = -1; } else { front1 = (front1+1) % MAX_SIZE; } return c; } void enqueue2(char c) { if (isEmpty2()) { front2 = rear2 = 0; } else if (isFull2()) { cout << "Queue 2 is full!" << endl; return; } else { rear2 = (rear2+1) % MAX_SIZE; } data[rear2] = c; } char dequeue2() { if (isEmpty2()) { cout << "Queue 2 is empty!" << endl; return '\0'; } char c = data[front2]; if (front2 == rear2) { front2 = rear2 = -1; } else { front2 = (front2+1) % MAX_SIZE; } return c; } }; int main() { CharBuffer buffer; buffer.enqueue1('A'); buffer.enqueue1('B'); buffer.enqueue1('C'); buffer.enqueue2('1'); buffer.enqueue2('2'); buffer.enqueue2('3'); cout << "Dequeuing from Queue 1: " << buffer.dequeue1() << endl; cout << "Dequeuing from Queue 1: " << buffer.dequeue1() << endl; cout << "Dequeuing from Queue 2: " << buffer.dequeue2() << endl; cout << "Dequeuing from Queue 2: " << buffer.dequeue2() << endl; return 0; } ``` 在这个示例中,我们使用了一个CharBuffer类来实现char buffer的两个队列循环。CharBuffer类中包含了两个循环队列,分别用于存储数据。我们在enqueue1()和dequeue1()方法中实现了对Queue 1的读写操作,在enqueue2()和dequeue2()方法中实现了对Queue 2的读写操作。 在main()函数中,我们向Queue 1和Queue 2中添加了3个字符元素,然后依次从Queue 1和Queue 2中取出了两个元素。 希望这个示例可以帮助你实现char buffer的两个队列循环。如果你还有其他问题,请随时问我。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值