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
33 #include "mm/memory.h"
35 #include "native/localref.h"
37 #include "threads/threads-common.h"
39 #include "toolbox/logging.h"
42 /* debug **********************************************************************/
44 #if !defined(NDEBUG) && 0
45 # define TRACELOCALREF(message) log_println("%s", message)
47 # define TRACELOCALREF(message)
51 /* global variables ***********************************************************/
53 #if !defined(ENABLE_THREADS)
54 localref_table *_no_threads_localref_table;
58 /* localref_table_init *********************************************************
60 Initializes the local references table of the current thread.
62 *******************************************************************************/
64 bool localref_table_init(void)
68 TRACELOCALREF("table init");
70 assert(LOCALREFTABLE == NULL);
72 lrt = GCNEW(localref_table);
77 localref_table_add(lrt);
82 /* localref_table_add **********************************************************
84 Add a new local references table to the current thread.
86 *******************************************************************************/
88 void localref_table_add(localref_table *lrt)
90 /* initialize the local reference table */
92 lrt->capacity = LOCALREFTABLE_CAPACITY;
95 lrt->prev = LOCALREFTABLE;
97 /* clear the references array (memset is faster the a for-loop) */
99 MSET(lrt->refs, 0, void*, LOCALREFTABLE_CAPACITY);
101 /* add given local references table to this thread */
107 /* localref_table_remove *******************************************************
109 Remoces the local references table from the current thread.
111 *******************************************************************************/
113 void localref_table_remove()
117 /* get current local reference table from thread */
122 assert(lrt->localframes == 1);
130 /* localref_frame_push *********************************************************
132 Creates a new local reference frame, in which at least a given
133 number of local references can be created.
135 *******************************************************************************/
137 bool localref_frame_push(int32_t capacity)
140 localref_table *nlrt;
141 int32_t additionalrefs;
143 TRACELOCALREF("frame push");
145 /* get current local reference table from thread */
150 assert(capacity > 0);
152 /* Allocate new local reference table on Java heap. Calculate the
153 additional memory we have to allocate. */
155 if (capacity > LOCALREFTABLE_CAPACITY)
156 additionalrefs = capacity - LOCALREFTABLE_CAPACITY;
160 nlrt = GCMNEW(u1, sizeof(localref_table) + additionalrefs * SIZEOF_VOID_P);
165 /* Set up the new local reference table and add it to the local
168 nlrt->capacity = capacity;
170 nlrt->localframes = lrt->localframes + 1;
173 /* store new local reference table in thread */
175 LOCALREFTABLE = nlrt;
181 /* localref_frame_pop_all ******************************************************
183 Pops off all the local reference frames of the current table.
185 *******************************************************************************/
187 void localref_frame_pop_all(void)
190 localref_table *plrt;
193 TRACELOCALREF("frame pop all");
195 /* get current local reference table from thread */
201 localframes = lrt->localframes;
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
207 if (localframes == 1)
210 /* release all current local frames */
212 for (; localframes > 1; localframes--) {
213 /* get previous frame */
217 /* clear all reference entries */
219 MSET(lrt->refs, 0, void*, lrt->capacity);
223 /* set new local references table */
228 /* store new local reference table in thread */
234 /* localref_dump ***************************************************************
236 Dumps all local reference tables, including all frames.
238 *******************************************************************************/
246 /* get current local reference table from thread */
250 log_println("--------- Local Reference Tables Dump ---------");
252 while (lrt != NULL) {
253 log_println("Frame #%d, Used=%d, Capacity=%d, Addr=%p:", lrt->localframes, lrt->used, lrt->capacity, (void *) lrt);
255 for (i = 0; i < lrt->used; i++) {
256 printf("\t0x%08lx ", (intptr_t) lrt->refs[i]);
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 * ---------------------------------------------------------------------
275 * indent-tabs-mode: t
279 * vim:noexpandtab:sw=4:ts=4: