* Merged with default branch at rev 16f3633aaa5a.
[cacao.git] / src / mm / cacao-gc / region.c
1 /* mm/cacao-gc/region.c - GC module for region management
2
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
7
8    This file is part of CACAO.
9
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.
14
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.
19
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
23    02110-1301, USA.
24
25    $Id$
26
27 */
28
29
30 #include "config.h"
31 #include "vm/types.h"
32
33 #include "region.h"
34 #include "mm/memory.h"
35 #include "toolbox/logging.h"
36
37
38 /* region_init *****************************************************************
39
40    Allocates the memory for a heap region and initiates the regioninfo struct
41    accordingly.
42
43 *******************************************************************************/
44
45 void *region_create(regioninfo_t *region, u4 size)
46 {
47         u1 *ptr;
48
49         /* some sanity check */
50         GC_ASSERT(region);
51
52         /* allocate memory for the region */
53         ptr = MNEW(u1, size);
54
55         if (ptr == NULL)
56                 return NULL;
57
58         /* initiate structure */
59         region->base = ptr;
60         region->end  = ptr + size;
61         region->ptr  = ptr;
62         region->size = size;
63         region->free = size;
64
65 #if defined(ENABLE_THREADS)
66         /* initiate the header for locking */
67         lock_init_object_lock((java_object_t *) region);
68 #endif
69
70 #if defined(ENABLE_MEMCHECK)
71         /* poison this region */
72         /* TODO: this should really be done MNEW above! */
73         region_invalidate(region);
74 #endif
75
76         GC_LOG( dolog("GC: Region allocated at [ %p ; %p ]", region->base, region->end); );
77
78         return ptr;
79 }
80
81
82 u4 region_resize(regioninfo_t *region, u4 size)
83 {
84         u1 *ptr;
85         u4 offset;
86         u4 used;
87
88         /* reallocate memory for the region */
89         ptr = MREALLOC(region->base, u1, region->size, size);
90
91         if (ptr == NULL)
92                 vm_abort("region_resize: realloc failed!");
93
94         /* was the region moved? */
95         offset = ptr - region->base;
96         used   = region->size - region->free;
97
98         /* update structure */
99         region->base = ptr;
100         region->end  = ptr + size;
101         region->ptr  = ptr + used;
102         region->size = size;
103         region->free = size - used;
104
105 #if defined(ENABLE_MEMCHECK)
106         /* poison this region */
107         region_invalidate(region);
108 #endif
109
110         GC_LOG( dolog("GC: Region resized to [ %p ; %p ]", region->base, region->end); );
111
112         return offset;
113 }
114
115
116 /* region_invalidate ***********************************************************
117
118    Invalidates the free memory area inside a heap region by overwriting it with
119    the clear byte.
120
121    REMEMBER: The region has to be compacted for this to work properly.
122
123 *******************************************************************************/
124
125 #if defined(ENABLE_MEMCHECK)
126 void region_invalidate(regioninfo_t *region)
127 {
128         /* some sanity check */
129         GC_ASSERT(region->free == region->end - region->ptr);
130
131         /* invalidate free memory */
132         memset(region->ptr, MEMORY_CLEAR_BYTE, region->free);
133 }
134 #endif /* defined(ENABLE_MEMCHECK) */
135
136
137 /*
138  * These are local overrides for various environment variables in Emacs.
139  * Please do not remove this and leave it at the end of the file, where
140  * Emacs will automagically detect them.
141  * ---------------------------------------------------------------------
142  * Local variables:
143  * mode: c
144  * indent-tabs-mode: t
145  * c-basic-offset: 4
146  * tab-width: 4
147  * End:
148  * vim:noexpandtab:sw=4:ts=4:
149  */