realloc.c raw

   1  
   2  #include <stdio.h>
   3  #include <stdlib.h>
   4  
   5  #include "gc.h"
   6  
   7  #define COUNT 10000000
   8  
   9  #define CHECK_OUT_OF_MEMORY(p)            \
  10    do {                                    \
  11      if (NULL == (p)) {                    \
  12        fprintf(stderr, "Out of memory\n"); \
  13        exit(69);                           \
  14      }                                     \
  15    } while (0)
  16  
  17  int
  18  main(void)
  19  {
  20    int i;
  21    unsigned long last_heap_size = 0;
  22  
  23    GC_INIT();
  24    if (GC_get_find_leak())
  25      printf("This test program is not designed for leak detection mode\n");
  26  
  27    for (i = 0; i < COUNT; i++) {
  28      int **p = GC_NEW(int *);
  29      int *q;
  30  
  31      CHECK_OUT_OF_MEMORY(p);
  32      q = (int *)GC_MALLOC_ATOMIC(sizeof(int));
  33      CHECK_OUT_OF_MEMORY(q);
  34      if (*p != NULL) {
  35        fprintf(stderr, "GC_malloc returned garbage (or NULL)\n");
  36        exit(1);
  37      }
  38  
  39      *p = (int *)GC_REALLOC(q, (i % 8 != 0 ? 2 : 4) * sizeof(int));
  40      CHECK_OUT_OF_MEMORY(*p);
  41  
  42      if (i % 10 == 0) {
  43        unsigned long heap_size = (unsigned long)GC_get_heap_size();
  44  
  45        if (heap_size != last_heap_size) {
  46          printf("Heap size: %lu\n", heap_size);
  47          last_heap_size = heap_size;
  48        }
  49      }
  50    }
  51    printf("SUCCEEDED\n");
  52    return 0;
  53  }
  54