220af9132bd507d736ea09c80358f012d8b60f88
[cacao.git] / src / native / localref.c
1 /* src/native/localref.c - Management of local reference tables
2
3    Copyright (C) 1996-2005, 2006, 2007 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: localref.c 8313 2007-08-15 22:11:35Z twisti $
26
27 */
28
29
30 #include "config.h"
31
32 #include <assert.h>
33 #include <stdint.h>
34
35 #include "mm/memory.h"
36
37 #include "native/localref.h"
38
39 #include "threads/threads-common.h"
40
41 #include "toolbox/logging.h"
42
43
44 /* debug **********************************************************************/
45
46 #if !defined(NDEBUG) && 0
47 # define TRACELOCALREF(message) log_println("%s", message)
48 #else
49 # define TRACELOCALREF(message)
50 #endif
51
52
53 /* global variables ***********************************************************/
54
55 #if !defined(ENABLE_THREADS)
56 localref_table *_no_threads_localref_table;
57 #endif
58
59
60 /* localref_table_init *********************************************************
61
62    Initializes the local references table of the current thread.
63
64 *******************************************************************************/
65
66 bool localref_table_init(void)
67 {
68         localref_table *lrt;
69
70         TRACELOCALREF("table init");
71
72         assert(LOCALREFTABLE == NULL);
73
74         lrt = GCNEW(localref_table);
75
76         if (lrt == NULL)
77                 return false;
78
79         localref_table_add(lrt);
80
81         return true;
82 }
83
84 /* localref_table_add **********************************************************
85
86    Add a new local references table to the current thread.
87
88 *******************************************************************************/
89
90 void localref_table_add(localref_table *lrt)
91 {
92         /* initialize the local reference table */
93
94         lrt->capacity    = LOCALREFTABLE_CAPACITY;
95         lrt->used        = 0;
96         lrt->localframes = 1;
97         lrt->prev        = LOCALREFTABLE;
98
99         /* clear the references array (memset is faster the a for-loop) */
100
101         MSET(lrt->refs, 0, void*, LOCALREFTABLE_CAPACITY);
102
103         /* add given local references table to this thread */
104
105         LOCALREFTABLE = lrt;
106 }
107
108
109 /* localref_table_remove *******************************************************
110
111    Remoces the local references table from the current thread.
112
113 *******************************************************************************/
114
115 void localref_table_remove()
116 {
117         localref_table *lrt;
118
119         /* get current local reference table from thread */
120
121         lrt = LOCALREFTABLE;
122
123         assert(lrt != NULL);
124         assert(lrt->localframes == 1);
125
126         lrt = lrt->prev;
127
128         LOCALREFTABLE = lrt;
129 }
130
131
132 /* localref_frame_push *********************************************************
133
134    Creates a new local reference frame, in which at least a given
135    number of local references can be created.
136
137 *******************************************************************************/
138
139 bool localref_frame_push(int32_t capacity)
140 {
141         localref_table *lrt;
142         localref_table *nlrt;
143         int32_t         additionalrefs;
144
145         TRACELOCALREF("frame push");
146
147         /* get current local reference table from thread */
148
149         lrt = LOCALREFTABLE;
150
151         assert(lrt != NULL);
152         assert(capacity > 0);
153
154         /* Allocate new local reference table on Java heap.  Calculate the
155            additional memory we have to allocate. */
156
157         if (capacity > LOCALREFTABLE_CAPACITY)
158                 additionalrefs = capacity - LOCALREFTABLE_CAPACITY;
159         else
160                 additionalrefs = 0;
161
162         nlrt = GCMNEW(u1, sizeof(localref_table) + additionalrefs * SIZEOF_VOID_P);
163
164         if (nlrt == NULL)
165                 return false;
166
167         /* Set up the new local reference table and add it to the local
168            frames chain. */
169
170         nlrt->capacity    = capacity;
171         nlrt->used        = 0;
172         nlrt->localframes = lrt->localframes + 1;
173         nlrt->prev        = lrt;
174
175         /* store new local reference table in thread */
176
177         LOCALREFTABLE = nlrt;
178
179         return true;
180 }
181
182
183 /* localref_frame_pop_all ******************************************************
184
185    Pops off all the local reference frames of the current table.
186
187 *******************************************************************************/
188
189 void localref_frame_pop_all(void)
190 {
191         localref_table *lrt;
192         localref_table *plrt;
193         int32_t         localframes;
194
195         TRACELOCALREF("frame pop all");
196
197         /* get current local reference table from thread */
198
199         lrt = LOCALREFTABLE;
200
201         assert(lrt != NULL);
202
203         localframes = lrt->localframes;
204
205         /* Don't delete the top local frame, as this one is allocated in
206            the native stub on the stack and is freed automagically on
207            return. */
208
209         if (localframes == 1)
210                 return;
211
212         /* release all current local frames */
213
214         for (; localframes > 1; localframes--) {
215                 /* get previous frame */
216
217                 plrt = lrt->prev;
218
219                 /* clear all reference entries */
220
221                 MSET(lrt->refs, 0, void*, lrt->capacity);
222
223                 lrt->prev = NULL;
224
225                 /* set new local references table */
226
227                 lrt = plrt;
228         }
229
230         /* store new local reference table in thread */
231
232         LOCALREFTABLE = lrt;
233 }
234
235
236 /* localref_dump ***************************************************************
237
238    Dumps all local reference tables, including all frames.
239
240 *******************************************************************************/
241
242 #if !defined(NDEBUG)
243 void localref_dump()
244 {
245         localref_table *lrt;
246         int i;
247
248         /* get current local reference table from thread */
249
250         lrt = LOCALREFTABLE;
251
252         log_println("--------- Local Reference Tables Dump ---------");
253
254         while (lrt != NULL) {
255                 log_println("Frame #%d, Used=%d, Capacity=%d, Addr=%p:", lrt->localframes, lrt->used, lrt->capacity, (void *) lrt);
256
257                 for (i = 0; i < lrt->used; i++) {
258                         printf("\t0x%08lx ", (intptr_t) lrt->refs[i]);
259                 }
260
261                 if (lrt->used != 0)
262                         printf("\n");
263
264                 lrt = lrt->prev;
265         }
266 }
267 #endif
268
269
270 /*
271  * These are local overrides for various environment variables in Emacs.
272  * Please do not remove this and leave it at the end of the file, where
273  * Emacs will automagically detect them.
274  * ---------------------------------------------------------------------
275  * Local variables:
276  * mode: c
277  * indent-tabs-mode: t
278  * c-basic-offset: 4
279  * tab-width: 4
280  * End:
281  * vim:noexpandtab:sw=4:ts=4:
282  */