]>
Commit | Line | Data |
---|---|---|
212380e3 AC |
1 | /* |
2 | * ircd-ratbox: A slightly useful ircd. | |
3 | * scache.c: Server names cache. | |
4 | * | |
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 | |
8 | * | |
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. | |
13 | * | |
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. | |
18 | * | |
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 | |
23 | * | |
24 | * $Id: scache.c 6 2005-09-10 01:02:21Z nenolod $ | |
25 | */ | |
26 | ||
27 | #include "stdinc.h" | |
28 | #include "client.h" | |
29 | #include "common.h" | |
30 | #include "irc_string.h" | |
31 | #include "ircd.h" | |
32 | #include "numeric.h" | |
33 | #include "send.h" | |
34 | #include "scache.h" | |
35 | #include "memory.h" | |
36 | ||
37 | ||
38 | /* | |
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. | |
44 | * -orabidoo | |
45 | */ | |
46 | ||
47 | ||
48 | #define SCACHE_HASH_SIZE 257 | |
49 | ||
50 | typedef struct scache_entry | |
51 | { | |
52 | char name[HOSTLEN + 1]; | |
53 | struct scache_entry *next; | |
54 | } | |
55 | SCACHE; | |
56 | ||
57 | static SCACHE *scache_hash[SCACHE_HASH_SIZE]; | |
58 | ||
59 | void | |
60 | clear_scache_hash_table(void) | |
61 | { | |
62 | memset(scache_hash, 0, sizeof(scache_hash)); | |
63 | } | |
64 | ||
65 | static int | |
66 | sc_hash(const char *string) | |
67 | { | |
68 | int hash_value; | |
69 | ||
70 | hash_value = 0; | |
71 | while (*string) | |
72 | { | |
73 | hash_value += ToLower(*string++); | |
74 | } | |
75 | ||
76 | return hash_value % SCACHE_HASH_SIZE; | |
77 | } | |
78 | ||
79 | /* | |
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 | |
85 | */ | |
86 | ||
87 | const char * | |
88 | find_or_add(const char *name) | |
89 | { | |
90 | int hash_index; | |
91 | SCACHE *ptr; | |
92 | ||
93 | ptr = scache_hash[hash_index = sc_hash(name)]; | |
94 | for (; ptr; ptr = ptr->next) | |
95 | { | |
96 | if(!irccmp(ptr->name, name)) | |
97 | return (ptr->name); | |
98 | } | |
99 | ||
100 | ptr = (SCACHE *) MyMalloc(sizeof(SCACHE)); | |
101 | s_assert(0 != ptr); | |
102 | ||
103 | strlcpy(ptr->name, name, sizeof(ptr->name)); | |
104 | ||
105 | ptr->next = scache_hash[hash_index]; | |
106 | scache_hash[hash_index] = ptr; | |
107 | return ptr->name; | |
108 | } | |
109 | ||
110 | /* | |
111 | * count_scache | |
112 | * inputs - pointer to where to leave number of servers cached | |
113 | * - pointer to where to leave total memory usage | |
114 | * output - NONE | |
115 | * side effects - | |
116 | */ | |
117 | void | |
118 | count_scache(size_t * number_servers_cached, size_t * mem_servers_cached) | |
119 | { | |
120 | SCACHE *scache_ptr; | |
121 | int i; | |
122 | ||
123 | *number_servers_cached = 0; | |
124 | *mem_servers_cached = 0; | |
125 | ||
126 | for (i = 0; i < SCACHE_HASH_SIZE; i++) | |
127 | { | |
128 | scache_ptr = scache_hash[i]; | |
129 | while (scache_ptr) | |
130 | { | |
131 | *number_servers_cached = *number_servers_cached + 1; | |
132 | *mem_servers_cached = *mem_servers_cached + | |
133 | (strlen(scache_ptr->name) + sizeof(SCACHE *)); | |
134 | ||
135 | scache_ptr = scache_ptr->next; | |
136 | } | |
137 | } | |
138 | } |