OSDN Git Service

* error.c (gfc_error_init_1): Remove blank line in front of
[pf3gnuchains/gcc-fork.git] / boehm-gc / allchblk.c
index 1505f8e..793c468 100644 (file)
  * modified is included with the above copyright notice.
  */
 
-#define DEBUG
-#undef DEBUG
+/* #define DEBUG */
 #include <stdio.h>
-#include "gc_priv.h"
+#include "private/gc_priv.h"
 
 GC_bool GC_use_entire_heap = 0;
 
@@ -47,6 +46,39 @@ GC_bool GC_use_entire_heap = 0;
 
 struct hblk * GC_hblkfreelist[N_HBLK_FLS+1] = { 0 };
 
+#ifndef USE_MUNMAP
+
+  word GC_free_bytes[N_HBLK_FLS+1] = { 0 };
+       /* Number of free bytes on each list.   */
+
+  /* Is bytes + the number of free bytes on lists n .. N_HBLK_FLS      */
+  /* > GC_max_large_allocd_bytes?                                      */
+# ifdef __GNUC__
+  __inline__
+# endif
+  static GC_bool GC_enough_large_bytes_left(bytes,n)
+  word bytes;
+  int n;
+  {
+    int i;
+    for (i = N_HBLK_FLS; i >= n; --i) {
+       bytes += GC_free_bytes[i];
+       if (bytes > GC_max_large_allocd_bytes) return TRUE;
+    }
+    return FALSE;
+  }
+
+# define INCR_FREE_BYTES(n, b) GC_free_bytes[n] += (b);
+
+# define FREE_ASSERT(e) GC_ASSERT(e)
+
+#else /* USE_MUNMAP */
+
+# define INCR_FREE_BYTES(n, b)
+# define FREE_ASSERT(e)
+
+#endif /* USE_MUNMAP */
+
 /* Map a number of blocks to the appropriate large block free list index. */
 int GC_hblk_fl_from_blocks(blocks_needed)
 word blocks_needed;
@@ -58,7 +90,6 @@ word blocks_needed;
     
 }
 
-# define HBLK_IS_FREE(hdr) ((hdr) -> hb_map == GC_invalid_map)
 # define PHDR(hhdr) HDR(hhdr -> hb_prev)
 # define NHDR(hhdr) HDR(hhdr -> hb_next)
 
@@ -79,7 +110,14 @@ void GC_print_hblkfreelist()
     
     for (i = 0; i <= N_HBLK_FLS; ++i) {
       h = GC_hblkfreelist[i];
-      if (0 != h) GC_printf1("Free list %ld:\n", (unsigned long)i);
+#     ifdef USE_MUNMAP
+        if (0 != h) GC_printf1("Free list %ld:\n",
+                              (unsigned long)i);
+#     else
+        if (0 != h) GC_printf2("Free list %ld (Total size %ld):\n",
+                              (unsigned long)i,
+                              (unsigned long)GC_free_bytes[i]);
+#     endif
       while (h != 0) {
         hhdr = HDR(h);
         sz = hhdr -> hb_sz;
@@ -95,10 +133,12 @@ void GC_print_hblkfreelist()
         h = hhdr -> hb_next;
       }
     }
-    if (total_free != GC_large_free_bytes) {
+#   ifndef USE_MUNMAP
+      if (total_free != GC_large_free_bytes) {
        GC_printf1("GC_large_free_bytes = %lu (INCONSISTENT!!)\n",
                   (unsigned long) GC_large_free_bytes);
-    }
+      }
+#   endif
     GC_printf1("Total of %lu bytes on free list\n", (unsigned long)total_free);
 }
 
@@ -143,7 +183,7 @@ void GC_dump_regions()
            hhdr = HDR(p);
            GC_printf1("\t0x%lx ", (unsigned long)p);
            if (IS_FORWARDING_ADDR_OR_NIL(hhdr)) {
-               GC_printf1("Missing header!!\n", hhdr);
+               GC_printf1("Missing header!!(%ld)\n", hhdr);
                p += HBLKSIZE;
                continue;
            }
