]> jfr.im git - solanum.git/blobdiff - ircd/hash.c
Change struct Dictionary(*) to rb_dictionary(_\1).
[solanum.git] / ircd / hash.c
index 0929ed4d33f114e79fe70813228bcd20a3a12fa9..fa491375a58eef5d33531cd815eb4d4fcef6b7ba 100644 (file)
@@ -20,8 +20,6 @@
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
  *  USA
- *
- *  $Id: hash.c 3177 2007-02-01 00:19:14Z jilles $
  */
 
 #include "stdinc.h"
 #include "cache.h"
 #include "s_newconf.h"
 #include "s_assert.h"
-#include "irc_dictionary.h"
-#include "irc_radixtree.h"
-
-rb_dlink_list *channelTable;
-rb_dlink_list *hostTable;
+#include "rb_dictionary.h"
+#include "rb_radixtree.h"
 
-struct Dictionary *client_connid_tree = NULL;
-struct Dictionary *client_zconnid_tree = NULL;
-struct irc_radixtree *client_id_tree = NULL;
-struct irc_radixtree *client_name_tree = NULL;
+rb_dictionary *client_connid_tree = NULL;
+rb_dictionary *client_zconnid_tree = NULL;
+struct rb_radixtree *client_id_tree = NULL;
+struct rb_radixtree *client_name_tree = NULL;
 
