queue.size -> queue.length

This commit is contained in:
jacekpoz 2024-03-10 14:06:03 +01:00
parent 8ef8ad7ddb
commit df960e552c
Signed by: poz
SSH key fingerprint: SHA256:JyLeVWE4bF3tDnFeUpUaJsPsNlJyBldDGV/dIKSLyN8
2 changed files with 8 additions and 8 deletions

View file

@ -6,7 +6,7 @@
typedef struct Queue Queue; typedef struct Queue Queue;
Queue *queue_new(size_t size, size_t element_size); Queue *queue_new(size_t length, size_t element_size);
void queue_free(Queue *queue); void queue_free(Queue *queue);

View file

@ -6,18 +6,18 @@
struct Queue { struct Queue {
size_t back; size_t back;
size_t front; size_t front;
size_t size; size_t length;
size_t element_size; size_t element_size;
void **data; void **data;
}; };
Queue *queue_new(size_t size, size_t element_size) { Queue *queue_new(size_t length, size_t element_size) {
Queue *ret = malloc(sizeof(Queue)); Queue *ret = malloc(sizeof(Queue));
ret->back = 0; ret->back = 0;
ret->front = 0; ret->front = 0;
ret->size = size; ret->length = length;
ret->element_size = element_size; ret->element_size = element_size;
ret->data = malloc(size * sizeof(void*)); ret->data = malloc(length * sizeof(void*));
return ret; return ret;
} }
@ -38,18 +38,18 @@ void *queue_read(Queue *q) {
void* ret = memcpy(malloc(q->element_size), q->data[q->front], q->element_size); void* ret = memcpy(malloc(q->element_size), q->data[q->front], q->element_size);
q->data[q->front] = NULL; q->data[q->front] = NULL;
q->front = (q->front + 1) % q->size; q->front = (q->front + 1) % q->length;
return ret; return ret;
} }
bool queue_write(Queue *q, void *value) { bool queue_write(Queue *q, void *value) {
if (((q->back + 1) % q->size) == q->front) { if (((q->back + 1) % q->length) == q->front) {
return false; return false;
} }
void *local_value = memcpy(malloc(q->element_size), value, q->element_size); void *local_value = memcpy(malloc(q->element_size), value, q->element_size);
q->data[q->back] = local_value; q->data[q->back] = local_value;
q->back = (q->back + 1) % q->size; q->back = (q->back + 1) % q->length;
return true; return true;
} }