staticrootstest.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include <stdio.h>
  2. #include <string.h>
  3. #ifndef GC_DEBUG
  4. # define GC_DEBUG
  5. #endif
  6. #include "gc.h"
  7. #include "gc_backptr.h"
  8. #ifndef GC_TEST_IMPORT_API
  9. # define GC_TEST_IMPORT_API extern
  10. #endif
  11. /* Should match that in staticrootslib.c. */
  12. struct treenode {
  13. struct treenode *x;
  14. struct treenode *y;
  15. };
  16. struct treenode *root[10] = { NULL };
  17. /* Same as "root" variable but initialized to some non-zero value (to */
  18. /* be placed to .data section instead of .bss). */
  19. struct treenode *root_nz[10] = { (struct treenode *)(GC_word)1 };
  20. static char *staticroot; /* intentionally static */
  21. GC_TEST_IMPORT_API struct treenode * libsrl_mktree(int i);
  22. GC_TEST_IMPORT_API void * libsrl_init(void);
  23. GC_TEST_IMPORT_API struct treenode ** libsrl_getpelem(int i, int j);
  24. GC_TEST_IMPORT_API struct treenode ** libsrl_getpelem2(int i, int j);
  25. void init_staticroot(void)
  26. {
  27. /* Intentionally put staticroot initialization in a function other */
  28. /* than main to prevent CSA warning that staticroot variable can be */
  29. /* changed to be a local one). */
  30. staticroot = (char *)libsrl_init();
  31. }
  32. int main(void)
  33. {
  34. int i, j;
  35. # ifdef STATICROOTSLIB_INIT_IN_MAIN
  36. GC_INIT();
  37. # endif
  38. init_staticroot();
  39. if (NULL == staticroot) {
  40. fprintf(stderr, "GC_malloc returned NULL\n");
  41. return 2;
  42. }
  43. memset(staticroot, 0x42, sizeof(struct treenode));
  44. GC_gcollect();
  45. for (j = 0; j < 4; j++) {
  46. for (i = 0; i < (int)(sizeof(root) / sizeof(root[0])); ++i) {
  47. # ifdef STATICROOTSLIB2
  48. *libsrl_getpelem2(i, j) = libsrl_mktree(12);
  49. # endif
  50. *libsrl_getpelem(i, j) = libsrl_mktree(12);
  51. ((j & 1) != 0 ? root_nz : root)[i] = libsrl_mktree(12);
  52. GC_gcollect();
  53. }
  54. for (i = 0; i < (int)sizeof(struct treenode); ++i) {
  55. if (staticroot[i] != 0x42) {
  56. fprintf(stderr, "Memory check failed\n");
  57. return -1;
  58. }
  59. }
  60. }
  61. return 0;
  62. }