File: realloc_test.c

package info (click to toggle)
libgc 1%3A8.2.8-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 5,128 kB
  • sloc: ansic: 40,056; sh: 4,449; cpp: 1,066; makefile: 200; asm: 77
file content (50 lines) | stat: -rw-r--r-- 1,046 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

#include <stdio.h>
#include <stdlib.h>

#include "gc.h"

#define COUNT 10000000

#define CHECK_OUT_OF_MEMORY(p) \
    do { \
        if (NULL == (p)) { \
            fprintf(stderr, "Out of memory\n"); \
            exit(69); \
        } \
    } while (0)

int main(void) {
  int i;
  unsigned long last_heap_size = 0;

  GC_INIT();
  if (GC_get_find_leak())
    printf("This test program is not designed for leak detection mode\n");

  for (i = 0; i < COUNT; i++) {
    int **p = GC_NEW(int *);
    int *q;

    CHECK_OUT_OF_MEMORY(p);
    q = (int *)GC_MALLOC_ATOMIC(sizeof(int));
    CHECK_OUT_OF_MEMORY(q);
    if (*p != NULL) {
      fprintf(stderr, "GC_malloc returned garbage (or NULL)\n");
      exit(1);
    }

    *p = (int *)GC_REALLOC(q, 2 * sizeof(int));
    CHECK_OUT_OF_MEMORY(*p);

    if (i % 10 == 0) {
      unsigned long heap_size = (unsigned long)GC_get_heap_size();

      if (heap_size != last_heap_size) {
        printf("Heap size: %lu\n", heap_size);
        last_heap_size = heap_size;
      }
    }
  }
  return 0;
}