Blame blacklst.c

Packit d28291
/*
Packit d28291
 * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
Packit d28291
 * Copyright (c) 1991-1994 by Xerox Corporation.  All rights reserved.
Packit d28291
 *
Packit d28291
 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
Packit d28291
 * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
Packit d28291
 *
Packit d28291
 * Permission is hereby granted to use or copy this program
Packit d28291
 * for any purpose,  provided the above notices are retained on all copies.
Packit d28291
 * Permission to modify the code and to distribute modified code is granted,
Packit d28291
 * provided the above notices are retained, and a notice that the code was
Packit d28291
 * modified is included with the above copyright notice.
Packit d28291
 */
Packit d28291
Packit d28291
#include "private/gc_priv.h"
Packit d28291
Packit d28291
/*
Packit d28291
 * We maintain several hash tables of hblks that have had false hits.
Packit d28291
 * Each contains one bit per hash bucket;  If any page in the bucket
Packit d28291
 * has had a false hit, we assume that all of them have.
Packit d28291
 * See the definition of page_hash_table in gc_private.h.
Packit d28291
 * False hits from the stack(s) are much more dangerous than false hits
Packit d28291
 * from elsewhere, since the former can pin a large object that spans the
Packit d28291
 * block, even though it does not start on the dangerous block.
Packit d28291
 */
Packit d28291
Packit d28291
/*
Packit d28291
 * Externally callable routines are:
Packit d28291
Packit d28291
 * GC_add_to_black_list_normal
Packit d28291
 * GC_add_to_black_list_stack
Packit d28291
 * GC_promote_black_lists
Packit d28291
 * GC_is_black_listed
Packit d28291
 *
Packit d28291
 * All require that the allocator lock is held.
Packit d28291
 */
Packit d28291
Packit d28291
/* Pointers to individual tables.  We replace one table by another by   */
Packit d28291
/* switching these pointers.                                            */
Packit d28291
STATIC word * GC_old_normal_bl = NULL;
Packit d28291
                /* Nonstack false references seen at last full          */
Packit d28291
                /* collection.                                          */
Packit d28291
STATIC word * GC_incomplete_normal_bl = NULL;
Packit d28291
                /* Nonstack false references seen since last            */
Packit d28291
                /* full collection.                                     */
Packit d28291
STATIC word * GC_old_stack_bl = NULL;
Packit d28291
STATIC word * GC_incomplete_stack_bl = NULL;
Packit d28291
Packit d28291
STATIC word GC_total_stack_black_listed = 0;
Packit d28291
                        /* Number of bytes on stack blacklist.  */
Packit d28291
Packit d28291
GC_INNER word GC_black_list_spacing = MINHINCR * HBLKSIZE;
Packit d28291
                        /* Initial rough guess. */
Packit d28291
Packit d28291
STATIC void GC_clear_bl(word *);
Packit d28291
Packit d28291
GC_INNER void GC_default_print_heap_obj_proc(ptr_t p)
Packit d28291
{
Packit d28291
    ptr_t base = GC_base(p);
Packit d28291
    int kind = HDR(base)->hb_obj_kind;
Packit d28291
Packit d28291
    GC_err_printf("object at %p of appr. %lu bytes (%s)\n",
Packit d28291
                  (void *)base, (unsigned long)GC_size(base),
Packit d28291
                  kind == PTRFREE ? "atomic" :
Packit d28291
                    IS_UNCOLLECTABLE(kind) ? "uncollectable" : "composite");
Packit d28291
}
Packit d28291
Packit d28291
GC_INNER void (*GC_print_heap_obj)(ptr_t p) = GC_default_print_heap_obj_proc;
Packit d28291
Packit d28291
#ifdef PRINT_BLACK_LIST
Packit d28291
  STATIC void GC_print_blacklisted_ptr(word p, ptr_t source,
Packit d28291
                                       const char *kind_str)