-struct irc_radixtree *resv_tree = NULL;
+struct rb_radixtree *channel_tree = NULL;
+struct rb_radixtree *resv_tree = NULL;
+struct rb_radixtree *hostname_tree = NULL;
 
 /*
  * look in whowas.c for the missing ...[WW_MAX]; entry
@@ -63,15 +60,15 @@ struct irc_radixtree *resv_tree = NULL;
 void
 init_hash(void)
 {
-       channelTable = rb_malloc(sizeof(rb_dlink_list) * CH_MAX);
-       hostTable = rb_malloc(sizeof(rb_dlink_list) * HOST_MAX);
+       client_connid_tree = rb_dictionary_create("client connid", rb_uint32cmp);
+       client_zconnid_tree = rb_dictionary_create("client zconnid", rb_uint32cmp);
+       client_id_tree = rb_radixtree_create("client id", NULL);
+       client_name_tree = rb_radixtree_create("client name", irccasecanon);
 
-       client_connid_tree = irc_dictionary_create("client connid", irc_uint32cmp);
-       client_zconnid_tree = irc_dictionary_create("client zconnid", irc_uint32cmp);
-       client_id_tree = irc_radixtree_create("client id", NULL);
-       client_name_tree = irc_radixtree_create("client name", irc_radixtree_irccasecanon);
+       channel_tree = rb_radixtree_create("channel", irccasecanon);
+       resv_tree = rb_radixtree_create("resv", irccasecanon);
 
-       resv_tree = irc_radixtree_create("resv", irc_radixtree_irccasecanon);
+       hostname_tree = rb_radixtree_create("hostname", irccasecanon);
 }
 
 u_int32_t
@@ -81,7 +78,7 @@ fnv_hash_upper(const unsigned char *s, int bits)
 
        while (*s)
        {
-               h ^= ToUpper(*s++);
+               h ^= irctoupper(*s++);
                h += (h<<1) + (h<<4) + (h<<7) + (h << 8) + (h << 24);
        }
        if (bits < 32)
@@ -126,7 +123,7 @@ fnv_hash_upper_len(const unsigned char *s, int bits, int len)
        const unsigned char *x = s + len;
        while (*s && s < x)
        {
-               h ^= ToUpper(*s++);
+               h ^= irctoupper(*s++);
                h += (h<<1) + (h<<4) + (h<<7) + (h << 8) + (h << 24);
        }
        if (bits < 32)
@@ -134,27 +131,6 @@ fnv_hash_upper_len(const unsigned char *s, int bits, int len)
        return h;
 }
 
-/* hash_channel()
- *
- * hashes a channel name, based on first 30 chars only for efficiency
- */
-static u_int32_t
-hash_channel(const char *name)
-{
-       return fnv_hash_upper_len((const unsigned char *) name, CH_MAX_BITS, 30);
-}
-
-/* hash_hostname()
- *
- * hashes a hostname, based on first 30 chars only, as thats likely to
- * be more dynamic than rest.
- */
-static u_int32_t
-hash_hostname(const char *name)
-{
-       return fnv_hash_upper_len((const unsigned char *) name, HOST_MAX_BITS, 30);
-}
-
 /* add_to_id_hash()
  *
  * adds an entry to the id hash table
@@ -165,7 +141,7 @@ add_to_id_hash(const char *name, struct Client *client_p)
        if(EmptyString(name) || (client_p == NULL))
                return;
 
-       irc_radixtree_add(client_id_tree, name, client_p);
+       rb_radixtree_add(client_id_tree, name, client_p);
 }
 
 /* add_to_client_hash()
@@ -180,7 +156,7 @@ add_to_client_hash(const char *name, struct Client *client_p)
        if(EmptyString(name) || (client_p == NULL))
                return;
 
-       irc_radixtree_add(client_name_tree, name, client_p);
+       rb_radixtree_add(client_name_tree, name, client_p);
 }
 
 /* add_to_hostname_hash()
@@ -190,15 +166,23 @@ add_to_client_hash(const char *name, struct Client *client_p)
 void
 add_to_hostname_hash(const char *hostname, struct Client *client_p)
 {
-       unsigned int hashv;
+       rb_dlink_list *list;
 
        s_assert(hostname != NULL);
        s_assert(client_p != NULL);
        if(EmptyString(hostname) || (client_p == NULL))
                return;
 
-       hashv = hash_hostname(hostname);
-       rb_dlinkAddAlloc(client_p, &hostTable[hashv]);
+       list = rb_radixtree_retrieve(hostname_tree, hostname);
+       if (list != NULL)
+       {
+               rb_dlinkAddAlloc(client_p, list);
+               return;
+       }
+
+       list = rb_malloc(sizeof(*list));
+       rb_radixtree_add(hostname_tree, hostname, list);
+       rb_dlinkAddAlloc(client_p, list);
 }
 
 /* add_to_resv_hash()
@@ -213,7 +197,7 @@ add_to_resv_hash(const char *name, struct ConfItem *aconf)
        if(EmptyString(name) || aconf == NULL)
                return;
 
-       irc_radixtree_add(resv_tree, name, aconf);
+       rb_radixtree_add(resv_tree, name, aconf);
 }
 
 /* del_from_id_hash()
@@ -228,7 +212,7 @@ del_from_id_hash(const char *id, struct Client *client_p)
        if(EmptyString(id) || client_p == NULL)
                return;
 
-       irc_radixtree_delete(client_id_tree, id);
+       rb_radixtree_delete(client_id_tree, id);
 }
 
 /* del_from_client_hash()
@@ -244,7 +228,7 @@ del_from_client_hash(const char *name, struct Client *client_p)
        if(EmptyString(name) || client_p == NULL)
                return;
 
-       irc_radixtree_delete(client_name_tree, name);
+       rb_radixtree_delete(client_name_tree, name);
 }
 
 /* del_from_channel_hash()
@@ -254,16 +238,13 @@ del_from_client_hash(const char *name, struct Client *client_p)
 void
 del_from_channel_hash(const char *name, struct Channel *chptr)
 {
-       unsigned int hashv;
-
        s_assert(name != NULL);
        s_assert(chptr != NULL);
 
        if(EmptyString(name) || chptr == NULL)
                return;
 
-       hashv = hash_channel(name);
-       rb_dlinkFindDestroy(chptr, &channelTable[hashv]);
+       rb_radixtree_delete(channel_tree, name);
 }
 
 /* del_from_hostname_hash()
@@ -273,14 +254,22 @@ del_from_channel_hash(const char *name, struct Channel *chptr)
 void
 del_from_hostname_hash(const char *hostname, struct Client *client_p)
 {
-       unsigned int hashv;
+       rb_dlink_list *list;
 
        if(hostname == NULL || client_p == NULL)
                return;
 
-       hashv = hash_hostname(hostname);
+       list = rb_radixtree_retrieve(hostname_tree, hostname);
+       if (list == NULL)
+               return;
+
+       rb_dlinkFindDestroy(client_p, list);
 
-       rb_dlinkFindDestroy(client_p, &hostTable[hashv]);
+       if (rb_dlink_list_length(list) == 0)
+       {
+               rb_radixtree_delete(hostname_tree, hostname);
+               rb_free(list);
+       }
 }
 
 /* del_from_resv_hash()
@@ -295,7 +284,7 @@ del_from_resv_hash(const char *name, struct ConfItem *aconf)
        if(EmptyString(name) || aconf == NULL)
                return;
 
-       irc_radixtree_delete(resv_tree, name);
+       rb_radixtree_delete(resv_tree, name);
 }
 
 /* find_id()
@@ -308,7 +297,7 @@ find_id(const char *name)
        if(EmptyString(name))
                return NULL;
 
-       return irc_radixtree_retrieve(client_id_tree, name);
+       return rb_radixtree_retrieve(client_id_tree, name);
 }
 
 /* find_client()
@@ -326,7 +315,7 @@ find_client(const char *name)
        if(IsDigit(*name))
                return (find_id(name));
 
-       return irc_radixtree_retrieve(client_name_tree, name);
+       return rb_radixtree_retrieve(client_name_tree, name);
 }
 
 /* find_named_client()
@@ -340,7 +329,7 @@ find_named_client(const char *name)
        if(EmptyString(name))
                return NULL;
 
-       return irc_radixtree_retrieve(client_name_tree, name);
+       return rb_radixtree_retrieve(client_name_tree, name);
 }
 
 /* find_server()
@@ -362,7 +351,7 @@ find_server(struct Client *source_p, const char *name)
                return(target_p);
        }
 
-       target_p = irc_radixtree_retrieve(client_name_tree, name);
+       target_p = rb_radixtree_retrieve(client_name_tree, name);
        if (target_p != NULL)
        {
                if(IsServer(target_p) || IsMe(target_p))
@@ -381,14 +370,16 @@ find_server(struct Client *source_p, const char *name)
 rb_dlink_node *
 find_hostname(const char *hostname)
 {
-       unsigned int hashv;
+       rb_dlink_list *hlist;
 
        if(EmptyString(hostname))
                return NULL;
 
-       hashv = hash_hostname(hostname);
+       hlist = rb_radixtree_retrieve(hostname_tree, hostname);
+       if (hlist == NULL)
+               return NULL;
 
-       return hostTable[hashv].head;
+       return hlist->head;
 }
 
 /* find_channel()
@@ -398,25 +389,11 @@ find_hostname(const char *hostname)
 struct Channel *
 find_channel(const char *name)
 {
-       struct Channel *chptr;
-       rb_dlink_node *ptr;
-       unsigned int hashv;
-
        s_assert(name != NULL);
        if(EmptyString(name))
                return NULL;
 
-       hashv = hash_channel(name);
-
-       RB_DLINK_FOREACH(ptr, channelTable[hashv].head)
-       {
-               chptr = ptr->data;
-
-               if(irccmp(name, chptr->chname) == 0)
-                       return chptr;
-       }
-
-       return NULL;
+       return rb_radixtree_retrieve(channel_tree, name);
 }
 
 /*
@@ -434,8 +411,6 @@ struct Channel *
 get_or_create_channel(struct Client *client_p, const char *chname, int *isnew)
 {
        struct Channel *chptr;
-       rb_dlink_node *ptr;
-       unsigned int hashv;
        int len;
        const char *s = chname;
 
@@ -458,30 +433,22 @@ get_or_create_channel(struct Client *client_p, const char *chname, int *isnew)
                s = t;
        }
 
-       hashv = hash_channel(s);
-
-       RB_DLINK_FOREACH(ptr, channelTable[hashv].head)
+       chptr = rb_radixtree_retrieve(channel_tree, s);
+       if (chptr != NULL)
        {
-               chptr = ptr->data;
-
-               if(irccmp(s, chptr->chname) == 0)
-               {
-                       if(isnew != NULL)
-                               *isnew = 0;
-                       return chptr;
-               }
+               if (isnew != NULL)
+                       *isnew = 0;
+               return chptr;
        }
 
        if(isnew != NULL)
                *isnew = 1;
 
        chptr = allocate_channel(s);
-
-       rb_dlinkAdd(chptr, &chptr->node, &global_channel_list);
-
        chptr->channelts = rb_current_time();   /* doesn't hurt to set it here */
 
-       rb_dlinkAddAlloc(chptr, &channelTable[hashv]);
+       rb_dlinkAdd(chptr, &chptr->node, &global_channel_list);
+       rb_radixtree_add(channel_tree, chptr->chname, chptr);
 
        return chptr;
 }
@@ -499,7 +466,7 @@ hash_find_resv(const char *name)
        if(EmptyString(name))
                return NULL;
 
-       aconf = irc_radixtree_retrieve(resv_tree, name);
+       aconf = rb_radixtree_retrieve(resv_tree, name);
        if (aconf != NULL)
        {
                aconf->port++;
@@ -513,15 +480,15 @@ void
 clear_resv_hash(void)
 {
        struct ConfItem *aconf;
-       struct irc_radixtree_iteration_state iter;
+       struct rb_radixtree_iteration_state iter;
 
-       IRC_RADIXTREE_FOREACH(aconf, &iter, resv_tree)
+       RB_RADIXTREE_FOREACH(aconf, &iter, resv_tree)
        {
                /* skip temp resvs */
                if(aconf->hold)
                        continue;
 
-               irc_radixtree_delete(resv_tree, aconf->host);
+               rb_radixtree_delete(resv_tree, aconf->host);
                free_conf(aconf);
        }
 }
@@ -529,25 +496,25 @@ clear_resv_hash(void)
 void
 add_to_zconnid_hash(struct Client *client_p)
 {
-       irc_dictionary_add(client_zconnid_tree, IRC_UINT_TO_POINTER(client_p->localClient->zconnid), client_p);
+       rb_dictionary_add(client_zconnid_tree, RB_UINT_TO_POINTER(client_p->localClient->zconnid), client_p);
 }
 
 void
 del_from_zconnid_hash(struct Client *client_p)
 {
-       irc_dictionary_delete(client_zconnid_tree, IRC_UINT_TO_POINTER(client_p->localClient->zconnid));
+       rb_dictionary_delete(client_zconnid_tree, RB_UINT_TO_POINTER(client_p->localClient->zconnid));
 }
 
 void
 add_to_cli_connid_hash(struct Client *client_p)
 {
-       irc_dictionary_add(client_connid_tree, IRC_UINT_TO_POINTER(client_p->localClient->connid), client_p);
+       rb_dictionary_add(client_connid_tree, RB_UINT_TO_POINTER(client_p->localClient->connid), client_p);
 }
 
 void
 del_from_cli_connid_hash(struct Client *client_p)
 {
-       irc_dictionary_delete(client_connid_tree, IRC_UINT_TO_POINTER(client_p->localClient->connid));
+       rb_dictionary_delete(client_connid_tree, RB_UINT_TO_POINTER(client_p->localClient->connid));
 }
 
 struct Client *
@@ -555,78 +522,13 @@ find_cli_connid_hash(uint32_t connid)
 {
        struct Client *target_p;
 
-       target_p = irc_dictionary_retrieve(client_connid_tree, IRC_UINT_TO_POINTER(connid));
+       target_p = rb_dictionary_retrieve(client_connid_tree, RB_UINT_TO_POINTER(connid));
        if (target_p != NULL)
                return target_p;
 
-       target_p = irc_dictionary_retrieve(client_zconnid_tree, IRC_UINT_TO_POINTER(connid));
+       target_p = rb_dictionary_retrieve(client_zconnid_tree, RB_UINT_TO_POINTER(connid));
        if (target_p != NULL)
                return target_p;
 
        return NULL;
 }
-
-static void
-output_hash(struct Client *source_p, const char *name, int length, int *counts, unsigned long deepest)
-{
-       unsigned long total = 0;
-       int i;
-       char buf[128];
-
-       sendto_one_numeric(source_p, RPL_STATSDEBUG,
-                       "B :%s Hash Statistics", name);
-
-       snprintf(buf, sizeof buf, "%.3f%%",
-                       (float) ((counts[0]*100) / (float) length));
-       sendto_one_numeric(source_p, RPL_STATSDEBUG,
-                       "B :Size: %d Empty: %d (%s)",
-                       length, counts[0], buf);
-
-       for(i = 1; i < 11; i++)
-       {
-               total += (counts[i] * i);
-       }
-
-       /* dont want to divide by 0! --fl */
-       if(counts[0] != length)
-       {
-               snprintf(buf, sizeof buf, "%.3f/%.3f",
-                               (float) (total / (length - counts[0])),
-                               (float) (total / length));
-               sendto_one_numeric(source_p, RPL_STATSDEBUG,
-                               "B :Average depth: %s Highest depth: %lu",
-                               buf, deepest);
-       }
-}
-
-
-static void
-count_hash(struct Client *source_p, rb_dlink_list *table, int length, const char *name)
-{
-       int counts[11];
-       unsigned long deepest = 0;
-       int i;
-
-       memset(counts, 0, sizeof(counts));
-
-       for(i = 0; i < length; i++)
-       {
-               if(rb_dlink_list_length(&table[i]) >= 10)
-                       counts[10]++;
-               else
-                       counts[rb_dlink_list_length(&table[i])]++;
-
-               if(rb_dlink_list_length(&table[i]) > deepest)
-                       deepest = rb_dlink_list_length(&table[i]);
-       }
-
-       output_hash(source_p, name, length, counts, deepest);
-}
-
-void
-hash_stats(struct Client *source_p)
-{
-       count_hash(source_p, channelTable, CH_MAX, "Channel");
-       sendto_one_numeric(source_p, RPL_STATSDEBUG, "B :--");
-       count_hash(source_p, hostTable, HOST_MAX, "Hostname");
-}