* configure.ac: New switch for disabling -O2 (--disable-optimizations).
[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.hpp"
30
31 #include "threads/mutex.hpp"
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 mutex here, as we need to
51            store the lock object in the new hashtable if it's resized.
52            Otherwise we get an IllegalMonitorStateException. */
53
54         hash->mutex   = Mutex_new();
55 #endif
56
57         /* set initial hash values */
58
59         hash->size    = size;
60         hash->entries = 0;
61         hash->ptr     = MNEW(void*, size);
62
63         /* MNEW always allocates memory zeroed out, no need to clear the table */
64 }
65
66
67 /* hashtable_resize ************************************************************
68
69    Creates a new hashtable with specified size and moves the important
70    stuff from the old hashtable.
71
72 *******************************************************************************/
73
74 hashtable *hashtable_resize(hashtable *hash, u4 size)
75 {
76         hashtable *newhash;
77
78         /* create new hashtable with specified size */
79
80         newhash = NEW(hashtable);
81
82         hashtable_create(newhash, size);
83
84 #if defined(ENABLE_THREADS)
85         /* We need to store the old lock object in the new hashtable.
86            Otherwise we get an IllegalMonitorStateException. */
87
88         Mutex_delete(newhash->mutex);
89
90         newhash->mutex   = hash->mutex;
91 #endif
92
93         /* store the number of entries in the new hashtable */
94
95         newhash->entries = hash->entries;
96
97         return newhash;
98 }
99
100
101 /* hashtable_free **************************************************************
102
103    Simply frees the hashtable.
104
105    ATTENTION: It does NOT free the lock object!
106
107 *******************************************************************************/
108
109 void hashtable_free(hashtable *hash)
110 {
111         MFREE(hash->ptr, void*, hash->size);
112         FREE(hash, hashtable);
113 }
114
115
116 /*
117  * These are local overrides for various environment variables in Emacs.
118  * Please do not remove this and leave it at the end of the file, where
119  * Emacs will automagically detect them.
120  * ---------------------------------------------------------------------
121  * Local variables:
122  * mode: c
123  * indent-tabs-mode: t
124  * c-basic-offset: 4
125  * tab-width: 4
126  * End:
127  */