fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * CDDL HEADER START
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The contents of this file are subject to the terms of the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Common Development and Distribution License (the "License").
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * You may not use this file except in compliance with the License.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * or http://www.opensolaris.org/os/licensing.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * See the License for the specific language governing permissions
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * and limitations under the License.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * When distributing Covered Code, include this CDDL HEADER in each
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * If applicable, add the following below this CDDL HEADER, with the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * fields enclosed by brackets "[]" replaced with your own identifying
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * information: Portions Copyright [yyyy] [name of copyright owner]
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * CDDL HEADER END
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Use is subject to license terms.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <string.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <stdlib.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <sys/nsctl/nsc_hash.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define HASH_PRIME 2039
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int calc_hash(const char *);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortehash_node_t **
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortensc_create_hash()
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte hash_node_t **hash;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte hash = (hash_node_t **)calloc(HASH_PRIME, sizeof (hash_node_t *));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (hash);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortensc_insert_node(hash_node_t **hash, void *data, const char *key)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int index;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte hash_node_t *node;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte node = (hash_node_t *)malloc(sizeof (hash_node_t));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!node) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (-1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte node->key = strdup(key);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte node->data = data;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * possible enhancement would be to search
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * in this index for a duplicate
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte index = calc_hash(key);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte node->next = hash[ index ];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte hash[ index ] = node;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * lookup
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Description:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Searches the hash to find a node.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Return values:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * 0 if not found.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * pointer to node if found.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortevoid *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortensc_lookup(hash_node_t **hash, const char *key)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int index;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte hash_node_t *node;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte index = calc_hash(key);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte node = hash[ index ];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while (node) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (strcmp(node->key, key) == 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (node->data);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte node = node->next;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortevoid *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortensc_remove_node(hash_node_t **hash, char *key)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int index;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte hash_node_t *node, *prev;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte void *retval;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte index = calc_hash(key);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!hash[ index ]) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (strcmp(hash[ index ]->key, key) == 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte node = hash[ index ];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte retval = node->data;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte hash[ index ] = hash[ index ]->next;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(node->key);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(node);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (retval);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte prev = hash[ index ];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte node = prev->next;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while (node && (strcmp(node->key, key) != 0)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte prev = node;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte node = node->next;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* did we find it? */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (node) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte prev->next = node->next;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte retval = node->data;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(node->key);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(node);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (retval);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortevoid
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortensc_remove_all(hash_node_t **hash, void (*callback)(void *))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int i;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte hash_node_t *p, *next;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 0; i < HASH_PRIME; i++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte p = hash[ i ];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while (p) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte next = p->next;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (callback) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte callback(p->data);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(p->key);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(p);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte p = next;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(hash);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/* ---------------------------------------------------------------------- */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Basic rotating hash, as per Knuth.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortecalc_hash(const char *key)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte unsigned int hash, i;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int len = strlen(key);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (hash = len, i = 0; i < len; i++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte hash = (hash << 5) ^ (hash >> 27) ^ key[ i ];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (hash % HASH_PRIME);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}