* src/mm/memory.c, src/mm/memory.h: Moved defines for memcheck into header file.
[cacao.git] / src / mm / boehm-gc / checksums.c
1 /*
2  * Copyright (c) 1992-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 /* Boehm, March 29, 1995 12:51 pm PST */
14
15 #include "config.h"
16
17 # ifdef CHECKSUMS
18
19 # include "private/gc_priv.h"
20
21 /* This is debugging code intended to verify the results of dirty bit   */
22 /* computations. Works only in a single threaded environment.           */
23 /* We assume that stubborn objects are changed only when they are       */
24 /* enabled for writing.  (Certain kinds of writing are actually         */
25 /* safe under other conditions.)                                        */
26 # define NSUMS 10000
27
28 # define OFFSET 0x10000
29
30 typedef struct {
31         GC_bool new_valid;
32         word old_sum;
33         word new_sum;
34         struct hblk * block;    /* Block to which this refers + OFFSET  */
35                                 /* to hide it from collector.           */
36 } page_entry;
37
38 page_entry GC_sums [NSUMS];
39
40 word GC_checksum(h)
41 struct hblk *h;
42 {
43     register word *p = (word *)h;
44     register word *lim = (word *)(h+1);
45     register word result = 0;
46     
47     while (p < lim) {
48         result += *p++;
49     }
50     return(result | 0x80000000 /* doesn't look like pointer */);
51 }
52
53 # ifdef STUBBORN_ALLOC
54 /* Check whether a stubborn object from the given block appears on      */
55 /* the appropriate free list.                                           */
56 GC_bool GC_on_free_list(h)
57 struct hblk *h;
58 {
59     register hdr * hhdr = HDR(h);
60     register int sz = hhdr -> hb_sz;
61     ptr_t p;
62     
63     if (sz > MAXOBJSZ) return(FALSE);
64     for (p = GC_sobjfreelist[sz]; p != 0; p = obj_link(p)) {
65         if (HBLKPTR(p) == h) return(TRUE);
66     }
67     return(FALSE);
68 }
69 # endif
70  
71 int GC_n_dirty_errors;
72 int GC_n_changed_errors;
73 int GC_n_clean;
74 int GC_n_dirty;
75
76 void GC_update_check_page(h, index)
77 struct hblk *h;
78 int index;
79 {
80     page_entry *pe = GC_sums + index;
81     register hdr * hhdr = HDR(h);
82     struct hblk *b;
83     
84     if (pe -> block != 0 && pe -> block != h + OFFSET) ABORT("goofed");
85     pe -> old_sum = pe -> new_sum;
86     pe -> new_sum = GC_checksum(h);
87 #   if !defined(MSWIN32) && !defined(MSWINCE)
88         if (pe -> new_sum != 0x80000000 && !GC_page_was_ever_dirty(h)) {
89             GC_printf1("GC_page_was_ever_dirty(0x%lx) is wrong\n",
90                        (unsigned long)h);
91         }
92 #   endif
93     if (GC_page_was_dirty(h)) {
94         GC_n_dirty++;
95     } else {
96         GC_n_clean++;
97     }
98     b = h;
99     while (IS_FORWARDING_ADDR_OR_NIL(hhdr) && hhdr != 0) {
100         b -= (word)hhdr;
101         hhdr = HDR(b);
102     }
103     if (pe -> new_valid
104         && hhdr != 0 && hhdr -> hb_descr != 0 /* may contain pointers */
105         && pe -> old_sum != pe -> new_sum) {
106         if (!GC_page_was_dirty(h) || !GC_page_was_ever_dirty(h)) {
107             /* Set breakpoint here */GC_n_dirty_errors++;
108         }
109 #       ifdef STUBBORN_ALLOC
110           if ( hhdr -> hb_map != GC_invalid_map
111             && hhdr -> hb_obj_kind == STUBBORN
112             && !GC_page_was_changed(h)
113             && !GC_on_free_list(h)) {
114             /* if GC_on_free_list(h) then reclaim may have touched it   */
115             /* without any allocations taking place.                    */
116             /* Set breakpoint here */GC_n_changed_errors++;
117           }
118 #       endif
119     }
120     pe -> new_valid = TRUE;
121     pe -> block = h + OFFSET;
122 }
123
124 word GC_bytes_in_used_blocks;
125
126 void GC_add_block(h, dummy)
127 struct hblk *h;
128 word dummy;
129 {
130    register hdr * hhdr = HDR(h);
131    register bytes = WORDS_TO_BYTES(hhdr -> hb_sz);
132    
133    bytes += HBLKSIZE-1;
134    bytes &= ~(HBLKSIZE-1);
135    GC_bytes_in_used_blocks += bytes;
136 }
137
138 void GC_check_blocks()
139 {
140     word bytes_in_free_blocks = GC_large_free_bytes;
141     
142     GC_bytes_in_used_blocks = 0;
143     GC_apply_to_all_blocks(GC_add_block, (word)0);
144     GC_printf2("GC_bytes_in_used_blocks = %ld, bytes_in_free_blocks = %ld ",
145                 GC_bytes_in_used_blocks, bytes_in_free_blocks);
146     GC_printf1("GC_heapsize = %ld\n", GC_heapsize);
147     if (GC_bytes_in_used_blocks + bytes_in_free_blocks != GC_heapsize) {
148         GC_printf0("LOST SOME BLOCKS!!\n");
149     }
150 }
151
152 /* Should be called immediately after GC_read_dirty and GC_read_changed. */
153 void GC_check_dirty()
154 {
155     register int index;
156     register unsigned i;
157     register struct hblk *h;
158     register ptr_t start;
159     
160     GC_check_blocks();
161     
162     GC_n_dirty_errors = 0;
163     GC_n_changed_errors = 0;
164     GC_n_clean = 0;
165     GC_n_dirty = 0;
166     
167     index = 0;
168     for (i = 0; i < GC_n_heap_sects; i++) {
169         start = GC_heap_sects[i].hs_start;
170         for (h = (struct hblk *)start;
171              h < (struct hblk *)(start + GC_heap_sects[i].hs_bytes);
172              h++) {
173              GC_update_check_page(h, index);
174              index++;
175              if (index >= NSUMS) goto out;
176         }
177     }
178 out:
179     GC_printf2("Checked %lu clean and %lu dirty pages\n",
180               (unsigned long) GC_n_clean, (unsigned long) GC_n_dirty);
181     if (GC_n_dirty_errors > 0) {
182         GC_printf1("Found %lu dirty bit errors\n",
183                    (unsigned long)GC_n_dirty_errors);
184     }
185     if (GC_n_changed_errors > 0) {
186         GC_printf1("Found %lu changed bit errors\n",
187                    (unsigned long)GC_n_changed_errors);
188         GC_printf0("These may be benign (provoked by nonpointer changes)\n");
189 #       ifdef THREADS
190             GC_printf0(
191             "Also expect 1 per thread currently allocating a stubborn obj.\n");
192 #       endif
193     }
194 }
195
196 # else
197
198 extern int GC_quiet;
199         /* ANSI C doesn't allow translation units to be empty.  */
200         /* So we guarantee this one is nonempty.                */
201
202 # endif /* CHECKSUMS */