8843324d7d278a423fd1ec1d3bb9dfa71f114405
[cacao.git] / src / toolbox / hashtable.c
1 /* src/toolbox/hashtable.c - functions for internal hashtables
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27 #include "vm/types.h"
28
29 #include "mm/memory.h"
30
31 #include "threads/lock-common.h"
32
33 #include "toolbox/hashtable.h"
34
35 #include "vm/global.h"
36
37
38 /* hashtable_create ************************************************************
39
40    Initializes a hashtable structure and allocates memory. The
41    parameter size specifies the initial size of the hashtable.
42         
43 *******************************************************************************/
44
45 void hashtable_create(hashtable *hash, u4 size)
46 {
47         /* initialize locking pointer */
48
49 #if defined(ENABLE_THREADS)
50         /* We need to seperately allocate a java_object_t here, as we
51            need to store the lock object in the new hashtable if it's
52            resized.  Otherwise we get an IllegalMonitorStateException. */
53
54         hash->header = NEW(java_object_t);
55
56         LOCK_INIT_OBJECT_LOCK(hash->header);
57 #endif
58
59         /* set initial hash values */
60
61         hash->size    = size;
62         hash->entries = 0;
63         hash->ptr     = MNEW(void*, size);
64
65         /* MNEW always allocates memory zeroed out, no need to clear the table */
66 }
67
68
69 /* hashtable_resize ************************************************************
70
71    Creates a new hashtable with specified size and moves the important
72    stuff from the old hashtable.
73
74 *******************************************************************************/
75
76 hashtable *hashtable_resize(hashtable *hash, u4 size)
77 {
78         hashtable *newhash;
79
80         /* create new hashtable with specified size */
81
82         newhash = NEW(hashtable);
83
84         hashtable_create(newhash, size);
85
86 #if defined(ENABLE_THREADS)
87         /* We need to store the old lock object in the new hashtable.
88            Otherwise we get an IllegalMonitorStateException. */
89
90         FREE(newhash->header, java_object_t);
91
92         newhash->header  = hash->header;
93 #endif
94
95         /* store the number of entries in the new hashtable */
96
97         newhash->entries = hash->entries;
98
99         return newhash;
100 }
101
102
103 /* hashtable_free **************************************************************
104
105    Simply frees the hashtable.
106
107    ATTENTION: It does NOT free the lock object!
108
109 *******************************************************************************/
110
111 void hashtable_free(hashtable *hash)
112 {
113         MFREE(hash->ptr, void*, hash->size);
114         FREE(hash, hashtable);
115 }
116
117
118 /*
119  * These are local overrides for various environment variables in Emacs.
120  * Please do not remove this and leave it at the end of the file, where
121  * Emacs will automagically detect them.
122  * ---------------------------------------------------------------------
123  * Local variables:
124  * mode: c
125  * indent-tabs-mode: t
126  * c-basic-offset: 4
127  * tab-width: 4
128  * End:
129  */