65fbcaf7bf80ededa6e37367f7b3a63da29ee272
[cacao.git] / src / mm / boehm-gc / obj_map.c
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   
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
22 #include "config.h"
23  
24 # include "private/gc_priv.h"
25
26 /* Consider pointers that are offset bytes displaced from the beginning */
27 /* of an object to be valid.                                            */
28
29 void GC_register_displacement(size_t offset)
30 {
31     DCL_LOCK_STATE;
32     
33     LOCK();
34     GC_register_displacement_inner(offset);
35     UNLOCK();
36 }
37
38 void GC_register_displacement_inner(size_t offset) 
39 {
40     if (offset >= VALID_OFFSET_SZ) {
41         ABORT("Bad argument to GC_register_displacement");
42     }
43     if (!GC_valid_offsets[offset]) {
44       GC_valid_offsets[offset] = TRUE;
45       GC_modws_valid_offsets[offset % sizeof(word)] = TRUE;
46     }
47 }
48
49 #ifdef MARK_BIT_PER_GRANULE
50 /* Add a heap block map for objects of size granules to obj_map.        */
51 /* Return FALSE on failure.                                             */
52 /* A size of 0 granules is used for large objects.                      */
53 GC_bool GC_add_map_entry(size_t granules)
54 {
55     unsigned displ;
56     short * new_map;
57     
58     if (granules > BYTES_TO_GRANULES(MAXOBJBYTES)) granules = 0;
59     if (GC_obj_map[granules] != 0) {
60         return(TRUE);
61     }
62     new_map = (short *)GC_scratch_alloc(MAP_LEN * sizeof(short));
63     if (new_map == 0) return(FALSE);
64     if (GC_print_stats)
65         GC_log_printf("Adding block map for size of %u granules (%u bytes)\n",
66                   (unsigned)granules, (unsigned)(GRANULES_TO_BYTES(granules)));
67     if (granules == 0) {
68       for (displ = 0; displ < BYTES_TO_GRANULES(HBLKSIZE); displ++) {
69         new_map[displ] = 1;  /* Nonzero to get us out of marker fast path. */
70       }
71     } else {
72       for (displ = 0; displ < BYTES_TO_GRANULES(HBLKSIZE); displ++) {
73         new_map[displ] = (short)(displ % granules);
74       }
75     }
76     GC_obj_map[granules] = new_map;
77     return(TRUE);
78 }
79 #endif
80
81 static GC_bool offsets_initialized = FALSE;
82
83 void GC_initialize_offsets(void)
84 {
85     if (!offsets_initialized) {
86       int i;
87       if (GC_all_interior_pointers) {
88         for (i = 0; i < VALID_OFFSET_SZ; ++i) GC_valid_offsets[i] = TRUE;
89       }
90       offsets_initialized = TRUE;
91     }
92 }