Packit d28291
  {
Packit d28291
    ptr_t base = GC_base(source);
Packit d28291
Packit d28291
    if (0 == base) {
Packit d28291
        GC_err_printf("Black listing (%s) %p referenced from %p in %s\n",
Packit d28291
                      kind_str, (void *)p, (void *)source,
Packit d28291
                      NULL != source ? "root set" : "register");
Packit d28291
    } else {
Packit d28291
        /* FIXME: We can't call the debug version of GC_print_heap_obj  */
Packit d28291
        /* (with PRINT_CALL_CHAIN) here because the lock is held and    */
Packit d28291
        /* the world is stopped.                                        */
Packit d28291
        GC_err_printf("Black listing (%s) %p referenced from %p in"
Packit d28291
                      " object at %p of appr. %lu bytes\n",
Packit d28291
                      kind_str, (void *)p, (void *)source,
Packit d28291
                      (void *)base, (unsigned long)GC_size(base));
Packit d28291
    }
Packit d28291
  }
Packit d28291
#endif /* PRINT_BLACK_LIST */
Packit d28291
Packit d28291
GC_INNER void GC_bl_init_no_interiors(void)
Packit d28291
{
Packit d28291
  if (GC_incomplete_normal_bl == 0) {
Packit d28291
    GC_old_normal_bl = (word *)GC_scratch_alloc(sizeof(page_hash_table));
Packit d28291
    GC_incomplete_normal_bl = (word *)GC_scratch_alloc(
Packit d28291
                                                  sizeof(page_hash_table));
Packit d28291
    if (GC_old_normal_bl == 0 || GC_incomplete_normal_bl == 0) {
Packit d28291
      GC_err_printf("Insufficient memory for black list\n");
Packit d28291
      EXIT();
Packit d28291
    }
Packit d28291
    GC_clear_bl(GC_old_normal_bl);
Packit d28291
    GC_clear_bl(GC_incomplete_normal_bl);
Packit d28291
  }
Packit d28291
}
Packit d28291
Packit d28291
GC_INNER void GC_bl_init(void)
Packit d28291
{
Packit d28291
    if (!GC_all_interior_pointers) {
Packit d28291
      GC_bl_init_no_interiors();
Packit d28291
    }
Packit d28291
    GC_ASSERT(NULL == GC_old_stack_bl && NULL == GC_incomplete_stack_bl);
Packit d28291
    GC_old_stack_bl = (word *)GC_scratch_alloc(sizeof(page_hash_table));
Packit d28291
    GC_incomplete_stack_bl = (word *)GC_scratch_alloc(sizeof(page_hash_table));
Packit d28291
    if (GC_old_stack_bl == 0 || GC_incomplete_stack_bl == 0) {
Packit d28291
        GC_err_printf("Insufficient memory for black list\n");
Packit d28291
        EXIT();
Packit d28291
    }
Packit d28291
    GC_clear_bl(GC_old_stack_bl);
Packit d28291
    GC_clear_bl(GC_incomplete_stack_bl);
Packit d28291
}
Packit d28291
Packit d28291
STATIC void GC_clear_bl(word *doomed)
Packit d28291
{
Packit d28291
    BZERO(doomed, sizeof(page_hash_table));
Packit d28291
}
Packit d28291
Packit d28291
STATIC void GC_copy_bl(word *old, word *new)
Packit d28291
{
Packit d28291
    BCOPY(old, new, sizeof(page_hash_table));
Packit d28291
}
Packit d28291
Packit d28291
static word total_stack_black_listed(void);
Packit d28291
Packit d28291
/* Signal the completion of a collection.  Turn the incomplete black    */
Packit d28291
/* lists into new black lists, etc.                                     */
Packit d28291
GC_INNER void GC_promote_black_lists(void)
Packit d28291
{
Packit d28291
    word * very_old_normal_bl = GC_old_normal_bl;
Packit d28291
    word * very_old_stack_bl = GC_old_stack_bl;
Packit d28291
Packit d28291
    GC_old_normal_bl = GC_incomplete_normal_bl;
Packit d28291
    GC_old_stack_bl = GC_incomplete_stack_bl;
Packit d28291
    if (!GC_all_interior_pointers) {
Packit d28291
      GC_clear_bl(very_old_normal_bl);
Packit d28291
    }
Packit d28291
    GC_clear_bl(very_old_stack_bl);
Packit d28291
    GC_incomplete_normal_bl = very_old_normal_bl;
Packit d28291
    GC_incomplete_stack_bl = very_old_stack_bl;
Packit d28291
    GC_total_stack_black_listed = total_stack_black_listed();
Packit d28291
    GC_VERBOSE_LOG_PRINTF(
Packit d28291
                "%lu bytes in heap blacklisted for interior pointers\n",
Packit d28291
                (unsigned long)GC_total_stack_black_listed);
Packit d28291
    if (GC_total_stack_black_listed != 0) {
Packit d28291
        GC_black_list_spacing =
Packit d28291
                HBLKSIZE*(GC_heapsize/GC_total_stack_black_listed);
Packit d28291
    }
Packit d28291
    if (GC_black_list_spacing < 3 * HBLKSIZE) {
Packit d28291
        GC_black_list_spacing = 3 * HBLKSIZE;
Packit d28291
    }
Packit d28291
    if (GC_black_list_spacing > MAXHINCR * HBLKSIZE) {
Packit d28291
        GC_black_list_spacing = MAXHINCR * HBLKSIZE;
Packit d28291
        /* Makes it easier to allocate really huge blocks, which otherwise */
Packit d28291
        /* may have problems with nonuniform blacklist distributions.      */
Packit d28291
        /* This way we should always succeed immediately after growing the */
Packit d28291
        /* heap.                                                           */
Packit d28291
    }
Packit d28291
}
Packit d28291
Packit d28291
GC_INNER void GC_unpromote_black_lists(void)
Packit d28291
{
Packit d28291
    if (!GC_all_interior_pointers) {
Packit d28291
      GC_copy_bl(GC_old_normal_bl, GC_incomplete_normal_bl);
Packit d28291
    }
Packit d28291
    GC_copy_bl(GC_old_stack_bl, GC_incomplete_stack_bl);
Packit d28291
}
Packit d28291
Packit d28291
/* P is not a valid pointer reference, but it falls inside      */
Packit d28291
/* the plausible heap bounds.                                   */
Packit d28291
/* Add it to the normal incomplete black list if appropriate.   */
Packit d28291
#ifdef PRINT_BLACK_LIST
Packit d28291
  GC_INNER void GC_add_to_black_list_normal(word p, ptr_t source)
