Back to index

plt-scheme  4.2.1
checksums.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1992-1994 by Xerox Corporation.  All rights reserved.
00003  *
00004  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
00005  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
00006  *
00007  * Permission is hereby granted to use or copy this program
00008  * for any purpose,  provided the above notices are retained on all copies.
00009  * Permission to modify the code and to distribute modified code is granted,
00010  * provided the above notices are retained, and a notice that the code was
00011  * modified is included with the above copyright notice.
00012  */
00013 /* Boehm, March 29, 1995 12:51 pm PST */
00014 # ifdef CHECKSUMS
00015 
00016 # include "private/gc_priv.h"
00017 
00018 /* This is debugging code intended to verify the results of dirty bit */
00019 /* computations. Works only in a single threaded environment.         */
00020 /* We assume that stubborn objects are changed only when they are     */
00021 /* enabled for writing.  (Certain kinds of writing are actually              */
00022 /* safe under other conditions.)                               */
00023 # define NSUMS 10000
00024 
00025 # define OFFSET 0x10000
00026 
00027 typedef struct {
00028        GC_bool new_valid;
00029        word old_sum;
00030        word new_sum;
00031        struct hblk * block; /* Block to which this refers + OFFSET  */
00032                             /* to hide it from collector.             */
00033 } page_entry;
00034 
00035 page_entry GC_sums [NSUMS];
00036 
00037 word GC_checksum(h)
00038 struct hblk *h;
00039 {
00040     register word *p = (word *)h;
00041     register word *lim = (word *)(h+1);
00042     register word result = 0;
00043     
00044     while (p < lim) {
00045         result += *p++;
00046     }
00047     return(result | 0x80000000 /* doesn't look like pointer */);
00048 }
00049 
00050 # ifdef STUBBORN_ALLOC
00051 /* Check whether a stubborn object from the given block appears on    */
00052 /* the appropriate free list.                                         */
00053 GC_bool GC_on_free_list(h)
00054 struct hblk *h;
00055 {
00056     register hdr * hhdr = HDR(h);
00057     register int sz = hhdr -> hb_sz;
00058     ptr_t p;
00059     
00060     if (sz > MAXOBJSZ) return(FALSE);
00061     for (p = GC_sobjfreelist[sz]; p != 0; p = obj_link(p)) {
00062         if (HBLKPTR(p) == h) return(TRUE);
00063     }
00064     return(FALSE);
00065 }
00066 # endif
00067  
00068 int GC_n_dirty_errors;
00069 int GC_n_changed_errors;
00070 int GC_n_clean;
00071 int GC_n_dirty;
00072 
00073 void GC_update_check_page(h, index)
00074 struct hblk *h;
00075 int index;
00076 {
00077     page_entry *pe = GC_sums + index;
00078     register hdr * hhdr = HDR(h);
00079     struct hblk *b;
00080     
00081     if (pe -> block != 0 && pe -> block != h + OFFSET) ABORT("goofed");
00082     pe -> old_sum = pe -> new_sum;
00083     pe -> new_sum = GC_checksum(h);
00084 #   if !defined(MSWIN32) && !defined(MSWINCE)
00085         if (pe -> new_sum != 0x80000000 && !GC_page_was_ever_dirty(h)) {
00086             GC_printf1("GC_page_was_ever_dirty(0x%lx) is wrong\n",
00087                      (unsigned long)h);
00088         }
00089 #   endif
00090     if (GC_page_was_dirty(h)) {
00091        GC_n_dirty++;
00092     } else {
00093        GC_n_clean++;
00094     }
00095     b = h;
00096     while (IS_FORWARDING_ADDR_OR_NIL(hhdr) && hhdr != 0) {
00097        b -= (word)hhdr;
00098        hhdr = HDR(b);
00099     }
00100     if (pe -> new_valid
00101        && hhdr != 0 && hhdr -> hb_descr != 0 /* may contain pointers */
00102        && pe -> old_sum != pe -> new_sum) {
00103        if (!GC_page_was_dirty(h) || !GC_page_was_ever_dirty(h)) {
00104            /* Set breakpoint here */GC_n_dirty_errors++;
00105        }
00106 #      ifdef STUBBORN_ALLOC
00107          if ( hhdr -> hb_map != GC_invalid_map
00108            && hhdr -> hb_obj_kind == STUBBORN
00109            && !GC_page_was_changed(h)
00110            && !GC_on_free_list(h)) {
00111            /* if GC_on_free_list(h) then reclaim may have touched it  */
00112            /* without any allocations taking place.                   */
00113            /* Set breakpoint here */GC_n_changed_errors++;
00114          }
00115 #      endif
00116     }
00117     pe -> new_valid = TRUE;
00118     pe -> block = h + OFFSET;
00119 }
00120 
00121 word GC_bytes_in_used_blocks;
00122 
00123 void GC_add_block(h, dummy)
00124 struct hblk *h;
00125 word dummy;
00126 {
00127    register hdr * hhdr = HDR(h);
00128    register bytes = WORDS_TO_BYTES(hhdr -> hb_sz);
00129    
00130    bytes += HBLKSIZE-1;
00131    bytes &= ~(HBLKSIZE-1);
00132    GC_bytes_in_used_blocks += bytes;
00133 }
00134 
00135 void GC_check_blocks()
00136 {
00137     word bytes_in_free_blocks = GC_large_free_bytes;
00138     
00139     GC_bytes_in_used_blocks = 0;
00140     GC_apply_to_all_blocks(GC_add_block, (word)0);
00141     GC_printf2("GC_bytes_in_used_blocks = %ld, bytes_in_free_blocks = %ld ",
00142               GC_bytes_in_used_blocks, bytes_in_free_blocks);
00143     GC_printf1("GC_heapsize = %ld\n", GC_heapsize);
00144     if (GC_bytes_in_used_blocks + bytes_in_free_blocks != GC_heapsize) {
00145        GC_printf0("LOST SOME BLOCKS!!\n");
00146     }
00147 }
00148 
00149 /* Should be called immediately after GC_read_dirty and GC_read_changed. */
00150 void GC_check_dirty()
00151 {
00152     register int index;
00153     register unsigned i;
00154     register struct hblk *h;
00155     register ptr_t start;
00156     
00157     GC_check_blocks();
00158     
00159     GC_n_dirty_errors = 0;
00160     GC_n_changed_errors = 0;
00161     GC_n_clean = 0;
00162     GC_n_dirty = 0;
00163     
00164     index = 0;
00165     for (i = 0; i < GC_n_heap_sects; i++) {
00166        start = GC_heap_sects[i].hs_start;
00167         for (h = (struct hblk *)start;
00168              h < (struct hblk *)(start + GC_heap_sects[i].hs_bytes);
00169              h++) {
00170              GC_update_check_page(h, index);
00171              index++;
00172              if (index >= NSUMS) goto out;
00173         }
00174     }
00175 out:
00176     GC_printf2("Checked %lu clean and %lu dirty pages\n",
00177              (unsigned long) GC_n_clean, (unsigned long) GC_n_dirty);
00178     if (GC_n_dirty_errors > 0) {
00179         GC_printf1("Found %lu dirty bit errors\n",
00180                  (unsigned long)GC_n_dirty_errors);
00181     }
00182     if (GC_n_changed_errors > 0) {
00183        GC_printf1("Found %lu changed bit errors\n",
00184                  (unsigned long)GC_n_changed_errors);
00185        GC_printf0("These may be benign (provoked by nonpointer changes)\n");
00186 #      ifdef THREADS
00187            GC_printf0(
00188            "Also expect 1 per thread currently allocating a stubborn obj.\n");
00189 #      endif
00190     }
00191 }
00192 
00193 # else
00194 
00195 extern int GC_quiet;
00196        /* ANSI C doesn't allow translation units to be empty.  */
00197        /* So we guarantee this one is nonempty.         */
00198 
00199 # endif /* CHECKSUMS */