From 2330ae048e522f44d9176804d95c6920f21eb73a Mon Sep 17 00:00:00 2001 From: lixianjing Date: Wed, 13 Nov 2019 10:24:46 +0800 Subject: [PATCH] optimize const data --- src/base/dialog.c | 14 +++++----- src/base/image_loader.c | 2 +- src/base/widget.c | 40 ++++++++++++++-------------- src/base/widget.h | 6 ++--- src/platforms/common/sys_tick.c | 19 +++++++------ src/platforms/raw/sys_tick_handler.c | 2 -- src/tkc/mem.c | 6 ++++- src/ui_loader/ui_serializer.c | 4 +-- src/widgets/button.c | 2 +- src/widgets/color_tile.c | 4 +-- src/widgets/combo_box.c | 36 ++++++++++++------------- src/widgets/digit_clock.c | 8 +++--- src/widgets/dragger.c | 4 +-- src/widgets/edit.c | 28 +++++++++---------- src/widgets/image.c | 11 ++++---- src/widgets/overlay.c | 2 +- src/widgets/pages.c | 2 +- src/widgets/popup.c | 18 ++++++------- 18 files changed, 105 insertions(+), 103 deletions(-) diff --git a/src/base/dialog.c b/src/base/dialog.c index a4e843006..616213b4e 100644 --- a/src/base/dialog.c +++ b/src/base/dialog.c @@ -42,13 +42,13 @@ static ret_t dialog_on_add_child(widget_t* widget, widget_t* child) { return RET_CONTINUE; } -static const char* s_dialog_properties[] = {WIDGET_PROP_ANIM_HINT, - WIDGET_PROP_OPEN_ANIM_HINT, - WIDGET_PROP_MOVE_FOCUS_PREV_KEY, - WIDGET_PROP_MOVE_FOCUS_NEXT_KEY, - WIDGET_PROP_CLOSE_ANIM_HINT, - WIDGET_PROP_THEME, - NULL}; +static const char* const s_dialog_properties[] = {WIDGET_PROP_ANIM_HINT, + WIDGET_PROP_OPEN_ANIM_HINT, + WIDGET_PROP_MOVE_FOCUS_PREV_KEY, + WIDGET_PROP_MOVE_FOCUS_NEXT_KEY, + WIDGET_PROP_CLOSE_ANIM_HINT, + WIDGET_PROP_THEME, + NULL}; static ret_t dialog_set_prop(widget_t* widget, const char* name, const value_t* v) { dialog_t* dialog = DIALOG(widget); diff --git a/src/base/image_loader.c b/src/base/image_loader.c index 4282514a6..ff3f91f7a 100644 --- a/src/base/image_loader.c +++ b/src/base/image_loader.c @@ -28,7 +28,7 @@ ret_t image_loader_load(image_loader_t* loader, const asset_info_t* asset, bitma return loader->load(loader, asset, bitmap); } -#define MAX_IMAGE_LOADERS 5 +#define MAX_IMAGE_LOADERS 3 static image_loader_t* s_image_loaders[MAX_IMAGE_LOADERS]; ret_t image_loader_register(image_loader_t* loader) { diff --git a/src/base/widget.c b/src/base/widget.c index 0dc65b063..c0d3953a5 100644 --- a/src/base/widget.c +++ b/src/base/widget.c @@ -1,4 +1,4 @@ -/** +/** * File: widget.c * Author: AWTK Develop Team * Brief: basic class of all widget @@ -2680,28 +2680,28 @@ ret_t widget_prepare_text_style(widget_t* widget, canvas_t* c) { return RET_OK; } -static const char* s_widget_persistent_props[] = {WIDGET_PROP_NAME, - WIDGET_PROP_STYLE, - WIDGET_PROP_TR_TEXT, - WIDGET_PROP_TEXT, - WIDGET_PROP_ANIMATION, - WIDGET_PROP_ENABLE, - WIDGET_PROP_VISIBLE, - WIDGET_PROP_FLOATING, - WIDGET_PROP_CHILDREN_LAYOUT, - WIDGET_PROP_SELF_LAYOUT, - WIDGET_PROP_OPACITY, - WIDGET_PROP_FOCUSED, - WIDGET_PROP_FOCUSABLE, - WIDGET_PROP_WITH_FOCUS_STATE, - WIDGET_PROP_SENSITIVE, - NULL}; +static const char* const s_widget_persistent_props[] = {WIDGET_PROP_NAME, + WIDGET_PROP_STYLE, + WIDGET_PROP_TR_TEXT, + WIDGET_PROP_TEXT, + WIDGET_PROP_ANIMATION, + WIDGET_PROP_ENABLE, + WIDGET_PROP_VISIBLE, + WIDGET_PROP_FLOATING, + WIDGET_PROP_CHILDREN_LAYOUT, + WIDGET_PROP_SELF_LAYOUT, + WIDGET_PROP_OPACITY, + WIDGET_PROP_FOCUSED, + WIDGET_PROP_FOCUSABLE, + WIDGET_PROP_WITH_FOCUS_STATE, + WIDGET_PROP_SENSITIVE, + NULL}; -const char** widget_get_persistent_props(void) { +const char* const* widget_get_persistent_props(void) { return s_widget_persistent_props; } -static ret_t widget_copy_props(widget_t* clone, widget_t* widget, const char** properties) { +static ret_t widget_copy_props(widget_t* clone, widget_t* widget, const char* const* properties) { if (properties != NULL) { value_t v; value_t defval; @@ -2771,7 +2771,7 @@ widget_t* widget_clone(widget_t* widget, widget_t* parent) { #define PROP_EQ(prop) (widget->prop == other->prop) bool_t widget_equal(widget_t* widget, widget_t* other) { bool_t ret = FALSE; - const char** properties = NULL; + const char* const* properties = NULL; return_value_if_fail(widget != NULL && other != NULL, FALSE); ret = PROP_EQ(opacity) && PROP_EQ(enable) && PROP_EQ(visible) && PROP_EQ(vt) && PROP_EQ(x) && diff --git a/src/base/widget.h b/src/base/widget.h index b2256660a..e3f4197a0 100644 --- a/src/base/widget.h +++ b/src/base/widget.h @@ -87,9 +87,9 @@ struct _widget_vtable_t { uint32_t size; const char* type; /*克隆widget时需要复制的属性*/ - const char** clone_properties; + const char* const* clone_properties; /*持久化widget时需要保存的属性*/ - const char** persistent_properties; + const char* const* persistent_properties; /** * 是否可以滚动。 */ @@ -2052,7 +2052,7 @@ ret_t widget_on_paint_end(widget_t* widget, canvas_t* c); #define WIDGET(w) ((widget_t*)(w)) -const char** widget_get_persistent_props(void); +const char* const* widget_get_persistent_props(void); bool_t widget_is_instance_of(widget_t* widget, const widget_vtable_t* vt); #define WIDGET_IS_INSTANCE_OF(widget, name) widget_is_instance_of(widget, TK_REF_VTABLE(name)) diff --git a/src/platforms/common/sys_tick.c b/src/platforms/common/sys_tick.c index 386aa29bc..0cf747ccb 100644 --- a/src/platforms/common/sys_tick.c +++ b/src/platforms/common/sys_tick.c @@ -22,10 +22,17 @@ #include "rtos.h" static volatile uint64_t g_sys_tick; +static bool_t s_sys_tick_enable = FALSE; + +void sys_tick_enable(bool_t enable) { + s_sys_tick_enable = enable; +} void SysTick_Handler(void) { - g_sys_tick++; - rtos_tick(); + if (s_sys_tick_enable) { + g_sys_tick++; + rtos_tick(); + } } uint64_t get_time_ms64() { @@ -35,11 +42,3 @@ uint64_t get_time_ms64() { void sleep_ms(uint32_t ms) { rtos_delay(ms); } - -void delay_ms(uint32_t ms) { - sleep_ms(ms); -} - -void delay_us(uint32_t us) { - sleep_ms(1); -} diff --git a/src/platforms/raw/sys_tick_handler.c b/src/platforms/raw/sys_tick_handler.c index 284a73c25..b2cca7d7f 100644 --- a/src/platforms/raw/sys_tick_handler.c +++ b/src/platforms/raw/sys_tick_handler.c @@ -39,5 +39,3 @@ void sleep_ms(uint32_t ms) { count++; } } - - diff --git a/src/tkc/mem.c b/src/tkc/mem.c index 130bbbba9..d7eac76c1 100644 --- a/src/tkc/mem.c +++ b/src/tkc/mem.c @@ -1,4 +1,4 @@ -/** +/** * File: mem.c * Author: AWTK Develop Team * Brief: simple memory manager @@ -246,6 +246,10 @@ static void* tk_realloc_impl(void* ptr, uint32_t size) { ret_t tk_mem_init(void* buffer, uint32_t size) { return_value_if_fail(buffer != NULL && size > MIN_SIZE, RET_BAD_PARAMS); + if (s_mem_info.buffer != NULL) { + return RET_FAIL; + } + memset(buffer, 0x00, size); s_mem_info.buffer = (char*)buffer; s_mem_info.size = size; diff --git a/src/ui_loader/ui_serializer.c b/src/ui_loader/ui_serializer.c index faba56a09..2627e2926 100644 --- a/src/ui_loader/ui_serializer.c +++ b/src/ui_loader/ui_serializer.c @@ -53,7 +53,7 @@ ret_t ui_widget_serialize_prop(ui_builder_t* writer, const char* name, value_t* } static ret_t ui_widget_serialize_props(ui_builder_t* writer, widget_t* widget, - const char** properties) { + const char* const* properties) { value_t v; value_t defv; uint32_t i = 0; @@ -101,7 +101,7 @@ ret_t ui_widget_serialize(ui_builder_t* writer, widget_t* widget) { ui_widget_serialize_props(writer, widget, widget_get_persistent_props()); if (widget->vt->clone_properties || widget->vt->persistent_properties) { - const char** properties = widget->vt->persistent_properties; + const char* const* properties = widget->vt->persistent_properties; if (properties == NULL) { properties = widget->vt->clone_properties; } diff --git a/src/widgets/button.c b/src/widgets/button.c index 149041e2a..b8afc1e70 100644 --- a/src/widgets/button.c +++ b/src/widgets/button.c @@ -199,7 +199,7 @@ static ret_t button_on_destroy(widget_t* widget) { return button_remove_timer(widget); } -static const char* s_button_properties[] = {WIDGET_PROP_REPEAT, NULL}; +static const char* const s_button_properties[] = {WIDGET_PROP_REPEAT, NULL}; TK_DECL_VTABLE(button) = {.size = sizeof(button_t), .type = WIDGET_TYPE_BUTTON, diff --git a/src/widgets/color_tile.c b/src/widgets/color_tile.c index e2da8ac6c..9b7bb1801 100644 --- a/src/widgets/color_tile.c +++ b/src/widgets/color_tile.c @@ -71,8 +71,8 @@ static ret_t color_tile_set_prop(widget_t* widget, const char* name, const value return RET_NOT_FOUND; } -static const char* s_color_tile_properties[] = {WIDGET_PROP_BG_COLOR, WIDGET_PROP_BORDER_COLOR, - NULL}; +static const char* const s_color_tile_properties[] = {WIDGET_PROP_BG_COLOR, + WIDGET_PROP_BORDER_COLOR, NULL}; TK_DECL_VTABLE(color_tile) = {.size = sizeof(color_tile_t), .type = WIDGET_TYPE_COLOR_TILE, .parent = TK_PARENT_VTABLE(widget), diff --git a/src/widgets/combo_box.c b/src/widgets/combo_box.c index f6389d2ef..410b704db 100644 --- a/src/widgets/combo_box.c +++ b/src/widgets/combo_box.c @@ -31,24 +31,24 @@ static ret_t combo_box_on_button_click(void* ctx, event_t* e); -const char* s_combo_box_properties[] = {WIDGET_PROP_MIN, - WIDGET_PROP_MAX, - WIDGET_PROP_STEP, - WIDGET_PROP_INPUT_TYPE, - WIDGET_PROP_READONLY, - WIDGET_PROP_AUTO_FIX, - WIDGET_PROP_MARGIN, - WIDGET_PROP_LEFT_MARGIN, - WIDGET_PROP_RIGHT_MARGIN, - WIDGET_PROP_TOP_MARGIN, - WIDGET_PROP_BOTTOM_MARGIN, - WIDGET_PROP_TIPS, - WIDGET_PROP_OPTIONS, - WIDGET_PROP_ITEM_HEIGHT, - WIDGET_PROP_OPEN_WINDOW, - WIDGET_PROP_SELECTED_INDEX, - WIDGET_PROP_LOCALIZE_OPTIONS, - NULL}; +const char* const s_combo_box_properties[] = {WIDGET_PROP_MIN, + WIDGET_PROP_MAX, + WIDGET_PROP_STEP, + WIDGET_PROP_INPUT_TYPE, + WIDGET_PROP_READONLY, + WIDGET_PROP_AUTO_FIX, + WIDGET_PROP_MARGIN, + WIDGET_PROP_LEFT_MARGIN, + WIDGET_PROP_RIGHT_MARGIN, + WIDGET_PROP_TOP_MARGIN, + WIDGET_PROP_BOTTOM_MARGIN, + WIDGET_PROP_TIPS, + WIDGET_PROP_OPTIONS, + WIDGET_PROP_ITEM_HEIGHT, + WIDGET_PROP_OPEN_WINDOW, + WIDGET_PROP_SELECTED_INDEX, + WIDGET_PROP_LOCALIZE_OPTIONS, + NULL}; static ret_t combo_box_active(widget_t* widget); static ret_t combo_box_add_selected_index(widget_t* widget, int32_t delta); diff --git a/src/widgets/digit_clock.c b/src/widgets/digit_clock.c index fa2a44985..c4b8cb68a 100644 --- a/src/widgets/digit_clock.c +++ b/src/widgets/digit_clock.c @@ -41,7 +41,7 @@ static uint32_t count_char(const char* p, char c) { static wchar_t* digit_clock_translate_wday(wchar_t* str, uint32_t size, uint32_t wday) { return_value_if_fail(wday < 7, NULL); - static const char* wdays[] = { + static const char* const wdays[] = { "Sun", "Mon", "Tues", "Wed", "Thur", "Fri", "Sat", }; @@ -52,8 +52,8 @@ static wchar_t* digit_clock_translate_wday(wchar_t* str, uint32_t size, uint32_t static wchar_t* digit_clock_translate_month(wchar_t* str, uint32_t size, uint32_t month) { return_value_if_fail(month < 13, NULL); - static const char* months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", - "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"}; + static const char* const months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", + "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"}; const char* utf8 = locale_info_tr(locale_info(), months[month - 1]); return utf8_to_utf16(utf8, str, size); @@ -219,7 +219,7 @@ static ret_t digit_clock_on_destroy(widget_t* widget) { return RET_OK; } -static const char* s_digit_clock_properties[] = {WIDGET_PROP_FORMAT, NULL}; +static const char* const s_digit_clock_properties[] = {WIDGET_PROP_FORMAT, NULL}; TK_DECL_VTABLE(digit_clock) = {.size = sizeof(digit_clock_t), .type = WIDGET_TYPE_DIGIT_CLOCK, diff --git a/src/widgets/dragger.c b/src/widgets/dragger.c index 35d0f5cb6..896536c4c 100644 --- a/src/widgets/dragger.c +++ b/src/widgets/dragger.c @@ -165,8 +165,8 @@ static ret_t dragger_set_prop(widget_t* widget, const char* name, const value_t* return RET_NOT_FOUND; } -static const char* s_dragger_clone_properties[] = {WIDGET_PROP_X_MIN, WIDGET_PROP_X_MAX, - WIDGET_PROP_Y_MIN, WIDGET_PROP_Y_MAX, NULL}; +static const char* const s_dragger_clone_properties[] = { + WIDGET_PROP_X_MIN, WIDGET_PROP_X_MAX, WIDGET_PROP_Y_MIN, WIDGET_PROP_Y_MAX, NULL}; TK_DECL_VTABLE(dragger) = {.size = sizeof(dragger_t), .type = WIDGET_TYPE_DRAGGER, .clone_properties = s_dragger_clone_properties, diff --git a/src/widgets/edit.c b/src/widgets/edit.c index aa4c1bcd3..5e09246e3 100644 --- a/src/widgets/edit.c +++ b/src/widgets/edit.c @@ -1133,20 +1133,20 @@ static ret_t edit_hook_children_button(const idle_info_t* info) { return RET_REMOVE; } -const char* s_edit_properties[] = {WIDGET_PROP_MIN, - WIDGET_PROP_MAX, - WIDGET_PROP_STEP, - WIDGET_PROP_INPUT_TYPE, - WIDGET_PROP_READONLY, - WIDGET_PROP_AUTO_FIX, - WIDGET_PROP_MARGIN, - WIDGET_PROP_LEFT_MARGIN, - WIDGET_PROP_RIGHT_MARGIN, - WIDGET_PROP_TOP_MARGIN, - WIDGET_PROP_BOTTOM_MARGIN, - WIDGET_PROP_TIPS, - WIDGET_PROP_PASSWORD_VISIBLE, - NULL}; +const char* const s_edit_properties[] = {WIDGET_PROP_MIN, + WIDGET_PROP_MAX, + WIDGET_PROP_STEP, + WIDGET_PROP_INPUT_TYPE, + WIDGET_PROP_READONLY, + WIDGET_PROP_AUTO_FIX, + WIDGET_PROP_MARGIN, + WIDGET_PROP_LEFT_MARGIN, + WIDGET_PROP_RIGHT_MARGIN, + WIDGET_PROP_TOP_MARGIN, + WIDGET_PROP_BOTTOM_MARGIN, + WIDGET_PROP_TIPS, + WIDGET_PROP_PASSWORD_VISIBLE, + NULL}; TK_DECL_VTABLE(edit) = {.size = sizeof(edit_t), .type = WIDGET_TYPE_EDIT, .focusable = TRUE, diff --git a/src/widgets/image.c b/src/widgets/image.c index 4fc11f47b..7ba03223e 100644 --- a/src/widgets/image.c +++ b/src/widgets/image.c @@ -94,11 +94,12 @@ static ret_t image_set_prop(widget_t* widget, const char* name, const value_t* v } } -static const char* s_image_clone_properties[] = {WIDGET_PROP_IMAGE, WIDGET_PROP_DRAW_TYPE, - WIDGET_PROP_SCALE_X, WIDGET_PROP_SCALE_Y, - WIDGET_PROP_ANCHOR_X, WIDGET_PROP_ANCHOR_Y, - WIDGET_PROP_ROTATION, WIDGET_PROP_CLICKABLE, - WIDGET_PROP_SELECTABLE, NULL}; +static const char* const s_image_clone_properties[] = { + WIDGET_PROP_IMAGE, WIDGET_PROP_DRAW_TYPE, + WIDGET_PROP_SCALE_X, WIDGET_PROP_SCALE_Y, + WIDGET_PROP_ANCHOR_X, WIDGET_PROP_ANCHOR_Y, + WIDGET_PROP_ROTATION, WIDGET_PROP_CLICKABLE, + WIDGET_PROP_SELECTABLE, NULL}; TK_DECL_VTABLE(image) = {.size = sizeof(image_t), .type = WIDGET_TYPE_IMAGE, diff --git a/src/widgets/overlay.c b/src/widgets/overlay.c index 97cf4cc22..1ff230c64 100644 --- a/src/widgets/overlay.c +++ b/src/widgets/overlay.c @@ -24,7 +24,7 @@ #include "base/enums.h" #include "widgets/overlay.h" -static const char* s_overlay_properties[] = { +static const char* const s_overlay_properties[] = { WIDGET_PROP_MOVE_FOCUS_PREV_KEY, WIDGET_PROP_MOVE_FOCUS_NEXT_KEY, WIDGET_PROP_THEME, NULL}; TK_DECL_VTABLE(overlay) = {.type = WIDGET_TYPE_OVERLAY, diff --git a/src/widgets/pages.c b/src/widgets/pages.c index b83bc37f7..edf13fc8e 100644 --- a/src/widgets/pages.c +++ b/src/widgets/pages.c @@ -109,7 +109,7 @@ static ret_t pages_set_prop(widget_t* widget, const char* name, const value_t* v return RET_NOT_FOUND; } -static const char* s_pages_clone_properties[] = {WIDGET_PROP_VALUE, NULL}; +static const char* const s_pages_clone_properties[] = {WIDGET_PROP_VALUE, NULL}; TK_DECL_VTABLE(pages) = {.size = sizeof(pages_t), .inputable = TRUE, diff --git a/src/widgets/popup.c b/src/widgets/popup.c index 19379593f..70a6c1d80 100644 --- a/src/widgets/popup.c +++ b/src/widgets/popup.c @@ -106,15 +106,15 @@ static ret_t popup_on_event(widget_t* widget, event_t* e) { return window_base_on_event(widget, e); } -static const char* s_popup_properties[] = {WIDGET_PROP_ANIM_HINT, - WIDGET_PROP_OPEN_ANIM_HINT, - WIDGET_PROP_CLOSE_ANIM_HINT, - WIDGET_PROP_THEME, - WIDGET_PROP_CLOSE_WHEN_CLICK, - WIDGET_PROP_CLOSE_WHEN_CLICK_OUTSIDE, - WIDGET_PROP_MOVE_FOCUS_PREV_KEY, - WIDGET_PROP_MOVE_FOCUS_NEXT_KEY, - NULL}; +static const char* const s_popup_properties[] = {WIDGET_PROP_ANIM_HINT, + WIDGET_PROP_OPEN_ANIM_HINT, + WIDGET_PROP_CLOSE_ANIM_HINT, + WIDGET_PROP_THEME, + WIDGET_PROP_CLOSE_WHEN_CLICK, + WIDGET_PROP_CLOSE_WHEN_CLICK_OUTSIDE, + WIDGET_PROP_MOVE_FOCUS_PREV_KEY, + WIDGET_PROP_MOVE_FOCUS_NEXT_KEY, + NULL}; TK_DECL_VTABLE(popup) = {.size = sizeof(popup_t), .type = WIDGET_TYPE_POPUP,