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
28 #include "irc_string.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.
46 #define SCACHE_HASH_SIZE 257
48 typedef struct scache_entry
50 char name
[HOSTLEN
+ 1];
51 struct scache_entry
*next
;
55 static SCACHE
*scache_hash
[SCACHE_HASH_SIZE
];
58 clear_scache_hash_table(void)
60 memset(scache_hash
, 0, sizeof(scache_hash
));
64 sc_hash(const char *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
86 find_or_add(const char *name
)
91 ptr
= scache_hash
[hash_index
= sc_hash(name
)];
92 for (; ptr
; ptr
= ptr
->next
)
94 if(!irccmp(ptr
->name
, name
))
98 ptr
= (SCACHE
*) MyMalloc(sizeof(SCACHE
));
101 strlcpy(ptr
->name
, name
, sizeof(ptr
->name
));
103 ptr
->next
= scache_hash
[hash_index
];
104 scache_hash
[hash_index
] = ptr
;
110 * inputs - pointer to where to leave number of servers cached
111 * - pointer to where to leave total memory usage
116 count_scache(size_t * number_servers_cached
, size_t * mem_servers_cached
)
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
];
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
;