#include "queue.h" queue q_new() { queue q = {0}; q.front = q.rear = Q_EMPTY_IDX; return q; } // check if q is empty bool q_is_empty(queue *q) { return q->front == Q_EMPTY_IDX; } // check if q is full bool q_is_full(queue *q) { return (q->rear + 1) % Q_MAX_SIZE == q->front; } // enqueue item to q // RETURNS Q_FULL if q is full // RETURNS Q_DONE if successful int q_enqueue(queue *q, void *item) { if (q_is_full(q)) return Q_FULL; if (q->front == Q_EMPTY_IDX) q->front = 0; q->rear = (q->rear + 1) % Q_MAX_SIZE; q->items[q->rear] = item; return Q_DONE; } // dequeue q // RETURNS NULL pointer if q is empty void *q_dequeue(queue *q) { if (q_is_empty(q)) return Q_NULL; void *item = q->items[q->front]; if (q->front == q->rear) q->front = q->rear = Q_EMPTY_IDX; else q->front = (q->front + 1) % Q_MAX_SIZE; return item; } // peek at q // RETURNS NULL pointer if q is empty void *q_peek(queue *q) { if (q_is_empty(q)) return Q_NULL; return q->items[q->front]; } #ifdef KK_DEBUG_ #include #include "draw.h" void q_print_type_enum(int e) { switch (e) { case DRAW: printf("DRAW"); break; case MOVEV: printf("MOVEV"); break; case MOVEH: printf("MOVEH"); break; case SETL: printf("SETL"); break; case SETX: printf("SETX"); break; case SWITCH: printf("SWITCH"); break; case AUTO_PAGING: printf("AUTO_PAGING"); break; case POPULATE_W: printf("POPULATE_W"); break; } printf(" "); } void q_debug(queue *q) { printf("[Q: "); if (q_is_empty(q)) goto end; int i = q->front; while (i != q->rear) { if (q->items[i] == NULL) { printf("NULL "); } else { int v = ((dtask *) q->items[i])->type; q_print_type_enum(v); } i = (i + 1) % Q_MAX_SIZE; } int v = ((dtask *) q->items[i])->type; q_print_type_enum(v); end: printf("]"); fflush(stdout); } #endif // KK_DEBUG_