Plug in variable size queue implementation for saving buffer space in iAP.

This commit is contained in:
Russell
2011-12-06 17:12:44 +00:00
parent f979c80dbd
commit 1e1cb9288d
3 changed files with 49 additions and 10 deletions

View File

@@ -1,22 +1,49 @@
#include <stdio.h>
#include "queue.h"
// Queue implementation
// Offers no protection against adding when full or dequeueing when empty.
// Uses read and write counts for pointers to distinguish full and empty cases.
// Works from c and xc
// Must allocate the memory outside of this and pass it in to init_queue so can statically allocate
// Must work for different element sizes
// This presumes that the xc compiler will not re-use the mem passed to init_queue
void init_queue(queue *q, unsigned arr[], int size) {
void init_queue(queue *q, unsigned char arr[], int size, int element_size) {
q->rdptr = 0;
q->wrptr = 0;
q->data = (uintptr_t)arr;
q->size = size; // presume that size is power of two
q->size = size; // in items, presume that size is power of two
q->element_size = element_size; // The size of each element in bytes
q->mask = size - 1;
}
extern inline void enqueue(queue *q, unsigned value) {
((unsigned *)q->data)[q->wrptr & q->mask] = value;
switch (q->element_size) {
case 4:
((unsigned *)q->data)[q->wrptr & q->mask] = value;
break;
case 1:
((unsigned char *)q->data)[q->wrptr & q->mask] = (unsigned char)value;
break;
default:
break;
}
q->wrptr++;
}
extern inline unsigned dequeue(queue *q) {
unsigned retval = ((unsigned *)q->data)[q->rdptr & q->mask];
unsigned retval;
switch (q->element_size) {
case 4:
retval = ((unsigned *)q->data)[q->rdptr & q->mask];
break;
case 1:
retval = ((unsigned char *)q->data)[q->rdptr & q->mask];
break;
default:
break;
}
q->rdptr++;
return retval;
}
@@ -41,7 +68,17 @@ extern inline int space(queue *q) {
void dump(queue *q) {
for (int i = q->rdptr; i != q->wrptr; i++) {
printf("a[%d] = %d\n", i & q->mask, ((unsigned *)q->data)[i & q->mask]);
switch (q->element_size) {
case 4:
printf("a[%d] = %d\n", i & q->mask, ((unsigned *)q->data)[i & q->mask]);
break;
case 1:
printf("a[%d] = %d\n", i & q->mask, ((unsigned char *)q->data)[i & q->mask]);
break;
default:
break;
}
}
}