Debian Codegraph @

external/bdwgc/tests/disclaim_test.c

raw
1 /*
2 * Copyright (c) 2011 by Hewlett-Packard Company. 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
15 /* Test that objects reachable from an object allocated with */
16 /* GC_malloc_with_finalizer is not reclaimable before the finalizer */
17 /* is called. */
18
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22
23 #ifdef HAVE_CONFIG_H
24 /* For GC_[P]THREADS */
25 # include "config.h"
26 #endif
27
28 #undef GC_NO_THREAD_REDIRECTS
29 #include "gc_disclaim.h"
30
31 #ifdef LINT2
32 /* Avoid include gc_priv.h. */
33 # ifndef GC_API_PRIV
34 # define GC_API_PRIV GC_API
35 # endif
36 # ifdef __cplusplus
37 extern "C" {
38 # endif
39 GC_API_PRIV long GC_random(void);
40 # ifdef __cplusplus
41 } /* extern "C" */
42 # endif
43 # undef rand
44 # define rand() (int)GC_random()
45 #endif /* LINT2 */
46
47 #define my_assert(e) \
48 if (!(e)) { \
49 fflush(stdout); \
50 fprintf(stderr, "Assertion failure, line %d: " #e "\n", __LINE__); \
51 exit(-1); \
52 }
53
54 int memeq(void *s, int c, size_t len)
55 {
56 while (len--) {
57 if (*(char *)s != c)
58 return 0;
59 s = (char *)s + 1;
60 }
61 return 1;
62 }
63
64 void GC_CALLBACK misc_sizes_dct(void *obj, void *cd)
65 {
66 unsigned log_size = *(unsigned char *)obj;
67 size_t size;
68
69 my_assert(log_size < sizeof(size_t) * 8);
70 my_assert(cd == NULL);
71 size = (size_t)1 << log_size;
72 my_assert(memeq((char *)obj + 1, 0x56, size - 1));
73 }
74
75 void test_misc_sizes(void)
76 {
77 static const struct GC_finalizer_closure fc = { misc_sizes_dct, NULL };
78 int i;
79 for (i = 1; i <= 20; ++i) { /* Up to 1 MiB. */
80 void *p = GC_finalized_malloc((size_t)1 << i, &fc);
81 if (p == NULL) {
82 fprintf(stderr, "Out of memory!\n");
83 exit(3);
84 }
85 my_assert(memeq(p, 0, (size_t)1 << i));
86 memset(p, 0x56, (size_t)1 << i);
87 *(unsigned char *)p = i;
88 }
89 }
90
91 typedef struct pair_s *pair_t;
92
93 struct pair_s {
94 char magic[16];
95 int checksum;
96 pair_t car;
97 pair_t cdr;
98 };
99
100 static const char * const pair_magic = "PAIR_MAGIC_BYTES";
101
102 int is_pair(pair_t p)
103 {
104 return memcmp(p->magic, pair_magic, sizeof(p->magic)) == 0;
105 }
106
107 void GC_CALLBACK pair_dct(void *obj, void *cd)
108 {
109 pair_t p = (pair_t)obj;
110 int checksum;
111
112 /* Check that obj and its car and cdr are not trashed. */
113 # ifdef DEBUG_DISCLAIM_DESTRUCT
114 printf("Destruct %p = (%p, %p)\n",
115 (void *)p, (void *)p->car, (void *)p->cdr);
116 # endif
117 my_assert(GC_base(obj));
118 my_assert(is_pair(p));
119 my_assert(!p->car || is_pair(p->car));
120 my_assert(!p->cdr || is_pair(p->cdr));
121 checksum = 782;
122 if (p->car) checksum += p->car->checksum;
123 if (p->cdr) checksum += p->cdr->checksum;
124 my_assert(p->checksum == checksum);
125
126 /* Invalidate it. */
127 memset(p->magic, '*', sizeof(p->magic));
128 p->checksum = 0;
129 p->car = (pair_t)cd;
130 p->cdr = NULL;
131 GC_end_stubborn_change(p);
132 }
133
134 pair_t
135 pair_new(pair_t car, pair_t cdr)
136 {
137 pair_t p;
138 static const struct GC_finalizer_closure fc = { pair_dct, NULL };
139
140 p = (pair_t)GC_finalized_malloc(sizeof(struct pair_s), &fc);
141 if (p == NULL) {
142 fprintf(stderr, "Out of memory!\n");
143 exit(3);
144 }
145 my_assert(!is_pair(p));
146 my_assert(memeq(p, 0, sizeof(struct pair_s)));
147 memcpy(p->magic, pair_magic, sizeof(p->magic));
148 p->checksum = 782 + (car? car->checksum : 0) + (cdr? cdr->checksum : 0);
149 p->car = car;
150 p->cdr = cdr;
151 GC_end_stubborn_change(p);
152 # ifdef DEBUG_DISCLAIM_DESTRUCT
153 printf("Construct %p = (%p, %p)\n",
154 (void *)p, (void *)p->car, (void *)p->cdr);
155 # endif
156 return p;
157 }
158
159 void
160 pair_check_rec(pair_t p)
161 {
162 while (p) {
163 int checksum = 782;
164 if (p->car) checksum += p->car->checksum;
165 if (p->cdr) checksum += p->cdr->checksum;
166 my_assert(p->checksum == checksum);
167 if (rand() % 2)
168 p = p->car;
169 else
170 p = p->cdr;
171 }
172 }
173
174 #ifdef GC_PTHREADS
175 # ifndef NTHREADS
176 # define NTHREADS 6
177 # endif
178 # include <pthread.h>
179 #else
180 # undef NTHREADS
181 # define NTHREADS 1
182 #endif
183
184 #define POP_SIZE 1000
185 #if NTHREADS > 1
186 # define MUTATE_CNT (2000000/NTHREADS)
187 #else
188 # define MUTATE_CNT 10000000
189 #endif
190 #define GROW_LIMIT (MUTATE_CNT/10)
191
192 void *test(void *data)
193 {
194 int i;
195 pair_t pop[POP_SIZE];
196 memset(pop, 0, sizeof(pop));
197 for (i = 0; i < MUTATE_CNT; ++i) {
198 int t = rand() % POP_SIZE;
199 switch (rand() % (i > GROW_LIMIT? 5 : 3)) {
200 case 0: case 3:
201 if (pop[t])
202 pop[t] = pop[t]->car;
203 break;
204 case 1: case 4:
205 if (pop[t])
206 pop[t] = pop[t]->cdr;
207 break;
208 case 2:
209 pop[t] = pair_new(pop[rand() % POP_SIZE],
210 pop[rand() % POP_SIZE]);
211 break;
212 }
213 if (rand() % 8 == 1)
214 pair_check_rec(pop[rand() % POP_SIZE]);
215 }
216 return data;
217 }
218
219 int main(void)
220 {
221 # if NTHREADS > 1
222 pthread_t th[NTHREADS];
223 int i;
224 # endif
225
226 GC_set_all_interior_pointers(0); /* for a stricter test */
227 GC_INIT();
228 GC_init_finalized_malloc();
229 # ifndef NO_INCREMENTAL
230 GC_enable_incremental();
231 # endif
232
233 test_misc_sizes();
234
235 # if NTHREADS > 1
236 printf("Threaded disclaim test.\n");
237 for (i = 0; i < NTHREADS; ++i) {
238 int err = pthread_create(&th[i], NULL, test, NULL);
239 if (err) {
240 fprintf(stderr, "Failed to create thread # %d: %s\n", i,
241 strerror(err));
242 exit(1);
243 }
244 }
245 for (i = 0; i < NTHREADS; ++i) {
246 int err = pthread_join(th[i], NULL);
247 if (err) {
248 fprintf(stderr, "Failed to join thread # %d: %s\n", i,
249 strerror(err));
250 exit(69);
251 }
252 }
253 # else
254 printf("Unthreaded disclaim test.\n");
255 test(NULL);
256 # endif
257 return 0;
258 }