diff --git a/quad/src/queue/queue.c b/quad/src/queue/queue.c index 243975b0a3f14a77e811ecb33249528ff98ba5c4..5e17f849b9d871d49cb847edd1cab6624149cdfb 100644 --- a/quad/src/queue/queue.c +++ b/quad/src/queue/queue.c @@ -1,9 +1,9 @@ #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);; diff --git a/quad/src/queue/queue.h b/quad/src/queue/queue.h index 1adf670ba24877b93087aba40470057bfcc766e8..b8c6181041025b862dd93e52017a2f2450363c2d 100644 --- a/quad/src/queue/queue.h +++ b/quad/src/queue/queue.h @@ -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);