[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 <chentingwei@rt-thread.com>

* [modify][components][utilities]This PR complies with RT-Thread code specification

Signed-off-by: WillianChan <chentingwei@rt-thread.com>

* [fix][components][utilities][var_export]replace the search algorithm and fix some problems

Signed-off-by: WillianChan <chentingwei@rt-thread.com>

* [modify][components][utilities][var_export]make the code more beautiful

Signed-off-by: WillianChan <chentingwei@rt-thread.com>
This commit is contained in:
WillianChan 陈威廉 2022-02-23 23:55:27 +08:00 committed by GitHub
parent 2c165449ee
commit 4106c43753
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 54 additions and 26 deletions

View File

@ -37,7 +37,29 @@ RT_USED const struct ve_exporter __ve_table_start = { "ve_start", "ve_start", 0}
#pragma section("VarExpTab$z", read) #pragma section("VarExpTab$z", read)
__declspec(allocate("VarExpTab$z")) __declspec(allocate("VarExpTab$z"))
RT_USED const struct ve_exporter __ve_table_end = { "ve_end", "ve_end", 2}; 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 */ /* initialize var export */
int var_export_init(void) 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_begin = (unsigned int *)&__ve_table_start;
unsigned int *ptr_end = (unsigned int *)&__ve_table_end; unsigned int *ptr_end = (unsigned int *)&__ve_table_end;
static ve_exporter_t ve_exporter_tab[2048]; static ve_exporter_t ve_exporter_tab[2048];
static char __vexp_strbuf1[1024];
static char __vexp_strbuf2[1024];
ve_exporter_t ve_exporter_temp; 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 */ /* past the three members in first ptr_begin */
ptr_begin += (sizeof(struct ve_exporter) / sizeof(unsigned int)); ptr_begin += (sizeof(struct ve_exporter) / sizeof(unsigned int));
while (*ptr_begin == 0) ptr_begin++; while (*ptr_begin == 0) ptr_begin++;
do ptr_end--; while (*ptr_end == 0); do ptr_end--; while (*ptr_end == 0);
ve_exporter_table = (const ve_exporter_t *)ptr_begin; /* Find var objects in custom segments to solve the problem of holes in objects in different files */
ve_exporter_num = (ptr_end - ptr_begin) / (sizeof(struct ve_exporter) / sizeof(unsigned int)) + 1; ve_exporter_num = ve_init_find_obj(ptr_begin, ptr_end, ve_exporter_tab);
/* check if the ve_exporter_num is out of bounds */ /* check if the ve_exporter_num is out of bounds */
RT_ASSERT(ve_exporter_num < (sizeof(ve_exporter_tab) / sizeof(ve_exporter_t))); RT_ASSERT(ve_exporter_num < (sizeof(ve_exporter_tab) / sizeof(ve_exporter_t)));
for (index_i = 0; index_i < ve_exporter_num; index_i++) /* bubble sort algorithms */
{
ve_exporter_tab[index_i] = ve_exporter_table[index_i];
}
for (index_i = 0; index_i < (ve_exporter_num - 1); index_i++) for (index_i = 0; index_i < (ve_exporter_num - 1); index_i++)
{ {
index_min = index_i; for (index_j = 0; index_j < ((ve_exporter_num - 1) - index_i); index_j++)
for (index_j = index_i + 1; index_j < ve_exporter_num; index_j++)
{ {
if (rt_strcmp(ve_exporter_tab[index_j].module, ve_exporter_tab[index_min].module) < 0 && /* splice ve_exporter's module and ve_exporter's identifier into a complete string */
rt_strcmp(ve_exporter_tab[index_j].identifier, ve_exporter_tab[index_min].identifier) < 0) 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_j];
ve_exporter_temp = ve_exporter_tab[index_min]; ve_exporter_tab[index_j] = ve_exporter_tab[index_j + 1];
ve_exporter_tab[index_min] = ve_exporter_tab[index_i]; ve_exporter_tab[index_j + 1] = ve_exporter_temp;
ve_exporter_tab[index_i] = ve_exporter_temp;
} }
} }
} }
ve_exporter_table = ve_exporter_tab; ve_exporter_table = ve_exporter_tab;
#endif /* __ARMCC_VERSION */ #endif /* __ARMCC_VERSION */
@ -155,14 +183,14 @@ const ve_exporter_t *ve_iter_next(ve_iterator_t *iter)
/* binary search based on identifier */ /* binary search based on identifier */
static const ve_exporter_t *ve_binary_search(ve_module_t *mod, const char *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; int ve_low_num = 0;
rt_size_t ve_high_num = mod->end - ve_exporter_table; int ve_high_num = mod->end - mod->begin;
rt_size_t ve_mid_num; int ve_mid_num = 0;
int strcmp_rst; 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); strcmp_rst = rt_strcmp(mod->begin[ve_mid_num].identifier, identifier);
if (strcmp_rst == 0) 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) else if (strcmp_rst > 0)
{ {
ve_high_num = ve_mid_num + 1; ve_high_num = ve_mid_num - 1;
} }
else else
{ {
ve_low_num = ve_mid_num - 1; ve_low_num = ve_mid_num + 1;
} }
} }