staticroots_lib.c raw

   1  
   2  /* This test file is intended to be compiled into a DLL. */
   3  
   4  #include <stdio.h>
   5  #include <stdlib.h>
   6  
   7  #ifndef GC_DEBUG
   8  #  define GC_DEBUG
   9  #endif
  10  
  11  #include "gc.h"
  12  
  13  #ifndef GC_TEST_EXPORT_API
  14  #  if defined(GC_VISIBILITY_HIDDEN_SET) && !defined(__CEGCC__) \
  15        && !defined(__CYGWIN__) && !defined(__MINGW32__)
  16  #    define GC_TEST_EXPORT_API \
  17        extern __attribute__((__visibility__("default")))
  18  #  else
  19  #    define GC_TEST_EXPORT_API extern
  20  #  endif
  21  #endif
  22  
  23  #define CHECK_OUT_OF_MEMORY(p)            \
  24    do {                                    \
  25      if (NULL == (p)) {                    \
  26        fprintf(stderr, "Out of memory\n"); \
  27        exit(69);                           \
  28      }                                     \
  29    } while (0)
  30  
  31  struct treenode {
  32    struct treenode *x;
  33    struct treenode *y;
  34  };
  35  
  36  static struct treenode *root[10] = { 0 };
  37  static struct treenode *root_nz[10] = { (struct treenode *)(GC_uintptr_t)2 };
  38  
  39  /* Declare it to avoid "no previous prototype" clang warning. */
  40  GC_TEST_EXPORT_API struct treenode **libsrl_getpelem(int i, int j);
  41  
  42  #ifdef STATICROOTSLIB2
  43  #  define libsrl_getpelem libsrl_getpelem2
  44  #else
  45  
  46  GC_TEST_EXPORT_API struct treenode *libsrl_mktree(int i);
  47  GC_TEST_EXPORT_API void *libsrl_init(void);
  48  
  49  GC_TEST_EXPORT_API struct treenode *
  50  libsrl_mktree(int i)
  51  {
  52    struct treenode *r = GC_NEW(struct treenode);
  53    struct treenode *x, *y;
  54  
  55    CHECK_OUT_OF_MEMORY(r);
  56    if (0 == i)
  57      return 0;
  58    if (1 == i) {
  59      r = (struct treenode *)GC_MALLOC_ATOMIC(sizeof(struct treenode));
  60      CHECK_OUT_OF_MEMORY(r);
  61    }
  62    x = libsrl_mktree(i - 1);
  63    y = libsrl_mktree(i - 1);
  64    r->x = x;
  65    r->y = y;
  66    if (i != 1) {
  67      GC_END_STUBBORN_CHANGE(r);
  68      GC_reachable_here(x);
  69      GC_reachable_here(y);
  70    }
  71    return r;
  72  }
  73  
  74  GC_TEST_EXPORT_API void *
  75  libsrl_init(void)
  76  {
  77  #  ifdef TEST_MANUAL_VDB
  78    GC_set_manual_vdb_allowed(1);
  79  #  endif
  80  #  ifndef STATICROOTSLIB_INIT_IN_MAIN
  81    GC_INIT();
  82  #  endif
  83  #  ifndef NO_INCREMENTAL
  84    GC_enable_incremental();
  85  #  endif
  86    return GC_MALLOC(sizeof(struct treenode));
  87  }
  88  
  89  #endif /* !STATICROOTSLIB2 */
  90  
  91  GC_TEST_EXPORT_API struct treenode **
  92  libsrl_getpelem(int i, int j)
  93  {
  94  #if defined(CPPCHECK)
  95    struct treenode node = { NULL, NULL };
  96  
  97    GC_noop1_ptr(node.x);
  98    GC_noop1_ptr(node.y);
  99  #endif
 100    return &((j & 1) != 0 ? root_nz : root)[i];
 101  }
 102