CACAO
hashtable.cpp
Go to the documentation of this file.
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 
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  hash->mutex = new Mutex();
47 
48  /* set initial hash values */
49 
50  hash->size = size;
51  hash->entries = 0;
52  hash->ptr = MNEW(void*, size);
53 
54  /* MNEW always allocates memory zeroed out, no need to clear the table */
55 }
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 
66 {
67  hashtable *newhash;
68 
69  /* create new hashtable with specified size */
70 
71  newhash = NEW(hashtable);
72 
73  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  delete newhash->mutex;
79 
80  newhash->mutex = hash->mutex;
81 
82  /* store the number of entries in the new hashtable */
83 
84  newhash->entries = hash->entries;
85 
86  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 
99 {
100  MFREE(hash->ptr, void*, hash->size);
101  FREE(hash, hashtable);
102 }
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  */
#define hash(_i1, _i2)
Definition: peephole.c:55
#define NEW(type)
Definition: memory.hpp:93
#define FREE(ptr, type)
Definition: memory.hpp:94
Dummy implementation of a mutex.
Definition: mutex-none.hpp:33
JNIEnv jthread jobject jclass jlong size
Definition: jvmti.h:387
void hashtable_free(hashtable *hash)
Definition: hashtable.cpp:98
void ** ptr
Definition: hashtable.hpp:781
hashtable * hashtable_resize(hashtable *hash, u4 size)
Definition: hashtable.cpp:65
uint32_t u4
Definition: types.hpp:46
#define MNEW(type, num)
Definition: memory.hpp:96
Mutex * mutex
Definition: hashtable.hpp:778
void hashtable_create(hashtable *hash, u4 size)
Definition: hashtable.cpp:38
#define MFREE(ptr, type, num)
Definition: memory.hpp:97