1 /* mm/cacao-gc/copy.c - GC module for copying heap regions
3 Copyright (C) 2006 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
36 #include "mm/memory.h"
37 #include "toolbox/logging.h"
38 #include "vm/global.h"
41 /* Global Variables ***********************************************************/
43 static java_object_t *next;
46 static u4 copy_object(u1 *old, u1 *new, u4 size)
50 GC_LOG2( printf("\tcopy_object: %p -> %p\n", old, new); );
52 /* copy old object content to new location */
53 MCOPY(new, old, u1, size);
55 /* invalidate old object */
56 /* TODO: implement me, but remember not to destroy the header! */
60 /* check if we need to attach the hashcode to the object */
61 if (GC_TEST_FLAGS((java_object_t *) new, HDRFLAG_HASH_TAKEN)) {
63 GC_LOG( printf("need to attach hash to %p\n", new); );
71 #define GC_FORWARD(ref,refptr,start,end) \
72 *(refptr) = copy_forward(ref, start, end)
74 static void *copy_forward(java_object_t *o, void *src_start, void *src_end)
78 /* TODO: this is only to make debug output more readable; remove me! */
82 if (POINTS_INTO(o, src_start, src_end)) {
84 /* update all references which point into the source region */
86 /* NOTE: we use the marking bit here to mark object which have already
87 * been copied; in such a case the *vftbl contains the location of
89 if (GC_IS_MARKED(o)) {
91 GC_LOG2( printf("\tForwarding reference: %p -> ", (void *) o);
92 heap_print_object((java_object_t *) o->vftbl);
95 /* return the location of an already existing copy */
100 GC_LOG2( printf("\tCopying object to %p: ", (void *) next);
101 heap_print_object(o); printf("\n"); );
103 /* calculate the size of the object to be copied */
104 o_size = get_object_size(o);
106 /* copy the object pointed to by O to location NEXT */
107 o_size = copy_object(o, next, o_size);
109 /* remember where the copy is located and mark original */
111 o->vftbl = (void *) next;
113 /* increment NEXT to point past the copy of the object */
114 next = ((u1 *) next) + o_size;
116 /* return the location of the copy */
123 GC_LOG2( printf("\tDoing nothing for outside reference: ");
124 heap_print_object(o); printf("\n"); );
126 /* do not change references not pointing into the source region */
133 void copy_me(regioninfo_t *src, regioninfo_t *dst, rootset_t *rs)
136 /*java_object_t *next;*/
138 java_object_t **refptr;
141 /* initialize the scan and next pointer */
142 scan = (java_object_t *) dst->base;
143 next = (java_object_t *) dst->base;
145 GC_LOG( dolog("GC: Copying object from rootset ..."); );
147 /* for each root pointer R: replace R with forward(R) */
149 for (i = 0; i < rs->refcount; i++) {
151 /* load the root reference */
152 ref = *( rs->refs[i].ref );
154 /* forward the object */
155 GC_FORWARD(ref, rs->refs[i].ref, src->base, src->end);
162 GC_LOG( dolog("GC: Copying referenced objects ...") );
164 /* update all references for objects in the destination region.
165 * when scan catches up with next, the algorithm is finished */
169 GC_LOG2( printf("Will also forward reference in ");
170 heap_print_object(scan); printf("\n"); );
172 if (IS_ARRAY(scan)) {
174 /* walk through the references of an Array */
175 FOREACH_ARRAY_REF(scan,ref,refptr,
177 GC_FORWARD(ref, refptr, src->base, src->end);
183 /* walk through the references of an Object */
184 FOREACH_OBJECT_REF(scan,ref,refptr,
186 GC_FORWARD(ref, refptr, src->base, src->end);
192 scan = ((u1 *) scan) + get_object_size(scan);
195 /* update destination region information */
196 /* TODO: there is more to update! */
199 /* some basic assumptions */
200 GC_ASSERT(scan == next);
201 GC_ASSERT(scan < dst->end);
206 * These are local overrides for various environment variables in Emacs.
207 * Please do not remove this and leave it at the end of the file, where
208 * Emacs will automagically detect them.
209 * ---------------------------------------------------------------------
212 * indent-tabs-mode: t
216 * vim:noexpandtab:sw=4:ts=4: