From cdb69e326b48cb6e558b10e7a47b5f8b701b5d6f Mon Sep 17 00:00:00 2001 From: jf-home Date: Sun, 23 Jun 2024 00:28:48 +0800 Subject: [PATCH] =?UTF-8?q?=E9=98=9F=E5=88=97=E8=B0=83=E8=AF=95=E9=80=9A?= =?UTF-8?q?=E8=BF=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/queue.c | 25 +++++++++++++++++-------- test/test_queue.c | 2 +- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/queue.c b/src/queue.c index d695d52..6daaf31 100644 --- a/src/queue.c +++ b/src/queue.c @@ -299,8 +299,10 @@ bool queue2_push(struct _queue* self, void* obj) { return false; } + void * obj_array = self->_front->obj; uint32_t index = self->_index_back; + memmove((char*)obj_array + index * self->_obj_size, obj, self->_obj_size); if(index >= self->capacity(self)) { index = 0; @@ -309,7 +311,6 @@ bool queue2_push(struct _queue* self, void* obj) { index++; } - memmove((char*)obj_array + index * self->_obj_size, obj, self->_obj_size); self->_index_back = index; self->_size++; return true; @@ -324,7 +325,10 @@ bool queue2_pop(struct _queue* self, void* obj) } void * obj_array = self->_front->obj; uint32_t index = self->_index_front; - + if(obj != NULL) + { + memmove(obj, (char*)obj_array + index * self->_obj_size,self->_obj_size); + } if(index >= self->capacity(self)) { index = 0; @@ -333,10 +337,6 @@ bool queue2_pop(struct _queue* self, void* obj) { index++; } - if(obj != NULL) - { - memmove(obj, (char*)obj_array + index * self->_obj_size,self->_obj_size); - } self->_index_front = index; self->_size--; return true; @@ -351,7 +351,15 @@ bool queue2_back(struct _queue* self, void* obj) } void * obj_array = self->_front->obj; uint32_t index = self->_index_back; - memmove(obj, (char *)obj_array + self->_obj_size, self->_obj_size); + if(index == 0) + { + index = self->capacity(self) - 1; + } + else + { + index--; + } + memmove(obj, (char *)obj_array + index * self->_obj_size, self->_obj_size); return true; } @@ -445,7 +453,8 @@ bool queue2_init(struct _queue * queue, uint32_t obj_size, uint32_t capacity) } queue->_back = queue->_front; - queue->_front->obj = calloc(queue->_capacity + 1, queue->_obj_size); + // queue->_front->obj = calloc(queue->_capacity + 1, queue->_obj_size); + queue->_front->obj = calloc(queue->_capacity, queue->_obj_size); if(queue->_front->obj == NULL) { return false; diff --git a/test/test_queue.c b/test/test_queue.c index 140dfd8..215e671 100644 --- a/test/test_queue.c +++ b/test/test_queue.c @@ -239,7 +239,7 @@ static void queue_test_char(void) uint32_t i = 0; char data[] = "abcdefghijk"; char temp = 0; - uint32_t len = sizeof(data) / sizeof(data[0]); + uint32_t len = sizeof(data) / sizeof(data[0]) - 1; struct _queue queue; queue2_init(&queue, sizeof(char), 64);