63cde222abaaa2a9bdaa9a143698dbc8b23bd742Timo Sirainen#ifndef AQUEUE_H
63cde222abaaa2a9bdaa9a143698dbc8b23bd742Timo Sirainen#define AQUEUE_H
63cde222abaaa2a9bdaa9a143698dbc8b23bd742Timo Sirainen
63cde222abaaa2a9bdaa9a143698dbc8b23bd742Timo Sirainen/* Dynamically growing queue. Use the array directly to access the data,
63cde222abaaa2a9bdaa9a143698dbc8b23bd742Timo Sirainen for example:
63cde222abaaa2a9bdaa9a143698dbc8b23bd742Timo Sirainen
63cde222abaaa2a9bdaa9a143698dbc8b23bd742Timo Sirainen count = queue_count(queue);
63cde222abaaa2a9bdaa9a143698dbc8b23bd742Timo Sirainen for (i = 0; i < count; i++) {
63cde222abaaa2a9bdaa9a143698dbc8b23bd742Timo Sirainen data = array[queue_idx(i)];
63cde222abaaa2a9bdaa9a143698dbc8b23bd742Timo Sirainen }
63cde222abaaa2a9bdaa9a143698dbc8b23bd742Timo Sirainen*/
63cde222abaaa2a9bdaa9a143698dbc8b23bd742Timo Sirainen
63cde222abaaa2a9bdaa9a143698dbc8b23bd742Timo Sirainenstruct aqueue {
63cde222abaaa2a9bdaa9a143698dbc8b23bd742Timo Sirainen struct array *arr;
63cde222abaaa2a9bdaa9a143698dbc8b23bd742Timo Sirainen unsigned int head, tail, area_size;
63cde222abaaa2a9bdaa9a143698dbc8b23bd742Timo Sirainen bool full;
63cde222abaaa2a9bdaa9a143698dbc8b23bd742Timo Sirainen};
63cde222abaaa2a9bdaa9a143698dbc8b23bd742Timo Sirainen
63cde222abaaa2a9bdaa9a143698dbc8b23bd742Timo Sirainenstruct aqueue *aqueue_init(struct array *array);
63cde222abaaa2a9bdaa9a143698dbc8b23bd742Timo Sirainenvoid aqueue_deinit(struct aqueue **aqueue);
63cde222abaaa2a9bdaa9a143698dbc8b23bd742Timo Sirainen
63cde222abaaa2a9bdaa9a143698dbc8b23bd742Timo Sirainen/* Append item to head */
63cde222abaaa2a9bdaa9a143698dbc8b23bd742Timo Sirainenvoid aqueue_append(struct aqueue *aqueue, const void *data);
63cde222abaaa2a9bdaa9a143698dbc8b23bd742Timo Sirainen/* Delete last item from tail */
63cde222abaaa2a9bdaa9a143698dbc8b23bd742Timo Sirainenvoid aqueue_delete_tail(struct aqueue *aqueue);
63cde222abaaa2a9bdaa9a143698dbc8b23bd742Timo Sirainen/* Remove item from n'th position */
63cde222abaaa2a9bdaa9a143698dbc8b23bd742Timo Sirainenvoid aqueue_delete(struct aqueue *aqueue, unsigned int n);
63cde222abaaa2a9bdaa9a143698dbc8b23bd742Timo Sirainen/* Clear the entire aqueue */
63cde222abaaa2a9bdaa9a143698dbc8b23bd742Timo Sirainenvoid aqueue_clear(struct aqueue *aqueue);
63cde222abaaa2a9bdaa9a143698dbc8b23bd742Timo Sirainen
63cde222abaaa2a9bdaa9a143698dbc8b23bd742Timo Sirainen/* Returns the number of items in aqueue. */
68a4946b12583b88fa802e52ebee45cd96056772Timo Sirainenunsigned int aqueue_count(const struct aqueue *aqueue) ATTR_PURE;
63cde222abaaa2a9bdaa9a143698dbc8b23bd742Timo Sirainen
63cde222abaaa2a9bdaa9a143698dbc8b23bd742Timo Sirainen/* Returns array index of n'th element in aqueue. */
68a4946b12583b88fa802e52ebee45cd96056772Timo Sirainenstatic inline unsigned int ATTR_PURE
63cde222abaaa2a9bdaa9a143698dbc8b23bd742Timo Sirainenaqueue_idx(const struct aqueue *aqueue, unsigned int n)
63cde222abaaa2a9bdaa9a143698dbc8b23bd742Timo Sirainen{
63cde222abaaa2a9bdaa9a143698dbc8b23bd742Timo Sirainen return (aqueue->tail + n) % aqueue->area_size;
63cde222abaaa2a9bdaa9a143698dbc8b23bd742Timo Sirainen}
63cde222abaaa2a9bdaa9a143698dbc8b23bd742Timo Sirainen
63cde222abaaa2a9bdaa9a143698dbc8b23bd742Timo Sirainen#endif