1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- #include "DataHelper.h"
- /**
- * 初始化一个先入先出缓冲
- *
- * @author lxz (041620 15:15:44)
- *
- * @param fifo 缓冲对象
- * @param buffer 数据缓冲
- * @param total 总数据大小
- */
- void dh_fifo_init(fifo_buffer_t *fifo, int total) {
- fifo->total = total;
- fifo->count = 0;
- fifo->head = 0;
- fifo->tail = 0;
- }
- /**
- * 缓冲保存一个数据
- *
- * @author lxz (041620 15:16:14)
- *
- * @param fifo 缓冲对象
- * @param dat 缓冲数据
- */
- int dh_fifo_push(fifo_buffer_t * fifo, unsigned char *dat ,unsigned int len) {
- if (fifo->count + 1 < fifo->total) {
- DH_ENTER_CRITCAL();
- // 分配内存并复制数据
- unsigned char *dataCopy = (unsigned char *)malloc(len);
- for (int i = 0; i < len; i++) {
- dataCopy[i] = dat[i];
- }
- fifo->buffer[fifo->head] = dataCopy;
- if (++fifo->head >= fifo->total) {
- fifo->head = 0;
- }
- fifo->count++;
- DH_EXIT_CRITCAL();
- return 1;
- }
- return 0;
- }
- /**
- *
- *
- * @author lxz (041620 15:16:43)
- *
- * @param fifo
- * @param dat
- *
- * @return int
- */
- int dh_fifo_pop(fifo_buffer_t *fifo, unsigned char *dat) {
- if (fifo->count > 0) {
- DH_ENTER_CRITCAL();
- memcpy(dat,fifo->buffer[fifo->tail],sizeof(fifo->buffer[fifo->tail]));
- free(fifo->buffer[fifo->tail]);
- if (++fifo->tail >= fifo->total) {
- fifo->tail = 0;
- }
- fifo->count--;
- DH_EXIT_CRITCAL();
- return (sizeof(fifo->buffer[fifo->tail]));
- }
- return 0;
- }
|