var_export.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #ifndef _VAR_EXPORT_H__
  12. #define _VAR_EXPORT_H__
  13. #include <rtthread.h>
  14. /* exported object */
  15. struct ve_exporter
  16. {
  17. const char *module; /* module name */
  18. const char *identifier; /* module identifier */
  19. rt_base_t value; /* module value */
  20. };
  21. typedef struct ve_exporter ve_exporter_t;
  22. /* module object */
  23. struct ve_module
  24. {
  25. const ve_exporter_t *begin; /* the first module of the same name */
  26. const ve_exporter_t *end; /* the last module of the same */
  27. };
  28. typedef struct ve_module ve_module_t;
  29. /* iterator object */
  30. struct ve_iterator
  31. {
  32. const ve_exporter_t *exp_index; /* iterator index */
  33. const ve_exporter_t *exp_end; /* iterate over exporter */
  34. };
  35. typedef struct ve_iterator ve_iterator_t;
  36. #define VE_NOT_FOUND (0xFFFFFFFFu) /* not found */
  37. /* exporter's export command */
  38. #if defined(__ARMCC_VERSION) || defined(__IAR_SYSTEMS_ICC__)
  39. #define VAR_EXPORT(module, identi, value) \
  40. const char _vexp_##identi##_module[] rt_section(".rodata.vexp") = #module; \
  41. const char _vexp_##identi##_identi[] rt_section(".rodata.vexp") = #identi; \
  42. rt_used const struct ve_exporter _vexp_##module##identi \
  43. rt_section("1."#module".VarExpTab."#identi) = \
  44. { \
  45. _vexp_##identi##_module, \
  46. _vexp_##identi##_identi, \
  47. value, \
  48. }
  49. #elif defined(__GNUC__)
  50. #define VAR_EXPORT(module, identi, value) \
  51. const char _vexp_##identi##_module[] rt_section(".rodata.vexp") = #module; \
  52. const char _vexp_##identi##_identi[] rt_section(".rodata.vexp") = #identi; \
  53. rt_used const struct ve_exporter _vexp_##module##identi \
  54. rt_section(#module".VarExpTab."#identi) = \
  55. { \
  56. _vexp_##identi##_module, \
  57. _vexp_##identi##_identi, \
  58. value, \
  59. }
  60. #elif defined(_MSC_VER)
  61. #pragma section("VarExpTab$f",read)
  62. #define VAR_EXPORT(module, identi, value) \
  63. const char _vexp_##identi##_module[] rt_section(".rodata.vexp") = #module; \
  64. const char _vexp_##identi##_identi[] rt_section(".rodata.vexp") = #identi; \
  65. __declspec(allocate("VarExpTab$f")) \
  66. rt_used const struct ve_exporter _vexp_##module##identi = \
  67. { \
  68. _vexp_##identi##_module, \
  69. _vexp_##identi##_identi, \
  70. value, \
  71. }
  72. #endif
  73. /* initialize var export */
  74. int ve_exporter_init(void);
  75. /* initialize module */
  76. int ve_module_init(ve_module_t *mod, const char *module);
  77. /* initialize iterator */
  78. void ve_iter_init(ve_module_t *mod, ve_iterator_t *iter);
  79. /* iterate backward */
  80. const ve_exporter_t *ve_iter_next(ve_iterator_t *iter);
  81. /* get the value by identifier */
  82. rt_base_t ve_value_get(ve_module_t *mod, const char *identifier);
  83. /* check if this value exists in the module*/
  84. rt_bool_t ve_value_exist(ve_module_t *mod, const char *identifier);
  85. rt_size_t ve_value_count(ve_module_t *mod);
  86. const ve_exporter_t *ve_binary_search(ve_module_t *mod, const char *identifier);
  87. #endif /* _VAR_EXPORT_H__ */