Packit d28291
#else
Packit d28291
  GC_INNER void GC_add_to_black_list_normal(word p)
Packit d28291
#endif
Packit d28291
{
Packit d28291
  if (GC_modws_valid_offsets[p & (sizeof(word)-1)]) {
Packit d28291
    word index = PHT_HASH((word)p);
Packit d28291
Packit d28291
    if (HDR(p) == 0 || get_pht_entry_from_index(GC_old_normal_bl, index)) {
Packit d28291
#     ifdef PRINT_BLACK_LIST
Packit d28291
        if (!get_pht_entry_from_index(GC_incomplete_normal_bl, index)) {
Packit d28291
          GC_print_blacklisted_ptr(p, source, "normal");
Packit d28291
        }
Packit d28291
#     endif
Packit d28291
      set_pht_entry_from_index(GC_incomplete_normal_bl, index);
Packit d28291
    } /* else this is probably just an interior pointer to an allocated */
Packit d28291
      /* object, and isn't worth black listing.                         */
Packit d28291
  }
Packit d28291
}
Packit d28291
Packit d28291
/* And the same for false pointers from the stack. */
Packit d28291
#ifdef PRINT_BLACK_LIST
Packit d28291
  GC_INNER void GC_add_to_black_list_stack(word p, ptr_t source)
Packit d28291
#else
Packit d28291
  GC_INNER void GC_add_to_black_list_stack(word p)
