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

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

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


Revision 1.1.1.1 - (show annotations) (download) (as text) (vendor branch)
Wed Jun 23 19:51:30 1999 UTC (22 years, 4 months ago) by segfault
Branch: eggdev
CVS Tags: start
Changes since 1.1: +0 -0 lines
File MIME type: text/x-chdr
Eggdrop 1.3.28 CVS Code

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23