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