/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (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
*/
/*
* Copyright 2003 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* This file contains a basic dictionary implementation which stores
* arbitrary key-value mappings. It is used by libpool to store
* information about element pointers (pool_elem_t) in the kernel
* provider implementation.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include "dict.h"
/*
* HASH_64_INIT is the same as the INIT value since it is the value
* used by FNV (FNV1_64_INIT). More details on FNV are available at:
*
*/
/*
* HASH_64_PRIME is a large prime number chosen to minimize hashing
* collisions.
*/
/*
* DICT_SIZE is chosen as it is the nearest prime to 2^9 (512). 512 is
* chosen as it is unlikely that this dictionary will contain more
* elements than this in normal operation. Of course overflow in each
* bucket is acceptable, but if there is too much overflow, then
* performance will degrade to that of a list.
*/
/*
* Data Types
*/
/*
* A key bucket.
*/
typedef struct dict_bucket
{
/*
* A dictionary which holds a mapping between a key and a value.
* dh_change - detects changes to the dictionary.
* dh_cmp - comparison function
* dh_hash - hashing function
* dh_buckets - key storage
* dh_size - # of buckets
*/
struct dict_hdl {
int (*dh_cmp)(const void *, const void *);
};
/*
* Utility functions. Mainly used for debugging
*/
#if defined(DEBUG)
static void bit_print_32(unsigned int);
static void bit_print_64(unsigned long long);
#endif /* DEBUG */
/*
* Default functions for hashing and comparing if the user does not specify
* these values when creating the dictionary.
*/
static int cmp_addr(const void *, const void *);
/*
* static functions
*/
#if defined(DEBUG)
/*
* Print to standard out the bit representation of the supplied value
*/
void
bit_print_32(unsigned int v)
{
for (i = 1; i <= 32; i++) {
v <<= 1;
if (i % 8 == 0 && i != 32)
(void) putchar(' ');
}
(void) putchar('\n');
}
/*
* Print to standard out the bit representation of the supplied value
*/
void
bit_print_64(unsigned long long v)
{
int i;
for (i = 1; i <= 64; i++) {
v <<= 1;
if (i % 8 == 0 && i != 64)
(void) putchar(' ');
}
(void) putchar('\n');
}
#endif /* DEBUG */
/*
* Default comparison function which is used if no comparison function
* is supplied when the dictionary is created. The default behaviour
* is to compare memory address.
*/
int
cmp_addr(const void *x, const void *y)
{
return (x != y);
}
/*
* The default hashing function which is used if no hashing function
* is provided when the dictionary is created. The default behaviour
* is to use the hash_buf() function.
*/
{
}
/*
* public interface
*/
/*
* Return a hash which is built by manipulating each byte in the
* supplied data. The hash logic follows the approach suggested in the
* FNV hash.
*/
{
hash *= HASH_64_PRIME;
}
return (hash);
}
/*
* Return a hash which is built by manipulating each byte in the
* supplied string. The hash logic follows the approach suggested in
* the FNV hash.
*/
{
while (*p) {
hash *= HASH_64_PRIME;
}
return (hash);
}
/*
* Return the number of keys held in the supplied dictionary.
*/
{
}
/*
* Free the supplied dictionary and all it's associated resource.
*/
void
{
uint64_t i;
}
}
}
}
/*
* Create a new dictionary using the supplied comparison and hashing
* functions. If none are supplied then the defaults are used.
*/
{
return (NULL);
== NULL) {
return (NULL);
}
return (hdl);
}
/*
* Get a value from the hash. Null is returned if the key cannot be
* found.
*/
void *
{
uint64_t i;
break;
}
/*
* Put an entry into the hash. Null is returned if this key was not
* already present, otherwise the previous value is returned.
*/
void *
{
uint64_t i;
break;
if (bucket) {
} else {
}
return (prev);
}
/*
* the key is found. NULL is returned if the key cannot be located.
*/
void *
{
uint64_t i;
return (value);
}
}
return (NULL);
}
/*
* For all entries in the dictionary call the user supplied function
* (apply) with the key, value and user supplied data. If the
* dictionary is modifed while this function is executing, then the
* function will fail with an assertion about table modifcation.
*/
void
void *cl)
{
uint64_t i;
assert(!"table modified illegally");
}
}
}