7k-for-each-elem.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* -*-comment-start: "//";comment-end:""-*-
  2. * GNU Mes --- Maxwell Equations of Software
  3. * Copyright © 2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  4. *
  5. * This file is part of GNU Mes.
  6. *
  7. * GNU Mes is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * GNU Mes is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <mes/lib.h>
  21. struct section
  22. {
  23. unsigned char *data;
  24. int offset;
  25. };
  26. struct sym
  27. {
  28. char *name;
  29. int index;
  30. };
  31. struct sym tab3[3] = { "foo", 0, "bar", 1, "baz", 2 };
  32. struct sym tab[] = { "foo", 0, "bar", 1, "baz", 2 };
  33. struct section section;
  34. #define for_each_elem(sec, startoff, elem, type) \
  35. for (elem = (type *) sec->data + startoff; \
  36. elem < (type *) (sec->data + sec->offset); elem++)
  37. #define for_each_elem2(sec, startoff, elem, type) \
  38. elem = sec->data + sizeof (type) * startoff; \
  39. for (;elem < ((type *) (sec->data + sec->offset)); elem++)
  40. int
  41. main ()
  42. {
  43. #if __i386__ || __arm__
  44. int sym_size = 8;
  45. #elif __GNUC__ && __x86_64__
  46. int sym_size = 16;
  47. #elif __MESC__ && __x86_64__
  48. int sym_size = 12;
  49. #endif
  50. struct sym *p;
  51. p = tab3;
  52. section.data = tab;
  53. section.offset = 24;
  54. int size = sizeof (struct sym);
  55. eputs ("size=");
  56. eputs (itoa (size));
  57. eputs ("\n");
  58. if (size != sym_size)
  59. return 1;
  60. struct section *psection = &section;
  61. p = (struct sym *) psection->data + 1;
  62. struct sym *q = tab;
  63. int i = (int) p;
  64. i -= (int) q;
  65. eputs ("diff=");
  66. eputs (itoa (i));
  67. eputs ("\n");
  68. if (i != sym_size)
  69. return 2;
  70. for_each_elem (psection, 1, p, struct section)
  71. {
  72. eputs ("i=");
  73. eputs (itoa (p->index));
  74. eputs (" name=");
  75. eputs (p->name);
  76. eputs ("\n");
  77. }
  78. for_each_elem2 (psection, 1, p, struct section)
  79. {
  80. eputs ("i=");
  81. eputs (itoa (p->index));
  82. eputs (" name=");
  83. eputs (p->name);
  84. eputs ("\n");
  85. }
  86. return 0;
  87. }