diff --git a/generic-queue/generic_queue.c b/generic-queue/generic_queue.c index 96d7d9c..c1c38da 100644 --- a/generic-queue/generic_queue.c +++ b/generic-queue/generic_queue.c @@ -1,10 +1,12 @@ #include "generic_queue.h" -void GENQ_init(generic_queue_t *q, size_t elem_size) +void GENQ_init(generic_queue_t *q, size_t size, size_t elem_size, uint8_t *queue) { + q->size = size; q->queue_elem_size = elem_size; q->tail = q->head = q->cnt_elems = 0; - q->max_elems = GENQ_SIZE/q->queue_elem_size; + q->max_elems = q->size/q->queue_elem_size; + q->queue = queue; } bool GENQ_is_empty(generic_queue_t *q) { @@ -14,7 +16,7 @@ bool GENQ_is_full(generic_queue_t *q) { return q->cnt_elems >= q->max_elems; } -bool GENQ_pop(generic_queue_t *q, void *e) +bool GENQ_pop(generic_queue_t *q, uint8_t *e) { if(GENQ_is_empty(q)) return 0; q->cnt_elems--; @@ -24,7 +26,7 @@ bool GENQ_pop(generic_queue_t *q, void *e) q->head %= q->max_elems; return 1; } -bool GENQ_push(generic_queue_t *q, void *e) +bool GENQ_push(generic_queue_t *q, uint8_t *e) { if(GENQ_is_full(q)) return 0; q->cnt_elems++; diff --git a/generic-queue/generic_queue.h b/generic-queue/generic_queue.h index 4472d18..0c70654 100644 --- a/generic-queue/generic_queue.h +++ b/generic-queue/generic_queue.h @@ -6,20 +6,26 @@ #include "stdlib.h" #include "string.h" -#define GENQ_SIZE 1000 - typedef struct{ size_t head, tail; - uint8_t queue[GENQ_SIZE]; + size_t size; size_t queue_elem_size; size_t max_elems; int cnt_elems; + uint8_t *queue; } generic_queue_t; -void GENQ_init(generic_queue_t *, size_t); -bool GENQ_is_empty(generic_queue_t*); -bool GENQ_is_full(generic_queue_t*); -bool GENQ_pop(generic_queue_t*, void *); -bool GENQ_push(generic_queue_t*, void *); +/** + * + * @param q pointer to the queue + * @param size size of the allocated memory in bytes + * @param elem_size max size of the single element of the queue + * @param queue pointer to the allocated memory for the queue +*/ +void GENQ_init(generic_queue_t *q, size_t size, size_t elem_size, uint8_t *queue); +bool GENQ_is_empty(generic_queue_t *q); +bool GENQ_is_full(generic_queue_t *q); +bool GENQ_pop(generic_queue_t *q, uint8_t *e); +bool GENQ_push(generic_queue_t*, uint8_t *e); #endif \ No newline at end of file diff --git a/generic-queue/test/test.c b/generic-queue/test/test.c index 98d4d73..a5004c9 100644 --- a/generic-queue/test/test.c +++ b/generic-queue/test/test.c @@ -12,7 +12,10 @@ typedef struct{ int main() { - GENQ_init(&q, sizeof(test)); + int tot = 30; + + uint8_t *mem = (uint8_t*)malloc(sizeof(test)*tot); + GENQ_init(&q, sizeof(test)*tot, sizeof(test), mem); test v[n]; char prova[] = "asdaa"; @@ -21,11 +24,10 @@ int main() v[i].v = i+1; memcpy(v[i].prova, prova, sizeof(char)*5); } - int tot = GENQ_SIZE / sizeof(test); int cnt = 0; for(int i=0;i