Debian Codegraph @

gc/tests/realloc_test.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 main(void) {
18 int i;
19 unsigned long last_heap_size = 0;
20
21 GC_INIT();
22 if (GC_get_find_leak())
23 printf("This test program is not designed for leak detection mode\n");
24
25 for (i = 0; i < COUNT; i++) {
26 int **p = GC_NEW(int *);
27 int *q;
28
29 CHECK_OUT_OF_MEMORY(p);
30 q = (int *)GC_MALLOC_ATOMIC(sizeof(int));
31 CHECK_OUT_OF_MEMORY(q);
32 if (*p != NULL) {
33 fprintf(stderr, "GC_malloc returned garbage (or NULL)\n");
34 exit(1);
35 }
36
37 *p = (int *)GC_REALLOC(q, 2 * sizeof(int));
38 CHECK_OUT_OF_MEMORY(*p);
39
40 if (i % 10 == 0) {
41 unsigned long heap_size = (unsigned long)GC_get_heap_size();
42
43 if (heap_size != last_heap_size) {
44 printf("Heap size: %lu\n", heap_size);
45 last_heap_size = heap_size;
46 }
47 }
48 }
49 return 0;
50 }