obj_map.c 3.01 KB
Newer Older
1
/*
eg's avatar
eg committed
2 3 4 5 6 7 8 9 10 11 12 13 14
 * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
 * Copyright (c) 1991, 1992 by Xerox Corporation.  All rights reserved.
 * Copyright (c) 1999-2001 by Hewlett-Packard Company. All rights reserved.
 *
 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
 * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
 *
 * Permission is hereby granted to use or copy this program
 * for any purpose,  provided the above notices are retained on all copies.
 * Permission to modify the code and to distribute modified code is granted,
 * provided the above notices are retained, and a notice that the code was
 * modified is included with the above copyright notice.
 */
15 16 17

#include "private/gc_priv.h"

eg's avatar
eg committed
18 19 20 21 22 23 24 25 26
/* Routines for maintaining maps describing heap block
 * layouts for various object sizes.  Allows fast pointer validity checks
 * and fast location of object start locations on machines (such as SPARC)
 * with slow division.
 */

/* Consider pointers that are offset bytes displaced from the beginning */
/* of an object to be valid.                                            */

27
GC_API void GC_CALL GC_register_displacement(size_t offset)
eg's avatar
eg committed
28 29
{
    DCL_LOCK_STATE;
30

eg's avatar
eg committed
31 32 33 34 35
    LOCK();
    GC_register_displacement_inner(offset);
    UNLOCK();
}

36
GC_INNER void GC_register_displacement_inner(size_t offset)
eg's avatar
eg committed
37 38 39 40 41 42 43 44 45 46
{
    if (offset >= VALID_OFFSET_SZ) {
        ABORT("Bad argument to GC_register_displacement");
    }
    if (!GC_valid_offsets[offset]) {
      GC_valid_offsets[offset] = TRUE;
      GC_modws_valid_offsets[offset % sizeof(word)] = TRUE;
    }
}

eg's avatar
eg committed
47
#ifdef MARK_BIT_PER_GRANULE
48 49 50 51 52
  /* Add a heap block map for objects of size granules to obj_map.      */
  /* Return FALSE on failure.                                           */
  /* A size of 0 granules is used for large objects.                    */
  GC_INNER GC_bool GC_add_map_entry(size_t granules)
  {
eg's avatar
eg committed
53
    unsigned displ;
54 55
    unsigned short * new_map;

eg's avatar
eg committed
56 57
    if (granules > BYTES_TO_GRANULES(MAXOBJBYTES)) granules = 0;
    if (GC_obj_map[granules] != 0) {
eg's avatar
eg committed
58 59
        return(TRUE);
    }
60
    new_map = (unsigned short *)GC_scratch_alloc(MAP_LEN * sizeof(short));
eg's avatar
eg committed
61
    if (new_map == 0) return(FALSE);
62 63 64
    GC_COND_LOG_PRINTF(
                "Adding block map for size of %u granules (%u bytes)\n",
                (unsigned)granules, (unsigned)GRANULES_TO_BYTES(granules));
eg's avatar
eg committed
65 66 67 68
    if (granules == 0) {
      for (displ = 0; displ < BYTES_TO_GRANULES(HBLKSIZE); displ++) {
        new_map[displ] = 1;  /* Nonzero to get us out of marker fast path. */
      }
eg's avatar
eg committed
69
    } else {
eg's avatar
eg committed
70
      for (displ = 0; displ < BYTES_TO_GRANULES(HBLKSIZE); displ++) {
71
        new_map[displ] = (unsigned short)(displ % granules);
eg's avatar
eg committed
72
      }
eg's avatar
eg committed
73
    }
eg's avatar
eg committed
74
    GC_obj_map[granules] = new_map;
eg's avatar
eg committed
75
    return(TRUE);
76 77
  }
#endif /* MARK_BIT_PER_GRANULE */
eg's avatar
eg committed
78

79
GC_INNER void GC_initialize_offsets(void)
eg's avatar
eg committed
80
{
81 82 83 84 85 86 87 88 89
  unsigned i;
  if (GC_all_interior_pointers) {
    for (i = 0; i < VALID_OFFSET_SZ; ++i)
      GC_valid_offsets[i] = TRUE;
  } else {
    BZERO(GC_valid_offsets, sizeof(GC_valid_offsets));
    for (i = 0; i < sizeof(word); ++i)
      GC_modws_valid_offsets[i] = FALSE;
  }
eg's avatar
eg committed
90
}