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
35 #include "mm/memory.h"
37 #include "native/localref.h"
39 #include "threads/threads-common.h"
42 /* global variables ***********************************************************/
44 #if !defined(ENABLE_THREADS)
45 localref_table *_no_threads_localref_table;
49 /* localref_table_init *********************************************************
51 Initializes the local references table of the current thread.
53 *******************************************************************************/
55 bool localref_table_init(void)
59 assert(LOCALREFTABLE == NULL);
61 lrt = GCNEW(localref_table);
66 localref_table_add(lrt);
71 /* localref_table_add **********************************************************
73 Add a new local references table to the current thread.
75 *******************************************************************************/
77 void localref_table_add(localref_table *lrt)
79 /* initialize the local reference table */
81 lrt->capacity = LOCALREFTABLE_CAPACITY;
84 lrt->prev = LOCALREFTABLE;
86 /* clear the references array (memset is faster the a for-loop) */
88 MSET(lrt->refs, 0, void*, LOCALREFTABLE_CAPACITY);
90 /* add given local references table to this thread */
96 /* localref_table_remove *******************************************************
98 Remoces the local references table from the current thread.
100 *******************************************************************************/
102 void localref_table_remove()
106 /* get current local reference table from thread */
110 assert(lrt->localframes == 1);
111 assert(lrt->prev != NULL);
119 /* localref_frame_push *********************************************************
121 Creates a new local reference frame, in which at least a given
122 number of local references can be created.
124 *******************************************************************************/
126 bool localref_frame_push(int32_t capacity)
129 localref_table *nlrt;
130 int32_t additionalrefs;
132 assert(capacity > 0);
134 /* Allocate new local reference table on Java heap. Calculate the
135 additional memory we have to allocate. */
137 if (capacity > LOCALREFTABLE_CAPACITY)
138 additionalrefs = capacity - LOCALREFTABLE_CAPACITY;
142 nlrt = GCMNEW(u1, sizeof(localref_table) + additionalrefs * SIZEOF_VOID_P);
147 /* get current local reference table from thread */
151 /* Set up the new local reference table and add it to the local
154 nlrt->capacity = capacity;
156 nlrt->localframes = lrt->localframes + 1;
159 /* store new local reference table in thread */
161 LOCALREFTABLE = nlrt;
167 /* localref_frame_pop_all ******************************************************
169 Pops off all the local reference frames of the current table.
171 *******************************************************************************/
173 void localref_frame_pop_all(void)
176 localref_table *plrt;
179 /* get current local reference table from thread */
183 localframes = lrt->localframes;
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
189 if (localframes == 1)
192 /* release all current local frames */
194 for (; localframes > 1; localframes--) {
195 /* get previous frame */
199 /* clear all reference entries */
201 MSET(lrt->refs, 0, void*, lrt->capacity);
205 /* set new local references table */
210 /* store new local reference table in thread */
216 /* localref_dump ***************************************************************
218 Dumps all local reference tables, including all frames.
220 *******************************************************************************/
228 /* get current local reference table from thread */
232 printf("\n--------- Local Reference Tables Dump ---------\n");
234 while (lrt != NULL) {
235 printf("Frame #%d, Used=%d, Capacity=%d, Addr=%p:\n", lrt->localframes, lrt->used, lrt->capacity, (void *) lrt);
237 for (i = 0; i < lrt->used; i++) {
238 printf("\t0x%08lx ", (intptr_t) lrt->refs[i]);
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 * ---------------------------------------------------------------------
257 * indent-tabs-mode: t
261 * vim:noexpandtab:sw=4:ts=4: