2 ** Copyright (C) 2002 by Kevin L. Mitchell <klmitch@mit.edu>
4 ** This library is free software; you can redistribute it and/or
5 ** modify it under the terms of the GNU Library General Public
6 ** License as published by the Free Software Foundation; either
7 ** version 2 of the License, or (at your option) any later version.
9 ** This library is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ** Library General Public License for more details.
14 ** You should have received a copy of the GNU Library General Public
15 ** License along with this library; if not, write to the Free
16 ** Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 #include "dbprim_int.h"
29 /** \ingroup dbprim_hash
30 * \brief Dynamically initialize a hash table.
32 * This function dynamically initializes a hash table.
34 * \param table A pointer to a #hash_table_t to be initialized.
35 * \param flags A bit-wise OR of #HASH_FLAG_AUTOGROW and
36 * #HASH_FLAG_AUTOSHRINK. If neither behavior is
38 * \param func A #hash_func_t function pointer for a hash function.
39 * \param comp A #hash_comp_t function pointer for a comparison
42 * A #hash_resize_t function pointer for determining
43 * whether resizing is permitted and/or for notification
45 * \param extra Extra pointer data that should be associated with the
48 * An initial modulus for the table. This will
49 * presumably be extracted by ht_modulus() in a previous
50 * invocation of the application. A 0 value is valid.
52 * \retval DB_ERR_BADARGS An invalid argument was given.
53 * \retval ENOMEM Unable to allocate memory.
56 ht_init(hash_table_t *table, unsigned long flags, hash_func_t func,
57 hash_comp_t comp, hash_resize_t resize, void *extra,
58 unsigned long init_mod)
63 initialize_dbpr_error_table(); /* set up error tables */
65 if (!table || !func || !comp) /* verify arguments */
66 return DB_ERR_BADARGS;
68 /* initialize the table */
69 table->ht_flags = flags & (HASH_FLAG_AUTOGROW | HASH_FLAG_AUTOSHRINK);
70 table->ht_modulus = _hash_prime(init_mod);
72 table->ht_rollover = _hash_rollover(table->ht_modulus);
73 table->ht_rollunder = _hash_rollunder(table->ht_modulus);
75 table->ht_func = func;
76 table->ht_comp = comp;
77 table->ht_resize = resize;
78 table->ht_extra = extra;
80 if (table->ht_modulus) { /* have an initial size? */
81 if (!(table->ht_table =
82 (link_head_t *)malloc(table->ht_modulus * sizeof(link_head_t))))
83 return errno; /* failed to allocate memory? */
85 for (i = 0; i < table->ht_modulus; i++) /* initialize the listhead array */
86 if ((retval = ll_init(&table->ht_table[i], table))) {
87 free(table->ht_table);
92 table->ht_magic = HASH_TABLE_MAGIC; /* set the magic number */