Debian Codegraph @

gc/tools/setjmp_t.c

raw
1 /*
2 * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
3 *
4 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
5 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
6 *
7 * Permission is hereby granted to use or copy this program
8 * for any purpose, provided the above notices are retained on all copies.
9 * Permission to modify the code and to distribute modified code is granted,
10 * provided the above notices are retained, and a notice that the code was
11 * modified is included with the above copyright notice.
12 */
13
14 /* Check whether setjmp actually saves registers in jmp_buf. */
15 /* If it doesn't, the generic mark_regs code won't work. */
16 /* Compilers vary as to whether they will put x in a */
17 /* (callee-save) register without -O. The code is */
18 /* contrived such that any decent compiler should put x in */
19 /* a callee-save register with -O. Thus it is */
20 /* recommended that this be run optimized. (If the machine */
21 /* has no callee-save registers, then the generic code is */
22 /* safe, but this will not be noticed by this piece of */
23 /* code.) This test appears to be far from perfect. */
24 #include <stdio.h>
25 #include <setjmp.h>
26 #include <string.h>
27 #include "private/gc_priv.h"
28
29 #ifdef OS2
30 /* GETPAGESIZE() is set to getpagesize() by default, but that */
31 /* doesn't really exist, and the collector doesn't need it. */
32 #define INCL_DOSFILEMGR
33 #define INCL_DOSMISC
34 #define INCL_DOSERRORS
35 #include <os2.h>
36
37 int getpagesize(void)
38 {
39 ULONG result[1];
40
41 if (DosQuerySysInfo(QSV_PAGE_SIZE, QSV_PAGE_SIZE,
42 (void *)result, sizeof(ULONG)) != NO_ERROR) {
43 fprintf(stderr, "DosQuerySysInfo failed\n");
44 result[0] = 4096;
45 }
46 return((int)(result[0]));
47 }
48
49 #elif defined(MSWIN32) || defined(MSWINCE) || defined(CYGWIN32)
50 int getpagesize(void)
51 {
52 SYSTEM_INFO sysinfo;
53 GetSystemInfo(&sysinfo);
54 return sysinfo.dwPageSize;
55 }
56 #endif
57
58 struct a_s {
59 char a_a;
60 char * a_b;
61 } a;
62
63 word nested_sp(void)
64 {
65 # if GC_GNUC_PREREQ(4, 0)
66 return (word)__builtin_frame_address(0);
67 # else
68 volatile word sp;
69 sp = (word)(&sp);
70 return sp;
71 # endif
72 }
73
74 /* To prevent nested_sp inlining. */
75 word (*volatile nested_sp_fn)(void) = nested_sp;
76
77 int g(int x);
78
79 #if defined(CPPCHECK) || !defined(__cplusplus)
80 const char *a_str = "a";
81 #else
82 # define a_str "a"
83 #endif
84
85 int main(void)
86 {
87 volatile word sp;
88 unsigned ps = GETPAGESIZE();
89 JMP_BUF b;
90 # if (!defined(__cplusplus) || __cplusplus < 201703L /* before c++17 */) \
91 && (!defined(__GNUC__) || defined(__OPTIMIZE__))
92 register
93 # endif
94 int x = (int)strlen(a_str); /* 1, slightly disguised */
95 static volatile int y = 0;
96
97 sp = (word)(&sp);
98 printf("This appears to be a %s running %s\n", MACH_TYPE, OS_TYPE);
99 # if defined(CPPCHECK)
100 (void)nested_sp(); /* to workaround a bug in cppcheck */
101 # endif
102 if (nested_sp_fn() < sp) {
103 printf("Stack appears to grow down, which is the default.\n");
104 printf("A good guess for STACKBOTTOM on this machine is 0x%lx.\n",
105 ((unsigned long)sp + ps) & ~(unsigned long)(ps-1));
106 } else {
107 printf("Stack appears to grow up.\n");
108 printf("Define STACK_GROWS_UP in gc_priv.h\n");
109 printf("A good guess for STACKBOTTOM on this machine is 0x%lx.\n",
110 (unsigned long)sp & ~(unsigned long)(ps-1)); /* round down */
111 }
112 printf("Note that this may vary between machines of ostensibly\n");
113 printf("the same architecture (e.g. Sun 3/50s and 3/80s).\n");
114 printf("On many machines the value is not fixed.\n");
115 printf("A good guess for ALIGNMENT on this machine is %lu.\n",
116 (unsigned long)((word)(&(a.a_b)) - (word)(&a)));
117
118 printf("The following is a very dubious test of one root marking"
119 " strategy.\n");
120 printf("Results may not be accurate/useful:\n");
121 /* Encourage the compiler to keep x in a callee-save register */
122 x = 2*x-1;
123 printf("\n");
124 x = 2*x-1;
125 (void)SETJMP(b);
126 if (y == 1) {
127 if (x == 2) {
128 printf("Setjmp-based generic mark_regs code probably won't work.\n");
129 printf("But we rarely try that anymore. If you have getcontect()\n");
130 printf("this probably doesn't matter.\n");
131 } else if (x == 1) {
132 printf("Setjmp-based register marking code may work.\n");
133 } else {
134 printf("Very strange setjmp implementation.\n");
135 }
136 }
137 y++;
138 x = 2;
139 if (y == 1) LONGJMP(b, 1);
140 printf("Some GC internal configuration stuff: \n");
141 printf("\tWORDSZ = %lu, ALIGNMENT = %d, GC_GRANULE_BYTES = %d\n",
142 (unsigned long)WORDSZ, ALIGNMENT, GC_GRANULE_BYTES);
143 printf("\tUsing one mark ");
144 # if defined(USE_MARK_BYTES)
145 printf("byte");
146 # else
147 printf("bit");
148 # endif
149 printf(" per ");
150 # if defined(MARK_BIT_PER_OBJ)
151 printf("object.\n");
152 # elif defined(MARK_BIT_PER_GRANULE)
153 printf("granule.\n");
154 # endif
155 # ifdef THREAD_LOCAL_ALLOC
156 printf("Thread local allocation enabled.\n");
157 # endif
158 # ifdef PARALLEL_MARK
159 printf("Parallel marking enabled.\n");
160 # endif
161 (void)g(x);
162 return(0);
163 }
164
165 int g(int x)
166 {
167 return(x);
168 }