/[cvs]/eggdrop1.9/src/mem.c
ViewVC logotype

Contents of /eggdrop1.9/src/mem.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.18 - (show annotations) (download) (as text)
Wed Oct 10 10:44:04 2001 UTC (17 years, 6 months ago) by tothwolf
Branch: MAIN
CVS Tags: HEAD
Changes since 1.17: +1 -1 lines
File MIME type: text/x-chdr
FILE REMOVED
all expmem functions and support removed
all memory status/debugging code removed
all #ifdef DEBUG_MEM code removed

nmalloc macro/n_malloc function removed, use malloc() now
mod_malloc function removed
my_strdup function removed, use malloc_strcpy macro now
channel_malloc removed, use malloc_memset macro now
n_malloc_null function removed, use malloc_memset macro now
user_malloc macro/_user_malloc function removed, use malloc() now

nrealloc macro/n_realloc function removed, use realloc() now
mod_realloc function removed

nfree macro/n_free function removed, use free() now
mod_free function removed
my_free macro removed, use free_null macro now
Tcl_Free compat macro now uses free() instead of removed n_free function

malloc->strcpy now uses malloc_strcpy macro
free(ptr);ptr=NULL; now uses free_null macro
malloc->bzero now uses malloc_memset macro
get_data_ptr macro/_get_data_ptr function removed, use malloc_memset macro now

compat memcpy removed
compat memset removed
egg_memcpy/my_memcpy misuse/mess changed to memcpy
egg_bzero/bzero macro removed, use memset() now

many uninitialized pointers now default to NULL

match.c is now compiled on it's own instead of being included into tclhash.c
match.c cleanup, removed broken/incomplete ircII compat code
wild_match macro removed, _wild_match function renamed to wild_match
wild_match_per function no longer declared static
added prototypes for wild_match and wild_match_per functions

Major function/macro changes:
  nmalloc -> malloc()
  nrealloc -> realloc()
  nfree -> free()

Bugs found and fixed during audit:
  buffer overflow in adduser()

Bugs fixed when code was removed:
  typo in filedb3.c: EBUG_MEM -> DEBUG_MEM
  extra egg_bzero call from filesys.c

