分享一个ringbuffer C程序
ringbuffer.h
- #ifndef __RINGBUFFER_H__
- #define __RINGBUFFER_H__
- #include <stdint.h>
- typedef struct RingBuffer_s {
- char * Head;
- char * Tail;
- char * Start;
- char * End;
- } RingBuffer_t;
- uint8_t rb_Get(RingBuffer_t *rb, char * c);
- uint8_t rb_Put(RingBuffer_t *rb, char c);
- void rb_Init(RingBuffer_t *rb, char * buffer, int size);
- #endif
复制代码
ringbuffer.c
- #include "ring_buffer.h"
- uint8_t rb_Put(RingBuffer_t *rb, char c)
- {
- char *next = rb->Head + 1;
- if (next == rb->End)
- next = rb->Start;
- if (next == rb->Tail)
- return 0;
- *rb->Head = c;
- rb->Head = next;
- return 1;
- }
- uint8_t rb_Get(RingBuffer_t *rb, char * c)
- {
- char *next;
- if (rb->Tail == rb->Head) // No data in ring
- {
- *c = 0;
- return 0;
- }
- *c = *rb->Tail; // Get data and move to next
- next = rb->Tail + 1;
- if (next == rb->End) // Check for ring
- next = rb->Start;
- rb->Tail = next;
- return 1;
- }
- void rb_Init(RingBuffer_t *rb, char * buffer, int size)
- {
- rb->Tail = rb->Head = rb->Start = buffer;
- rb->End = buffer + size;
- }
复制代码
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
方便接收后处理数据内容
一周热门 更多>