Blame modules/pam_console/hashtable.h

Packit 7e982e
/* Copyright (C) 2002 Christopher Clark <firstname.lastname@cl.cam.ac.uk> */
Packit 7e982e
Packit 7e982e
#ifndef __HASHTABLE_CWC22_H__
Packit 7e982e
#define __HASHTABLE_CWC22_H__
Packit 7e982e
Packit 7e982e
struct hashtable;
Packit 7e982e
Packit 7e982e
/* Example of use:
Packit 7e982e
 *
Packit 7e982e
 *      struct hashtable  *h;
Packit 7e982e
 *      struct some_key   *k;
Packit 7e982e
 *      struct some_value *v;
Packit 7e982e
 *
Packit 7e982e
 *      static unsigned int         hash_from_key_fn( void *k );
Packit 7e982e
 *      static int                  keys_equal_fn ( void *key1, void *key2 );
Packit 7e982e
 *
Packit 7e982e
 *      h = create_hashtable(16, hash_from_key_fn, keys_equal_fn);
Packit 7e982e
 *      k = (struct some_key *)     malloc(sizeof(struct some_key));
Packit 7e982e
 *      v = (struct some_value *)   malloc(sizeof(struct some_value));
Packit 7e982e
 *
Packit 7e982e
 *      (initialise k and v to suitable values)
Packit 7e982e
 * 
Packit 7e982e
 *      if (! hashtable_insert(h,k,v) )
Packit 7e982e
 *      {     exit(-1);               }
Packit 7e982e
 *
Packit 7e982e
 *      if (NULL == (found = hashtable_search(h,k) ))
Packit 7e982e
 *      {    printf("not found!");                  }
Packit 7e982e
 *
Packit 7e982e
 *      if (NULL == (found = hashtable_remove(h,k) ))
Packit 7e982e
 *      {    printf("Not found\n");                 }
Packit 7e982e
 *
Packit 7e982e
 */
Packit 7e982e
Packit 7e982e
/* Macros may be used to define type-safe(r) hashtable access functions, with
Packit 7e982e
 * methods specialized to take known key and value types as parameters.
Packit 7e982e
 * 
Packit 7e982e
 * Example:
Packit 7e982e
 *
Packit 7e982e
 * Insert this at the start of your file:
Packit 7e982e
 *
Packit 7e982e
 * DEFINE_HASHTABLE_INSERT(insert_some, struct some_key, struct some_value);
Packit 7e982e
 * DEFINE_HASHTABLE_SEARCH(search_some, struct some_key, struct some_value);
Packit 7e982e
 * DEFINE_HASHTABLE_REMOVE(remove_some, struct some_key, struct some_value);
Packit 7e982e
 *
Packit 7e982e
 * This defines the functions 'insert_some', 'search_some' and 'remove_some'.
Packit 7e982e
 * These operate just like hashtable_insert etc., with the same parameters,
Packit 7e982e
 * but their function signatures have 'struct some_key *' rather than
Packit 7e982e
 * 'void *', and hence can generate compile time errors if your program is
Packit 7e982e
 * supplying incorrect data as a key (and similarly for value).
Packit 7e982e
 *
Packit 7e982e
 * Note that the hash and key equality functions passed to create_hashtable
Packit 7e982e
 * still take 'void *' parameters instead of 'some key *'. This shouldn't be
Packit 7e982e
 * a difficult issue as they're only defined and passed once, and the other
Packit 7e982e
 * functions will ensure that only valid keys are supplied to them.
Packit 7e982e
 *
Packit 7e982e
 * The cost for this checking is increased code size and runtime overhead
Packit 7e982e
 * - if performance is important, it may be worth switching back to the
Packit 7e982e
 * unsafe methods once your program has been debugged with the safe methods.
Packit 7e982e
 * This just requires switching to some simple alternative defines - eg:
Packit 7e982e
 * #define insert_some hashtable_insert
Packit 7e982e
 *
Packit 7e982e
 */
Packit 7e982e
Packit 7e982e
/*****************************************************************************
Packit 7e982e
 * create_hashtable
Packit 7e982e
   
Packit 7e982e
 * @name                    create_hashtable
Packit 7e982e
 * @param   minsize         minimum initial size of hashtable
Packit 7e982e
 * @param   hashfunction    function for hashing keys
Packit 7e982e
 * @param   key_eq_fn       function for determining key equality
Packit 7e982e
 * @return                  newly created hashtable or NULL on failure
Packit 7e982e
 */
Packit 7e982e
Packit 7e982e
struct hashtable *
Packit 7e982e
create_hashtable(unsigned int minsize,
Packit 7e982e
                 unsigned int (*hashfunction) (void*),
Packit 7e982e
                 int (*key_eq_fn) (void*,void*));
Packit 7e982e
Packit 7e982e
/*****************************************************************************
Packit 7e982e
 * hashtable_insert
Packit 7e982e
   
Packit 7e982e
 * @name        hashtable_insert
Packit 7e982e
 * @param   h   the hashtable to insert into
Packit 7e982e
 * @param   k   the key - hashtable claims ownership and will free on removal
Packit 7e982e
 * @param   v   the value - does not claim ownership
Packit 7e982e
 * @return      non-zero for successful insertion
Packit 7e982e
 *
Packit 7e982e
 * This function will cause the table to expand if the insertion would take
Packit 7e982e
 * the ratio of entries to table size over the maximum load factor.
Packit 7e982e
 *
Packit 7e982e
 * This function does not check for repeated insertions with a duplicate key.
Packit 7e982e
 * The value returned when using a duplicate key is undefined -- when
Packit 7e982e
 * the hashtable changes size, the order of retrieval of duplicate key
Packit 7e982e
 * entries is reversed.
Packit 7e982e
 * If in doubt, remove before insert.
Packit 7e982e
 */
