队列调试通过

This commit is contained in:
建峰 2024-06-23 00:28:48 +08:00
parent 0ee0eb90b0
commit cdb69e326b
2 changed files with 18 additions and 9 deletions

View File

@ -299,8 +299,10 @@ bool queue2_push(struct _queue* self, void* obj)
{ {
return false; return false;
} }
void * obj_array = self->_front->obj; void * obj_array = self->_front->obj;
uint32_t index = self->_index_back; uint32_t index = self->_index_back;
memmove((char*)obj_array + index * self->_obj_size, obj, self->_obj_size);
if(index >= self->capacity(self)) if(index >= self->capacity(self))
{ {
index = 0; index = 0;
@ -309,7 +311,6 @@ bool queue2_push(struct _queue* self, void* obj)
{ {
index++; index++;
} }
memmove((char*)obj_array + index * self->_obj_size, obj, self->_obj_size);
self->_index_back = index; self->_index_back = index;
self->_size++; self->_size++;
return true; return true;
@ -324,7 +325,10 @@ bool queue2_pop(struct _queue* self, void* obj)
} }
void * obj_array = self->_front->obj; void * obj_array = self->_front->obj;
uint32_t index = self->_index_front; 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)) if(index >= self->capacity(self))
{ {
index = 0; index = 0;
@ -333,10 +337,6 @@ bool queue2_pop(struct _queue* self, void* obj)
{ {
index++; index++;
} }
if(obj != NULL)
{
memmove(obj, (char*)obj_array + index * self->_obj_size,self->_obj_size);
}
self->_index_front = index; self->_index_front = index;
self->_size--; self->_size--;
return true; return true;
@ -351,7 +351,15 @@ bool queue2_back(struct _queue* self, void* obj)
} }
void * obj_array = self->_front->obj; void * obj_array = self->_front->obj;
uint32_t index = self->_index_back; 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; return true;
} }
@ -445,7 +453,8 @@ bool queue2_init(struct _queue * queue, uint32_t obj_size, uint32_t capacity)
} }
queue->_back = queue->_front; 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) if(queue->_front->obj == NULL)
{ {
return false; return false;

View File

@ -239,7 +239,7 @@ static void queue_test_char(void)
uint32_t i = 0; uint32_t i = 0;
char data[] = "abcdefghijk"; char data[] = "abcdefghijk";
char temp = 0; char temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]); uint32_t len = sizeof(data) / sizeof(data[0]) - 1;
struct _queue queue; struct _queue queue;
queue2_init(&queue, sizeof(char), 64); queue2_init(&queue, sizeof(char), 64);