1 /* src/native/localref.c - Management of local reference tables
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
8 This file is part of CACAO.
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.
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.
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
25 $Id: localref.c 8313 2007-08-15 22:11:35Z twisti $
35 #include "mm/memory.h"
37 #include "native/localref.h"
39 #include "threads/threads-common.h"
41 #include "toolbox/logging.h"
44 /* debug **********************************************************************/
46 #if !defined(NDEBUG) && 0
47 # define TRACELOCALREF(message) log_println("%s", message)
49 # define TRACELOCALREF(message)
53 /* global variables ***********************************************************/
55 #if !defined(ENABLE_THREADS)
56 localref_table *_no_threads_localref_table;
60 /* localref_table_init *********************************************************
62 Initializes the local references table of the current thread.
64 *******************************************************************************/
66 bool localref_table_init(void)
70 TRACELOCALREF("table init");
72 assert(LOCALREFTABLE == NULL);
74 lrt = GCNEW(localref_table);
79 localref_table_add(lrt);
84 /* localref_table_add **********************************************************
86 Add a new local references table to the current thread.
88 *******************************************************************************/
90 void localref_table_add(localref_table *lrt)
92 /* initialize the local reference table */
94 lrt->capacity = LOCALREFTABLE_CAPACITY;
97 lrt->prev = LOCALREFTABLE;
99 /* clear the references array (memset is faster the a for-loop) */
101 MSET(lrt->refs, 0, void*, LOCALREFTABLE_CAPACITY);
103 /* add given local references table to this thread */
109 /* localref_table_remove *******************************************************
111 Remoces the local references table from the current thread.
113 *******************************************************************************/
115 void localref_table_remove()
119 /* get current local reference table from thread */
124 assert(lrt->localframes == 1);
132 /* localref_frame_push *********************************************************
134 Creates a new local reference frame, in which at least a given
135 number of local references can be created.
137 *******************************************************************************/
139 bool localref_frame_push(int32_t capacity)
142 localref_table *nlrt;
143 int32_t additionalrefs;
145 TRACELOCALREF("frame push");
147 /* get current local reference table from thread */
152 assert(capacity > 0);
154 /* Allocate new local reference table on Java heap. Calculate the
155 additional memory we have to allocate. */
157 if (capacity > LOCALREFTABLE_CAPACITY)
158 additionalrefs = capacity - LOCALREFTABLE_CAPACITY;
162 nlrt = GCMNEW(u1, sizeof(localref_table) + additionalrefs * SIZEOF_VOID_P);
167 /* Set up the new local reference table and add it to the local
170 nlrt->capacity = capacity;
172 nlrt->localframes = lrt->localframes + 1;
175 /* store new local reference table in thread */
177 LOCALREFTABLE = nlrt;
183 /* localref_frame_pop_all ******************************************************
185 Pops off all the local reference frames of the current table.
187 *******************************************************************************/
189 void localref_frame_pop_all(void)
192 localref_table *plrt;
195 TRACELOCALREF("frame pop all");
197 /* get current local reference table from thread */
203 localframes = lrt->localframes;
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
209 if (localframes == 1)
212 /* release all current local frames */
214 for (; localframes > 1; localframes--) {
215 /* get previous frame */
219 /* clear all reference entries */
221 MSET(lrt->refs, 0, void*, lrt->capacity);
225 /* set new local references table */
230 /* store new local reference table in thread */
236 /* localref_dump ***************************************************************
238 Dumps all local reference tables, including all frames.
240 *******************************************************************************/
248 /* get current local reference table from thread */
252 log_println("--------- Local Reference Tables Dump ---------");
254 while (lrt != NULL) {
255 log_println("Frame #%d, Used=%d, Capacity=%d, Addr=%p:", lrt->localframes, lrt->used, lrt->capacity, (void *) lrt);
257 for (i = 0; i < lrt->used; i++) {
258 printf("\t0x%08lx ", (intptr_t) lrt->refs[i]);
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 * ---------------------------------------------------------------------
277 * indent-tabs-mode: t
281 * vim:noexpandtab:sw=4:ts=4: