unrealircd

- supernets unrealircd source & configuration
git clone git://git.acid.vegas/unrealircd.git
Log | Files | Refs | Archive | README | LICENSE

scache.c (2260B)

      1 /* License: GPLv1 */
      2 
      3 /** @file
      4  * @brief String cache - only used for server names.
      5  */
      6 
      7 #include "unrealircd.h"
      8 
      9 /*
     10  * ircd used to store full servernames in User as well as in the
     11  * whowas info.  there can be some 40k such structures alive at any
     12  * given time, while the number of unique server names a server sees in
     13  * its lifetime is at most a few hundred.  by tokenizing server names
     14  * internally, the server can easily save 2 or 3 megs of RAM.
     15  * -orabidoo
     16  */
     17 /*
     18  * I could have tucked this code into hash.c I suppose but lets keep it
     19  * separate for now -Dianora
     20  */
     21 
     22 #define SCACHE_HASH_SIZE 257
     23 
     24 typedef struct SCACHE SCACHE;
     25 struct SCACHE {
     26 	char name[HOSTLEN + 1];
     27 	SCACHE *next;
     28 };
     29 
     30 static SCACHE *scache_hash[SCACHE_HASH_SIZE];
     31 
     32 /*
     33  * renamed to keep it consistent with the other hash functions -Dianora 
     34  */
     35 /*
     36  * orabidoo had named it init_scache_hash(); 
     37  */
     38 
     39 void clear_scache_hash_table(void)
     40 {
     41 	memset((char *)scache_hash, '\0', sizeof(scache_hash));
     42 }
     43 
     44 static int hash(char *string)
     45 {
     46 	int  hash_value;
     47 
     48 	hash_value = 0;
     49 	while (*string)
     50 		hash_value += (*string++ & 0xDF);
     51 
     52 	return hash_value % SCACHE_HASH_SIZE;
     53 }
     54 
     55 /** Add a string to the string cache.
     56  * this takes a server name, and returns a pointer to the same string
     57  * (up to case) in the server name token list, adding it to the list if
     58  * it's not there.  care must be taken not to call this with
     59  * user-supplied arguments that haven't been verified to be a valid,
     60  * existing, servername.  use the hash in list.c for those.  -orabidoo
     61  * @param name	A valid server name
     62  * @returns Pointer to the server name
     63  */
     64 char *find_or_add(char *name)
     65 {
     66 	int  hash_index;
     67 	SCACHE *ptr, *newptr;
     68 
     69 	ptr = scache_hash[hash_index = hash(name)];
     70 	while (ptr)
     71 	{
     72 		if (!mycmp(ptr->name, name))
     73 		{
     74 			return (ptr->name);
     75 		}
     76 		else
     77 		{
     78 			ptr = ptr->next;
     79 		}
     80 	}
     81 
     82 	/*
     83 	 * not found -- add it 
     84 	 */
     85 	if ((ptr = scache_hash[hash_index]))
     86 	{
     87 		newptr = scache_hash[hash_index] = safe_alloc(sizeof(SCACHE));
     88 		strlcpy(newptr->name, name, sizeof(newptr->name));
     89 		newptr->next = ptr;
     90 		return (newptr->name);
     91 	}
     92 	else
     93 	{
     94 		ptr = scache_hash[hash_index] = safe_alloc(sizeof(SCACHE));
     95 		strlcpy(ptr->name, name, sizeof(newptr->name));
     96 		ptr->next = (SCACHE *) NULL;
     97 		return (ptr->name);
     98 	}
     99 }