嵌入式可用的简易环形缓冲区

/*
 * 写和读的时候分别操作的是不同的指针,因此中断信号的写和程序读互相干扰的情况难发生
 */
//-------------ring buffer definitions------------
#define GPRS_RING_BUF_LEN 256

volatile uint8 gprsDatBuf [GPRS_RING_BUF_LEN];
uint16 gprsDatFirstIdx = 0;
uint16 gprsDatEndIdx = 0; //指向的是最后一个有效数据之后的那块内存

uint8 gprsDatLineBuf[256];
uint16 gprsDatLineLength = 0;

static uint16 inline bytes_in_use() {
    if (gprsDatEndIdx >= gprsDatFirstIdx) {
        return gprsDatEndIdx - gprsDatFirstIdx;
    } else {
        return (GPRS_RING_BUF_LEN - gprsDatFirstIdx) + gprsDatEndIdx;
    }
}
static boolean inline gprs_buf_append(uint8 c) {
    if (bytes_in_use() < GPRS_RING_BUF_LEN - 1) {//Always Keep One Slot Open。见维基百科。
        gprsDatEndIdx=(gprsDatEndIdx+1) % GPRS_RING_BUF_LEN;
        gprsDatBuf[gprsDatEndIdx] = c;
        return TRUE;
    }
    return FALSE;
}
static boolean inline gprs_buf_pop_head(uint8* c) {
    if (bytes_in_use() == 0) {
        return FALSE;
    }
    gprsDatFirstIdx = (gprsDatFirstIdx+1) % GPRS_RING_BUF_LEN;
    *c = gprsDatBuf[gprsDatFirstIdx];
    return TRUE;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值