queue.h (1630B)
1 #pragma once 2 3 #include "types.h" 4 5 /* 6 * A FIFO-ish queue. 7 */ 8 typedef struct Queue { 9 10 // The current number of elements in the queue. 11 u32 length; 12 13 // The size in bytes of an element. 14 u32 stride; 15 16 // The size in bytes of a QueueItem. 17 u32 stride_padded; 18 19 // The maximum number of elements in the queue. 20 u32 capacity; 21 22 // The current element being accessed during iteration. 23 struct QueueItem *cursor; 24 25 // The front of the queue. 26 struct QueueItem *head; 27 28 // The end of the queue. 29 struct QueueItem *tail; 30 31 // Block of memory holding all QueueItems. 32 struct QueueItem *data; 33 34 } Queue; 35 36 /* 37 * Initialize a queue which can hold a maximum of `capacity` elements of 38 * `stride` bytes in size. 39 */ 40 b8 Queue_init(Queue *queue, u32 stride, u32 capacity); 41 42 /* 43 * Internal clean-up of a queue. 44 */ 45 b8 Queue_free(Queue *queue); 46 47 /* 48 * Add an element to the end of the queue. 49 */ 50 b8 Queue_enqueue(Queue *queue, const void *value); 51 52 /* 53 * Remove an element from the front of the queue. 54 */ 55 b8 Queue_dequeue(Queue *queue, void *value); 56 57 /* 58 * Retrieve the element at the front of the queue without removing it. 59 */ 60 b8 Queue_peek(const Queue *queue, void *value); 61 62 /* 63 * Iterate through the elements of a queue. Starts at the first element in the 64 * queue. Returns false when the end of the queue is reached. 65 */ 66 b8 Queue_peekNext(Queue *queue, void *value); 67 68 /* 69 * Called when iteration of a queue is stopped before reaching the end. 70 */ 71 b8 Queue_peekDone(Queue *queue); 72 73 /* 74 * Removes the current element of a queue during iteration. 75 */ 76 b8 Queue_yank(Queue *queue, void *value); 77