Main Page | Modules | Class List | Directories | File List | Class Members | File Members | Related Pages

sfxhash.h

Go to the documentation of this file.
00001 /*
00002 *
00003 *  sfxhash.h
00004 *
00005 *  generic hash table - stores and maps key + data pairs
00006 *  (supports memcap and automatic memory recovery when out of memory)
00007 *
00008 *  Copyright (C) 2001 Marc A Norton
00009 *  Copyright (C) 2003 Sourcefire,Inc.
00010 *
00011 */
00012 
00013 #ifndef _SFXHASH_
00014 #define _SFXHASH_
00015 
00016 #include <stdlib.h>
00017 #include <string.h>
00018 #include <time.h>
00019 
00020 #include "sfmemcap.h"
00021 #include "sfhashfcn.h"
00022 /*
00023 *   ERROR DEFINES
00024 */
00025 #define SFXHASH_NOMEM    -2
00026 #define SFXHASH_ERR      -1
00027 #define SFXHASH_OK        0
00028 #define SFXHASH_INTABLE   1
00029 
00030 /*
00031 *   HASH NODE
00032 */
00033 typedef struct _sfxhash_node
00034 {
00035   struct _sfxhash_node * gnext, * gprev; /* global node list - used for ageing nodes */
00036   struct _sfxhash_node * next,  * prev;  /* row node list */
00037 
00038   int    rindex; /* row index of table this node belongs to. */
00039 
00040   void * key;   /* Pointer to the key. */
00041   void * data;  /* Pointer to the users data, this is not copied ! */
00042      
00043 } SFXHASH_NODE;
00044 
00045 /*
00046 *    SFGX HASH Table
00047 */
00048 typedef struct _sfxhash
00049 {
00050   SFHASHFCN     * sfhashfcn; /* hash function */
00051   int             keysize; /* bytes in key, if <= 0 -> keys are strings */
00052   int             datasize;/* bytes in key, if == 0 -> user data */
00053   SFXHASH_NODE ** table;   /* array of node ptr's */
00054   unsigned        nrows;   /* # rows int the hash table use a prime number 211, 9871 */
00055   unsigned        count;   /* total # nodes in table */
00056   
00057   unsigned        crow;    // findfirst/next row in table
00058   SFXHASH_NODE  * cnode;   // findfirst/next node ptr
00059   int             splay;
00060 
00061   MEMCAP          mc;
00062   unsigned        overhead_bytes;  /** # of bytes that will be unavailable for nodes inside the table */    
00063   unsigned        overhead_blocks; /** # of blocks consumed by the table */
00064   unsigned        find_fail;
00065   unsigned        find_success;
00066     
00067   SFXHASH_NODE  * ghead, * gtail;  /* global - root of all nodes allocated in table */
00068 
00069   SFXHASH_NODE  * fhead, * ftail;  /* free list of nodes */
00070   int             recycle_nodes;
00071   unsigned        anr_tries; /* ANR requests to the userfunc */
00072   unsigned        anr_count; /* # ANR ops performaed */
00073   int             anr_flag;  /* 0=off, !0=on */
00074 
00075   int (*anrfree)( void * key, void * data );
00076   int (*usrfree)( void * key, void * data );
00077 } SFXHASH;
00078 
00079 
00080 /*
00081 *   HASH PROTOTYPES
00082 */
00083 SFXHASH       * sfxhash_new( int nrows, int keysize, int datasize, int memcap, 
00084                              int anr_flag, 
00085                              int (*anrfunc)(void *key, void * data),
00086                              int (*usrfunc)(void *key, void * data),
00087                              int recycle_flag );
00088 
00089 void            sfxhash_delete( SFXHASH * h );
00090 
00091 int             sfxhash_add ( SFXHASH * h, void * key, void * data );
00092 SFXHASH_NODE * sfxhash_get_node( SFXHASH * t, void * key );
00093 int             sfxhash_remove( SFXHASH * h, void * key );
00094 unsigned        sfxhash_count( SFXHASH * h );
00095 unsigned        sfxhash_anr_count( SFXHASH * h );
00096 
00097 void          * sfxhash_mru( SFXHASH * t );
00098 void          * sfxhash_lru( SFXHASH * t );
00099 SFXHASH_NODE  * sfxhash_mru_node( SFXHASH * t );
00100 SFXHASH_NODE  * sfxhash_lru_node( SFXHASH * t );
00101 void          * sfxhash_find( SFXHASH * h, void * key );
00102 SFXHASH_NODE  * sfxhash_find_node( SFXHASH * t, void * key);
00103 
00104 SFXHASH_NODE  * sfxhash_findfirst( SFXHASH * h );
00105 SFXHASH_NODE  * sfxhash_findnext ( SFXHASH * h );
00106 
00107 SFXHASH_NODE  * sfxhash_ghead( SFXHASH * h );
00108 SFXHASH_NODE  * sfxhash_gnext( SFXHASH_NODE * n );
00109 void sfxhash_gmovetofront( SFXHASH *t, SFXHASH_NODE * hnode );
00110 
00111 
00112 void            sfxhash_splaymode( SFXHASH * h, int mode );
00113 
00114 void          * sfxhash_alloc( SFXHASH * t, unsigned nbytes );
00115 void            sfxhash_free( SFXHASH * t, void * p );
00116 int             sfxhash_free_node(SFXHASH *t, SFXHASH_NODE *node);
00117 
00118 unsigned        sfxhash_maxdepth( SFXHASH * t );
00119 unsigned        sfxhash_overhead_bytes( SFXHASH * t );
00120 unsigned        sfxhash_overhead_blocks( SFXHASH * t );
00121 unsigned        sfxhash_find_success( SFXHASH * h );
00122 unsigned        sfxhash_find_fail( SFXHASH * h );
00123 unsigned        sfxhash_find_total( SFXHASH * h );
00124 
00125 
00126 int sfxhash_set_keyops( SFXHASH *h ,
00127                         unsigned (*hash_fcn)( SFHASHFCN * p,
00128                                               unsigned char *d,
00129                                               int n),
00130                         int (*keycmp_fcn)( const void *s1,
00131                                            const void *s2,
00132                                            size_t n));
00133 
00134 
00135 #endif
00136 

Generated on Sun May 14 14:51:18 2006 by  doxygen 1.4.2