Packit d28291
#endif
Packit d28291
{
Packit d28291
  word index = PHT_HASH((word)p);
Packit d28291
Packit d28291
  if (HDR(p) == 0 || get_pht_entry_from_index(GC_old_stack_bl, index)) {
Packit d28291
#   ifdef PRINT_BLACK_LIST
Packit d28291
      if (!get_pht_entry_from_index(GC_incomplete_stack_bl, index)) {
Packit d28291
        GC_print_blacklisted_ptr(p, source, "stack");
Packit d28291
      }
Packit d28291
#   endif
Packit d28291
    set_pht_entry_from_index(GC_incomplete_stack_bl, index);
Packit d28291
  }
Packit d28291
}
Packit d28291
Packit d28291
/*
Packit d28291
 * Is the block starting at h of size len bytes black listed?   If so,
Packit d28291
 * return the address of the next plausible r such that (r, len) might not
Packit d28291
 * be black listed.  (R may not actually be in the heap.  We guarantee only
Packit d28291
 * that every smaller value of r after h is also black listed.)
Packit d28291
 * If (h,len) is not black listed, return 0.
Packit d28291
 * Knows about the structure of the black list hash tables.
Packit d28291
 */
Packit d28291
struct hblk * GC_is_black_listed(struct hblk *h, word len)
Packit d28291
{
Packit d28291
    word index = PHT_HASH((word)h);
Packit d28291
    word i;
Packit d28291
    word nblocks;
Packit d28291
Packit d28291
    if (!GC_all_interior_pointers
Packit d28291
        && (get_pht_entry_from_index(GC_old_normal_bl, index)
Packit d28291
            || get_pht_entry_from_index(GC_incomplete_normal_bl, index))) {
Packit d28291
      return (h+1);
Packit d28291
    }
Packit d28291
Packit d28291
    nblocks = divHBLKSZ(len);
Packit d28291
    for (i = 0;;) {
Packit d28291
        if (GC_old_stack_bl[divWORDSZ(index)] == 0
Packit d28291
            && GC_incomplete_stack_bl[divWORDSZ(index)] == 0) {
Packit d28291
            /* An easy case */
Packit d28291
          i += WORDSZ - modWORDSZ(index);
Packit d28291
        } else {
Packit d28291
          if (get_pht_entry_from_index(GC_old_stack_bl, index)
Packit d28291
              || get_pht_entry_from_index(GC_incomplete_stack_bl, index)) {
Packit d28291
            return(h+i+1);
Packit d28291
          }
Packit d28291
          i++;
Packit d28291
        }
Packit d28291
        if (i >= nblocks) break;
Packit d28291
        index = PHT_HASH((word)(h+i));
Packit d28291
    }
Packit d28291
    return(0);
Packit d28291
}
Packit d28291
Packit d28291
/* Return the number of blacklisted blocks in a given range.    */
Packit d28291
/* Used only for statistical purposes.                          */
Packit d28291
/* Looks only at the GC_incomplete_stack_bl.                    */
Packit d28291
STATIC word GC_number_stack_black_listed(struct hblk *start,
Packit d28291
                                         struct hblk *endp1)
Packit d28291
{
Packit d28291
    register struct hblk * h;
Packit d28291
    word result = 0;
Packit d28291
Packit d28291
    for (h = start; (word)h < (word)endp1; h++) {
Packit d28291
        word index = PHT_HASH((word)h);
Packit d28291
Packit d28291
        if (get_pht_entry_from_index(GC_old_stack_bl, index)) result++;
Packit d28291
    }
Packit d28291
    return(result);
Packit d28291
}
Packit d28291
Packit d28291
/* Return the total number of (stack) black-listed bytes. */
Packit d28291
static word total_stack_black_listed(void)
Packit d28291
{
Packit d28291
    register unsigned i;
Packit d28291
    word total = 0;
Packit d28291
Packit d28291
    for (i = 0; i < GC_n_heap_sects; i++) {
Packit d28291
        struct hblk * start = (struct hblk *) GC_heap_sects[i].hs_start;
Packit d28291
        struct hblk * endp1 = start + divHBLKSZ(GC_heap_sects[i].hs_bytes);
Packit d28291
Packit d28291
        total += GC_number_stack_black_listed(start, endp1);
Packit d28291
    }
Packit d28291
    return(total * HBLKSIZE);
Packit d28291
}