2021-07-23 19:48:30 +08:00
|
|
|
/*
|
2022-02-14 14:03:34 +08:00
|
|
|
* Copyright (c) 2006-2022, RT-Thread Development Team
|
2021-07-23 19:48:30 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2021-06-04 WillianChan first version
|
|
|
|
* 2021-06-08 WillianChan support to MS VC++ compiler
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <var_export.h>
|
|
|
|
|
|
|
|
static const ve_exporter_t *ve_exporter_table = RT_NULL;
|
|
|
|
static rt_size_t ve_exporter_num = 0;
|
|
|
|
|
|
|
|
/* for IAR compiler */
|
|
|
|
#if defined(__ICCARM__) || defined(__ICCRX__)
|
|
|
|
#pragma section="VarExpTab"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* for ARM C and IAR Compiler */
|
2021-12-30 03:14:07 +08:00
|
|
|
#if defined(__ARMCC_VERSION) || defined (__ICCARM__) || defined(__ICCRX__)
|
2022-12-12 02:12:03 +08:00
|
|
|
static rt_used const struct ve_exporter __ve_table_start
|
|
|
|
rt_section("0.""VarExpTab") = {"ve_start", "ve_start", 0};
|
2021-07-23 19:48:30 +08:00
|
|
|
|
2022-12-12 02:12:03 +08:00
|
|
|
static rt_used const struct ve_exporter __ve_table_end
|
|
|
|
rt_section("2.""VarExpTab") = {"ve_end", "ve_end", 2};
|
2021-07-23 19:48:30 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* for MS VC++ compiler */
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#pragma section("VarExpTab$a", read)
|
|
|
|
__declspec(allocate("VarExpTab$a"))
|
2022-12-12 02:12:03 +08:00
|
|
|
rt_used const struct ve_exporter __ve_table_start = { "ve_start", "ve_start", 0};
|
2021-07-23 19:48:30 +08:00
|
|
|
|
|
|
|
#pragma section("VarExpTab$z", read)
|
|
|
|
__declspec(allocate("VarExpTab$z"))
|
2022-12-12 02:12:03 +08:00
|
|
|
rt_used const struct ve_exporter __ve_table_end = { "ve_end", "ve_end", 2};
|
2022-02-23 23:55:27 +08:00
|
|
|
|
|
|
|
/* 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 */
|
2021-07-23 19:48:30 +08:00
|
|
|
|
|
|
|
/* initialize var export */
|
|
|
|
int var_export_init(void)
|
|
|
|
{
|
|
|
|
/* initialize the var export table.*/
|
2022-02-14 14:03:34 +08:00
|
|
|
#if defined(__ARMCC_VERSION) /* for ARM C Compiler */
|
2021-07-23 19:48:30 +08:00
|
|
|
ve_exporter_table = &__ve_table_start + 1;
|
|
|
|
ve_exporter_num = &__ve_table_end - &__ve_table_start;
|
2022-02-14 14:03:34 +08:00
|
|
|
#elif defined (__IAR_SYSTEMS_ICC__) /* for IAR Compiler */
|
|
|
|
ve_exporter_table = &__ve_table_start + 1;
|
|
|
|
ve_exporter_num = &__ve_table_end - &__ve_table_start - 1;
|
2021-07-23 19:48:30 +08:00
|
|
|
#elif defined (__GNUC__) /* for GCC Compiler */
|
|
|
|
extern const int __ve_table_start;
|
|
|
|
extern const int __ve_table_end;
|
|
|
|
ve_exporter_table = (const ve_exporter_t *)&__ve_table_start;
|
|
|
|
ve_exporter_num = (const ve_exporter_t *)&__ve_table_end - ve_exporter_table;
|
|
|
|
#elif defined (_MSC_VER) /* for MS VC++ compiler */
|
|
|
|
unsigned int *ptr_begin = (unsigned int *)&__ve_table_start;
|
|
|
|
unsigned int *ptr_end = (unsigned int *)&__ve_table_end;
|
2021-09-02 17:47:44 +08:00
|
|
|
static ve_exporter_t ve_exporter_tab[2048];
|
2022-02-23 23:55:27 +08:00
|
|
|
static char __vexp_strbuf1[1024];
|
|
|
|
static char __vexp_strbuf2[1024];
|
2021-09-02 17:47:44 +08:00
|
|
|
ve_exporter_t ve_exporter_temp;
|
2022-04-02 16:21:45 +08:00
|
|
|
rt_size_t index_i, index_j;
|
2021-07-23 19:48:30 +08:00
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
2022-02-23 23:55:27 +08:00
|
|
|
/* 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);
|
2021-07-23 19:48:30 +08:00
|
|
|
|
2021-09-02 20:02:08 +08:00
|
|
|
/* check if the ve_exporter_num is out of bounds */
|
|
|
|
RT_ASSERT(ve_exporter_num < (sizeof(ve_exporter_tab) / sizeof(ve_exporter_t)));
|
|
|
|
|
2022-02-23 23:55:27 +08:00
|
|
|
/* bubble sort algorithms */
|
2021-07-23 19:48:30 +08:00
|
|
|
for (index_i = 0; index_i < (ve_exporter_num - 1); index_i++)
|
|
|
|
{
|
2022-02-23 23:55:27 +08:00
|
|
|
for (index_j = 0; index_j < ((ve_exporter_num - 1) - index_i); index_j++)
|
2021-07-23 19:48:30 +08:00
|
|
|
{
|
2022-02-23 23:55:27 +08:00
|
|
|
/* 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)
|
2021-07-23 19:48:30 +08:00
|
|
|
{
|
2022-02-23 23:55:27 +08:00
|
|
|
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;
|
2021-07-23 19:48:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-23 23:55:27 +08:00
|
|
|
|
2021-09-02 17:47:44 +08:00
|
|
|
ve_exporter_table = ve_exporter_tab;
|
2021-12-30 03:14:07 +08:00
|
|
|
#endif /* __ARMCC_VERSION */
|
2021-07-23 19:48:30 +08:00
|
|
|
|
|
|
|
return ve_exporter_num;
|
|
|
|
}
|
|
|
|
INIT_PREV_EXPORT(var_export_init);
|
|
|
|
|
|
|
|
/* initialize module */
|
|
|
|
int ve_module_init(ve_module_t *mod, const char *module)
|
|
|
|
{
|
|
|
|
const ve_exporter_t *exporter = ve_exporter_table;
|
|
|
|
rt_bool_t first_exist = RT_FALSE;
|
|
|
|
rt_size_t found_index;
|
|
|
|
|
|
|
|
for (found_index = 0; found_index < ve_exporter_num; found_index++)
|
|
|
|
{
|
|
|
|
if (!rt_strcmp(exporter->module, module))
|
|
|
|
{
|
|
|
|
if (first_exist == RT_FALSE)
|
|
|
|
{
|
|
|
|
mod->begin = exporter;
|
|
|
|
first_exist = RT_TRUE;
|
|
|
|
}
|
|
|
|
mod->end = exporter;
|
|
|
|
}
|
|
|
|
exporter++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (first_exist == RT_FALSE)
|
|
|
|
{
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* initialize iterator */
|
|
|
|
void ve_iter_init(ve_module_t *mod, ve_iterator_t *iter)
|
|
|
|
{
|
|
|
|
if (iter)
|
|
|
|
{
|
|
|
|
iter->exp_index = mod->begin;
|
|
|
|
iter->exp_end = mod->end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* iterate backward */
|
|
|
|
const ve_exporter_t *ve_iter_next(ve_iterator_t *iter)
|
|
|
|
{
|
|
|
|
if (iter->exp_index <= iter->exp_end)
|
|
|
|
{
|
|
|
|
return iter->exp_index++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return RT_NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* binary search based on identifier */
|
2022-06-08 09:51:36 +08:00
|
|
|
const ve_exporter_t *ve_binary_search(ve_module_t *mod, const char *identifier)
|
2021-07-23 19:48:30 +08:00
|
|
|
{
|
2022-02-23 23:55:27 +08:00
|
|
|
int ve_low_num = 0;
|
|
|
|
int ve_high_num = mod->end - mod->begin;
|
|
|
|
int ve_mid_num = 0;
|
|
|
|
int strcmp_rst = 0;
|
2021-07-23 19:48:30 +08:00
|
|
|
|
2022-02-23 23:55:27 +08:00
|
|
|
while ((ve_low_num <= ve_high_num) && (ve_high_num >= 0) && (ve_low_num >= 0))
|
2021-07-23 19:48:30 +08:00
|
|
|
{
|
2022-02-23 23:55:27 +08:00
|
|
|
ve_mid_num = (ve_high_num + ve_low_num) / 2;
|
2021-07-23 19:48:30 +08:00
|
|
|
strcmp_rst = rt_strcmp(mod->begin[ve_mid_num].identifier, identifier);
|
|
|
|
|
|
|
|
if (strcmp_rst == 0)
|
|
|
|
{
|
|
|
|
return &mod->begin[ve_mid_num];
|
|
|
|
}
|
|
|
|
else if (strcmp_rst > 0)
|
|
|
|
{
|
2022-02-23 23:55:27 +08:00
|
|
|
ve_high_num = ve_mid_num - 1;
|
2021-07-23 19:48:30 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-02-23 23:55:27 +08:00
|
|
|
ve_low_num = ve_mid_num + 1;
|
2021-07-23 19:48:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return RT_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get the value by identifier */
|
|
|
|
rt_base_t ve_value_get(ve_module_t *mod, const char *identifier)
|
|
|
|
{
|
|
|
|
const ve_exporter_t *exporter = ve_binary_search(mod, identifier);
|
|
|
|
|
|
|
|
if (exporter)
|
|
|
|
{
|
|
|
|
return exporter->value;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return VE_NOT_FOUND;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if this value exists in the module*/
|
|
|
|
rt_bool_t ve_value_exist(ve_module_t *mod, const char *identifier)
|
|
|
|
{
|
|
|
|
if (ve_binary_search(mod, identifier))
|
|
|
|
{
|
|
|
|
return RT_TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return RT_FALSE;
|
|
|
|
}
|
|
|
|
}
|
2022-06-08 09:51:36 +08:00
|
|
|
|
|
|
|
rt_size_t ve_value_count(ve_module_t *mod)
|
|
|
|
{
|
|
|
|
return mod->end - mod->begin + 1;
|
|
|
|
}
|