Moved string stuff into string.[ch], utf into utf8.[ch] and class stuff
[cacao.git] / src / vm / tables.c
1 /* 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
32    Contains support functions for:
33        - Reading of Java class files
34        - Unicode symbols
35        - the heap
36        - additional support functions
37
38    $Id: tables.c 1930 2005-02-10 10:54:28Z twisti $
39
40 */
41
42
43 #include <string.h>
44 #include <stdlib.h>
45 #include <assert.h>
46 #include <sys/types.h>
47 #include <sys/mman.h>
48 #include <unistd.h>
49
50 #include "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"
62
63
64 hashtable string_hash;  /* hashtable for javastrings  */
65
66
67 /******************************************************************************
68  *********************** hashtable functions **********************************
69  ******************************************************************************/
70
71 /* hashsize must be power of 2 */
72
73 #define UTF_HASHSTART   16384   /* initial size of utf-hash */    
74 #define HASHSTART        2048   /* initial size of javastring and class-hash */
75
76
77 /******************** function: init_hashtable ******************************
78
79     Initializes a hashtable structure and allocates memory.
80     The parameter size specifies the initial size of the hashtable.
81         
82 *****************************************************************************/
83
84 void init_hashtable(hashtable *hash, u4 size)
85 {
86         u4 i;
87
88         hash->entries = 0;
89         hash->size    = size;
90         hash->ptr     = MNEW(void*, size);
91
92         /* clear table */
93         for (i = 0; i < size; i++) hash->ptr[i] = NULL;
94 }
95
96
97 /*********************** function: tables_init  *****************************
98
99     creates hashtables for symboltables 
100         (called once at startup)                         
101         
102 *****************************************************************************/
103
104 void tables_init()
105 {
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 */ 
109
110 /*      if (opt_eager) */
111 /*              list_init(&unlinkedclasses, OFFSET(classinfo, listnode)); */
112
113 #if defined(STATISTICS)
114         if (opt_stat)
115                 count_utf_len += sizeof(utf*) * utf_hash.size;
116 #endif
117 }
118
119
120 /********************** function: tables_close ******************************
121
122         free memory for hashtables                    
123         
124 *****************************************************************************/
125
126 void tables_close()
127 {
128         utf *u = NULL;
129         literalstring *s;
130         u4 i;
131         
132         /* dispose utf symbols */
133         for (i = 0; i < utf_hash.size; i++) {
134                 u = utf_hash.ptr[i];
135                 while (u) {
136                         /* process elements in external hash chain */
137                         utf *nextu = u->hashlink;
138                         MFREE(u->text, u1, u->blength);
139                         FREE(u, utf);
140                         u = nextu;
141                 }       
142         }
143
144         /* dispose javastrings */
145         for (i = 0; i < string_hash.size; i++) {
146                 s = string_hash.ptr[i];
147                 while (u) {
148                         /* process elements in external hash chain */
149                         literalstring *nexts = s->hashlink;
150                         literalstring_free(s->string);
151                         FREE(s, literalstring);
152                         s = nexts;
153                 }       
154         }
155
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);
160 }
161
162
163 /******************************************************************************
164 *********************** Misc support functions ********************************
165 ******************************************************************************/
166
167
168 /******************** Function: desc_to_type **********************************
169    
170         Determines the corresponding Java base data type for a given type
171         descriptor.
172         
173 ******************************************************************************/
174
175 u2 desc_to_type(utf *descriptor)
176 {
177         char *utf_ptr = descriptor->text;  /* current position in utf text */
178         char logtext[MAXLOGTEXT];
179
180         if (descriptor->blength < 1) panic("Type-Descriptor is empty string");
181         
182         switch (*utf_ptr++) {
183         case 'B': 
184         case 'C':
185         case 'I':
186         case 'S':  
187         case 'Z':  return TYPE_INT;
188         case 'D':  return TYPE_DOUBLE;
189         case 'F':  return TYPE_FLOAT;
190         case 'J':  return TYPE_LONG;
191         case 'L':
192         case '[':  return TYPE_ADDRESS;
193         }
194                         
195         sprintf(logtext, "Invalid Type-Descriptor: ");
196         utf_sprint(logtext+strlen(logtext), descriptor);
197         error("%s",logtext);
198
199         return 0;
200 }
201
202
203 /********************** Function: desc_typesize *******************************
204
205         Calculates the lenght in bytes needed for a data element of the type given
206         by its type descriptor.
207         
208 ******************************************************************************/
209
210 u2 desc_typesize(utf *descriptor)
211 {
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);
218         default:           return 0;
219         }
220 }
221
222
223 /*
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  * ---------------------------------------------------------------------
228  * Local variables:
229  * mode: c
230  * indent-tabs-mode: t
231  * c-basic-offset: 4
232  * tab-width: 4
233  * End:
234  */