Skip to content
Snippets Groups Projects
Commit ced15288 authored by bbartels's avatar bbartels
Browse files

quad: fix queue implementation so usage meets expectations

parent 5e430fa0
No related branches found
No related tags found
No related merge requests found
#include "queue.h"
int queue_init(struct Queue *q, volatile unsigned char *buff, unsigned int max_size) {
int queue_init(struct Queue *q, volatile unsigned char *buff, unsigned int max_size_plus1) {
q->buff = buff;
q->max_size = max_size + 1;
q->start = max_size;
q->max_size_plus1 = max_size_plus1;
q->start = max_size_plus1 - 1;
q->end = 0;
return 0;
}
......@@ -13,7 +13,7 @@ struct Queue * queue_malloc(unsigned int max_size) {
if (buff == NULL) return NULL;
struct Queue *q = malloc(sizeof(struct Queue));
if (q == NULL) return NULL;
queue_init(q, buff, max_size);
queue_init(q, buff, max_size + 1);
return q;
}
......@@ -28,7 +28,7 @@ int queue_add(struct Queue *q, unsigned char c) {
}
q->buff[q->end] = c;
q->end += 1;
q->end %= q->max_size;
q->end %= q->max_size_plus1;
return 0;
}
......@@ -37,17 +37,17 @@ int queue_remove(struct Queue *q, unsigned char *c) {
return -1;
}
q->start += 1;
q->start %= q->max_size;
q->start %= q->max_size_plus1;
*c = q->buff[q->start];
return 0;
}
int queue_size(struct Queue *q) {
return (q->max_size + q->end - q->start - 1) % q->max_size;
return (q->max_size_plus1 + q->end - q->start - 1) % q->max_size_plus1;
}
int queue_full(struct Queue *q) {
return q->start == q->end;;
return q->start == q->end;
}
int queue_empty(struct Queue *q) {
......@@ -57,7 +57,7 @@ int queue_empty(struct Queue *q) {
void queue_print(struct Queue *q) {
int i;
printf("buffer: (size: %d, full: %d, empty: %d)\n", queue_size(q), queue_full(q), queue_empty(q));
for (i = 0; i < q->max_size; i += 1) {
for (i = 0; i < q->max_size_plus1; i += 1) {
char c = (char) q->buff[i];
if (c == 0) c = '-';
printf("%c", c);;
......
......@@ -6,13 +6,13 @@
struct Queue {
volatile unsigned char *buff;
unsigned int max_size;
unsigned int max_size_plus1;
unsigned int start;
unsigned int end;
};
int queue_init(struct Queue *q, volatile unsigned char *buff, unsigned int max_size);
int queue_init(struct Queue *q, volatile unsigned char *buff, unsigned int max_size_plus1);
struct Queue * queue_malloc(unsigned int max_size);
void queue_free(struct Queue *q);
int queue_add(struct Queue *q, unsigned char c);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment