Back to index

lightning-sunbird  0.9+nobinonly
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 "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 2000
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 colector.              */
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     
00080     if (pe -> block != 0 && pe -> block != h + OFFSET) ABORT("goofed");
00081     pe -> old_sum = pe -> new_sum;
00082     pe -> new_sum = GC_checksum(h);
00083 #   ifndef MSWIN32
00084         if (pe -> new_sum != 0 && !GC_page_was_ever_dirty(h)) {
00085             GC_printf1("GC_page_was_ever_dirty(0x%lx) is wrong\n",
00086                      (unsigned long)h);
00087         }
00088 #   endif
00089     if (GC_page_was_dirty(h)) {
00090        GC_n_dirty++;
00091     } else {
00092        GC_n_clean++;
00093     }
00094     if (pe -> new_valid && pe -> old_sum != pe -> new_sum) {
00095        if (!GC_page_was_dirty(h) || !GC_page_was_ever_dirty(h)) {
00096            /* Set breakpoint here */GC_n_dirty_errors++;
00097        }
00098 #      ifdef STUBBORN_ALLOC
00099          if (!IS_FORWARDING_ADDR_OR_NIL(hhdr)
00100            && hhdr -> hb_map != GC_invalid_map
00101            && hhdr -> hb_obj_kind == STUBBORN
00102            && !GC_page_was_changed(h)
00103            && !GC_on_free_list(h)) {
00104            /* if GC_on_free_list(h) then reclaim may have touched it  */
00105            /* without any allocations taking place.                   */
00106            /* Set breakpoint here */GC_n_changed_errors++;
00107          }
00108 #      endif
00109     }
00110     pe -> new_valid = TRUE;
00111     pe -> block = h + OFFSET;
00112 }
00113 
00114 word GC_bytes_in_used_blocks;
00115 
00116 void GC_add_block(h, dummy)
00117 struct hblk *h;
00118 word dummy;
00119 {
00120    register hdr * hhdr = HDR(h);
00121    register bytes = WORDS_TO_BYTES(hhdr -> hb_sz);
00122    
00123    bytes += HDR_BYTES + HBLKSIZE-1;
00124    bytes &= ~(HBLKSIZE-1);
00125    GC_bytes_in_used_blocks += bytes;
00126 }
00127 
00128 void GC_check_blocks()
00129 {
00130     word bytes_in_free_blocks = 0;
00131     struct hblk * h = GC_hblkfreelist;
00132     hdr * hhdr = HDR(h);
00133     word sz;
00134     
00135     GC_bytes_in_used_blocks = 0;
00136     GC_apply_to_all_blocks(GC_add_block, (word)0);
00137     while (h != 0) {
00138         sz = hhdr -> hb_sz;
00139         bytes_in_free_blocks += sz;
00140         h = hhdr -> hb_next;
00141         hhdr = HDR(h);
00142     }
00143     GC_printf2("GC_bytes_in_used_blocks = %ld, bytes_in_free_blocks = %ld ",
00144               GC_bytes_in_used_blocks, bytes_in_free_blocks);
00145     GC_printf1("GC_heapsize = %ld\n", GC_heapsize);
00146     if (GC_bytes_in_used_blocks + bytes_in_free_blocks != GC_heapsize) {
00147        GC_printf0("LOST SOME BLOCKS!!\n");
00148     }
00149 }
00150 
00151 /* Should be called immediately after GC_read_dirty and GC_read_changed. */
00152 void GC_check_dirty()
00153 {
00154     register int index;
00155     register unsigned i;
00156     register struct hblk *h;
00157     register ptr_t start;
00158     
00159     GC_check_blocks();
00160     
00161     GC_n_dirty_errors = 0;
00162     GC_n_changed_errors = 0;
00163     GC_n_clean = 0;
00164     GC_n_dirty = 0;
00165     
00166     index = 0;
00167     for (i = 0; i < GC_n_heap_sects; i++) {
00168        start = GC_heap_sects[i].hs_start;
00169         for (h = (struct hblk *)start;
00170              h < (struct hblk *)(start + GC_heap_sects[i].hs_bytes);
00171              h++) {
00172              GC_update_check_page(h, index);
00173              index++;
00174              if (index >= NSUMS) goto out;
00175         }
00176     }
00177 out:
00178     GC_printf2("Checked %lu clean and %lu dirty pages\n",
00179              (unsigned long) GC_n_clean, (unsigned long) GC_n_dirty);
00180     if (GC_n_dirty_errors > 0) {
00181         GC_printf1("Found %lu dirty bit errors\n",
00182                  (unsigned long)GC_n_dirty_errors);
00183     }
00184     if (GC_n_changed_errors > 0) {
00185        GC_printf1("Found %lu changed bit errors\n",
00186                  (unsigned long)GC_n_changed_errors);
00187        GC_printf0("These may be benign (provoked by nonpointer changes)\n");
00188 #      ifdef THREADS
00189            GC_printf0(
00190            "Also expect 1 per thread currently allocating a stubborn obj.\n");
00191 #      endif
00192     }
00193 }
00194 
00195 # else
00196 
00197 extern int GC_quiet;
00198        /* ANSI C doesn't allow translation units to be empty.  */
00199        /* So we guarantee this one is nonempty.         */
00200 
00201 # endif /* CHECKSUMS */