var_export.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-06-04 WillianChan first version
  9. * 2021-06-08 WillianChan support to MS VC++ compiler
  10. */
  11. #include <var_export.h>
  12. static const ve_exporter_t *ve_exporter_table = RT_NULL;
  13. static rt_size_t ve_exporter_num = 0;
  14. /* for IAR compiler */
  15. #if defined(__ICCARM__) || defined(__ICCRX__)
  16. #pragma section="VarExpTab"
  17. #endif
  18. /* for ARM C and IAR Compiler */
  19. #if defined(__ARMCC_VERSION) || defined (__ICCARM__) || defined(__ICCRX__)
  20. static rt_used const struct ve_exporter __ve_table_start
  21. rt_section("0.""VarExpTab") = {"ve_start", "ve_start", 0};
  22. static rt_used const struct ve_exporter __ve_table_end
  23. rt_section("2.""VarExpTab") = {"ve_end", "ve_end", 2};
  24. #endif
  25. /* for MS VC++ compiler */
  26. #if defined(_MSC_VER)
  27. #pragma section("VarExpTab$a", read)
  28. __declspec(allocate("VarExpTab$a"))
  29. rt_used const struct ve_exporter __ve_table_start = { "ve_start", "ve_start", 0};
  30. #pragma section("VarExpTab$z", read)
  31. __declspec(allocate("VarExpTab$z"))
  32. rt_used const struct ve_exporter __ve_table_end = { "ve_end", "ve_end", 2};
  33. /* Find var objects in VarExpTab segments */
  34. static int ve_init_find_obj(unsigned int *begin, unsigned int *end, ve_exporter_t *table)
  35. {
  36. int obj_count = 0;
  37. while (begin < end)
  38. {
  39. if (*begin != RT_NULL)
  40. {
  41. *table++ = *((struct ve_exporter *)begin);
  42. begin += sizeof(struct ve_exporter) / sizeof(unsigned int);
  43. obj_count += 1;
  44. }
  45. else
  46. {
  47. begin++;
  48. }
  49. }
  50. return obj_count;
  51. }
  52. #endif /* _MSC_VER */
  53. /* initialize var export */
  54. int var_export_init(void)
  55. {
  56. /* initialize the var export table.*/
  57. #if defined(__ARMCC_VERSION) /* for ARM C Compiler */
  58. ve_exporter_table = &__ve_table_start + 1;
  59. ve_exporter_num = &__ve_table_end - &__ve_table_start;
  60. #elif defined (__IAR_SYSTEMS_ICC__) /* for IAR Compiler */
  61. ve_exporter_table = &__ve_table_start + 1;
  62. ve_exporter_num = &__ve_table_end - &__ve_table_start - 1;
  63. #elif defined (__GNUC__) /* for GCC Compiler */
  64. extern const int __ve_table_start;
  65. extern const int __ve_table_end;
  66. ve_exporter_table = (const ve_exporter_t *)&__ve_table_start;
  67. ve_exporter_num = (const ve_exporter_t *)&__ve_table_end - ve_exporter_table;
  68. #elif defined (_MSC_VER) /* for MS VC++ compiler */
  69. unsigned int *ptr_begin = (unsigned int *)&__ve_table_start;
  70. unsigned int *ptr_end = (unsigned int *)&__ve_table_end;
  71. static ve_exporter_t ve_exporter_tab[2048];
  72. static char __vexp_strbuf1[1024];
  73. static char __vexp_strbuf2[1024];
  74. ve_exporter_t ve_exporter_temp;
  75. rt_size_t index_i, index_j;
  76. /* past the three members in first ptr_begin */
  77. ptr_begin += (sizeof(struct ve_exporter) / sizeof(unsigned int));
  78. while (*ptr_begin == 0) ptr_begin++;
  79. do ptr_end--; while (*ptr_end == 0);
  80. /* Find var objects in custom segments to solve the problem of holes in objects in different files */
  81. ve_exporter_num = ve_init_find_obj(ptr_begin, ptr_end, ve_exporter_tab);
  82. /* check if the ve_exporter_num is out of bounds */
  83. RT_ASSERT(ve_exporter_num < (sizeof(ve_exporter_tab) / sizeof(ve_exporter_t)));
  84. /* bubble sort algorithms */
  85. for (index_i = 0; index_i < (ve_exporter_num - 1); index_i++)
  86. {
  87. for (index_j = 0; index_j < ((ve_exporter_num - 1) - index_i); index_j++)
  88. {
  89. /* splice ve_exporter's module and ve_exporter's identifier into a complete string */
  90. rt_snprintf(__vexp_strbuf1,
  91. sizeof(__vexp_strbuf1),
  92. "%s%s",
  93. ve_exporter_tab[index_j].module,
  94. ve_exporter_tab[index_j].identifier);
  95. rt_snprintf(__vexp_strbuf2,
  96. sizeof(__vexp_strbuf2),
  97. "%s%s",
  98. ve_exporter_tab[index_j + 1].module,
  99. ve_exporter_tab[index_j + 1].identifier);
  100. if (rt_strcmp(__vexp_strbuf1, __vexp_strbuf2) > 0)
  101. {
  102. ve_exporter_temp = ve_exporter_tab[index_j];
  103. ve_exporter_tab[index_j] = ve_exporter_tab[index_j + 1];
  104. ve_exporter_tab[index_j + 1] = ve_exporter_temp;
  105. }
  106. }
  107. }
  108. ve_exporter_table = ve_exporter_tab;
  109. #endif /* __ARMCC_VERSION */
  110. return ve_exporter_num;
  111. }
  112. INIT_BOARD_EXPORT(var_export_init);
  113. /* initialize module */
  114. int ve_module_init(ve_module_t *mod, const char *module)
  115. {
  116. const ve_exporter_t *exporter = ve_exporter_table;
  117. rt_bool_t first_exist = RT_FALSE;
  118. rt_size_t found_index;
  119. for (found_index = 0; found_index < ve_exporter_num; found_index++)
  120. {
  121. if (!rt_strcmp(exporter->module, module))
  122. {
  123. if (first_exist == RT_FALSE)
  124. {
  125. mod->begin = exporter;
  126. first_exist = RT_TRUE;
  127. }
  128. mod->end = exporter;
  129. }
  130. exporter++;
  131. }
  132. if (first_exist == RT_FALSE)
  133. {
  134. return -RT_ERROR;
  135. }
  136. return RT_EOK;
  137. }
  138. /* initialize iterator */
  139. void ve_iter_init(ve_module_t *mod, ve_iterator_t *iter)
  140. {
  141. if (iter)
  142. {
  143. iter->exp_index = mod->begin;
  144. iter->exp_end = mod->end;
  145. }
  146. }
  147. /* iterate backward */
  148. const ve_exporter_t *ve_iter_next(ve_iterator_t *iter)
  149. {
  150. if (iter->exp_index <= iter->exp_end)
  151. {
  152. return iter->exp_index++;
  153. }
  154. else
  155. {
  156. return RT_NULL;
  157. }
  158. }
  159. /* binary search based on identifier */
  160. const ve_exporter_t *ve_binary_search(ve_module_t *mod, const char *identifier)
  161. {
  162. int ve_low_num = 0;
  163. int ve_high_num = mod->end - mod->begin;
  164. int ve_mid_num = 0;
  165. int strcmp_rst = 0;
  166. while ((ve_low_num <= ve_high_num) && (ve_high_num >= 0) && (ve_low_num >= 0))
  167. {
  168. ve_mid_num = (ve_high_num + ve_low_num) / 2;
  169. strcmp_rst = rt_strcmp(mod->begin[ve_mid_num].identifier, identifier);
  170. if (strcmp_rst == 0)
  171. {
  172. return &mod->begin[ve_mid_num];
  173. }
  174. else if (strcmp_rst > 0)
  175. {
  176. ve_high_num = ve_mid_num - 1;
  177. }
  178. else
  179. {
  180. ve_low_num = ve_mid_num + 1;
  181. }
  182. }
  183. return RT_NULL;
  184. }
  185. /* get the value by identifier */
  186. rt_base_t ve_value_get(ve_module_t *mod, const char *identifier)
  187. {
  188. const ve_exporter_t *exporter = ve_binary_search(mod, identifier);
  189. if (exporter)
  190. {
  191. return exporter->value;
  192. }
  193. else
  194. {
  195. return VE_NOT_FOUND;
  196. }
  197. }
  198. /* check if this value exists in the module*/
  199. rt_bool_t ve_value_exist(ve_module_t *mod, const char *identifier)
  200. {
  201. if (ve_binary_search(mod, identifier))
  202. {
  203. return RT_TRUE;
  204. }
  205. else
  206. {
  207. return RT_FALSE;
  208. }
  209. }
  210. rt_size_t ve_value_count(ve_module_t *mod)
  211. {
  212. return mod->end - mod->begin + 1;
  213. }