obj_map.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
  3. * Copyright (c) 1991, 1992 by Xerox Corporation. All rights reserved.
  4. * Copyright (c) 1999-2001 by Hewlett-Packard Company. All rights reserved.
  5. *
  6. * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
  7. * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
  8. *
  9. * Permission is hereby granted to use or copy this program
  10. * for any purpose, provided the above notices are retained on all copies.
  11. * Permission to modify the code and to distribute modified code is granted,
  12. * provided the above notices are retained, and a notice that the code was
  13. * modified is included with the above copyright notice.
  14. */
  15. #include "private/gc_priv.h"
  16. /* Routines for maintaining maps describing heap block
  17. * layouts for various object sizes. Allows fast pointer validity checks
  18. * and fast location of object start locations on machines (such as SPARC)
  19. * with slow division.
  20. */
  21. /* Consider pointers that are offset bytes displaced from the beginning */
  22. /* of an object to be valid. */
  23. GC_API void GC_CALL GC_register_displacement(size_t offset)
  24. {
  25. DCL_LOCK_STATE;
  26. LOCK();
  27. GC_register_displacement_inner(offset);
  28. UNLOCK();
  29. }
  30. GC_INNER void GC_register_displacement_inner(size_t offset)
  31. {
  32. if (offset >= VALID_OFFSET_SZ) {
  33. ABORT("Bad argument to GC_register_displacement");
  34. }
  35. if (!GC_valid_offsets[offset]) {
  36. GC_valid_offsets[offset] = TRUE;
  37. GC_modws_valid_offsets[offset % sizeof(word)] = TRUE;
  38. }
  39. }
  40. #ifdef MARK_BIT_PER_GRANULE
  41. /* Add a heap block map for objects of size granules to obj_map. */
  42. /* Return FALSE on failure. */
  43. /* A size of 0 granules is used for large objects. */
  44. GC_INNER GC_bool GC_add_map_entry(size_t granules)
  45. {
  46. unsigned displ;
  47. unsigned short * new_map;
  48. if (granules > BYTES_TO_GRANULES(MAXOBJBYTES)) granules = 0;
  49. if (GC_obj_map[granules] != 0) {
  50. return(TRUE);
  51. }
  52. new_map = (unsigned short *)GC_scratch_alloc(MAP_LEN * sizeof(short));
  53. if (new_map == 0) return(FALSE);
  54. GC_COND_LOG_PRINTF(
  55. "Adding block map for size of %u granules (%u bytes)\n",
  56. (unsigned)granules, (unsigned)GRANULES_TO_BYTES(granules));
  57. if (granules == 0) {
  58. for (displ = 0; displ < BYTES_TO_GRANULES(HBLKSIZE); displ++) {
  59. new_map[displ] = 1; /* Nonzero to get us out of marker fast path. */
  60. }
  61. } else {
  62. for (displ = 0; displ < BYTES_TO_GRANULES(HBLKSIZE); displ++) {
  63. new_map[displ] = (unsigned short)(displ % granules);
  64. }
  65. }
  66. GC_obj_map[granules] = new_map;
  67. return(TRUE);
  68. }
  69. #endif /* MARK_BIT_PER_GRANULE */
  70. GC_INNER void GC_initialize_offsets(void)
  71. {
  72. unsigned i;
  73. if (GC_all_interior_pointers) {
  74. for (i = 0; i < VALID_OFFSET_SZ; ++i)
  75. GC_valid_offsets[i] = TRUE;
  76. } else {
  77. BZERO(GC_valid_offsets, sizeof(GC_valid_offsets));
  78. for (i = 0; i < sizeof(word); ++i)
  79. GC_modws_valid_offsets[i] = FALSE;
  80. }
  81. }