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
32 Contains support functions for:
33 - Reading of Java class files
36 - additional support functions
38 $Id: tables.c 1930 2005-02-10 10:54:28Z twisti $
46 #include <sys/types.h>
51 #include "mm/memory.h"
52 #include "native/native.h"
53 #include "toolbox/logging.h"
54 #include "vm/builtin.h"
55 #include "vm/exceptions.h"
56 #include "vm/global.h"
57 #include "vm/loader.h"
58 #include "vm/options.h"
59 #include "vm/statistics.h"
60 #include "vm/stringlocal.h"
61 #include "vm/tables.h"
64 hashtable string_hash; /* hashtable for javastrings */
67 /******************************************************************************
68 *********************** hashtable functions **********************************
69 ******************************************************************************/
71 /* hashsize must be power of 2 */
73 #define UTF_HASHSTART 16384 /* initial size of utf-hash */
74 #define HASHSTART 2048 /* initial size of javastring and class-hash */
77 /******************** function: init_hashtable ******************************
79 Initializes a hashtable structure and allocates memory.
80 The parameter size specifies the initial size of the hashtable.
82 *****************************************************************************/
84 void init_hashtable(hashtable *hash, u4 size)
90 hash->ptr = MNEW(void*, size);
93 for (i = 0; i < size; i++) hash->ptr[i] = NULL;
97 /*********************** function: tables_init *****************************
99 creates hashtables for symboltables
100 (called once at startup)
102 *****************************************************************************/
106 init_hashtable(&utf_hash, UTF_HASHSTART); /* hashtable for utf8-symbols */
107 init_hashtable(&string_hash, HASHSTART); /* hashtable for javastrings */
108 init_hashtable(&class_hash, HASHSTART); /* hashtable for classes */
111 /* list_init(&unlinkedclasses, OFFSET(classinfo, listnode)); */
113 #if defined(STATISTICS)
115 count_utf_len += sizeof(utf*) * utf_hash.size;
120 /********************** function: tables_close ******************************
122 free memory for hashtables
124 *****************************************************************************/
132 /* dispose utf symbols */
133 for (i = 0; i < utf_hash.size; i++) {
136 /* process elements in external hash chain */
137 utf *nextu = u->hashlink;
138 MFREE(u->text, u1, u->blength);
144 /* dispose javastrings */
145 for (i = 0; i < string_hash.size; i++) {
146 s = string_hash.ptr[i];
148 /* process elements in external hash chain */
149 literalstring *nexts = s->hashlink;
150 literalstring_free(s->string);
151 FREE(s, literalstring);
156 /* dispose hashtable structures */
157 MFREE(utf_hash.ptr, void*, utf_hash.size);
158 MFREE(string_hash.ptr, void*, string_hash.size);
159 MFREE(class_hash.ptr, void*, class_hash.size);
163 /******************************************************************************
164 *********************** Misc support functions ********************************
165 ******************************************************************************/
168 /******************** Function: desc_to_type **********************************
170 Determines the corresponding Java base data type for a given type
173 ******************************************************************************/
175 u2 desc_to_type(utf *descriptor)
177 char *utf_ptr = descriptor->text; /* current position in utf text */
178 char logtext[MAXLOGTEXT];
180 if (descriptor->blength < 1) panic("Type-Descriptor is empty string");
182 switch (*utf_ptr++) {
187 case 'Z': return TYPE_INT;
188 case 'D': return TYPE_DOUBLE;
189 case 'F': return TYPE_FLOAT;
190 case 'J': return TYPE_LONG;
192 case '[': return TYPE_ADDRESS;
195 sprintf(logtext, "Invalid Type-Descriptor: ");
196 utf_sprint(logtext+strlen(logtext), descriptor);
203 /********************** Function: desc_typesize *******************************
205 Calculates the lenght in bytes needed for a data element of the type given
206 by its type descriptor.
208 ******************************************************************************/
210 u2 desc_typesize(utf *descriptor)
212 switch (desc_to_type(descriptor)) {
213 case TYPE_INT: return 4;
214 case TYPE_LONG: return 8;
215 case TYPE_FLOAT: return 4;
216 case TYPE_DOUBLE: return 8;
217 case TYPE_ADDRESS: return sizeof(voidptr);
224 * These are local overrides for various environment variables in Emacs.
225 * Please do not remove this and leave it at the end of the file, where
226 * Emacs will automagically detect them.
227 * ---------------------------------------------------------------------
230 * indent-tabs-mode: t