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