1 /* src/toolbox/hashtable.c - functions for internal hashtables
3 Copyright (C) 1996-2005, 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
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.
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.
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
29 #include "mm/memory.h"
31 #include "threads/mutex.hpp"
33 #include "toolbox/hashtable.h"
35 #include "vm/global.h"
38 /* hashtable_create ************************************************************
40 Initializes a hashtable structure and allocates memory. The
41 parameter size specifies the initial size of the hashtable.
43 *******************************************************************************/
45 void hashtable_create(hashtable *hash, u4 size)
47 /* initialize locking pointer */
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. */
54 hash->mutex = Mutex_new();
57 /* set initial hash values */
61 hash->ptr = MNEW(void*, size);
63 /* MNEW always allocates memory zeroed out, no need to clear the table */
67 /* hashtable_resize ************************************************************
69 Creates a new hashtable with specified size and moves the important
70 stuff from the old hashtable.
72 *******************************************************************************/
74 hashtable *hashtable_resize(hashtable *hash, u4 size)
78 /* create new hashtable with specified size */
80 newhash = NEW(hashtable);
82 hashtable_create(newhash, size);
84 #if defined(ENABLE_THREADS)
85 /* We need to store the old lock object in the new hashtable.
86 Otherwise we get an IllegalMonitorStateException. */
88 Mutex_delete(newhash->mutex);
90 newhash->mutex = hash->mutex;
93 /* store the number of entries in the new hashtable */
95 newhash->entries = hash->entries;
101 /* hashtable_free **************************************************************
103 Simply frees the hashtable.
105 ATTENTION: It does NOT free the lock object!
107 *******************************************************************************/
109 void hashtable_free(hashtable *hash)
111 MFREE(hash->ptr, void*, hash->size);
112 FREE(hash, hashtable);
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 * ---------------------------------------------------------------------
123 * indent-tabs-mode: t