Packit 7e982e
Packit 7e982e
int 
Packit 7e982e
hashtable_insert(struct hashtable *h, void *k, void *v);
Packit 7e982e
Packit 7e982e
#define DEFINE_HASHTABLE_INSERT(fnname, keytype, valuetype) \
Packit 7e982e
int fnname (struct hashtable *h, keytype *k, valuetype *v) \
Packit 7e982e
{ \
Packit 7e982e
    return hashtable_insert(h,k,v); \
Packit 7e982e
}
Packit 7e982e
Packit 7e982e
/*****************************************************************************
Packit 7e982e
 * hashtable_search
Packit 7e982e
   
Packit 7e982e
 * @name        hashtable_search
Packit 7e982e
 * @param   h   the hashtable to search
Packit 7e982e
 * @param   k   the key to search for  - does not claim ownership
Packit 7e982e
 * @return      the value associated with the key, or NULL if none found
Packit 7e982e
 */
Packit 7e982e
Packit 7e982e
void *
Packit 7e982e
hashtable_search(struct hashtable *h, void *k);
Packit 7e982e
Packit 7e982e
#define DEFINE_HASHTABLE_SEARCH(fnname, keytype, valuetype) \
Packit 7e982e
valuetype * fnname (struct hashtable *h, keytype *k) \
Packit 7e982e
{ \
Packit 7e982e
    return (valuetype *) (hashtable_search(h,k)); \
Packit 7e982e
}
Packit 7e982e
Packit 7e982e
/*****************************************************************************
Packit 7e982e
 * hashtable_remove
Packit 7e982e
   
Packit 7e982e
 * @name        hashtable_remove
Packit 7e982e
 * @param   h   the hashtable to remove the item from
Packit 7e982e
 * @param   k   the key to search for  - does not claim ownership
Packit 7e982e
 * @return      the value associated with the key, or NULL if none found
Packit 7e982e
 */
Packit 7e982e
Packit 7e982e
void * /* returns value */
Packit 7e982e
hashtable_remove(struct hashtable *h, void *k, int free_key);
Packit 7e982e
Packit 7e982e
#define DEFINE_HASHTABLE_REMOVE(fnname, keytype, valuetype) \
Packit 7e982e
valuetype * fnname (struct hashtable *h, keytype *k) \
Packit 7e982e
{ \
Packit 7e982e
    return (valuetype *) (hashtable_remove(h,k)); \
Packit 7e982e
}
Packit 7e982e
Packit 7e982e
Packit 7e982e
/*****************************************************************************
Packit 7e982e
 * hashtable_count
Packit 7e982e
   
Packit 7e982e
 * @name        hashtable_count
Packit 7e982e
 * @param   h   the hashtable
Packit 7e982e
 * @return      the number of items stored in the hashtable
Packit 7e982e
 */
Packit 7e982e
unsigned int
Packit 7e982e
hashtable_count(struct hashtable *h);
Packit 7e982e
Packit 7e982e
Packit 7e982e
/*****************************************************************************
Packit 7e982e
 * hashtable_destroy
Packit 7e982e
   
Packit 7e982e
 * @name        hashtable_destroy
Packit 7e982e
 * @param   h   the hashtable
Packit 7e982e
 * @param       free_values     whether to call 'free' on the remaining values
Packit 7e982e
 */
Packit 7e982e
Packit 7e982e
void
Packit 7e982e
hashtable_destroy(struct hashtable *h, int free_values);
Packit 7e982e
Packit 7e982e
#endif /* __HASHTABLE_CWC22_H__ */
Packit 7e982e
Packit 7e982e
/*
Packit 7e982e
 * Copyright (c) 2002, Christopher Clark
Packit 7e982e
 * All rights reserved.
Packit 7e982e
 * 
Packit 7e982e
 * Redistribution and use in source and binary forms, with or without
Packit 7e982e
 * modification, are permitted provided that the following conditions
Packit 7e982e
 * are met:
Packit 7e982e
 * 
Packit 7e982e
 * * Redistributions of source code must retain the above copyright
Packit 7e982e
 * notice, this list of conditions and the following disclaimer.
Packit 7e982e
 * 
Packit 7e982e
 * * Redistributions in binary form must reproduce the above copyright
Packit 7e982e
 * notice, this list of conditions and the following disclaimer in the
Packit 7e982e
 * documentation and/or other materials provided with the distribution.
Packit 7e982e
 * 
Packit 7e982e
 * * Neither the name of the original author; nor the names of any contributors
Packit 7e982e
 * may be used to endorse or promote products derived from this software
Packit 7e982e
 * without specific prior written permission.
Packit 7e982e
 * 
Packit 7e982e
 * 
Packit 7e982e
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit 7e982e
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit 7e982e
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit 7e982e
 * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER
Packit 7e982e
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
Packit 7e982e
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
Packit 7e982e
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
Packit 7e982e
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
Packit 7e982e
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
Packit 7e982e
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Packit 7e982e
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 7e982e
*/