* don't include "md.h", not needed
[cacao.git] / src / vm / tables.c
1 /* src/vm/tables.c - 
2
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
7
8    This file is part of CACAO.
9
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.
14
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.
19
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
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Reinhard Grafl
28
29    Changes: Mark Probst
30             Andreas Krall
31             Christian Thalinger
32
33    Contains support functions for:
34        - Reading of Java class files
35        - Unicode symbols
36        - the heap
37        - additional support functions
38
39    $Id: tables.c 2505 2005-05-23 08:23:40Z twisti $
40
41 */
42
43
44 #include <string.h>
45 #include <stdlib.h>
46 #include <assert.h>
47 #include <sys/types.h>
48 #include <sys/mman.h>
49 #include <unistd.h>
50
51 #include "types.h"
52 #include "mm/memory.h"
53 #include "native/native.h"
54 #include "toolbox/logging.h"
55 #include "vm/builtin.h"
56 #include "vm/exceptions.h"
57 #include "vm/global.h"
58 #include "vm/loader.h"
59 #include "vm/options.h"
60 #include "vm/statistics.h"
61 #include "vm/stringlocal.h"
62 #include "vm/tables.h"
63 #include "vm/classcache.h"
64
65
66 hashtable string_hash;  /* hashtable for javastrings  */
67
68
69 /******************************************************************************
70  *********************** hashtable functions **********************************
71  ******************************************************************************/
72
73 /* hashsize must be power of 2 */
74
75 #define UTF_HASHSTART   16384   /* initial size of utf-hash */    
76 #define HASHSTART        2048   /* initial size of javastring and class-hash */
77
78
79 /******************** function: init_hashtable ******************************
80
81     Initializes a hashtable structure and allocates memory.
82     The parameter size specifies the initial size of the hashtable.
83         
84 *****************************************************************************/
85
86 void init_hashtable(hashtable *hash, u4 size)
87 {
88         u4 i;
89
90         hash->entries = 0;
91         hash->size    = size;
92         hash->ptr     = MNEW(void*, size);
93
94         /* clear table */
95         for (i = 0; i < size; i++) hash->ptr[i] = NULL;
96 }
97
98
99 /*********************** function: tables_init  *****************************
100
101     creates hashtables for symboltables 
102         (called once at startup)                         
103         
104 *****************************************************************************/
105
106 void tables_init()
107 {
108         init_hashtable(&utf_hash,    UTF_HASHSTART);  /* hashtable for utf8-symbols */
109         init_hashtable(&string_hash, HASHSTART);      /* hashtable for javastrings */
110
111         classcache_init();
112
113 /*      if (opt_eager) */
114 /*              list_init(&unlinkedclasses, OFFSET(classinfo, listnode)); */
115
116 #if defined(STATISTICS)
117         if (opt_stat)
118                 count_utf_len += sizeof(utf*) * utf_hash.size;
119 #endif
120 }
121
122
123 /********************** function: tables_close ******************************
124
125         free memory for hashtables                    
126         
127 *****************************************************************************/
128
129 void tables_close()
130 {
131         utf *u = NULL;
132         literalstring *s;
133         u4 i;
134
135         classcache_free();
136         
137         /* dispose utf symbols */
138         for (i = 0; i < utf_hash.size; i++) {
139                 u = utf_hash.ptr[i];
140                 while (u) {
141                         /* process elements in external hash chain */
142                         utf *nextu = u->hashlink;
143                         MFREE(u->text, u1, u->blength);
144                         FREE(u, utf);
145                         u = nextu;
146                 }       
147         }
148
149         /* dispose javastrings */
150         for (i = 0; i < string_hash.size; i++) {
151                 s = string_hash.ptr[i];
152                 while (u) {
153                         /* process elements in external hash chain */
154                         literalstring *nexts = s->hashlink;
155                         literalstring_free(s->string);
156                         FREE(s, literalstring);
157                         s = nexts;
158                 }       
159         }
160
161         /* dispose hashtable structures */
162         MFREE(utf_hash.ptr,    void*, utf_hash.size);
163         MFREE(string_hash.ptr, void*, string_hash.size);
164 }
165
166
167 /******************************************************************************
168 *********************** Misc support functions ********************************
169 ******************************************************************************/
170
171
172 /******************** Function: desc_to_type **********************************
173    
174         Determines the corresponding Java base data type for a given type
175         descriptor.
176         
177 ******************************************************************************/
178
179 u2 desc_to_type(utf *descriptor)
180 {
181         char *utf_ptr = descriptor->text;  /* current position in utf text */
182
183         if (descriptor->blength < 1) {
184                 log_text("Type-Descriptor is empty string");
185                 assert(0);
186         }
187         
188         switch (*utf_ptr++) {
189         case 'B': 
190         case 'C':
191         case 'I':
192         case 'S':  
193         case 'Z':  return TYPE_INT;
194         case 'D':  return TYPE_DOUBLE;
195         case 'F':  return TYPE_FLOAT;
196         case 'J':  return TYPE_LONG;
197         case 'L':
198         case '[':  return TYPE_ADDRESS;
199         }
200                         
201         assert(0);
202
203         return 0;
204 }
205
206
207 /********************** Function: desc_typesize *******************************
208
209         Calculates the lenght in bytes needed for a data element of the type given
210         by its type descriptor.
211         
212 ******************************************************************************/
213
214 u2 desc_typesize(utf *descriptor)
215 {
216         switch (desc_to_type(descriptor)) {
217         case TYPE_INT:     return 4;
218         case TYPE_LONG:    return 8;
219         case TYPE_FLOAT:   return 4;
220         case TYPE_DOUBLE:  return 8;
221         case TYPE_ADDRESS: return sizeof(voidptr);
222         default:           return 0;
223         }
224 }
225
226
227 /*
228  * These are local overrides for various environment variables in Emacs.
229  * Please do not remove this and leave it at the end of the file, where
230  * Emacs will automagically detect them.
231  * ---------------------------------------------------------------------
232  * Local variables:
233  * mode: c
234  * indent-tabs-mode: t
235  * c-basic-offset: 4
236  * tab-width: 4
237  * End:
238  */