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
24 * $Id: scache.c 20702 2005-08-31 20:59:02Z leeh $
30 #include "irc_string.h"
39 * ircd used to store full servernames in anUser as well as in the
40 * whowas info. there can be some 40k such structures alive at any
41 * given time, while the number of unique server names a server sees
42 * in its lifetime is at most a few hundred. by tokenizing server
43 * names internally, the server can easily save 2 or 3 megs of RAM.
48 #define SCACHE_HASH_SIZE 257
50 typedef struct scache_entry
52 char name
[HOSTLEN
+ 1];
53 struct scache_entry
*next
;
57 static SCACHE
*scache_hash
[SCACHE_HASH_SIZE
];
60 clear_scache_hash_table(void)
62 memset(scache_hash
, 0, sizeof(scache_hash
));
66 sc_hash(const char *string
)
73 hash_value
+= ToLower(*string
++);
76 return hash_value
% SCACHE_HASH_SIZE
;
80 * this takes a server name, and returns a pointer to the same string
81 * (up to case) in the server name token list, adding it to the list if
82 * it's not there. care must be taken not to call this with
83 * user-supplied arguments that haven't been verified to be a valid,
84 * existing, servername. use the hash in list.c for those. -orabidoo
88 find_or_add(const char *name
)
93 ptr
= scache_hash
[hash_index
= sc_hash(name
)];
94 for (; ptr
; ptr
= ptr
->next
)
96 if(!irccmp(ptr
->name
, name
))
100 ptr
= (SCACHE
*) MyMalloc(sizeof(SCACHE
));
103 strlcpy(ptr
->name
, name
, sizeof(ptr
->name
));
105 ptr
->next
= scache_hash
[hash_index
];
106 scache_hash
[hash_index
] = ptr
;
112 * inputs - pointer to where to leave number of servers cached
113 * - pointer to where to leave total memory usage
118 count_scache(size_t * number_servers_cached
, size_t * mem_servers_cached
)
123 *number_servers_cached
= 0;
124 *mem_servers_cached
= 0;
126 for (i
= 0; i
< SCACHE_HASH_SIZE
; i
++)
128 scache_ptr
= scache_hash
[i
];
131 *number_servers_cached
= *number_servers_cached
+ 1;
132 *mem_servers_cached
= *mem_servers_cached
+
133 (strlen(scache_ptr
->name
) + sizeof(SCACHE
*));
135 scache_ptr
= scache_ptr
->next
;