#ifndef __FIFO_H__ #define __FIFO_H__ #ifdef __cplusplus extern "C" { #endif #define FIFO_HEAD(name, type) \ struct name { \ struct type *fifo; \ int front, tail, capacity; \ } #define FIFO_INIT(head, _capacity) do { \ (head)->fifo = pvPortMalloc(sizeof(*(head)->fifo) * _capacity); \ (head)->front = (head)->tail = -1; \ (head)->capacity = _capacity; \ } while (0) #define FIFO_EMPTY(head) ((head)->front == -1) #define FIFO_FULL(head) (((head)->front == ((head)->tail + 1)) \ || (((head)->front == 0) && ((head)->tail == ((head)->capacity - 1)))) #define FIFO_CAPACITY(head) ((head)->capacity) #define FIFO_SIZE(head) (FIFO_EMPTY(head) ?\ 0 : ((((head)->tail + (head)->capacity - (head)->front) % (head)->capacity) + 1)) #define FIFO_PUSH(head, elm) do { \ if (FIFO_EMPTY(head)) \ (head)->front = (head)->tail = 0; \ else \ (head)->tail = ((head)->tail == (head)->capacity - 1) \ ? 0 : (head)->tail + 1; \ (head)->fifo[(head)->tail] = *elm; \ } while (0) #define FIFO_POP(head, pelm) do { \ *(pelm) = (head)->fifo[(head)->front]; \ if ((head)->front == (head)->tail) \ (head)->front = (head)->tail = -1; \ else \ (head)->front = ((head)->front == (head)->capacity - 1) \ ? 0 : (head)->front + 1; \ } while (0) #define FIFO_FOREACH(var, head, idx) \ for (idx = (head)->front, var = (head)->fifo[idx]; \ idx < (head)->front + FIFO_SIZE(head); \ idx = idx + 1, var = (head)->fifo[idx % (head)->capacity]) #define FIFO_GET_FRONT(head, pelm) (*(pelm) = (head)->fifo[(head)->front]) #define FIFO_GET_FRONT_PTR(head, pelm) ((pelm) = &(head)->fifo[(head)->front]) #define FIFO_GET_TAIL(head, pelm) (*(pelm) = (head)->fifo[(head)->tail]) #ifdef __cplusplus } #endif #endif /* __FIFO_H__ */