3 Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4 R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5 C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6 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., 59 Temple Place - Suite 330, Boston, MA
25 Contact: cacao@complang.tuwien.ac.at
27 Authors: Reinhard Grafl
33 Contains support functions for:
34 - Reading of Java class files
37 - additional support functions
39 $Id: tables.c 3679 2005-11-16 12:12:02Z twisti $
47 #include <sys/types.h>
54 #include "mm/memory.h"
55 #include "native/native.h"
56 #include "toolbox/logging.h"
57 #include "vm/builtin.h"
58 #include "vm/exceptions.h"
59 #include "vm/global.h"
60 #include "vm/loader.h"
61 #include "vm/options.h"
62 #include "vm/statistics.h"
63 #include "vm/stringlocal.h"
64 #include "vm/tables.h"
65 #include "vm/classcache.h"
68 hashtable string_hash; /* hashtable for javastrings */
71 /******************************************************************************
72 *********************** hashtable functions **********************************
73 ******************************************************************************/
75 /* hashsize must be power of 2 */
77 #define UTF_HASHSTART 16384 /* initial size of utf-hash */
78 #define HASHSTART 2048 /* initial size of javastring and class-hash */
81 /******************** function: init_hashtable ******************************
83 Initializes a hashtable structure and allocates memory.
84 The parameter size specifies the initial size of the hashtable.
86 *****************************************************************************/
88 void init_hashtable(hashtable *hash, u4 size)
94 hash->ptr = MNEW(void*, size);
97 for (i = 0; i < size; i++) hash->ptr[i] = NULL;
101 /* tables_init *****************************************************************
103 Creates hashtables for symboltables (called once at startup).
105 *******************************************************************************/
107 bool tables_init(void)
109 init_hashtable(&utf_hash, UTF_HASHSTART); /* hashtable for utf8-symbols */
110 init_hashtable(&string_hash, HASHSTART); /* hashtable for javastrings */
113 /* list_init(&unlinkedclasses, OFFSET(classinfo, listnode)); */
115 #if defined(STATISTICS)
117 count_utf_len += sizeof(utf*) * utf_hash.size;
120 /* everything's ok */
126 /********************** function: tables_close ******************************
128 free memory for hashtables
130 *****************************************************************************/
140 /* dispose utf symbols */
141 for (i = 0; i < utf_hash.size; i++) {
144 /* process elements in external hash chain */
145 utf *nextu = u->hashlink;
146 MFREE(u->text, u1, u->blength);
152 /* dispose javastrings */
153 for (i = 0; i < string_hash.size; i++) {
154 s = string_hash.ptr[i];
156 /* process elements in external hash chain */
157 literalstring *nexts = s->hashlink;
158 literalstring_free(s->string);
159 FREE(s, literalstring);
164 /* dispose hashtable structures */
165 MFREE(utf_hash.ptr, void*, utf_hash.size);
166 MFREE(string_hash.ptr, void*, string_hash.size);
170 /******************************************************************************
171 *********************** Misc support functions ********************************
172 ******************************************************************************/
175 /******************** Function: desc_to_type **********************************
177 Determines the corresponding Java base data type for a given type
180 ******************************************************************************/
182 u2 desc_to_type(utf *descriptor)
184 char *utf_ptr = descriptor->text; /* current position in utf text */
186 if (descriptor->blength < 1) {
187 log_text("Type-Descriptor is empty string");
191 switch (*utf_ptr++) {
196 case 'Z': return TYPE_INT;
197 case 'D': return TYPE_DOUBLE;
198 case 'F': return TYPE_FLOAT;
199 case 'J': return TYPE_LONG;
201 case '[': return TYPE_ADDRESS;
210 /********************** Function: desc_typesize *******************************
212 Calculates the lenght in bytes needed for a data element of the type given
213 by its type descriptor.
215 ******************************************************************************/
217 u2 desc_typesize(utf *descriptor)
219 switch (desc_to_type(descriptor)) {
220 case TYPE_INT: return 4;
221 case TYPE_LONG: return 8;
222 case TYPE_FLOAT: return 4;
223 case TYPE_DOUBLE: return 8;
224 case TYPE_ADDRESS: return sizeof(voidptr);
231 * These are local overrides for various environment variables in Emacs.
232 * Please do not remove this and leave it at the end of the file, where
233 * Emacs will automagically detect them.
234 * ---------------------------------------------------------------------
237 * indent-tabs-mode: t