Blame modules/pam_console/hashtable.h

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