2 * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
4 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
5 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
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.
13 /* Boehm, September 19, 1995 1:26 pm PDT */
19 void GC_default_same_obj_print_proc(GC_PTR p
, GC_PTR q
)
21 void GC_default_same_obj_print_proc (p
, q
)
25 GC_err_printf2("0x%lx and 0x%lx are not in the same object\n",
26 (unsigned long)p
, (unsigned long)q
);
27 ABORT("GC_same_obj test failed");
30 void (*GC_same_obj_print_proc
) GC_PROTO((GC_PTR
, GC_PTR
))
31 = GC_default_same_obj_print_proc
;
33 /* Check that p and q point to the same object. Call */
34 /* *GC_same_obj_print_proc if they don't. */
35 /* Returns the first argument. (Return value may be hard */
36 /* to use,due to typing issues. But if we had a suitable */
37 /* preprocessor ...) */
38 /* Succeeds if neither p nor q points to the heap. */
39 /* We assume this is performance critical. (It shouldn't */
40 /* be called by production code, but this can easily make */
41 /* debugging intolerably slow.) */
43 GC_PTR
GC_same_obj(register void *p
, register void *q
)
45 GC_PTR
GC_same_obj(p
, q
)
49 register struct hblk
*h
;
51 register ptr_t base
, limit
;
54 if (!GC_is_initialized
) GC_init();
57 if (divHBLKSZ((word
)p
) != divHBLKSZ((word
)q
)
58 && HDR((word
)q
) != 0) {
63 /* If it's a pointer to the middle of a large object, move it */
64 /* to the beginning. */
65 if (IS_FORWARDING_ADDR_OR_NIL(hhdr
)) {
66 h
= HBLKPTR(p
) - (word
)hhdr
;
68 while (IS_FORWARDING_ADDR_OR_NIL(hhdr
)) {
69 h
= FORWARDED_ADDR(h
, hhdr
);
72 limit
= (ptr_t
)((word
*)h
+ HDR_WORDS
+ hhdr
-> hb_sz
);
73 if ((ptr_t
)p
>= limit
|| (ptr_t
)q
>= limit
|| (ptr_t
)q
< (ptr_t
)h
) {
78 sz
= WORDS_TO_BYTES(hhdr
-> hb_sz
);
79 if (sz
> WORDS_TO_BYTES(MAXOBJSZ
)) {
80 base
= (ptr_t
)HBLKPTR(p
);
82 if ((ptr_t
)p
>= limit
) {
86 # ifdef ALL_INTERIOR_POINTERS
87 register map_entry_type map_entry
;
90 pdispl
= HBLKDISPL(p
);
91 map_entry
= MAP_ENTRY((hhdr
-> hb_map
), pdispl
);
92 if (map_entry
== OBJ_INVALID
) {
95 base
= (char *)((word
)p
& ~(WORDS_TO_BYTES(1) - 1));
96 base
-= WORDS_TO_BYTES(map_entry
);
99 register int offset
= HBLKDISPL(p
) - HDR_BYTES
;
100 register word correction
= offset
% sz
;
102 if (HBLKPTR(p
) != HBLKPTR(q
)) {
103 /* The following computation otherwise fails in this case */
106 base
= (ptr_t
)p
- correction
;
110 /* [base, limit) delimits the object containing p, if any. */
111 /* If p is not inside a valid object, then either q is */
112 /* also outside any valid object, or it is outside */
114 if ((ptr_t
)q
>= limit
|| (ptr_t
)q
< base
) {
119 (*GC_same_obj_print_proc
)((ptr_t
)p
, (ptr_t
)q
);
124 void GC_default_is_valid_displacement_print_proc (GC_PTR p
)
126 void GC_default_is_valid_displacement_print_proc (p
)
130 GC_err_printf1("0x%lx does not point to valid object displacement\n",
132 ABORT("GC_is_valid_displacement test failed");
135 void (*GC_is_valid_displacement_print_proc
) GC_PROTO((GC_PTR
)) =
136 GC_default_is_valid_displacement_print_proc
;
138 /* Check that if p is a pointer to a heap page, then it points to */
139 /* a valid displacement within a heap object. */
140 /* Uninteresting with ALL_INTERIOR_POINTERS. */
141 /* Always returns its argument. */
142 /* Note that we don't lock, since nothing relevant about the header */
143 /* should change while we have a valid object pointer to the block. */
145 void * GC_is_valid_displacement(void *p
)
147 char *GC_is_valid_displacement(p
)
152 register word pdispl
;
153 register struct hblk
*h
;
154 register map_entry_type map_entry
;
157 if (!GC_is_initialized
) GC_init();
159 if (hhdr
== 0) return(p
);
161 # ifdef ALL_INTERIOR_POINTERS
162 while (IS_FORWARDING_ADDR_OR_NIL(hhdr
)) {
163 h
= FORWARDED_ADDR(h
, hhdr
);
167 if (IS_FORWARDING_ADDR_OR_NIL(hhdr
)) {
170 sz
= WORDS_TO_BYTES(hhdr
-> hb_sz
);
171 pdispl
= HBLKDISPL(p
);
172 map_entry
= MAP_ENTRY((hhdr
-> hb_map
), pdispl
);
173 if (map_entry
== OBJ_INVALID
174 || sz
> MAXOBJSZ
&& (ptr_t
)p
>= (ptr_t
)h
+ sz
) {
179 (*GC_is_valid_displacement_print_proc
)((ptr_t
)p
);
184 void GC_default_is_visible_print_proc(GC_PTR p
)
186 void GC_default_is_visible_print_proc(p
)
190 GC_err_printf1("0x%lx is not a GC visible pointer location\n",
192 ABORT("GC_is_visible test failed");
195 void (*GC_is_visible_print_proc
) GC_PROTO((GC_PTR p
)) =
196 GC_default_is_visible_print_proc
;
198 /* Could p be a stack address? */
199 GC_bool
GC_on_stack(p
)
206 # ifdef STACK_GROWS_DOWN
207 if ((ptr_t
)p
>= (ptr_t
)(&dummy
) && (ptr_t
)p
< GC_stackbottom
) {
211 if ((ptr_t
)p
<= (ptr_t
)(&dummy
) && (ptr_t
)p
> GC_stackbottom
) {
219 /* Check that p is visible */
220 /* to the collector as a possibly pointer containing location. */
221 /* If it isn't invoke *GC_is_visible_print_proc. */
222 /* Returns the argument in all cases. May erroneously succeed */
223 /* in hard cases. (This is intended for debugging use with */
224 /* untyped allocations. The idea is that it should be possible, though */
225 /* slow, to add such a call to all indirect pointer stores.) */
226 /* Currently useless for multithreaded worlds. */
228 void * GC_is_visible(void *p
)
230 char *GC_is_visible(p
)
236 if ((word
)p
& (ALIGNMENT
- 1)) goto fail
;
237 if (!GC_is_initialized
) GC_init();
240 if (hhdr
!= 0 && GC_base(p
) == 0) {
243 /* May be inside thread stack. We can't do much. */
247 /* Check stack first: */
248 if (GC_on_stack(p
)) return(p
);
253 if (GC_is_static_root(p
)) return(p
);
254 /* Else do it again correctly: */
255 # if (defined(DYNAMIC_LOADING) || defined(MSWIN32) || defined(PCR)) \
258 GC_register_dynamic_libraries();
259 result
= GC_is_static_root(p
);
261 if (result
) return(p
);
265 /* p points to the heap. */
267 ptr_t base
= GC_base(p
); /* Should be manually inlined? */
269 if (base
== 0) goto fail
;
270 if (HBLKPTR(base
) != HBLKPTR(p
)) hhdr
= HDR((word
)p
);
271 descr
= hhdr
-> hb_descr
;
273 switch(descr
& DS_TAGS
) {
275 if ((word
)((ptr_t
)p
- (ptr_t
)base
) > (word
)descr
) goto fail
;
278 if ((ptr_t
)p
- (ptr_t
)base
279 >= WORDS_TO_BYTES(BITMAP_BITS
)
280 || ((word
)p
& (sizeof(word
) - 1))) goto fail
;
281 if (!((1 << (WORDSZ
- ((ptr_t
)p
- (ptr_t
)base
) - 1))
285 /* We could try to decipher this partially. */
286 /* For now we just punt. */
289 descr
= *(word
*)((ptr_t
)base
+ (descr
& ~DS_TAGS
));
296 (*GC_is_visible_print_proc
)((ptr_t
)p
);
301 GC_PTR
GC_pre_incr (p
, how_much
)
306 GC_PTR result
= GC_same_obj((GC_PTR
)((word
)initial
+ how_much
), initial
);
308 # ifndef ALL_INTERIOR_POINTERS
309 (void) GC_is_valid_displacement(result
);
311 return (*p
= result
);
314 GC_PTR
GC_post_incr (p
, how_much
)
319 GC_PTR result
= GC_same_obj((GC_PTR
)((word
)initial
+ how_much
), initial
);
321 # ifndef ALL_INTERIOR_POINTERS
322 (void) GC_is_valid_displacement(result
);