/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
*/
/*
* Generic hash table library. The hash table is an array of pointers
* to items. Hash collisions are handled using linked lists from the
* table entries. A handle is associated with each table, which is used
* to maintain the hash table.
*
* +------+ +-------+ +----+ +----+
* |handle|---> |index 0|--->|item|--->|item|--->
* | ... | +-------+ +----+ +----+
* | ... | |index 1|--->
* +------+ +-------+ +----+ +----+ +----+
* |index 2|--->|item|--->|item|--->|item|--->
* +-------+ +----+ +----+ +----+
* | ... |--->
* +-------+
* | ... |--->
* +-------+
* |index n|--->
* +-------+
*
*/
#include <stdlib.h>
#include <strings.h>
#include <smbns_hash.h>
/*
* ht_is_power2
*
* Inline function to determine if a value is a power of two. This
* function is used by the library to validate the table size when
* a new table is created.
*
* Returns 1 if value given is power of two, otherwise returns 0.
*/
static size_t
{
}
/*
* ht_create_table
*
* Create a hash table. The table size must be a positive integer and
* must be a power of two. The key size must be a positive integer.
* For null terminated keys, the key size does not need to include the
* null terminating character. The type of key is indicated by the flags.
*
* The handle and the table are are malloc'd using a single call, to
* avoid two allocations. The table is located immediately after the
* handle.
*
* On success a pointer to an opaque handle is returned. Otherwise a
* null pointer is returned.
*/
{
size_t i;
if ((table_size == 0) || (key_size == 0))
return (NULL);
if (ht_is_power2(table_size) == 0)
return (NULL);
return (NULL);
/*LINTED E_BAD_PTR_CAST_ALIGN*/
ht->ht_total_items = 0;
ht->ht_callback = 0;
for (i = 0; i < table_size; i++)
return (ht);
}
/*
* ht_destroy_table
*
* Destroy a hash table. All entries in the table are removed, which
* may invoke the callback if it's installed, and the memory is freed.
*/
void
{
if (handle == 0)
return;
/* To remove marked entries */
(void) ht_clean_table(handle);
}
/*
* ht_get_total_items
*
* Return the total number of items in the table. Returns -1 if the
* handle is invalid.
*/
{
if (handle == 0)
return ((size_t)-1);
return (handle->ht_total_items);
}
/*
* ht_default_hash
*
* Default hash function to compute the table index (hash value) based
* on the specified key. This will identify the location for the
* corresponding item in the hash table. The handle and key pointers
* should be validated before this function is called.
*
* Returns the table index location for the item.
*/
static size_t
{
unsigned int hash_ndx = 0;
while (*key) {
++key;
}
} else {
while (key_len--) {
++key;
}
}
return (rval);
}
/*
* ht_set_cmpfn
*
* Replace the current compare function. As the this is function
* for comparing items' key, it should not be called while there are
* items in the table.
*/
void
{
if (handle)
}
/*
* ht_add_item
*
* Adds an item to a hash table. The hash table is identified by the
* handle and the key is used to generate a hashed index. The data
* item can be null; it is never dereferenced. We don't check for
* duplicates. If duplicate keys are added to the table, the last
* item added will be to the front of the duplicate list.
*
* The table sequence number may be modified here.
*
* If the item is successfully inserted, a pointer to the item object
* is returned. Otherwise a null pointer is returned.
*/
HT_ITEM *
{
return (NULL);
} else {
return (NULL);
/* Include the null terminator */
++key_len;
}
return (NULL);
/*
* Add to the front of the list.
*/
handle->ht_total_items++;
handle->ht_sequence++;
return (item);
}
/*
* ht_replace_item
*
* Replace an item in a hash table. The item associated with key is removed
* using ht_remove_item and a new item is added using ht_add_item. We rely
* on parameter validation in ht_remove_item and ht_add_item.
*
* The table sequence number may be modified here.
*/
HT_ITEM *
{
}
/*
* ht_remove_item
*
* Remove an item from a hash table. If there are duplicate keys, then the
* first key found will be deleted. Note that the data pointer is never
* dereferenced. If a callback is installed, it will be invoked and the
* return value will be null. Otherwise, the data pointer supplied by the
* application will be returned. If there is an error, a null pointer will
* be returned.
*
* The table sequence number may be modified here.
*/
void *
{
int key_len;
void *data = 0;
return (NULL);
else
prev = 0;
while (cur) {
/* found key */
if (prev == 0)
else
if (handle->ht_callback)
else
/*
* Since the key and the item were allocated as
* a single chunk, we only need one free here.
*/
handle->ht_total_items--;
handle->ht_sequence++;
break;
}
}
return (data);
}
/*
* ht_find_item
*
* Find an item in a hash table. If there are duplicate keys then the
* first item found (which will be the last one added) will be returned.
*
* Returns a pointer to an item. Otherwise returns a null pointer to
* indicate an error or that the key didn't match anything in the table.
*/
HT_ITEM *
{
int key_len;
return (NULL);
else
while (cur) {
return (cur);
}
return (NULL);
}
/*
* ht_register_callback
*
* Register an application callback function that can be used to process
* an item when it is removed from the table, i.e. free any memory
* allocated for that data item.
*
* The previous callback function pointer, which may be null, before
* registering the new one. This provides the caller with the option to
* restore a previous callback as required.
*/
{
if (handle == 0)
return (NULL);
return (old_callback);
}
/*
* ht_clean_table
*
* This function removes all the items that are marked for deletion. Note
* that this will invoke the callback, if one has been installed. If this
* call is used, the callback mechanism is the only way for an application
* to free the item data if it was dynamically allocated.
*
* The table sequence number may be modified here.
*
* Returns 0 if the handle is valid; otherwise returns -1.
*/
{
size_t i;
if (handle == 0)
return ((size_t)-1);
for (i = 0; i < handle->ht_table_size; i++) {
prev = 0;
while (cur) {
/*
* We have a marked item: remove it.
*/
if (prev == 0)
else
if (handle->ht_callback)
/*
* Since the key and the item were allocated as
* a single chunk, we only need one free here.
*/
handle->ht_sequence++;
if (prev == 0)
else
continue;
}
}
}
return (0);
}
/*
* ht_mark_delete
*
* This function marks an item for deletion, which may be useful when
* table scan. Marked items can be removed later using ht_clean_table.
*/
void
{
handle->ht_total_items--;
}
}
/*
* ht_clear_delete
*
* This function clear an item from marked for deletion list.
*/
void
{
handle->ht_total_items++;
}
}
/*
* ht_bucket_search
*
* Returns first item which is not marked as deleted
* in the specified bucket by 'head'
*/
static HT_ITEM *
{
return (item);
}
/*
* ht_findfirst
*
* This function is used to begin an iteration through the hash table.
* The iterator is initialized and the first item in the table (as
* determined by the hash algorithm) is returned. The current sequence
* number is stored in the iterator to determine whether or not the
* the table has changed between calls. If the table is empty, a null
* pointer is returned.
*/
HT_ITEM *
{
return (NULL);
if (item != 0) {
return (item);
}
}
return (NULL);
}
/*
* ht_findnext
*
* Find the next item in the table for the given iterator. Iterators must
* be initialized by ht_findfirst, which will also return the first item
* in the table. If an item is available, a pointer to it is returned.
* Otherwise a null pointer is returned. A null pointer may indicate:
*
* - an invalid iterator (i.e. ht_findfirst has not been called)
* - the entire table has been traversed
*
* The caller can use ht_get_total_items to determine whether or not all
* of the items in the table have been visited.
*/
HT_ITEM *
{
iterator->hti_sequence == 0) {
/* Invalid iterator */
return (NULL);
}
/*
* No more items or the table has changed
* since the last call.
*/
return (NULL);
}
/*
* Check for another item in the current bucket.
*/
if (item != 0) {
return (item);
}
/*
* Nothing else in the current bucket. Look for another
* bucket with something in it and return the head item.
*/
if (item != 0) {
return (item);
}
}
iterator->hti_sequence = 0;
return (NULL);
}