00001 /* 00002 ** Copyright (C) 2002 by Kevin L. Mitchell <klmitch@mit.edu> 00003 ** 00004 ** This library is free software; you can redistribute it and/or 00005 ** modify it under the terms of the GNU Library General Public 00006 ** License as published by the Free Software Foundation; either 00007 ** version 2 of the License, or (at your option) any later version. 00008 ** 00009 ** This library is distributed in the hope that it will be useful, 00010 ** but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 ** Library General Public License for more details. 00013 ** 00014 ** You should have received a copy of the GNU Library General Public 00015 ** License along with this library; if not, write to the Free 00016 ** Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, 00017 ** MA 02111-1307, USA 00018 ** 00019 ** @(#)$Id: ht_init.c,v 1.3 2006/07/13 05:36:16 klmitch Exp $ 00020 */ 00028 #include <errno.h> 00029 #include <stdlib.h> 00030 00031 #include "dbprim.h" 00032 #include "dbprim_int.h" 00033 00034 RCSTAG("@(#)$Id: ht_init.c,v 1.3 2006/07/13 05:36:16 klmitch Exp $"); 00035 00036 unsigned long 00037 ht_init(hash_table_t *table, unsigned long flags, hash_func_t func, 00038 hash_comp_t comp, hash_resize_t resize, void *extra, 00039 unsigned long init_mod) 00040 { 00041 int i; 00042 unsigned long retval; 00043 00044 initialize_dbpr_error_table(); /* set up error tables */ 00045 00046 if (!table || !func || !comp) /* verify arguments */ 00047 return DB_ERR_BADARGS; 00048 00049 /* initialize the table */ 00050 table->ht_flags = flags & (HASH_FLAG_AUTOGROW | HASH_FLAG_AUTOSHRINK); 00051 table->ht_modulus = _hash_prime(init_mod); 00052 table->ht_count = 0; 00053 table->ht_rollover = _hash_rollover(table->ht_modulus); 00054 table->ht_rollunder = _hash_rollunder(table->ht_modulus); 00055 table->ht_table = 0; 00056 table->ht_func = func; 00057 table->ht_comp = comp; 00058 table->ht_resize = resize; 00059 table->ht_extra = extra; 00060 00061 if (table->ht_modulus) { /* have an initial size? */ 00062 if (!(table->ht_table = 00063 (link_head_t *)malloc(table->ht_modulus * sizeof(link_head_t)))) 00064 return errno; /* failed to allocate memory? */ 00065 00066 for (i = 0; i < table->ht_modulus; i++) /* initialize the listhead array */ 00067 if ((retval = ll_init(&table->ht_table[i], table))) { 00068 free(table->ht_table); 00069 return retval; 00070 } 00071 } 00072 00073 table->ht_magic = HASH_TABLE_MAGIC; /* set the magic number */ 00074 00075 return 0; 00076 }