From 0ee0eb90b00d20e3fc440ccda3a702a906c44d06 Mon Sep 17 00:00:00 2001 From: jf-home Date: Sat, 22 Jun 2024 18:36:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=B4=E7=B4=A2=E5=BC=95=E6=88=96=E8=80=85?= =?UTF-8?q?=E5=85=A5=E9=98=9F=E8=BF=98=E5=AD=98=E5=9C=A8=E9=97=AE=E9=A2=98?= =?UTF-8?q?=EF=BC=8C=E7=A8=8D=E5=90=8E=E5=9B=9E=E6=9D=A5=E7=BB=A7=E7=BB=AD?= =?UTF-8?q?=E8=A7=A3=E5=86=B3=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 3 ++- src/queue.c | 18 +++++++++++------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index f110707..3e27229 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -5,6 +5,7 @@ "common.h": "c", "unicstl.h": "c", "test.h": "c", - "queue.h": "c" + "queue.h": "c", + "stdbool.h": "c" } } \ No newline at end of file diff --git a/src/queue.c b/src/queue.c index 630093f..d695d52 100644 --- a/src/queue.c +++ b/src/queue.c @@ -301,11 +301,14 @@ bool queue2_push(struct _queue* self, void* obj) } void * obj_array = self->_front->obj; uint32_t index = self->_index_back; - index++; if(index >= self->capacity(self)) { index = 0; } + else + { + index++; + } memmove((char*)obj_array + index * self->_obj_size, obj, self->_obj_size); self->_index_back = index; self->_size++; @@ -322,13 +325,13 @@ bool queue2_pop(struct _queue* self, void* obj) void * obj_array = self->_front->obj; uint32_t index = self->_index_front; - if(index == 0) + if(index >= self->capacity(self)) { - index = self->capacity(self) - 1; + index = 0; } else { - index--; + index++; } if(obj != NULL) { @@ -348,7 +351,7 @@ bool queue2_back(struct _queue* self, void* obj) } void * obj_array = self->_front->obj; uint32_t index = self->_index_back; - memmove(obj, obj_array + self->_obj_size, self->_obj_size); + memmove(obj, (char *)obj_array + self->_obj_size, self->_obj_size); return true; } @@ -361,7 +364,7 @@ bool queue2_front(struct _queue* self, void* obj) } void * obj_array = self->_front->obj; uint32_t index = self->_index_front; - memmove(obj, obj_array + index * self->_obj_size, self->_obj_size); + memmove(obj, (char *)obj_array + index * self->_obj_size, self->_obj_size); return true; } @@ -428,6 +431,7 @@ bool queue2_init(struct _queue * queue, uint32_t obj_size, uint32_t capacity) queue->front = queue2_front; queue->clear = queue2_clear; queue->empty = queue_empty; + queue->full = queue2_full; queue->size = queue_size; queue->capacity = queue2_capacity; queue->destory = queue2_destory; @@ -439,7 +443,7 @@ bool queue2_init(struct _queue * queue, uint32_t obj_size, uint32_t capacity) { return false; } - queue->_back = queue->_back; + queue->_back = queue->_front; queue->_front->obj = calloc(queue->_capacity + 1, queue->_obj_size); if(queue->_front->obj == NULL)