Hash_table * hash_initialize (size_t candidate, const Hash_tuning *tuning, Hash_hasher hasher, Hash_comparator comparator, Hash_data_freer data_freer) { Hash_table *table; if (hasher == NULL || comparator == NULL) return NULL; table = malloc (sizeof *table); if (table == NULL) return NULL; if (!tuning) tuning = &default_tuning; table->tuning = tuning; if (!check_tuning (table)) { /* Fail if the tuning options are invalid. This is the only occasion when the user gets some feedback about it. Once the table is created, if the user provides invalid tuning options, we silently revert to using the defaults, and ignore further request to change the tuning options. */ goto fail; } if (!tuning->is_n_buckets) { float new_candidate = candidate / tuning->growth_threshold; if (SIZE_MAX <= new_candidate) goto fail; candidate = new_candidate; } if (xalloc_oversized (candidate, sizeof *table->bucket)) goto fail; table->n_buckets = next_prime (candidate); if (xalloc_oversized (table->n_buckets, sizeof *table->bucket)) goto fail; table->bucket = calloc (table->n_buckets, sizeof *table->bucket); table->bucket_limit = table->bucket + table->n_buckets; table->n_buckets_used = 0; table->n_entries = 0; table->hasher = hasher; table->comparator = comparator; table->data_freer = data_freer; table->free_entry_list = NULL; #if USE_OBSTACK obstack_init (&table->entry_stack); #endif return table; fail: free (table); return NULL; }
Hash_table * hash_initialize (unsigned candidate, const Hash_tuning *tuning, Hash_hasher hasher, Hash_comparator comparator, Hash_data_freer data_freer) { Hash_table *table; struct hash_entry *bucket; if (hasher == NULL || comparator == NULL) return NULL; table = (Hash_table *) malloc (sizeof (Hash_table)); if (table == NULL) return NULL; if (!tuning) tuning = &default_tuning; table->tuning = tuning; if (!check_tuning (table)) { /* Fail if the tuning options are invalid. This is the only occasion when the user gets some feedback about it. Once the table is created, if the user provides invalid tuning options, we silently revert to using the defaults, and ignore further request to change the tuning options. */ free (table); return NULL; } table->n_buckets = next_prime (tuning->is_n_buckets ? candidate : (unsigned) (candidate / tuning->growth_threshold)); table->bucket = (struct hash_entry *) malloc (table->n_buckets * sizeof (struct hash_entry)); if (table->bucket == NULL) { free (table); return NULL; } table->bucket_limit = table->bucket + table->n_buckets; for (bucket = table->bucket; bucket < table->bucket_limit; bucket++) { bucket->data = NULL; bucket->next = NULL; } table->n_buckets_used = 0; table->n_entries = 0; table->hasher = hasher; table->comparator = comparator; table->data_freer = data_freer; table->free_entry_list = NULL; #if USE_OBSTACK obstack_init (&table->entry_stack); #endif return table; }
Hash_table * hash_initialize (size_t candidate, const Hash_tuning *tuning, Hash_hasher hasher, Hash_comparator comparator, Hash_data_freer data_freer) { Hash_table *table; if (hasher == NULL) hasher = raw_hasher; if (comparator == NULL) comparator = raw_comparator; table = malloc (sizeof *table); if (table == NULL) return NULL; if (!tuning) tuning = &default_tuning; table->tuning = tuning; if (!check_tuning (table)) { /* Fail if the tuning options are invalid. This is the only occasion when the user gets some feedback about it. Once the table is created, if the user provides invalid tuning options, we silently revert to using the defaults, and ignore further request to change the tuning options. */ goto fail; } table->n_buckets = compute_bucket_size (candidate, tuning); if (!table->n_buckets) goto fail; table->bucket = calloc (table->n_buckets, sizeof *table->bucket); if (table->bucket == NULL) goto fail; table->bucket_limit = table->bucket + table->n_buckets; table->n_buckets_used = 0; table->n_entries = 0; table->hasher = hasher; table->comparator = comparator; table->data_freer = data_freer; table->free_entry_list = NULL; #if USE_OBSTACK obstack_init (&table->entry_stack); #endif return table; fail: free (table); return NULL; }