HW12/simple_circular_buffer.h

36 lines
1012 B
C
Raw Permalink Normal View History

2020-12-05 10:25:54 +09:00
#ifndef _SIMPLE_CIRCULAR_BUFFER_
#define _SIMPLE_CIRCULAR_BUFFER_
#include<stddef.h>
2020-12-14 19:57:12 +09:00
//queue type declare
2020-12-05 10:25:54 +09:00
#define queue_struct(queue_type,queue_size) struct{\
queue_type data [(queue_size)+1];\
size_t begin;\
size_t end;\
}
2020-12-14 19:57:12 +09:00
//queue's attribute
2020-12-05 10:25:54 +09:00
#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)))
2020-12-14 19:57:12 +09:00
//queue's methods
2020-12-05 10:25:54 +09:00
#define queue_init(queue) do{\
(queue)->begin = 0;\
(queue)->end = 0;\
}while(0)
2020-12-14 19:57:12 +09:00
//this do not check that queue is full.
2020-12-05 10:25:54 +09:00
#define enqueue(queue,element) do{ \
(queue)->data[(queue)->end] = (element);\
(queue)->end = ((queue)->end + 1) % (queue_size(queue) + 1);\
}while(0)
2020-12-14 19:57:12 +09:00
//this do not check that queue is empty.
2020-12-05 10:25:54 +09:00
#define dequeue(queue) \
(((queue)->begin = ((queue)->begin + 1) % (queue_size(queue) + 1)) ,\
(queue)->data[((queue)->begin + queue_size(queue)) % (queue_size(queue) + 1)])
#endif