@@ -219,14 +259,25 @@ void GC_remove_from_fl(hhdr, n)
 hdr * hhdr;
 int n;
 {
+    int index;
+
     GC_ASSERT(((hhdr -> hb_sz) & (HBLKSIZE-1)) == 0);
+#   ifndef USE_MUNMAP
+      /* We always need index to mainatin free counts. */
+      if (FL_UNKNOWN == n) {
+          index = GC_hblk_fl_from_blocks(divHBLKSZ(hhdr -> hb_sz));
+      } else {
+         index = n;
+      }
+#   endif
     if (hhdr -> hb_prev == 0) {
-        int index;
-       if (FL_UNKNOWN == n) {
+#      ifdef USE_MUNMAP
+         if (FL_UNKNOWN == n) {
             index = GC_hblk_fl_from_blocks(divHBLKSZ(hhdr -> hb_sz));
-       } else {
+         } else {
            index = n;
-       }
+         }
+#      endif
        GC_ASSERT(HDR(GC_hblkfreelist[index]) == hhdr);
        GC_hblkfreelist[index] = hhdr -> hb_next;
     } else {
@@ -234,6 +285,8 @@ int n;
        GET_HDR(hhdr -> hb_prev, phdr);
        phdr -> hb_next = hhdr -> hb_next;
     }
+    INCR_FREE_BYTES(index, - (signed_word)(hhdr -> hb_sz));
+    FREE_ASSERT(GC_free_bytes[index] >= 0);
     if (0 != hhdr -> hb_next) {
        hdr * nhdr;
        GC_ASSERT(!IS_FORWARDING_ADDR_OR_NIL(NHDR(hhdr)));
@@ -294,6 +347,8 @@ hdr * hhdr;
 #   endif
     GC_ASSERT(((hhdr -> hb_sz) & (HBLKSIZE-1)) == 0);
     GC_hblkfreelist[index] = h;
+    INCR_FREE_BYTES(index, hhdr -> hb_sz);
+    FREE_ASSERT(GC_free_bytes[index] <= GC_large_free_bytes)
     hhdr -> hb_next = second;
     hhdr -> hb_prev = 0;
     if (0 != second) {
@@ -322,9 +377,8 @@ void GC_unmap_old(void)
        if (!IS_MAPPED(hhdr)) continue;
        threshold = (unsigned short)(GC_gc_no - UNMAP_THRESHOLD);
        last_rec = hhdr -> hb_last_reclaimed;
-       if (last_rec > GC_gc_no
-           || last_rec < threshold && threshold < GC_gc_no
-                                      /* not recently wrapped */) {
+       if ((last_rec > GC_gc_no || last_rec < threshold)
+           && threshold < GC_gc_no /* not recently wrapped */) {
           sz = hhdr -> hb_sz;
          GC_unmap((ptr_t)h, sz);
          hhdr -> hb_flags |= WAS_UNMAPPED;
@@ -369,6 +423,7 @@ void GC_merge_unmapped(void)
              } else {
                GC_remap((ptr_t)h, size);
                hhdr -> hb_flags &= ~WAS_UNMAPPED;
+               hhdr -> hb_last_reclaimed = nexthdr -> hb_last_reclaimed;
              }
            } else {
              /* Unmap any gap in the middle */
@@ -415,11 +470,15 @@ int index;
     if (total_size == bytes) return h;
     rest = (struct hblk *)((word)h + bytes);
     rest_hdr = GC_install_header(rest);
-    if (0 == rest_hdr) return(0);
+    if (0 == rest_hdr) {
+       /* This may be very bad news ... */
+       WARN("Header allocation failed: Dropping block.\n", 0);
+       return(0);
+    }
     rest_hdr -> hb_sz = total_size - bytes;
     rest_hdr -> hb_flags = 0;
 #   ifdef GC_ASSERTIONS
-      // Mark h not free, to avoid assertion about adjacent free blocks.
+      /* Mark h not free, to avoid assertion about adjacent free blocks. */
         hhdr -> hb_map = 0;
 #   endif
     GC_add_to_fl(rest, rest_hdr);
@@ -463,6 +522,8 @@ int index;  /* Index of free list */
       if (0 != next) {
        HDR(next) -> hb_prev = n;
       }
+      INCR_FREE_BYTES(index, -(signed_word)h_size);
+      FREE_ASSERT(GC_free_bytes[index] > 0);
 #     ifdef GC_ASSERTIONS
        nhdr -> hb_map = 0;     /* Don't fail test for consecutive      */
                                /* free blocks in GC_add_to_fl.         */
@@ -484,20 +545,23 @@ struct hblk * GC_allochblk_nth();
  * NOTE: We set obj_map field in header correctly.
  *       Caller is responsible for building an object freelist in block.
  *
- * We clear the block if it is destined for large objects, and if
- * kind requires that newly allocated objects be cleared.
+ * Unlike older versions of the collectors, the client is responsible
+ * for clearing the block, if necessary.
  */
 struct hblk *
 GC_allochblk(sz, kind, flags)
 word sz;
 int kind;
-unsigned char flags;  /* IGNORE_OFF_PAGE or 0 */
+unsigned flags;  /* IGNORE_OFF_PAGE or 0 */
 {
-    int start_list = GC_hblk_fl_from_blocks(OBJ_SZ_TO_BLOCKS(sz));
+    word blocks = OBJ_SZ_TO_BLOCKS(sz);
+    int start_list = GC_hblk_fl_from_blocks(blocks);
     int i;
     for (i = start_list; i <= N_HBLK_FLS; ++i) {
        struct hblk * result = GC_allochblk_nth(sz, kind, flags, i);
-       if (0 != result) return result;
+       if (0 != result) {
+           return result;
+       }
     }
     return 0;
 }
@@ -526,12 +590,28 @@ int n;
            GET_HDR(hbp, hhdr);
            size_avail = hhdr->hb_sz;
            if (size_avail < size_needed) continue;
-           if (!GC_use_entire_heap) {
-               if (size_avail != size_needed
-                   && USED_HEAP_SIZE >= GC_requested_heapsize
-                   && !GC_incremental && GC_should_collect()) {
+           if (!GC_use_entire_heap
+               && size_avail != size_needed
+               && USED_HEAP_SIZE >= GC_requested_heapsize
+               && !TRUE_INCREMENTAL && GC_should_collect()) {
+#              ifdef USE_MUNMAP
                    continue;
-               } 
+#              else
+                   /* If we have enough large blocks left to cover any */
+                   /* previous request for large blocks, we go ahead   */
+                   /* and split.  Assuming a steady state, that should */
+                   /* be safe.  It means that we can use the full      */
+                   /* heap if we allocate only small objects.          */
+                   if (!GC_enough_large_bytes_left(GC_large_allocd_bytes, n)) {
+                     continue;
+                   } 
+                   /* If we are deallocating lots of memory from       */
+                   /* finalizers, fail and collect sooner rather       */
+                   /* than later.                                      */
+                   if (GC_finalizer_mem_freed > (GC_heapsize >> 4))  {
+                     continue;
+                   }
+#              endif /* !USE_MUNMAP */
            }
            /* If the next heap block is obviously better, go on.       */
            /* This prevents us from disassembling a single large block */
@@ -562,7 +642,8 @@ int n;
              
              while ((ptr_t)lasthbp <= search_end
                     && (thishbp = GC_is_black_listed(lasthbp,
-                                                     (word)eff_size_needed))) {
+                                                     (word)eff_size_needed))
+                       != 0) {
                lasthbp = thishbp;
              }
              size_avail -= (ptr_t)lasthbp - (ptr_t)hbp;
@@ -573,7 +654,7 @@ int n;
                  /* Make sure it's mapped before we mangle it. */
 #                  ifdef USE_MUNMAP
                      if (!IS_MAPPED(hhdr)) {
-                       GC_remap((ptr_t)hbp, size_avail);
+                       GC_remap((ptr_t)hbp, hhdr -> hb_sz);
                        hhdr -> hb_flags &= ~WAS_UNMAPPED;
                      }
 #                  endif
@@ -589,8 +670,14 @@ int n;
                         && orig_avail - size_needed
                            > (signed_word)BL_LIMIT) {
                /* Punt, since anything else risks unreasonable heap growth. */
-               WARN("Needed to allocate blacklisted block at 0x%lx\n",
-                    (word)hbp);
+               if (++GC_large_alloc_warn_suppressed
+                   >= GC_large_alloc_warn_interval) {
+                 WARN("Repeated allocation of very large block "
+                      "(appr. size %ld):\n"
+                      "\tMay lead to memory leak and poor performance.\n",
+                      size_needed);
+                 GC_large_alloc_warn_suppressed = 0;
+               }
                size_avail = orig_avail;
              } else if (size_avail == 0 && size_needed == HBLKSIZE
                         && IS_MAPPED(hhdr)) {
@@ -618,10 +705,10 @@ int n;
                        if (h == hbp || 0 != (hhdr = GC_install_header(h))) {
                          (void) setup_header(
                                  hhdr,
-                                 BYTES_TO_WORDS(HBLKSIZE - HDR_BYTES),
+                                 BYTES_TO_WORDS(HBLKSIZE),
                                  PTRFREE, 0); /* Cant fail */
                          if (GC_debugging_started) {
-                           BZERO(h + HDR_BYTES, HBLKSIZE - HDR_BYTES);
+                           BZERO(h, HBLKSIZE);
                          }
                        }
                      }
@@ -638,7 +725,7 @@ int n;
            if( size_avail >= size_needed ) {
 #              ifdef USE_MUNMAP
                  if (!IS_MAPPED(hhdr)) {
-                   GC_remap((ptr_t)hbp, size_avail);
+                   GC_remap((ptr_t)hbp, hhdr -> hb_sz);
                    hhdr -> hb_flags &= ~WAS_UNMAPPED;
                  }
 #              endif
@@ -651,9 +738,6 @@ int n;
 
     if (0 == hbp) return 0;
        
-    /* Notify virtual dirty bit implementation that we are about to write. */
-       GC_write_hint(hbp);
-    
     /* Add it to map of valid blocks */
        if (!GC_install_counts(hbp, (word)size_needed)) return(0);
        /* This leaks memory under very rare conditions. */
@@ -663,13 +747,12 @@ int n;
             GC_remove_counts(hbp, (word)size_needed);
             return(0); /* ditto */
         }
-        
-    /* Clear block if necessary */
-       if (GC_debugging_started
-           || sz > MAXOBJSZ && GC_obj_kinds[kind].ok_init) {
-           BZERO(hbp + HDR_BYTES,  size_needed - HDR_BYTES);
-       }
 
+    /* Notify virtual dirty bit implementation that we are about to write.  */
+    /* Ensure that pointerfree objects are not protected if it's avoidable. */
+       GC_remove_protection(hbp, divHBLKSZ(size_needed),
+                            (hhdr -> hb_descr == 0) /* pointer-free */);
+        
     /* We just successfully allocated a block.  Restart count of       */
     /* consecutive failures.                                           */
     {
@@ -707,11 +790,15 @@ signed_word size;
     size = HBLKSIZE * OBJ_SZ_TO_BLOCKS(size);
     GC_remove_counts(hbp, (word)size);
     hhdr->hb_sz = size;
+#   ifdef USE_MUNMAP
+      hhdr -> hb_last_reclaimed = GC_gc_no;
+#   endif
     
     /* Check for duplicate deallocation in the easy case */
       if (HBLK_IS_FREE(hhdr)) {
         GC_printf1("Duplicate large block deallocation of 0x%lx\n",
                   (unsigned long) hbp);
+       ABORT("Duplicate large block deallocation");
       }
 
     GC_ASSERT(IS_MAPPED(hhdr));
@@ -731,11 +818,17 @@ signed_word size;
        if (IS_MAPPED(prevhdr)) {
          GC_remove_from_fl(prevhdr, FL_UNKNOWN);
          prevhdr -> hb_sz += hhdr -> hb_sz;
+#        ifdef USE_MUNMAP
+           prevhdr -> hb_last_reclaimed = GC_gc_no;
+#        endif
          GC_remove_header(hbp);
          hbp = prev;
          hhdr = prevhdr;
        }
       }
+    /* FIXME: It is not clear we really always want to do these merges */
+    /* with -DUSE_MUNMAP, since it updates ages and hence prevents     */
+    /* unmapping.                                                      */
 
     GC_large_free_bytes += size;
     GC_add_to_fl(hbp, hhdr);