2021-10-10 14:39:17 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "../main.h"
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
void * data; // the data buffer
|
|
|
|
uintptr_t size; // size of data buffer
|
|
|
|
void * begin, *end; // begin and past-the-end for in-queue data
|
|
|
|
uintptr_t count; // number of in-queue bytes
|
2021-11-14 18:19:32 +08:00
|
|
|
} queue_Queue;
|
2021-10-10 14:39:17 +08:00
|
|
|
|
|
|
|
// initialize a queue with a existing buffer
|
2021-11-14 18:19:32 +08:00
|
|
|
void queue_InitBuffered(queue_Queue *q, void *buffer, uintptr_t size);
|
2021-10-10 14:39:17 +08:00
|
|
|
|
|
|
|
// writes one byte to the queue, discarding if full
|
2021-11-14 18:19:32 +08:00
|
|
|
void queue_PushByte(queue_Queue *q, const uint8_t b);
|
2021-10-10 14:39:17 +08:00
|
|
|
|
|
|
|
// pops one byte from the front of the queue, returning it
|
2021-11-14 18:19:32 +08:00
|
|
|
uint8_t queue_PopByte(queue_Queue *q);
|
2021-10-10 14:39:17 +08:00
|
|
|
|
2021-10-23 20:15:41 +08:00
|
|
|
// write Size bytes to the queue, none written if there is not space for all the bytes
|
2021-11-14 18:19:32 +08:00
|
|
|
void queue_Push(queue_Queue *q, const void *buffer, uintptr_t size);
|
2021-10-23 20:15:41 +08:00
|
|
|
|
|
|
|
// pops Size bytes from the queue, none popped if there are no enough data
|
|
|
|
// returns the number of bytes popped (either Size or 0)
|
2021-11-14 18:19:32 +08:00
|
|
|
uintptr_t queue_Pop(queue_Queue *q, void *buffer, uintptr_t size);
|
2021-10-23 20:15:41 +08:00
|
|
|
|
2021-10-10 14:39:17 +08:00
|
|
|
// return the byte at the front of the queue
|
2021-11-14 18:19:32 +08:00
|
|
|
uint8_t queue_FrontByte(queue_Queue *q);
|
2021-10-10 14:39:17 +08:00
|
|
|
|
|
|
|
// tells if the queue is empty
|
2021-11-14 18:19:32 +08:00
|
|
|
bool queue_Empty(queue_Queue *q);
|
2021-10-10 14:39:17 +08:00
|
|
|
|
|
|
|
// returns the number of bytes in the queue
|
2021-11-14 18:19:32 +08:00
|
|
|
uintptr_t queue_Size(queue_Queue *q);
|
2021-10-10 14:39:17 +08:00
|
|
|
|
|
|
|
// returns the empty space left at the end of the queue
|
2021-11-14 18:19:32 +08:00
|
|
|
uintptr_t queue_Space(queue_Queue *q);
|
2021-10-10 14:39:17 +08:00
|
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|