From 4106c437531f850994d2b0b09eed9a2a2e12d1af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?WillianChan=20=E9=99=88=E5=A8=81=E5=BB=89?= <43905695+willianchanlovegithub@users.noreply.github.com> Date: Wed, 23 Feb 2022 23:55:27 +0800 Subject: [PATCH] [fix][components][utilities][var_export]replace the search algorithm and fix some problems (#5611) * [fix][components][utilities]adjust the order of compiler macros Signed-off-by: WillianChan * [modify][components][utilities]This PR complies with RT-Thread code specification Signed-off-by: WillianChan * [fix][components][utilities][var_export]replace the search algorithm and fix some problems Signed-off-by: WillianChan * [modify][components][utilities][var_export]make the code more beautiful Signed-off-by: WillianChan --- components/utilities/var_export/var_export.c | 80 +++++++++++++------- 1 file changed, 54 insertions(+), 26 deletions(-) diff --git a/components/utilities/var_export/var_export.c b/components/utilities/var_export/var_export.c index 0a6275c180..f4ab40c63a 100644 --- a/components/utilities/var_export/var_export.c +++ b/components/utilities/var_export/var_export.c @@ -37,7 +37,29 @@ RT_USED const struct ve_exporter __ve_table_start = { "ve_start", "ve_start", 0} #pragma section("VarExpTab$z", read) __declspec(allocate("VarExpTab$z")) RT_USED const struct ve_exporter __ve_table_end = { "ve_end", "ve_end", 2}; -#endif + +/* Find var objects in VarExpTab segments */ +static int ve_init_find_obj(unsigned int *begin, unsigned int *end, ve_exporter_t *table) +{ + int obj_count = 0; + + while (begin < end) + { + if (*begin != RT_NULL) + { + *table++ = *((struct ve_exporter *)begin); + begin += sizeof(struct ve_exporter) / sizeof(unsigned int); + obj_count += 1; + } + else + { + begin++; + } + } + + return obj_count; +} +#endif /* _MSC_VER */ /* initialize var export */ int var_export_init(void) @@ -58,41 +80,47 @@ int var_export_init(void) unsigned int *ptr_begin = (unsigned int *)&__ve_table_start; unsigned int *ptr_end = (unsigned int *)&__ve_table_end; static ve_exporter_t ve_exporter_tab[2048]; + static char __vexp_strbuf1[1024]; + static char __vexp_strbuf2[1024]; ve_exporter_t ve_exporter_temp; - int index_i, index_j, index_min; + int index_i, index_j; /* past the three members in first ptr_begin */ ptr_begin += (sizeof(struct ve_exporter) / sizeof(unsigned int)); while (*ptr_begin == 0) ptr_begin++; do ptr_end--; while (*ptr_end == 0); - ve_exporter_table = (const ve_exporter_t *)ptr_begin; - ve_exporter_num = (ptr_end - ptr_begin) / (sizeof(struct ve_exporter) / sizeof(unsigned int)) + 1; + /* Find var objects in custom segments to solve the problem of holes in objects in different files */ + ve_exporter_num = ve_init_find_obj(ptr_begin, ptr_end, ve_exporter_tab); /* check if the ve_exporter_num is out of bounds */ RT_ASSERT(ve_exporter_num < (sizeof(ve_exporter_tab) / sizeof(ve_exporter_t))); - for (index_i = 0; index_i < ve_exporter_num; index_i++) - { - ve_exporter_tab[index_i] = ve_exporter_table[index_i]; - } - + /* bubble sort algorithms */ for (index_i = 0; index_i < (ve_exporter_num - 1); index_i++) { - index_min = index_i; - - for (index_j = index_i + 1; index_j < ve_exporter_num; index_j++) + for (index_j = 0; index_j < ((ve_exporter_num - 1) - index_i); index_j++) { - if (rt_strcmp(ve_exporter_tab[index_j].module, ve_exporter_tab[index_min].module) < 0 && - rt_strcmp(ve_exporter_tab[index_j].identifier, ve_exporter_tab[index_min].identifier) < 0) + /* splice ve_exporter's module and ve_exporter's identifier into a complete string */ + rt_snprintf(__vexp_strbuf1, + sizeof(__vexp_strbuf1), + "%s%s", + ve_exporter_tab[index_j].module, + ve_exporter_tab[index_j].identifier); + rt_snprintf(__vexp_strbuf2, + sizeof(__vexp_strbuf2), + "%s%s", + ve_exporter_tab[index_j + 1].module, + ve_exporter_tab[index_j + 1].identifier); + if (rt_strcmp(__vexp_strbuf1, __vexp_strbuf2) > 0) { - index_min = index_j; - ve_exporter_temp = ve_exporter_tab[index_min]; - ve_exporter_tab[index_min] = ve_exporter_tab[index_i]; - ve_exporter_tab[index_i] = ve_exporter_temp; + ve_exporter_temp = ve_exporter_tab[index_j]; + ve_exporter_tab[index_j] = ve_exporter_tab[index_j + 1]; + ve_exporter_tab[index_j + 1] = ve_exporter_temp; } } } + ve_exporter_table = ve_exporter_tab; #endif /* __ARMCC_VERSION */ @@ -155,14 +183,14 @@ const ve_exporter_t *ve_iter_next(ve_iterator_t *iter) /* binary search based on identifier */ static const ve_exporter_t *ve_binary_search(ve_module_t *mod, const char *identifier) { - rt_size_t ve_low_num = mod->begin - ve_exporter_table; - rt_size_t ve_high_num = mod->end - ve_exporter_table; - rt_size_t ve_mid_num; - int strcmp_rst; + int ve_low_num = 0; + int ve_high_num = mod->end - mod->begin; + int ve_mid_num = 0; + int strcmp_rst = 0; - while (ve_low_num <= ve_high_num) + while ((ve_low_num <= ve_high_num) && (ve_high_num >= 0) && (ve_low_num >= 0)) { - ve_mid_num = (ve_high_num - ve_low_num) / 2; + ve_mid_num = (ve_high_num + ve_low_num) / 2; strcmp_rst = rt_strcmp(mod->begin[ve_mid_num].identifier, identifier); if (strcmp_rst == 0) @@ -171,11 +199,11 @@ static const ve_exporter_t *ve_binary_search(ve_module_t *mod, const char *ident } else if (strcmp_rst > 0) { - ve_high_num = ve_mid_num + 1; + ve_high_num = ve_mid_num - 1; } else { - ve_low_num = ve_mid_num - 1; + ve_low_num = ve_mid_num + 1; } }