* src/vm/hashtable.h,
[cacao.git] / src / vm / hashtable.h
1 /* src/vm/hashtable.h - functions for internal hashtables
2
3    Copyright (C) 1996-2005, 2006 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
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., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    Contact: cacao@cacaojvm.org
26
27    Authors: Reinhard Grafl
28
29    Changes: Christian Thalinger
30
31    $Id: hashtable.h 4921 2006-05-15 14:24:36Z twisti $
32
33 */
34
35
36 #ifndef _HASHTABLE_H
37 #define _HASHTABLE_H
38
39 /* forward typedefs ***********************************************************/
40
41 typedef struct hashtable hashtable;
42
43 #include "config.h"
44 #include "vm/types.h"
45
46 #include "vm/utf8.h"
47
48
49 /* data structures for hashtables ********************************************
50
51    All utf-symbols, javastrings and classes are stored in global
52    hashtables, so every symbol exists only once. Equal symbols have
53    identical pointers.  The functions for adding hashtable elements
54    search the table for the element with the specified name/text and
55    return it on success. Otherwise a new hashtable element is created.
56
57    The hashtables use external linking for handling collisions. The
58    hashtable structure contains a pointer <ptr> to the array of
59    hashtable slots. The number of hashtable slots and therefore the
60    size of this array is specified by the element <size> of hashtable
61    structure. <entries> contains the number of all hashtable elements
62    stored in the table, including those in the external chains.  The
63    hashtable element structures (utf, literalstring, classinfo)
64    contain both a pointer to the next hashtable element as a link for
65    the external hash chain and the key of the element. The key is
66    computed from the text of the string or the classname by using up
67    to 8 characters.
68         
69    If the number of entries in the hashtable exceeds twice the size of
70    the hashtableslot-array it is supposed that the average length of
71    the external chains has reached a value beyond 2. Therefore the
72    functions for adding hashtable elements (utf_new, class_new,
73    literalstring_new) double the hashtableslot-array. In this
74    restructuring process all elements have to be inserted into the new
75    hashtable and new external chains must be built.
76
77    Example for the layout of a hashtable:
78
79 hashtable.ptr-->+-------------------+
80                 |                   |
81                          ...
82                 |                   |
83                 +-------------------+   +-------------------+   +-------------------+
84                 | hashtable element |-->| hashtable element |-->| hashtable element |-->NULL
85                 +-------------------+   +-------------------+   +-------------------+
86                 | hashtable element |
87                 +-------------------+   +-------------------+   
88                 | hashtable element |-->| hashtable element |-->NULL
89                 +-------------------+   +-------------------+   
90                 | hashtable element |-->NULL
91                 +-------------------+
92                 |                   |
93                          ...
94                 |                   |
95                 +-------------------+
96
97 */
98
99
100 /* hashtable ******************************************************************/
101
102 struct hashtable {            
103 #if defined(ENABLE_THREADS)
104         java_objectheader  *header;         /* required for locking               */
105 #endif
106         u4                  size;           /* current size of the hashtable      */
107         u4                  entries;        /* number of entries in the table     */
108         void              **ptr;            /* pointer to hashtable               */
109 };
110
111
112 /* function prototypes ********************************************************/
113
114 /* create hashtable */
115 void hashtable_create(hashtable *hash, u4 size);
116
117 /* creates and resizes a hashtable */
118 hashtable *hashtable_resize(hashtable *hash, u4 size);
119
120 /* frees a hashtable */
121 void hashtable_free(hashtable *hash);
122
123 #endif /* _HASHTABLE_H */
124
125
126 /*
127  * These are local overrides for various environment variables in Emacs.
128  * Please do not remove this and leave it at the end of the file, where
129  * Emacs will automagically detect them.
130  * ---------------------------------------------------------------------
131  * Local variables:
132  * mode: c
133  * indent-tabs-mode: t
134  * c-basic-offset: 4
135  * tab-width: 4
136  * End:
137  */