Restore stats_spy hook that was removed in commit 401f2454671ca233e35b0e6e4f3fa4c43cd...
[seven-1.x.git] / src / scache.c
blob74ad60e07cbc8a31d0d6fb6094d214fc5b7aae13
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * scache.c: Server names cache.
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
25 #include "stdinc.h"
26 #include "client.h"
27 #include "common.h"
28 #include "irc_string.h"
29 #include "ircd.h"
30 #include "numeric.h"
31 #include "send.h"
32 #include "scache.h"
33 #include "memory.h"
37 * ircd used to store full servernames in anUser as well as in the
38 * whowas info. there can be some 40k such structures alive at any
39 * given time, while the number of unique server names a server sees
40 * in its lifetime is at most a few hundred. by tokenizing server
41 * names internally, the server can easily save 2 or 3 megs of RAM.
42 * -orabidoo
46 #define SCACHE_HASH_SIZE 257
48 typedef struct scache_entry
50 char name[HOSTLEN + 1];
51 struct scache_entry *next;
53 SCACHE;
55 static SCACHE *scache_hash[SCACHE_HASH_SIZE];
57 void
58 clear_scache_hash_table(void)
60 memset(scache_hash, 0, sizeof(scache_hash));
63 static int
64 sc_hash(const char *string)
66 int hash_value;
68 hash_value = 0;
69 while (*string)
71 hash_value += ToLower(*string++);
74 return hash_value % SCACHE_HASH_SIZE;
78 * this takes a server name, and returns a pointer to the same string
79 * (up to case) in the server name token list, adding it to the list if
80 * it's not there. care must be taken not to call this with
81 * user-supplied arguments that haven't been verified to be a valid,
82 * existing, servername. use the hash in list.c for those. -orabidoo
85 const char *
86 find_or_add(const char *name)
88 int hash_index;
89 SCACHE *ptr;
91 ptr = scache_hash[hash_index = sc_hash(name)];
92 for (; ptr; ptr = ptr->next)
94 if(!irccmp(ptr->name, name))
95 return (ptr->name);
98 ptr = (SCACHE *) MyMalloc(sizeof(SCACHE));
99 s_assert(0 != ptr);
101 strlcpy(ptr->name, name, sizeof(ptr->name));
103 ptr->next = scache_hash[hash_index];
104 scache_hash[hash_index] = ptr;
105 return ptr->name;
109 * count_scache
110 * inputs - pointer to where to leave number of servers cached
111 * - pointer to where to leave total memory usage
112 * output - NONE
113 * side effects -
115 void
116 count_scache(size_t * number_servers_cached, size_t * mem_servers_cached)
118 SCACHE *scache_ptr;
119 int i;
121 *number_servers_cached = 0;
122 *mem_servers_cached = 0;
124 for (i = 0; i < SCACHE_HASH_SIZE; i++)
126 scache_ptr = scache_hash[i];
127 while (scache_ptr)
129 *number_servers_cached = *number_servers_cached + 1;
130 *mem_servers_cached = *mem_servers_cached +
131 (strlen(scache_ptr->name) + sizeof(SCACHE *));
133 scache_ptr = scache_ptr->next;