Debian Codegraph @

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