From 6829ec79f3c115157a2c9ace92e02344f2bf79bf Mon Sep 17 00:00:00 2001 From: xianjimli Date: Wed, 16 Jan 2019 18:13:36 +0800 Subject: [PATCH] refactor --- docs/changes.md | 1 + src/base/assets_manager.c | 39 ++++----- src/base/assets_manager.h | 4 +- src/base/font_manager.c | 48 ++++------- src/base/font_manager.h | 4 +- src/base/image_manager.c | 104 ++++++++++++------------ src/base/image_manager.h | 6 +- src/base/widget.c | 8 +- src/base/widget.h | 5 +- src/base/widget_factory.c | 38 +++------ src/base/widget_factory.h | 4 +- src/ext_widgets/keyboard/keyboard.c | 2 +- src/ext_widgets/keyboard/keyboard.h | 2 +- src/ext_widgets/slide_menu/slide_menu.h | 2 +- src/tkc/darray.c | 17 ++-- src/tkc/darray.h | 16 ++-- src/tkc/slist.c | 11 +-- src/tkc/utils.c | 14 ++++ src/tkc/utils.h | 4 + 19 files changed, 151 insertions(+), 178 deletions(-) diff --git a/docs/changes.md b/docs/changes.md index 6ad2300513..59d51c1645 100644 --- a/docs/changes.md +++ b/docs/changes.md @@ -3,6 +3,7 @@ * 增加darray类,逐步替换以前的array类。 * 增加widget pool用于控件的缓存和重复利用,可以通过WITH\_WIDGET\_POOL启用。 * 增加widget\_create函数,简化子类控件的创建函数,同时让通用控件本身的由widget pool集中管理。 + * 使用array的地方替换为darray。 * 2019/01/15 * 整理API文档:progress\_circle/rich\_text/slide\_menu/slide\_view/svg\_image/switch diff --git a/src/base/assets_manager.c b/src/base/assets_manager.c index 0c54572f73..9432f28f84 100644 --- a/src/base/assets_manager.c +++ b/src/base/assets_manager.c @@ -25,6 +25,17 @@ #include "base/system_info.h" #include "base/assets_manager.h" +static int asset_cache_cmp_type(const void* a, const void* b) { + const asset_info_t* aa = (const asset_info_t*)a; + const asset_info_t* bb = (const asset_info_t*)b; + + if (aa->is_in_rom) { + return -1; + } + + return aa->type - bb->type; +} + static assets_manager_t* s_assets_manager = NULL; #ifdef WITH_FS_RES @@ -280,7 +291,8 @@ assets_manager_t* assets_manager_create(uint32_t init_nr) { assets_manager_t* assets_manager_init(assets_manager_t* rm, uint32_t init_nr) { return_value_if_fail(rm != NULL, NULL); - array_init(&(rm->assets), init_nr); + darray_init(&(rm->assets), init_nr, (tk_destroy_t)asset_info_unref, + (tk_compare_t)asset_cache_cmp_type); return rm; } @@ -300,8 +312,7 @@ ret_t assets_manager_add(assets_manager_t* rm, const void* info) { return_value_if_fail(rm != NULL && info != NULL, RET_BAD_PARAMS); asset_info_ref((asset_info_t*)r); - - return array_push(&(rm->assets), (void*)r); + return darray_push(&(rm->assets), (void*)r); } const asset_info_t* assets_manager_find_in_cache(assets_manager_t* rm, asset_type_t type, @@ -347,26 +358,17 @@ ret_t assets_manager_unref(assets_manager_t* rm, const asset_info_t* info) { if (!(info->is_in_rom)) { bool_t remove = info->refcount <= 1; - asset_info_unref((asset_info_t*)info); if (remove) { - array_remove(&(rm->assets), NULL, (void*)info, NULL); + tk_compare_t cmp = rm->assets.compare; + rm->assets.compare = pointer_compare; + darray_remove(&(rm->assets), (void*)info); + rm->assets.compare = cmp; } } return RET_OK; } -static int asset_cache_cmp_type(const void* a, const void* b) { - const asset_info_t* aa = (const asset_info_t*)a; - const asset_info_t* bb = (const asset_info_t*)b; - - if (aa->is_in_rom) { - return -1; - } - - return aa->type - bb->type; -} - ret_t assets_manager_clear_cache(assets_manager_t* rm, asset_type_t type) { asset_info_t info; @@ -374,8 +376,7 @@ ret_t assets_manager_clear_cache(assets_manager_t* rm, asset_type_t type) { info.type = type; return_value_if_fail(rm != NULL, RET_BAD_PARAMS); - return array_remove_all(&(rm->assets), asset_cache_cmp_type, &info, - (tk_destroy_t)asset_info_unref); + return darray_remove_all(&(rm->assets), &info); } ret_t assets_manager_deinit(assets_manager_t* rm) { @@ -392,7 +393,7 @@ ret_t assets_manager_deinit(assets_manager_t* rm) { } TKMEM_FREE(rm->res_root); - array_deinit(&(rm->assets)); + darray_deinit(&(rm->assets)); return RET_OK; } diff --git a/src/base/assets_manager.h b/src/base/assets_manager.h index c956109926..cc3ab9a47c 100644 --- a/src/base/assets_manager.h +++ b/src/base/assets_manager.h @@ -22,7 +22,7 @@ #ifndef TK_ASSETS_MANAGER_H #define TK_ASSETS_MANAGER_H -#include "tkc/array.h" +#include "tkc/darray.h" BEGIN_C_DECLS @@ -205,7 +205,7 @@ typedef struct _asset_info_t { * */ typedef struct _assets_manager_t { - array_t assets; + darray_t assets; char* res_root; } assets_manager_t; diff --git a/src/base/font_manager.c b/src/base/font_manager.c index c994d978a4..4a9b1166ce 100644 --- a/src/base/font_manager.c +++ b/src/base/font_manager.c @@ -24,6 +24,19 @@ static font_manager_t* s_font_manager = NULL; +typedef struct _font_cmp_info_t { + const char* name; + uint32_t size; +} font_cmp_info_t; + +static int32_t font_cmp(font_t* font, font_cmp_info_t* info) { + if (font_match(font, info->name, info->size)) { + return 0; + } + + return -1; +} + font_manager_t* font_manager(void) { return s_font_manager; } @@ -42,7 +55,7 @@ font_manager_t* font_manager_create(font_loader_t* loader) { font_manager_t* font_manager_init(font_manager_t* fm, font_loader_t* loader) { return_value_if_fail(fm != NULL, NULL); - array_init(&(fm->fonts), 2); + darray_init(&(fm->fonts), 2, (tk_destroy_t)font_destroy, (tk_compare_t)font_cmp); fm->loader = loader; @@ -60,27 +73,14 @@ ret_t font_manager_set_assets_manager(font_manager_t* fm, assets_manager_t* am) ret_t font_manager_add_font(font_manager_t* fm, font_t* font) { return_value_if_fail(fm != NULL && font != NULL, RET_BAD_PARAMS); - return array_push(&(fm->fonts), font); + return darray_push(&(fm->fonts), font); } static font_t* font_manager_lookup(font_manager_t* fm, const char* name, font_size_t size) { - uint32_t i = 0; - uint32_t nr = 0; - font_t** fonts = NULL; + font_cmp_info_t info = {name, size}; return_value_if_fail(fm != NULL, NULL); - nr = fm->fonts.size; - fonts = (font_t**)fm->fonts.elms; - return_value_if_fail(nr > 0, NULL); - - for (i = 0; i < nr; i++) { - font_t* iter = fonts[i]; - if (font_match(iter, name, size)) { - return iter; - } - } - - return NULL; + return darray_find(&(fm->fonts), &info); } font_t* font_manager_load(font_manager_t* fm, const char* name, uint32_t size) { @@ -123,21 +123,9 @@ font_t* font_manager_get_font(font_manager_t* fm, const char* name, font_size_t } ret_t font_manager_deinit(font_manager_t* fm) { - uint32_t i = 0; - uint32_t nr = 0; - font_t** fonts = NULL; return_value_if_fail(fm != NULL, RET_BAD_PARAMS); - nr = fm->fonts.size; - fonts = (font_t**)fm->fonts.elms; - for (i = 0; i < nr; i++) { - font_t* iter = fonts[i]; - font_destroy(iter); - } - - array_deinit(&(fm->fonts)); - - return RET_OK; + return darray_deinit(&(fm->fonts)); } ret_t font_manager_destroy(font_manager_t* fm) { diff --git a/src/base/font_manager.h b/src/base/font_manager.h index 6e64818d82..a871f28f1b 100644 --- a/src/base/font_manager.h +++ b/src/base/font_manager.h @@ -22,7 +22,7 @@ #ifndef TK_FONT_MANAGER_H #define TK_FONT_MANAGER_H -#include "tkc/array.h" +#include "tkc/darray.h" #include "base/font_loader.h" #include "base/assets_manager.h" @@ -34,7 +34,7 @@ BEGIN_C_DECLS * (如果使用nanovg,字体由nanovg内部管理) */ typedef struct _font_manager_t { - array_t fonts; + darray_t fonts; /** * @property {font_loader_t*} loader diff --git a/src/base/image_manager.c b/src/base/image_manager.c index 4972651860..574e7d1cfe 100644 --- a/src/base/image_manager.c +++ b/src/base/image_manager.c @@ -32,6 +32,27 @@ typedef struct _bitmap_cache_t { uint32_t last_access_time; } bitmap_cache_t; +static int bitmap_cache_cmp_time(bitmap_cache_t* a, bitmap_cache_t* b) { + return (a->last_access_time <= b->last_access_time) ? 0 : -1; +} + +static int bitmap_cache_cmp_name(bitmap_cache_t* a, bitmap_cache_t* b) { + return strcmp(a->name, b->name); +} + +static int bitmap_cache_cmp_data(bitmap_cache_t* a, bitmap_cache_t* b) { + return (char*)(a->image.data) - (char*)(b->image.data); +} + +static ret_t bitmap_cache_destroy(bitmap_cache_t* cache) { + return_value_if_fail(cache != NULL, RET_BAD_PARAMS); + + bitmap_destroy(&(cache->image)); + TKMEM_FREE(cache); + + return RET_OK; +} + static image_manager_t* s_image_manager = NULL; image_manager_t* image_manager() { return s_image_manager; @@ -54,7 +75,7 @@ image_manager_t* image_manager_init(image_manager_t* imm, image_loader_t* loader return_value_if_fail(imm != NULL, NULL); imm->loader = loader; - array_init(&(imm->images), 0); + darray_init(&(imm->images), 0, (tk_destroy_t)bitmap_cache_destroy, NULL); imm->assets_manager = assets_manager(); return imm; @@ -76,53 +97,49 @@ ret_t image_manager_add(image_manager_t* imm, const char* name, const bitmap_t* cache->last_access_time = cache->created_time; cache->image.name = cache->name; - return array_push(&(imm->images), cache); + return darray_push(&(imm->images), cache); } ret_t image_manager_lookup(image_manager_t* imm, const char* name, bitmap_t* image) { - uint32_t i = 0; - uint32_t nr = 0; + bitmap_cache_t info; bitmap_cache_t* iter = NULL; - bitmap_cache_t** all = NULL; return_value_if_fail(imm != NULL && name != NULL && image != NULL, RET_BAD_PARAMS); - all = (bitmap_cache_t**)(imm->images.elms); - for (i = 0, nr = imm->images.size; i < nr; i++) { - iter = all[i]; - if (strcmp(name, iter->name) == 0) { - *image = iter->image; - image->destroy = NULL; - image->specific_destroy = NULL; - image->should_free_data = FALSE; + tk_strncpy(info.name, name, TK_NAME_LEN); + imm->images.compare = (tk_compare_t)bitmap_cache_cmp_name; + iter = darray_find(&(imm->images), &info); - iter->access_count++; - iter->last_access_time = time_now_s(); + if (iter != NULL) { + *image = iter->image; + image->destroy = NULL; + image->specific_destroy = NULL; + image->should_free_data = FALSE; - return RET_OK; - } + iter->access_count++; + iter->last_access_time = time_now_s(); + + return RET_OK; } return RET_NOT_FOUND; } ret_t image_manager_update_specific(image_manager_t* imm, bitmap_t* image) { - uint32_t i = 0; - uint32_t nr = 0; + bitmap_cache_t info; bitmap_cache_t* iter = NULL; - bitmap_cache_t** all = NULL; return_value_if_fail(imm != NULL && image != NULL, RET_BAD_PARAMS); - all = (bitmap_cache_t**)(imm->images.elms); - for (i = 0, nr = imm->images.size; i < nr; i++) { - iter = all[i]; - if (image->data == iter->image.data) { - iter->image.flags = image->flags; - iter->image.specific = image->specific; - iter->image.specific_ctx = image->specific_ctx; - iter->image.specific_destroy = image->specific_destroy; + info.image.data = image->data; + imm->images.compare = (tk_compare_t)bitmap_cache_cmp_data; + iter = darray_find(&(imm->images), &info); - return RET_OK; - } + if (iter != NULL) { + iter->image.flags = image->flags; + iter->image.specific = image->specific; + iter->image.specific_ctx = image->specific_ctx; + iter->image.specific_destroy = image->specific_destroy; + + return RET_OK; } return RET_NOT_FOUND; @@ -176,41 +193,20 @@ ret_t image_manager_set_assets_manager(image_manager_t* imm, assets_manager_t* a return RET_OK; } -static int bitmap_cache_cmp_time(bitmap_cache_t* a, bitmap_cache_t* b) { - return (a->last_access_time <= b->last_access_time) ? 0 : -1; -} - -static ret_t bitmap_cache_destroy(bitmap_cache_t* cache) { - return_value_if_fail(cache != NULL, RET_BAD_PARAMS); - - bitmap_destroy(&(cache->image)); - TKMEM_FREE(cache); - - return RET_OK; -} - ret_t image_manager_unload_unused(image_manager_t* imm, uint32_t time_delta_s) { bitmap_cache_t b; b.last_access_time = time_now_s() - time_delta_s; return_value_if_fail(imm != NULL && imm->loader != NULL, RET_BAD_PARAMS); - return array_remove_all(&(imm->images), (tk_compare_t)bitmap_cache_cmp_time, &b, - (tk_destroy_t)bitmap_cache_destroy); + imm->images.compare = (tk_compare_t)bitmap_cache_cmp_time; + return darray_remove_all(&(imm->images), &b); } ret_t image_manager_deinit(image_manager_t* imm) { - uint32_t i = 0; - uint32_t nr = 0; - bitmap_cache_t** all = NULL; return_value_if_fail(imm != NULL && imm->loader != NULL, RET_BAD_PARAMS); - all = (bitmap_cache_t**)(imm->images.elms); - for (i = 0, nr = imm->images.size; i < nr; i++) { - bitmap_cache_destroy(all[i]); - } - - array_deinit(&(imm->images)); imm->loader = NULL; + darray_deinit(&(imm->images)); return RET_OK; } diff --git a/src/base/image_manager.h b/src/base/image_manager.h index 75932602bc..df6a45a020 100644 --- a/src/base/image_manager.h +++ b/src/base/image_manager.h @@ -22,7 +22,7 @@ #ifndef TK_IMAGE_MANAGER_H #define TK_IMAGE_MANAGER_H -#include "tkc/array.h" +#include "tkc/darray.h" #include "base/image_loader.h" #include "base/assets_manager.h" @@ -46,11 +46,11 @@ typedef struct _bitmap_header_t { */ typedef struct _image_manager_t { /** - * @property {array_t} images + * @property {darray_t} images * @annotation ["private"] * 缓存的图片。 */ - array_t images; + darray_t images; /** * @property {image_loader_t*} loader diff --git a/src/base/widget.c b/src/base/widget.c index 7612fdc0d5..22aaf83933 100644 --- a/src/base/widget.c +++ b/src/base/widget.c @@ -371,7 +371,7 @@ ret_t widget_add_child(widget_t* widget, widget_t* child) { child->parent = widget; widget->need_relayout_children = TRUE; if (widget->children == NULL) { - widget->children = array_create(4); + widget->children = darray_create(4, NULL, NULL); } if (widget->vt->on_add_child) { @@ -380,7 +380,7 @@ ret_t widget_add_child(widget_t* widget, widget_t* child) { } } - return array_push(widget->children, child); + return darray_push(widget->children, child); } ret_t widget_remove_child(widget_t* widget, widget_t* child) { @@ -407,7 +407,7 @@ ret_t widget_remove_child(widget_t* widget, widget_t* child) { } child->parent = NULL; - return array_remove(widget->children, NULL, child, NULL); + return darray_remove(widget->children, child); } ret_t widget_insert_child(widget_t* widget, uint32_t index, widget_t* child) { @@ -1411,7 +1411,7 @@ static ret_t widget_destroy_sync(widget_t* widget) { if (widget->children != NULL) { widget_destroy_children(widget); - array_destroy(widget->children); + darray_destroy(widget->children); widget->children = NULL; } diff --git a/src/base/widget.h b/src/base/widget.h index 124c306331..de42bda003 100644 --- a/src/base/widget.h +++ b/src/base/widget.h @@ -26,6 +26,7 @@ #include "tkc/mem.h" #include "tkc/wstr.h" #include "tkc/value.h" +#include "tkc/darray.h" #include "tkc/rect.h" #include "tkc/emitter.h" @@ -334,11 +335,11 @@ struct _widget_t { */ widget_t* key_target; /** - * @property {array_t*} children + * @property {darray_t*} children * @annotation ["readable"] * 全部子控件。 */ - array_t* children; + darray_t* children; /** * @property {emitter_t*} emitter * @annotation ["readable"] diff --git a/src/base/widget_factory.c b/src/base/widget_factory.c index 586836aa45..fb62ac722a 100644 --- a/src/base/widget_factory.c +++ b/src/base/widget_factory.c @@ -62,6 +62,10 @@ typedef struct _creator_item_t { widget_create_t create; } creator_item_t; +static int32_t creator_item_cmp(const creator_item_t* iter, const char* type) { + return strcmp(iter->type, type); +} + static const creator_item_t s_builtin_creators[] = { {WIDGET_TYPE_DIALOG, dialog_create}, {WIDGET_TYPE_DIALOG_TITLE, dialog_title_create}, @@ -122,7 +126,7 @@ widget_factory_t* widget_factory_create(void) { widget_factory_t* widget_factory_init(widget_factory_t* factory) { return_value_if_fail(factory != NULL, NULL); - array_init(&(factory->creators), 0); + darray_init(&(factory->creators), 0, default_destroy, (tk_compare_t)creator_item_cmp); return factory; } @@ -136,17 +140,14 @@ ret_t widget_factory_register(widget_factory_t* factory, const char* type, widge item->create = create; tk_strncpy(item->type, type, TK_NAME_LEN); - array_push(&(factory->creators), item); + darray_push(&(factory->creators), item); return RET_OK; } widget_t* widget_factory_create_widget(widget_factory_t* factory, const char* type, widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h) { - uint32_t i = 0; - uint32_t nr = 0; const creator_item_t* iter = NULL; - const creator_item_t** items = NULL; return_value_if_fail(factory != NULL && type != NULL, NULL); iter = widget_factory_find_builtin_creator(type); @@ -154,17 +155,10 @@ widget_t* widget_factory_create_widget(widget_factory_t* factory, const char* ty return iter->create(parent, x, y, w, h); } - items = (const creator_item_t**)(factory->creators.elms); - return_value_if_fail(items != NULL, NULL); - - for (i = 0, nr = factory->creators.size; i < nr; i++) { - iter = items[i]; - if (tk_str_eq(iter->type, type)) { - return iter->create(parent, x, y, w, h); - } - } + iter = darray_find(&(factory->creators), type); + return_value_if_fail(iter != NULL, NULL); - return NULL; + return iter->create(parent, x, y, w, h); } ret_t widget_factory_set(widget_factory_t* factory) { @@ -174,21 +168,9 @@ ret_t widget_factory_set(widget_factory_t* factory) { } ret_t widget_factory_deinit(widget_factory_t* factory) { - uint32_t i = 0; - uint32_t nr = 0; - creator_item_t* iter = NULL; - creator_item_t** items = NULL; return_value_if_fail(factory != NULL, RET_BAD_PARAMS); - items = (creator_item_t**)(factory->creators.elms); - if (items != NULL) { - for (i = 0, nr = factory->creators.size; i < nr; i++) { - iter = items[i]; - TKMEM_FREE(iter); - } - - array_deinit(&(factory->creators)); - } + darray_deinit(&(factory->creators)); return RET_OK; } diff --git a/src/base/widget_factory.h b/src/base/widget_factory.h index f5ef70417e..6967ca4418 100644 --- a/src/base/widget_factory.h +++ b/src/base/widget_factory.h @@ -22,13 +22,13 @@ #ifndef TK_WIDGET_FACTORY_H #define TK_WIDGET_FACTORY_H -#include "tkc/array.h" +#include "tkc/darray.h" #include "base/widget.h" BEGIN_C_DECLS typedef struct _widget_factory_t { - array_t creators; + darray_t creators; } widget_factory_t; widget_factory_t* widget_factory(void); diff --git a/src/ext_widgets/keyboard/keyboard.c b/src/ext_widgets/keyboard/keyboard.c index f8cef6d8fc..b52ada33ec 100644 --- a/src/ext_widgets/keyboard/keyboard.c +++ b/src/ext_widgets/keyboard/keyboard.c @@ -64,7 +64,7 @@ widget_t* keyboard_create(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h) { keyboard_t* keyboard = KEYBOARD(widget); return_value_if_fail(keyboard != NULL, NULL); - array_init(&(keyboard->action_buttons), 0); + array_init(&(keyboard->action_buttons), 0, NULL, NULL); widget_on(widget, EVT_WINDOW_LOAD, keyboard_on_load, widget); keyboard->action_info_id = input_method_on(input_method(), EVT_IM_ACTION_INFO, keyboard_on_action_info, widget); diff --git a/src/ext_widgets/keyboard/keyboard.h b/src/ext_widgets/keyboard/keyboard.h index b17b296097..77aa137e43 100644 --- a/src/ext_widgets/keyboard/keyboard.h +++ b/src/ext_widgets/keyboard/keyboard.h @@ -72,7 +72,7 @@ typedef struct _keyboard_t { window_base_t window; /*private*/ - array_t action_buttons; + darray_t action_buttons; uint32_t action_info_id; } keyboard_t; diff --git a/src/ext_widgets/slide_menu/slide_menu.h b/src/ext_widgets/slide_menu/slide_menu.h index 778c23109c..b05eba720b 100644 --- a/src/ext_widgets/slide_menu/slide_menu.h +++ b/src/ext_widgets/slide_menu/slide_menu.h @@ -72,7 +72,7 @@ BEGIN_C_DECLS * ```c * widget_on(slide_menu, EVT_VALUE_CHANGED, on_current_changed, slide_menu); * ``` - + * 可按下面的方法关注当前按钮被点击的事件: * * ```c diff --git a/src/tkc/darray.c b/src/tkc/darray.c index d02e3eb08a..884ab81cf0 100644 --- a/src/tkc/darray.c +++ b/src/tkc/darray.c @@ -20,17 +20,10 @@ */ #include "tkc/darray.h" +#include "tkc/utils.h" #include "tkc/mem.h" -static ret_t dummy_destroy(void* data) { - return RET_OK; -} - -static int32_t dummy_compare(const void* a, const void* b) { - return ((const char*)a - (const char*)b); -} - -darray_t* darray_create(uint16_t capacity, tk_destroy_t destroy, tk_compare_t compare) { +darray_t* darray_create(uint32_t capacity, tk_destroy_t destroy, tk_compare_t compare) { darray_t* darray = TKMEM_ZALLOC(darray_t); return_value_if_fail(darray != NULL, NULL); @@ -43,14 +36,14 @@ darray_t* darray_create(uint16_t capacity, tk_destroy_t destroy, tk_compare_t co } } -darray_t* darray_init(darray_t* darray, uint16_t capacity, tk_destroy_t destroy, +darray_t* darray_init(darray_t* darray, uint32_t capacity, tk_destroy_t destroy, tk_compare_t compare) { return_value_if_fail(darray != NULL, NULL); darray->size = 0; darray->elms = NULL; darray->destroy = destroy != NULL ? destroy : dummy_destroy; - darray->compare = compare != NULL ? compare : dummy_compare; + darray->compare = compare != NULL ? compare : pointer_compare; if (capacity > 0) { darray->elms = TKMEM_ZALLOCN(void*, capacity); @@ -66,7 +59,7 @@ static bool_t darray_extend(darray_t* darray) { return TRUE; } else { void* elms = NULL; - uint16_t capacity = (darray->capacity >> 1) + darray->capacity + 1; + uint32_t capacity = (darray->capacity >> 1) + darray->capacity + 1; elms = TKMEM_REALLOCT(void*, darray->elms, capacity); if (elms) { diff --git a/src/tkc/darray.h b/src/tkc/darray.h index e29119e709..96cb7be9d9 100644 --- a/src/tkc/darray.h +++ b/src/tkc/darray.h @@ -50,17 +50,17 @@ BEGIN_C_DECLS */ typedef struct _darray_t { /** - * @property {uint16_t} size + * @property {uint32_t} size * @annotation ["readable"] * 数组中元素的个数。 */ - uint16_t size; + uint32_t size; /** - * @property {uint16_t} capacity + * @property {uint32_t} capacity * @annotation ["readable"] * 数组的容量大小。 */ - uint16_t capacity; + uint32_t capacity; /** * @property {void**} elms * @annotation ["readable"] @@ -87,26 +87,26 @@ typedef struct _darray_t { * @annotation ["constructor"] * 创建darray对象。 * - * @param {uint16_t*} capacity 数组的初始容量。 + * @param {uint32_t*} capacity 数组的初始容量。 * @param {tk_destroy_t} destroy 元素销毁函数。 * @param {tk_compare_t} compare 元素比较函数。 * * @return {darray_t*} 数组对象。 */ -darray_t* darray_create(uint16_t capacity, tk_destroy_t destroy, tk_compare_t compare); +darray_t* darray_create(uint32_t capacity, tk_destroy_t destroy, tk_compare_t compare); /** * @method darray_init * 初始化darray对象。 * * @param {darray_t*} darray 数组对象。 - * @param {uint16_t*} capacity 数组的初始容量。 + * @param {uint32_t*} capacity 数组的初始容量。 * @param {tk_destroy_t} destroy 元素销毁函数。 * @param {tk_compare_t} compare 元素比较函数。 * * @return {darray_t*} 数组对象。 */ -darray_t* darray_init(darray_t* darray, uint16_t capacity, tk_destroy_t destroy, +darray_t* darray_init(darray_t* darray, uint32_t capacity, tk_destroy_t destroy, tk_compare_t compare); /** diff --git a/src/tkc/slist.c b/src/tkc/slist.c index b46793bc38..b521bc3f53 100644 --- a/src/tkc/slist.c +++ b/src/tkc/slist.c @@ -20,6 +20,7 @@ */ #include "tkc/mem.h" +#include "tkc/utils.h" #include "tkc/slist.h" static slist_node_t* slist_node_create(void* data) { @@ -40,14 +41,6 @@ static ret_t slist_node_destroy(slist_node_t* node, tk_destroy_t destroy) { return RET_OK; } -static ret_t dummy_destroy(void* data) { - return RET_OK; -} - -static int32_t dummy_compare(const void* a, const void* b) { - return ((const char*)a - (const char*)b); -} - slist_t* slist_create(tk_destroy_t destroy, tk_compare_t compare) { slist_t* slist = TKMEM_ZALLOC(slist_t); return_value_if_fail(slist != NULL, NULL); @@ -59,7 +52,7 @@ slist_t* slist_init(slist_t* slist, tk_destroy_t destroy, tk_compare_t compare) return_value_if_fail(slist != NULL, NULL); slist->first = NULL; slist->destroy = destroy != NULL ? destroy : dummy_destroy; - slist->compare = compare != NULL ? compare : dummy_compare; + slist->compare = compare != NULL ? compare : pointer_compare; return slist; } diff --git a/src/tkc/utils.c b/src/tkc/utils.c index 8c79740065..a762f1c096 100644 --- a/src/tkc/utils.c +++ b/src/tkc/utils.c @@ -603,3 +603,17 @@ double tk_watof(const wchar_t* str) { return tk_atof(num); } + +ret_t default_destroy(void* data) { + TKMEM_FREE(data); + + return RET_OK; +} + +ret_t dummy_destroy(void* data) { + return RET_OK; +} + +int32_t pointer_compare(const void* a, const void* b) { + return ((const char*)a - (const char*)b); +} diff --git a/src/tkc/utils.h b/src/tkc/utils.h index d55ceb6e0c..c68f160dbc 100644 --- a/src/tkc/utils.h +++ b/src/tkc/utils.h @@ -67,6 +67,10 @@ ret_t xml_file_expand_read(const char* filename, str_t* s); * 本函数仅供内部使用,可以提高速度,但使用不当极度危险。它要求dst为NULL或内存块的首地址,本函数调用之后,dst可能无效,请保留返回的地址*/ char* tk_str_copy(char* dst, const char* src); +ret_t dummy_destroy(void* data); +ret_t default_destroy(void* data); +int32_t pointer_compare(const void* a, const void* b); + END_C_DECLS #endif /*TK_UTILS_H*/