* Removed all Id tags.
[cacao.git] / src / toolbox / hashtable.c
1 /* src/toolbox/hashtable.c - functions for internal hashtables
2
3    Copyright (C) 1996-2005, 2006, 2007 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 */
26
27
28 #include "config.h"
29 #include "vm/types.h"
30
31 #include "mm/memory.h"
32
33 #include "threads/lock-common.h"
34
35 #include "toolbox/hashtable.h"
36
37 #include "vm/global.h"
38
39
40 /* hashtable_create ************************************************************
41
42    Initializes a hashtable structure and allocates memory. The
43    parameter size specifies the initial size of the hashtable.
44         
45 *******************************************************************************/
46
47 void hashtable_create(hashtable *hash, u4 size)
48 {
49         /* initialize locking pointer */
50
51 #if defined(ENABLE_THREADS)
52         /* We need to seperately allocate a java_object_t here, as we
53            need to store the lock object in the new hashtable if it's
54            resized.  Otherwise we get an IllegalMonitorStateException. */
55
56         hash->header = NEW(java_object_t);
57
58         LOCK_INIT_OBJECT_LOCK(hash->header);
59 #endif
60
61         /* set initial hash values */
62
63         hash->size    = size;
64         hash->entries = 0;
65         hash->ptr     = MNEW(void*, size);
66
67         /* MNEW always allocates memory zeroed out, no need to clear the table */
68 }
69
70
71 /* hashtable_resize ************************************************************
72
73    Creates a new hashtable with specified size and moves the important
74    stuff from the old hashtable.
75
76 *******************************************************************************/
77
78 hashtable *hashtable_resize(hashtable *hash, u4 size)
79 {
80         hashtable *newhash;
81
82         /* create new hashtable with specified size */
83
84         newhash = NEW(hashtable);
85
86         hashtable_create(newhash, size);
87
88 #if defined(ENABLE_THREADS)
89         /* We need to store the old lock object in the new hashtable.
90            Otherwise we get an IllegalMonitorStateException. */
91
92         FREE(newhash->header, java_object_t);
93
94         newhash->header  = hash->header;
95 #endif
96
97         /* store the number of entries in the new hashtable */
98
99         newhash->entries = hash->entries;
100
101         return newhash;
102 }
103
104
105 /* hashtable_free **************************************************************
106
107    Simply frees the hashtable.
108
109    ATTENTION: It does NOT free the lock object!
110
111 *******************************************************************************/
112
113 void hashtable_free(hashtable *hash)
114 {
115         MFREE(hash->ptr, void*, hash->size);
116         FREE(hash, hashtable);
117 }
118
119
120 /*
121  * These are local overrides for various environment variables in Emacs.
122  * Please do not remove this and leave it at the end of the file, where
123  * Emacs will automagically detect them.
124  * ---------------------------------------------------------------------
125  * Local variables:
126  * mode: c
127  * indent-tabs-mode: t
128  * c-basic-offset: 4
129  * tab-width: 4
130  * End:
131  */