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