1 |
/* |
2 |
** Copyright (C) 2002 by Kevin L. Mitchell <klmitch@mit.edu> |
3 |
** |
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. |
8 |
** |
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. |
13 |
** |
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, |
17 |
** MA 02111-1307, USA |
18 |
** |
19 |
** @(#)$Id: ht_init.c,v 1.2 2003/06/12 01:10:04 klmitch Exp $ |
20 |
*/ |
21 |
#include <errno.h> |
22 |
#include <stdlib.h> |
23 |
|
24 |
#include "dbprim.h" |
25 |
#include "dbprim_int.h" |
26 |
|
27 |
RCSTAG("@(#)$Id: ht_init.c,v 1.2 2003/06/12 01:10:04 klmitch Exp $"); |
28 |
|
29 |
/** \ingroup dbprim_hash |
30 |
* \brief Dynamically initialize a hash table. |
31 |
* |
32 |
* This function dynamically initializes a hash table. |
33 |
* |
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 |
37 |
* desired, use 0. |
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 |
40 |
* function. |
41 |
* \param resize |
42 |
* A #hash_resize_t function pointer for determining |
43 |
* whether resizing is permitted and/or for notification |
44 |
* of the resize. |
45 |
* \param extra Extra pointer data that should be associated with the |
46 |
* hash table. |
47 |
* \param init_mod |
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. |
51 |
* |
52 |
* \retval DB_ERR_BADARGS An invalid argument was given. |
53 |
* \retval ENOMEM Unable to allocate memory. |
54 |
*/ |
55 |
unsigned long |
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) |
59 |
{ |
60 |
int i; |
61 |
unsigned long retval; |
62 |
|
63 |
initialize_dbpr_error_table(); /* set up error tables */ |
64 |
|
65 |
if (!table || !func || !comp) /* verify arguments */ |
66 |
return DB_ERR_BADARGS; |
67 |
|
68 |
/* initialize the table */ |
69 |
table->ht_flags = flags & (HASH_FLAG_AUTOGROW | HASH_FLAG_AUTOSHRINK); |
70 |
table->ht_modulus = _hash_prime(init_mod); |
71 |
table->ht_count = 0; |
72 |
table->ht_rollover = _hash_rollover(table->ht_modulus); |
73 |
table->ht_rollunder = _hash_rollunder(table->ht_modulus); |
74 |
table->ht_table = 0; |
75 |
table->ht_func = func; |
76 |
table->ht_comp = comp; |
77 |
table->ht_resize = resize; |
78 |
table->ht_extra = extra; |
79 |
|
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? */ |
84 |
|
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); |
88 |
return retval; |
89 |
} |
90 |
} |
91 |
|
92 |
table->ht_magic = HASH_TABLE_MAGIC; /* set the magic number */ |
93 |
|
94 |
return 0; |
95 |
} |