HW12/simple_circular_buffer.h
2020-12-05 01:30:30 +00:00

30 lines
893 B
C

#ifndef _SIMPLE_CIRCULAR_BUFFER_
#define _SIMPLE_CIRCULAR_BUFFER_
#include<stddef.h>
#define queue_struct(queue_type,queue_size) struct{\
queue_type data [(queue_size)+1];\
size_t begin;\
size_t end;\
}
#define queue_size(queue) ((sizeof((queue)->data)/sizeof((queue)->data[0])) - 1)
#define queue_isempty(queue) ((queue)->begin == (queue)->end)
#define queue_isfull(queue) ((queue)->begin == (((queue)->end + 1) % queue_size(queue)))
#define queue_init(queue) do{\
(queue)->begin = 0;\
(queue)->end = 0;\
}while(0)
//unchecked
#define enqueue(queue,element) do{ \
(queue)->data[(queue)->end] = (element);\
(queue)->end = ((queue)->end + 1) % (queue_size(queue) + 1);\
}while(0)
//unchecked
#define dequeue(queue) \
(((queue)->begin = ((queue)->begin + 1) % (queue_size(queue) + 1)) ,\
(queue)->data[((queue)->begin + queue_size(queue)) % (queue_size(queue) + 1)])
#endif