mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-05-17 19:41:36 +08:00
init函数可以多次调用,但是为了防止内存泄漏。因此每次初始化时先执行一次destory操作
This commit is contained in:
parent
43cfbfcda6
commit
d8012720b6
3
.vscode/launch.json
vendored
3
.vscode/launch.json
vendored
@ -8,7 +8,8 @@
|
||||
"name": "unicstl",
|
||||
"type": "cppdbg",
|
||||
"request": "launch",
|
||||
"program": "${workspaceFolder}/build/release/bin/test.exe",
|
||||
// "program": "${workspaceFolder}/build/release/bin/test.exe",
|
||||
"program": "${workspaceFolder}/build/release/bin/demo.exe",
|
||||
"args": [],
|
||||
"stopAtEntry": false,
|
||||
"cwd": "${fileDirname}",
|
||||
|
@ -58,10 +58,10 @@ int main()
|
||||
printf("----- unicstl demo -----\n");
|
||||
// while (1)
|
||||
{
|
||||
demo_list();
|
||||
// demo_list();
|
||||
// demo_stack();
|
||||
// demo_deque();
|
||||
// demo_queue();
|
||||
demo_queue();
|
||||
// demo_tree();
|
||||
// demo_heap();
|
||||
}
|
||||
|
28
src/queue.c
28
src/queue.c
@ -134,12 +134,15 @@ static bool queue_clear(struct _queue* self)
|
||||
static bool queue_empty(struct _queue* self)
|
||||
{
|
||||
assert(self != NULL);
|
||||
return self->_size == 0;
|
||||
assert(self->size != NULL);
|
||||
return self->size(self) == 0;
|
||||
}
|
||||
|
||||
static bool queue_full(struct _queue* self)
|
||||
{
|
||||
assert(self != NULL);
|
||||
assert(self->size != NULL);
|
||||
assert(self->capacity != NULL);
|
||||
return self->size(self) == self->capacity(self);
|
||||
}
|
||||
|
||||
@ -307,12 +310,6 @@ bool queue_init(struct _queue * queue, uint32_t obj_size)
|
||||
return false;
|
||||
}
|
||||
|
||||
// The queue is only initialized once
|
||||
if(queue->_obj_size != 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// attribute init
|
||||
queue->_size = 0;
|
||||
queue->_obj_size = obj_size;
|
||||
@ -330,13 +327,19 @@ bool queue_init(struct _queue * queue, uint32_t obj_size)
|
||||
queue->empty = queue_empty;
|
||||
queue->full = queue_full;
|
||||
queue->size = queue_size;
|
||||
queue->capacity = queue_capacity;
|
||||
|
||||
queue->destory = queue_destory;
|
||||
queue->print = queue_print;
|
||||
|
||||
// 防止多次调用init导致内存泄漏
|
||||
queue->destory(queue);
|
||||
|
||||
// init front & back
|
||||
queue->_front = NULL;
|
||||
queue->_back = NULL;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool queue_init2(struct _queue * queue, uint32_t obj_size, uint32_t capacity)
|
||||
@ -349,12 +352,6 @@ bool queue_init2(struct _queue * queue, uint32_t obj_size, uint32_t capacity)
|
||||
return false;
|
||||
}
|
||||
|
||||
// The queue is only initialized once
|
||||
if(queue->_obj_size != 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// attribute init
|
||||
queue->_size = 0;
|
||||
queue->_obj_size = obj_size;
|
||||
@ -377,6 +374,11 @@ bool queue_init2(struct _queue * queue, uint32_t obj_size, uint32_t capacity)
|
||||
queue->destory = queue2_destory;
|
||||
queue->print = queue2_print;
|
||||
|
||||
// 防止多次调用init导致内存泄漏
|
||||
queue->_front = NULL;
|
||||
queue->_back = NULL;
|
||||
queue->destory(queue);
|
||||
|
||||
// init front & back
|
||||
queue->_front = (struct _queue_node *)malloc(sizeof(struct _queue_node));
|
||||
if(queue->_front == NULL)
|
||||
|
@ -33,7 +33,7 @@ static void test_queue_init(void)
|
||||
TEST_ASSERT_FALSE(queue_init(NULL, sizeof(int)));
|
||||
TEST_ASSERT_FALSE(queue_init(queue, 0));
|
||||
TEST_ASSERT_TRUE(queue_init(queue, sizeof(int)));
|
||||
TEST_ASSERT_FALSE(queue_init(queue, sizeof(int))); // The queue is only initialized once
|
||||
TEST_ASSERT_TRUE(queue_init(queue, sizeof(char)));
|
||||
queue_free(&queue);
|
||||
|
||||
// ------------------------------
|
||||
@ -42,7 +42,7 @@ static void test_queue_init(void)
|
||||
TEST_ASSERT_FALSE(queue_init2(queue, 0, 1));
|
||||
TEST_ASSERT_FALSE(queue_init2(queue, sizeof(int), 0));
|
||||
TEST_ASSERT_TRUE(queue_init2(queue, sizeof(int), 1));
|
||||
TEST_ASSERT_FALSE(queue_init2(queue, sizeof(int), 1)); // The queue is only initialized once
|
||||
TEST_ASSERT_TRUE(queue_init2(queue, sizeof(int), 5));
|
||||
queue_free(&queue);
|
||||
}
|
||||
|
||||
@ -56,7 +56,8 @@ static void test_queue_push(void)
|
||||
|
||||
queue_t queue = NULL;
|
||||
|
||||
// ------------------------------ queue = queue_new();
|
||||
// ------------------------------
|
||||
queue = queue_new();
|
||||
queue_init(queue, sizeof(int));
|
||||
TEST_ASSERT_TRUE(queue->empty(queue));
|
||||
for(i = 0; i < len; i++)
|
||||
|
Loading…
Reference in New Issue
Block a user