From ebb493d2f9f2bccb2ee8fa237144ef0aa480fde6 Mon Sep 17 00:00:00 2001 From: tqfx Date: Wed, 27 Mar 2024 20:58:14 +0800 Subject: [PATCH] optimize implementation --- include/a/que.h | 9 ++++--- src/que.c | 73 +++++++++++++++++++++++++++++++-------------------------- src/vec.c | 5 ++-- test/que.h | 10 ++++---- 4 files changed, 54 insertions(+), 43 deletions(-) diff --git a/include/a/que.h b/include/a/que.h index 83a5957..75fe325 100644 --- a/include/a/que.h +++ b/include/a/que.h @@ -145,8 +145,11 @@ A_EXTERN void *a_que_at(a_que const *ctx, a_imax idx); @brief drop all the elements for a pointer to queue structure @param[in] ctx points to an instance of queue structure @param[in] dtor current element destructor + @return the execution state of the function + @retval 0 success + @retval 1 failure */ -A_EXTERN void a_que_drop(a_que *ctx, void (*dtor)(void *)); +A_EXTERN int a_que_drop(a_que *ctx, void (*dtor)(void *)); /*! @brief edit size of a element for a pointer to queue structure @@ -271,7 +274,7 @@ A_EXTERN void *a_que_remove(a_que *ctx, a_size idx); @code{.c} a_que_foreach(T, it, ctx) { - assert(a_que_siz(ctx) == sizeof(*it)); + assert(a_que_siz(ctx) >= sizeof(*it)); } @endcode @param T type of elements in the queue @@ -291,7 +294,7 @@ A_EXTERN void *a_que_remove(a_que *ctx, a_size idx); @code{.c} a_que_foreach_reverse(T, it, ctx) { - assert(a_que_siz(ctx) == sizeof(*it)); + assert(a_que_siz(ctx) >= sizeof(*it)); } @endcode @param T type of elements in the queue diff --git a/src/que.c b/src/que.c index 5db6ce3..2c7ba84 100644 --- a/src/que.c +++ b/src/que.c @@ -30,7 +30,7 @@ static a_list *a_que_new_(a_que *ctx) static int a_que_die_(a_que *ctx, a_list *node) { - if (!node) { return A_INVALID; } + if (a_unlikely(!node)) { return A_INVALID; } if (ctx->mem_ <= ctx->cur_) { a_size const mem = ctx->mem_ + (ctx->mem_ >> 1) + 1; @@ -44,19 +44,6 @@ static int a_que_die_(a_que *ctx, a_list *node) return A_SUCCESS; } -static void a_que_drop_(a_que *ctx) -{ - a_list *const head = &ctx->head_; - for (a_list *node = head->next; node != head; node = head->next) - { - if (a_que_die_(ctx, node) == A_SUCCESS) - { - a_list_del_node(node); - a_list_dtor(node); - } - } -} - a_que *a_que_new(a_size size) { a_que *const ctx = (a_que *)a_alloc(A_NULL, sizeof(a_que)); @@ -75,9 +62,10 @@ void a_que_die(a_que *ctx, void (*dtor)(void *)) void a_que_ctor(a_que *ctx, a_size size) { + if (!size) { size = sizeof(a_cast); } a_list_ctor(&ctx->head_); - ctx->siz_ = size ? size : sizeof(a_cast); ctx->ptr_ = A_NULL; + ctx->siz_ = size; ctx->num_ = 0; ctx->cur_ = 0; ctx->mem_ = 0; @@ -85,23 +73,29 @@ void a_que_ctor(a_que *ctx, a_size size) void a_que_dtor(a_que *ctx, void (*dtor)(void *)) { - a_list **node; - a_que_drop_(ctx); - node = ctx->ptr_; if (dtor) { - for (; ctx->cur_; --ctx->cur_) + for (a_list **node = ctx->ptr_; ctx->cur_; --ctx->cur_) { dtor(*node + 1); a_alloc(*node++, 0); } + a_list_forsafe_next(node, next, &ctx->head_) + { + dtor(node + 1); + a_alloc(node, 0); + } } else { - for (; ctx->cur_; --ctx->cur_) + for (a_list **node = ctx->ptr_; ctx->cur_; --ctx->cur_) { a_alloc(*node++, 0); } + a_list_forsafe_next(node, next, &ctx->head_) + { + a_alloc(node, 0); + } } a_alloc(ctx->ptr_, 0); ctx->ptr_ = A_NULL; @@ -135,9 +129,18 @@ void *a_que_at(a_que const *ctx, a_imax idx) return A_NULL; } -void a_que_drop(a_que *ctx, void (*dtor)(void *)) +int a_que_drop(a_que *ctx, void (*dtor)(void *)) { - a_que_drop_(ctx); + a_list *const head = &ctx->head_; + for (a_list *node = head->next; node != head; node = head->next) + { + if (a_que_die_(ctx, node) == A_SUCCESS) + { + a_list_del_node(node); + a_list_dtor(node); + } + else { return A_FAILURE; } + } if (dtor) { a_size cur = ctx->cur_; @@ -147,25 +150,29 @@ void a_que_drop(a_que *ctx, void (*dtor)(void *)) dtor(*node + 1); } } + return A_SUCCESS; } int a_que_edit(a_que *ctx, a_size size, void (*dtor)(void *)) { - size = size ? size : sizeof(a_cast); - a_que_drop(ctx, dtor); - if (size > ctx->siz_) + int ok = a_que_drop(ctx, dtor); + if (ok == A_SUCCESS) { - a_size cur = ctx->cur_; - a_list **node = ctx->ptr_; - for (; cur; ++node, --cur) + if (!size) { size = sizeof(a_cast); } + if (size > ctx->siz_) { - void *const ptr = a_alloc(*node, sizeof(a_list) + size); - if (a_unlikely(!ptr)) { return A_FAILURE; } - *node = (a_list *)ptr; + a_size cur = ctx->cur_; + a_list **node = ctx->ptr_; + for (; cur; ++node, --cur) + { + void *const ptr = a_alloc(*node, sizeof(a_list) + size); + if (a_unlikely(!ptr)) { return A_FAILURE; } + *node = (a_list *)ptr; + } } + ctx->siz_ = size; } - ctx->siz_ = size; - return A_SUCCESS; + return ok; } int a_que_swap(a_que const *ctx, a_size lhs, a_size rhs) diff --git a/src/vec.c b/src/vec.c index 6e5df02..9271618 100644 --- a/src/vec.c +++ b/src/vec.c @@ -73,8 +73,9 @@ void a_vec_die(a_vec *ctx, void (*dtor)(void *)) void a_vec_ctor(a_vec *ctx, a_size size) { - ctx->siz_ = size ? size : sizeof(a_cast); + if (!size) { size = sizeof(a_cast); } ctx->ptr_ = A_NULL; + ctx->siz_ = size; ctx->num_ = 0; ctx->mem_ = 0; } @@ -123,7 +124,7 @@ void a_vec_move(a_vec *ctx, a_vec *obj) void a_vec_edit(a_vec *ctx, a_size size, void (*dtor)(void *)) { - size = size ? size : sizeof(a_cast); + if (!size) { size = sizeof(a_cast); } a_vec_drop_(ctx, 0, dtor); ctx->mem_ = ctx->mem_ * ctx->siz_ / size; ctx->siz_ = size; diff --git a/test/que.h b/test/que.h index 38d210f..d281e1a 100644 --- a/test/que.h +++ b/test/que.h @@ -4,8 +4,8 @@ static void dtor(void *ptr) { - a_u32 *p = a_u32_(*, ptr); - printf("%" PRIu32 " ", *p); + a_u32 *u = a_u32_(*, ptr); + printf("%" PRIu32 " ", *u); } static void test(void) @@ -41,7 +41,7 @@ static void test(void) { (void)(a_que_pull_fore(a_u64, ctx)); } - a_que_edit(ctx, sizeof(a_u32), A_NULL); + a_que_edit(ctx, sizeof(a_u32) + 8, A_NULL); for (a_u32 i = 5; i--;) { a_u32 *obj = a_que_push_fore(a_u32, ctx); @@ -70,13 +70,13 @@ static void test(void) } a_que_foreach(a_u32, it, ctx) { - TEST_BUG(a_que_siz(ctx) == sizeof(*it)); + TEST_BUG(a_que_siz(ctx) >= sizeof(*it)); printf("%" PRIu32 " ", *it); } putchar('\n'); a_que_foreach_reverse(a_u32, it, ctx) { - TEST_BUG(a_que_siz(ctx) == sizeof(*it)); + TEST_BUG(a_que_siz(ctx) >= sizeof(*it)); printf("%" PRIu32 " ", *it); } putchar('\n'); -- 2.11.4.GIT