staticroots.c raw

   1  
   2  #include <stdio.h>
   3  #include <string.h>
   4  
   5  #ifndef GC_DEBUG
   6  #  define GC_DEBUG
   7  #endif
   8  
   9  #include "gc.h"
  10  #include "gc/gc_backptr.h"
  11  
  12  #ifndef GC_TEST_IMPORT_API
  13  #  define GC_TEST_IMPORT_API extern
  14  #endif
  15  
  16  /* Should match that in `staticroots_lib.c` file. */
  17  struct treenode {
  18    struct treenode *x;
  19    struct treenode *y;
  20  };
  21  
  22  struct treenode *root[10] = { NULL };
  23  
  24  /*
  25   * Same as `root` variable but initialized to some nonzero value (to be
  26   * placed to `.data` section instead of `.bss`).
  27   */
  28  struct treenode *root_nz[10] = { (struct treenode *)(GC_uintptr_t)1 };
  29  
  30  /* Note: this is `static` intentionally. */
  31  static char *staticroot;
  32  
  33  GC_TEST_IMPORT_API struct treenode *libsrl_mktree(int i);
  34  GC_TEST_IMPORT_API void *libsrl_init(void);
  35  GC_TEST_IMPORT_API struct treenode **libsrl_getpelem(int i, int j);
  36  
  37  GC_TEST_IMPORT_API struct treenode **libsrl_getpelem2(int i, int j);
  38  
  39  static void
  40  init_staticroot(void)
  41  {
  42    /*
  43     * Intentionally put `staticroot` initialization in a function other than
  44     * `main` to prevent CSA warning (that `staticroot` variable can be changed
  45     * to be a local one).
  46     */
  47    staticroot = (char *)libsrl_init();
  48  }
  49  
  50  int
  51  main(void)
  52  {
  53    int i, j;
  54  
  55  #ifdef STATICROOTSLIB_INIT_IN_MAIN
  56    GC_INIT();
  57  #endif
  58    init_staticroot();
  59    if (GC_get_find_leak())
  60      printf("This test program is not designed for leak detection mode\n");
  61    if (NULL == staticroot) {
  62      fprintf(stderr, "GC_malloc returned NULL\n");
  63      return 2;
  64    }
  65    memset(staticroot, 0x42, sizeof(struct treenode));
  66    GC_gcollect();
  67    for (j = 0; j < 4; j++) {
  68      for (i = 0; i < (int)(sizeof(root) / sizeof(root[0])); ++i) {
  69  #ifdef STATICROOTSLIB2
  70        *libsrl_getpelem2(i, j) = libsrl_mktree(12);
  71  #endif
  72        *libsrl_getpelem(i, j) = libsrl_mktree(12);
  73        ((j & 1) != 0 ? root_nz : root)[i] = libsrl_mktree(12);
  74        GC_gcollect();
  75      }
  76      for (i = 0; i < (int)sizeof(struct treenode); ++i) {
  77        if (staticroot[i] != 0x42) {
  78          fprintf(stderr, "Memory check failed\n");
  79          return 1;
  80        }
  81      }
  82    }
  83    printf("SUCCEEDED\n");
  84    return 0;
  85  }
  86