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