diff --git a/src/queue.c b/src/queue.c index 520ddd8..6978389 100644 --- a/src/queue.c +++ b/src/queue.c @@ -4,24 +4,25 @@ #include "avclandrv.h" #include "queue.h" -void constructQueue(Queue_t *q, void *buf, uint8_t size, uint8_t len, - uint8_t constructFull) { +void constructQueue(Queue_t *q, void **slots, void *items, uint8_t itemSize, + uint8_t len, uint8_t constructFull) { q->read = 0; q->size = len; + q->buf = slots; if (!!constructFull) { q->write = len; for (uint8_t i = 0; i < len; ++i) { - q->buf[i] = buf; - buf = (char *)buf + size; + q->buf[i] = items; + items = (char *)items + itemSize; } } else { q->write = 0; } } -void constructEmptyQueue(Queue_t *q, uint8_t size, uint8_t len) { - constructQueue(q, NULL, size, len, 0); +void constructEmptyQueue(Queue_t *q, void **slots, uint8_t len) { + constructQueue(q, slots, NULL, 0, len, 0); } uint8_t isEmpty(const Queue_t *q) { return (q->write == q->read); } @@ -43,7 +44,7 @@ uint8_t pushQueue(Queue_t *q, void *x) { return 0; } -const void *peekQueue(const Queue_t *q) { +void *peekQueue(const Queue_t *q) { if (isEmpty(q)) return NULL; diff --git a/src/queue.h b/src/queue.h index 250bd91..0aa8ebf 100644 --- a/src/queue.h +++ b/src/queue.h @@ -5,14 +5,15 @@ typedef struct Queue_struct { uint8_t write; uint8_t read; - uint8_t size; - void *buf[]; + uint8_t size; // MUST be a power of 2 (qMask depends on it) + void **buf; // size entries, owned by caller } Queue_t; -void constructQueue(Queue_t *q, void *buf, uint8_t size, uint8_t len, - uint8_t constructFull); -void constructEmptyQueue(Queue_t *q, uint8_t size, uint8_t len); +void constructQueue(Queue_t *q, void **slots, void *items, uint8_t itemSize, + uint8_t len, uint8_t constructFull); +void constructEmptyQueue(Queue_t *q, void **slots, uint8_t len); uint8_t isEmpty(const Queue_t *q); +inline void incrementRead(Queue_t *q) { q->read++; }; uint8_t pushQueue(Queue_t *q, void *x); -const void *peekQueue(const Queue_t *q); +void *peekQueue(const Queue_t *q); void *popQueue(Queue_t *q); diff --git a/src/sniffer.c b/src/sniffer.c index 02612e8..44f8186 100644 --- a/src/sniffer.c +++ b/src/sniffer.c @@ -37,11 +37,18 @@ const char *const offon[] = {"OFF", "ON"}; #define CACHE_SIZE 16 +_Static_assert((CACHE_SIZE & (CACHE_SIZE - 1)) == 0, + "CACHE_SIZE must be a power of two (qMask depends on it)"); AVCLAN_frame_t frames[CACHE_SIZE]; RFrame_t responses[CACHE_SIZE]; uint8_t framesdata[CACHE_SIZE][MAXMSGLEN]; +void *cacheSlots[CACHE_SIZE]; +void *rcacheSlots[CACHE_SIZE]; +void *incomingSlots[CACHE_SIZE]; +void *outgoingSlots[CACHE_SIZE]; + Queue_t cache, rcache, incoming, outgoing; volatile uint8_t enqueueStatus = 0; @@ -107,11 +114,13 @@ int main() { frames[i].data = framesdata[i]; } - constructQueue(&cache, frames, sizeof(AVCLAN_frame_t), CACHE_SIZE, 1); - constructEmptyQueue(&incoming, sizeof(AVCLAN_frame_t), CACHE_SIZE); + constructQueue(&cache, cacheSlots, frames, sizeof(AVCLAN_frame_t), CACHE_SIZE, + 1); + constructEmptyQueue(&incoming, incomingSlots, CACHE_SIZE); - constructQueue(&rcache, responses, sizeof(RFrame_t), CACHE_SIZE, 1); - constructEmptyQueue(&outgoing, sizeof(RFrame_t), CACHE_SIZE); + constructQueue(&rcache, rcacheSlots, responses, sizeof(RFrame_t), CACHE_SIZE, + 1); + constructEmptyQueue(&outgoing, outgoingSlots, CACHE_SIZE); Setup(); print_help();