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