1 /*
2 * mem.c -- handles:
3 * memory allocation and deallocation
4 * keeping track of what memory is being used by whom
5 *
6 * $Id: mem.c,v 1.17 2001/08/16 13:27:38 drummer Exp $
7 */
8 /*
9 * Copyright (C) 1997 Robey Pointer
10 * Copyright (C) 1999, 2000, 2001 Eggheads Development Team
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26
27 #define MEMTBLSIZE 25000 /* yikes! */
28 #define COMPILING_MEM
29
30 #include "main.h"
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <errno.h>
35
36 #include "mod/modvals.h"
37
38
39 extern module_entry *module_list;
40
41 #ifdef DEBUG_MEM
42 unsigned long memused = 0;
43 static int lastused = 0;
44
45 struct {
46 void *ptr;
47 int size;
48 short line;
49 char file[20];
50 } memtbl[MEMTBLSIZE];
51 #endif
52
53 /* Prototypes */
54 int expected_memory();
55 int expmem_chanprog();
56 int expmem_misc();
57 int expmem_fileq();
58 int expmem_users();
59 int expmem_dccutil();
60 int expmem_botnet();
61 int expmem_tcl();
62 int expmem_tclhash();
63 int expmem_net();
64 int expmem_modules();
65 int expmem_tcldcc();
66 int expmem_dns();
67
68
69 /* Initialize the memory structure
70 */
71 void init_mem()
72 {
73 #ifdef DEBUG_MEM
74 int i;
75
76 for (i = 0; i < MEMTBLSIZE; i++)
77 memtbl[i].ptr = NULL;
78 #endif
79 }
80
81 /* Tell someone the gory memory details
82 */
83 void tell_mem_status(char *nick)
84 {
85 #ifdef DEBUG_MEM
86 float per;
87
88 per = ((lastused * 1.0) / (MEMTBLSIZE * 1.0)) * 100.0;
89 dprintf(DP_HELP, "NOTICE %s :Memory table usage: %d/%d (%.1f%% full)\n",
90 nick, lastused, MEMTBLSIZE, per);
91 #endif
92 dprintf(DP_HELP, "NOTICE %s :Think I'm using about %dk.\n", nick,
93 (int) (expected_memory() / 1024));
94 }
95
96 void tell_mem_status_dcc(int idx)
97 {
98 #ifdef DEBUG_MEM
99 int exp;
100 float per;
101
102 exp = expected_memory(); /* in main.c ? */
103 per = ((lastused * 1.0) / (MEMTBLSIZE * 1.0)) * 100.0;
104 dprintf(idx, "Memory table: %d/%d (%.1f%% full)\n", lastused, MEMTBLSIZE,
105 per);
106 per = ((exp * 1.0) / (memused * 1.0)) * 100.0;
107 if (per != 100.0)
108 dprintf(idx, "Memory fault: only accounting for %d/%ld (%.1f%%)\n",
109 exp, memused, per);
110 dprintf(idx, "Memory table itself occupies an additional %dk static\n",
111 (int) (sizeof(memtbl) / 1024));
112 #endif
113 }
114
115 void debug_mem_to_dcc(int idx)
116 {
117 #ifdef DEBUG_MEM
118 #define MAX_MEM 11
119 unsigned long exp[MAX_MEM], use[MAX_MEM], l;
120 int i, j;
121 char fn[20], sofar[81];
122 module_entry *me;
123 char *p;
124
125 exp[0] = expmem_dns();
126 exp[1] = expmem_chanprog();
127 exp[2] = expmem_misc();
128 exp[3] = expmem_users();
129 exp[4] = expmem_net();
130 exp[5] = expmem_dccutil();
131 exp[6] = expmem_botnet();
132 exp[7] = expmem_tcl();
133 exp[8] = expmem_tclhash();
134 exp[9] = expmem_modules(1);
135 exp[10] = expmem_tcldcc();
136 for (me = module_list; me; me = me->next)
137 me->mem_work = 0;
138 for (i = 0; i < MAX_MEM; i++)
139 use[i] = 0;
140 for (i = 0; i < lastused; i++) {
141 strcpy(fn, memtbl[i].file);
142 p = strchr(fn, ':');
143 if (p)
144 *p = 0;
145 l = memtbl[i].size;
146 if (!strcmp(fn, "dns.c"))
147 use[0] += l;
148 else if (!strcmp(fn, "chanprog.c"))
149 use[1] += l;
150 else if (!strcmp(fn, "misc.c"))
151 use[2] += l;
152 else if (!strcmp(fn, "userrec.c"))
153 use[3] += l;
154 else if (!strcmp(fn, "net.c"))
155 use[4] += l;
156 else if (!strcmp(fn, "dccutil.c"))
157 use[5] += l;
158 else if (!strcmp(fn, "botnet.c"))
159 use[6] += l;
160 else if (!strcmp(fn, "tcl.c"))
161 use[7] += l;
162 else if (!strcmp(fn, "tclhash.c"))
163 use[8] += l;
164 else if (!strcmp(fn, "modules.c"))
165 use[9] += l;
166 else if (!strcmp(fn, "tcldcc.c"))
167 use[10] += l;
168 else if (p) {
169 for (me = module_list; me; me = me->next)
170 if (!strcmp(fn, me->name))
171 me->mem_work += l;
172 } else
173 dprintf(idx, "Not logging file %s!\n", fn);
174 }
175 for (i = 0; i < MAX_MEM; i++) {
176 switch (i) {
177 case 0:
178 strcpy(fn, "dns.c");
179 break;
180 case 1:
181 strcpy(fn, "chanprog.c");
182 break;
183 case 2:
184 strcpy(fn, "misc.c");
185 break;
186 case 3:
187 strcpy(fn, "userrec.c");
188 break;
189 case 4:
190 strcpy(fn, "net.c");
191 break;
192 case 5:
193 strcpy(fn, "dccutil.c");
194 break;
195 case 6:
196 strcpy(fn, "botnet.c");
197 break;
198 case 7:
199 strcpy(fn, "tcl.c");
200 break;
201 case 8:
202 strcpy(fn, "tclhash.c");
203 break;
204 case 9:
205 strcpy(fn, "modules.c");
206 break;
207 case 10:
208 strcpy(fn, "tcldcc.c");
209 break;
210 }
211 if (use[i] == exp[i]) {
212 dprintf(idx, "File '%-10s' accounted for %lu/%lu (ok)\n", fn, exp[i],
213 use[i]);
214 } else {
215 dprintf(idx, "File '%-10s' accounted for %lu/%lu (debug follows:)\n",
216 fn, exp[i], use[i]);
217 strcpy(sofar, " ");
218 for (j = 0; j < lastused; j++) {
219 if ((p = strchr(memtbl[j].file, ':')))
220 *p = 0;
221 if (!egg_strcasecmp(memtbl[j].file, fn)) {
222 if (p)
223 sprintf(&sofar[strlen(sofar)], "%-10s/%-4d:(%04d) ",
224 p + 1, memtbl[j].line, memtbl[j].size);
225 else
226 sprintf(&sofar[strlen(sofar)], "%-4d:(%04d) ",
227 memtbl[j].line, memtbl[j].size);
228
229 if (strlen(sofar) > 60) {
230 sofar[strlen(sofar) - 1] = 0;
231 dprintf(idx, "%s\n", sofar);
232 strcpy(sofar, " ");
233 }
234 }
235 if (p)
236 *p = ':';
237 }
238 if (sofar[0]) {
239 sofar[strlen(sofar) - 1] = 0;
240 dprintf(idx, "%s\n", sofar);
241 }
242 }
243 }
244 for (me = module_list; me; me = me->next) {
245 Function *f = me->funcs;
246 int expt = 0;
247
248 if ((f != NULL) && (f[MODCALL_EXPMEM] != NULL))
249 expt = f[MODCALL_EXPMEM] ();
250 if (me->mem_work == expt) {
251 dprintf(idx, "Module '%-10s' accounted for %lu/%lu (ok)\n", me->name,
252 expt, me->mem_work);
253 } else {
254 dprintf(idx, "Module '%-10s' accounted for %lu/%lu (debug follows:)\n",
255 me->name, expt, me->mem_work);
256 strcpy(sofar, " ");
257 for (j = 0; j < lastused; j++) {
258 strcpy(fn, memtbl[j].file);
259 if ((p = strchr(fn, ':')) != NULL) {
260 *p = 0;
261 if (!egg_strcasecmp(fn, me->name)) {
262 sprintf(&sofar[strlen(sofar)], "%-10s/%-4d:(%04X) ", p + 1,
263 memtbl[j].line, memtbl[j].size);
264 if (strlen(sofar) > 60) {
265 sofar[strlen(sofar) - 1] = 0;
266 dprintf(idx, "%s\n", sofar);
267 strcpy(sofar, " ");
268 }
269 *p = ':';
270 }
271 }
272 }
273 if (sofar[0]) {
274 sofar[strlen(sofar) - 1] = 0;
275 dprintf(idx, "%s\n", sofar);
276 }
277 }
278 }
279 dprintf(idx, "--- End of debug memory list.\n");
280 #else
281 dprintf(idx, "Compiled without extensive memory debugging (sorry).\n");
282 #endif
283 tell_netdebug(idx);
284 }
285
286 void *n_malloc(int size, const char *file, int line)
287 {
288 void *x;
289 #ifdef DEBUG_MEM
290 int i = 0;
291 char *p;
292 #endif
293
294 x = (void *) malloc(size);
295 if (x == NULL) {
296 putlog(LOG_MISC, "*", "*** FAILED MALLOC %s (%d) (%d): %s", file, line,
297 size, strerror(errno));
298 fatal("Memory allocation failed", 0);
299 }
300 #ifdef DEBUG_MEM
301 if (lastused == MEMTBLSIZE) {
302 putlog(LOG_MISC, "*", "*** MEMORY TABLE FULL: %s (%d)", file, line);
303 fatal("Memory table full", 0);
304 }
305 i = lastused;
306 memtbl[i].ptr = x;
307 memtbl[i].line = line;
308 memtbl[i].size = size;
309 p = strrchr(file, '/');
310 strncpy(memtbl[i].file, p ? p + 1 : file, 19);
311 memtbl[i].file[19] = 0;
312 memused += size;
313 lastused++;
314 #endif
315 return x;
316 }
317
318 void *n_realloc(void *ptr, int size, const char *file, int line)
319 {
320 void *x;
321 int i = 0;
322 #ifdef DEBUG_MEM
323 char *p;
324 #endif
325
326 /* ptr == NULL is valid. Avoiding duplicate code further down */
327 if (!ptr)
328 return n_malloc(size, file, line);
329
330 x = (void *) realloc(ptr, size);
331 if (x == NULL) {
332 i = i;
333 putlog(LOG_MISC, "*", "*** FAILED REALLOC %s (%d)", file, line);
334 return NULL;
335 }
336 #ifdef DEBUG_MEM
337 for (i = 0; (i < lastused) && (memtbl[i].ptr != ptr); i++);
338 if (i == lastused) {
339 putlog(LOG_MISC, "*", "*** ATTEMPTING TO REALLOC NON-MALLOC'D PTR: %s (%d)",
340 file, line);
341 return NULL;
342 }
343 memused -= memtbl[i].size;
344 memtbl[i].ptr = x;
345 memtbl[i].line = line;
346 memtbl[i].size = size;
347 p = strrchr(file, '/');
348 strncpy(memtbl[i].file, p ? p + 1 : file, 19);
349 memtbl[i].file[19] = 0;
350 memused += size;
351 #endif
352 return x;
353 }
354
355 void n_free(void *ptr, const char *file, int line)
356 {
357 int i = 0;
358
359 if (ptr == NULL) {
360 putlog(LOG_MISC, "*", "*** ATTEMPTING TO FREE NULL PTR: %s (%d)",
361 file, line);
362 i = i;
363 return;
364 }
365 #ifdef DEBUG_MEM
366 /* Give tcl builtins an escape mechanism */
367 if (line) {
368 for (i = 0; (i < lastused) && (memtbl[i].ptr != ptr); i++);
369 if (i == lastused) {
370 putlog(LOG_MISC, "*", "*** ATTEMPTING TO FREE NON-MALLOC'D PTR: %s (%d)",
371 file, line);
372 return;
373 }
374 memused -= memtbl[i].size;
375 lastused--;
376 memtbl[i].ptr = memtbl[lastused].ptr;
377 memtbl[i].size = memtbl[lastused].size;
378 memtbl[i].line = memtbl[lastused].line;
379 strcpy(memtbl[i].file, memtbl[lastused].file);
380 }
381 #endif
382 free(ptr);
383 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23