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