A
download mem.c
Language: C
License: GPL
Copyright: (C) 1997 Robey Pointer (C) 1999 - 2006 Eggheads Development Team
LOC: 341
Project Info
Eggdrop
Server: Eggheads
Type: zip
...eads\e\Eggdrop\Eggdrop\src\
   bg.c
   bg.h
   botcmd.c
   botmsg.c
   botnet.c
   chan.h
   chanprog.c
   cmds.c
   cmdt.h
   dcc.c
   dccutil.c
   dns.c
   dns.h
   eggdrop.h
   flags.c
   flags.h
   lang.h
   language.c
   main.c
   main.h
   Makefile.in
   match.c
   mem.c
   misc.c
   misc_file.c
   misc_file.h
   modules.c
   modules.h
   net.c
   patch.h
   proto.h
   rfc1459.c
   stat.h
   tandem.h
   tcl.c
   tcldcc.c
   tclegg.h
   tclhash.c
   tclhash.h
   tclmisc.c
   tcluser.c
   userent.c
   userrec.c
   users.c
   users.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
 * mem.c -- handles:
 *   memory allocation and deallocation
 *   keeping track of what memory is being used by whom
 *
 * $Id: mem.c,v 1.26 2006-03-28 02:35:50 wcc Exp $
 */
/*
 * Copyright (C) 1997 Robey Pointer
 * Copyright (C) 1999 - 2006 Eggheads Development Team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#define MEMTBLSIZE 25000        /* yikes! */
#define COMPILING_MEM

#include "main.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "mod/modvals.h"


extern module_entry *module_list;

#ifdef DEBUG_MEM
unsigned long memused = 0;
static int lastused = 0;

struct {
  void *ptr;
  int size;
  short line;
  char file[20];
} memtbl[MEMTBLSIZE];
#endif

/* Prototypes */
int expected_memory();
int expmem_chanprog();
int expmem_misc();
int expmem_fileq();
int expmem_users();
int expmem_dccutil();
int expmem_botnet();
int expmem_tcl();
int expmem_tclhash();
int expmem_tclmisc();
int expmem_net();
int expmem_modules();
int expmem_language();
int expmem_tcldcc();
int expmem_dns();


/* Initialize the memory structure
 */
void init_mem()
{
#ifdef DEBUG_MEM
  int i;

  for (i = 0; i < MEMTBLSIZE; i++)
    memtbl[i].ptr = NULL;
#endif
}

/* Tell someone the gory memory details
 */
void tell_mem_status(char *nick)
{
#ifdef DEBUG_MEM
  float per;

  per = ((lastused * 1.0) / (MEMTBLSIZE * 1.0)) * 100.0;
  dprintf(DP_HELP, "NOTICE %s :Memory table usage: %d/%d (%.1f%% full)\n",
          nick, lastused, MEMTBLSIZE, per);
#endif
  dprintf(DP_HELP, "NOTICE %s :Think I'm using about %dk.\n", nick,
          (int) (expected_memory() / 1024));
}

void tell_mem_status_dcc(int idx)
{
#ifdef DEBUG_MEM
  int exp;
  float per;

  exp = expected_memory();      /* in main.c ? */
  per = ((lastused * 1.0) / (MEMTBLSIZE * 1.0)) * 100.0;
  dprintf(idx, "Memory table: %d/%d (%.1f%% full)\n", lastused, MEMTBLSIZE,
          per);
  per = ((exp * 1.0) / (memused * 1.0)) * 100.0;
  if (per != 100.0)
    dprintf(idx, "Memory fault: only accounting for %d/%ld (%.1f%%)\n",
            exp, memused, per);
  dprintf(idx, "Memory table itself occupies an additional %dk static\n",
          (int) (sizeof(memtbl) / 1024));
#endif
}

void debug_mem_to_dcc(int idx)
{
#ifdef DEBUG_MEM
#  define MAX_MEM 13
  unsigned long exp[MAX_MEM], use[MAX_MEM], l;
  int i, j;
  char fn[20], sofar[81];
  module_entry *me;
  char *p;

  exp[0] = expmem_language();
  exp[1] = expmem_chanprog();
  exp[2] = expmem_misc();
  exp[3] = expmem_users();
  exp[4] = expmem_net();
  exp[5] = expmem_dccutil();
  exp[6] = expmem_botnet();
  exp[7] = expmem_tcl();
  exp[8] = expmem_tclhash();
  exp[9] = expmem_tclmisc();
  exp[10] = expmem_modules(1);
  exp[11] = expmem_tcldcc();
  exp[12] = expmem_dns();

  for (me = module_list; me; me = me->next)
    me->mem_work = 0;

  for (i = 0; i < MAX_MEM; i++)
    use[i] = 0;

  for (i = 0; i < lastused; i++) {
    strcpy(fn, memtbl[i].file);
    p = strchr(fn, ':');
    if (p)
      *p = 0;
    l = memtbl[i].size;
    if (!strcmp(fn, "language.c"))
      use[0] += l;
    else if (!strcmp(fn, "chanprog.c"))
      use[1] += l;
    else if (!strcmp(fn, "misc.c"))
      use[2] += l;
    else if (!strcmp(fn, "userrec.c"))
      use[3] += l;
    else if (!strcmp(fn, "net.c"))
      use[4] += l;
    else if (!strcmp(fn, "dccutil.c"))
      use[5] += l;
    else if (!strcmp(fn, "botnet.c"))
      use[6] += l;
    else if (!strcmp(fn, "tcl.c"))
      use[7] += l;
    else if (!strcmp(fn, "tclhash.c"))
      use[8] += l;
    else if (!strcmp(fn, "tclmisc.c"))
      use[9] += l;
    else if (!strcmp(fn, "modules.c"))
      use[10] += l;
    else if (!strcmp(fn, "tcldcc.c"))
      use[11] += l;
    else if (!strcmp(fn, "dns.c"))
      use[12] += l;
    else if (p) {
      for (me = module_list; me; me = me->next)
        if (!strcmp(fn, me->name))
          me->mem_work += l;
    } else
      dprintf(idx, "Not logging file %s!\n", fn);
  }

  for (i = 0; i < MAX_MEM; i++) {
    switch (i) {
    case 0:
      strcpy(fn, "language.c");
      break;
    case 1:
      strcpy(fn, "chanprog.c");
      break;
    case 2:
      strcpy(fn, "misc.c");
      break;
    case 3:
      strcpy(fn, "userrec.c");
      break;
    case 4:
      strcpy(fn, "net.c");
      break;
    case 5:
      strcpy(fn, "dccutil.c");
      break;
    case 6:
      strcpy(fn, "botnet.c");
      break;
    case 7:
      strcpy(fn, "tcl.c");
      break;
    case 8:
      strcpy(fn, "tclhash.c");
      break;
    case 9:
      strcpy(fn, "tclmisc.c");
      break;
    case 10:
      strcpy(fn, "modules.c");
      break;
    case 11:
      strcpy(fn, "tcldcc.c");
      break;
    case 12:
      strcpy(fn, "dns.c");
      break;
    }

    if (use[i] == exp[i])
      dprintf(idx, "File '%-10s' accounted for %lu/%lu (ok)\n", fn, exp[i],
              use[i]);
    else {
      dprintf(idx, "File '%-10s' accounted for %lu/%lu (debug follows:)\n",
              fn, exp[i], use[i]);
      strcpy(sofar, "   ");
      for (j = 0; j < lastused; j++) {
        if ((p = strchr(memtbl[j].file, ':')))
          *p = 0;
        if (!egg_strcasecmp(memtbl[j].file, fn)) {
          if (p)
            sprintf(&sofar[strlen(sofar)], "%-10s/%-4d:(%04d) ",
                    p + 1, memtbl[j].line, memtbl[j].size);
          else
            sprintf(&sofar[strlen(sofar)], "%-4d:(%04d) ",
                    memtbl[j].line, memtbl[j].size);

          if (strlen(sofar) > 60) {
            sofar[strlen(sofar) - 1] = 0;
            dprintf(idx, "%s\n", sofar);
            strcpy(sofar, "   ");
          }
        }
        if (p)
          *p = ':';
      }
      if (sofar[0]) {
        sofar[strlen(sofar) - 1] = 0;
        dprintf(idx, "%s\n", sofar);
      }
    }
  }

  for (me = module_list; me; me = me->next) {
    Function *f = me->funcs;
    int expt = 0;

    if ((f != NULL) && (f[MODCALL_EXPMEM] != NULL))
      expt = f[MODCALL_EXPMEM] ();
    if (me->mem_work == expt)
      dprintf(idx, "Module '%-10s' accounted for %lu/%lu (ok)\n", me->name,
              expt, me->mem_work);
    else {
      dprintf(idx, "Module '%-10s' accounted for %lu/%lu (debug follows:)\n",
              me->name, expt, me->mem_work);
      strcpy(sofar, "   ");
      for (j = 0; j < lastused; j++) {
        strcpy(fn, memtbl[j].file);
        if ((p = strchr(fn, ':')) != NULL) {
          *p = 0;
          if (!egg_strcasecmp(fn, me->name)) {
            sprintf(&sofar[strlen(sofar)], "%-10s/%-4d:(%04X) ", p + 1,
                    memtbl[j].line, memtbl[j].size);
            if (strlen(sofar) > 60) {
              sofar[strlen(sofar) - 1] = 0;
              dprintf(idx, "%s\n", sofar);
              strcpy(sofar, "   ");
            }
            *p = ':';
          }
        }
      }
      if (sofar[0]) {
        sofar[strlen(sofar) - 1] = 0;
        dprintf(idx, "%s\n", sofar);
      }
    }
  }

  dprintf(idx, "--- End of debug memory list.\n");
#else
  dprintf(idx, "Compiled without extensive memory debugging (sorry).\n");
#endif
  tell_netdebug(idx);
}

void *n_malloc(int size, const char *file, int line)
{
  void *x;

#ifdef DEBUG_MEM
  int i = 0;
  char *p;
#endif

  x = (void *) malloc(size);
  if (x == NULL) {
    putlog(LOG_MISC, "*", "*** FAILED MALLOC %s (%d) (%d): %s", file, line,
           size, strerror(errno));
    fatal("Memory allocation failed", 0);
  }
#ifdef DEBUG_MEM
  if (lastused == MEMTBLSIZE) {
    putlog(LOG_MISC, "*", "*** MEMORY TABLE FULL: %s (%d)", file, line);
    fatal("Memory table full", 0);
  }
  i = lastused;
  memtbl[i].ptr = x;
  memtbl[i].line = line;
  memtbl[i].size = size;
  p = strrchr(file, '/');
  strncpy(memtbl[i].file, p ? p + 1 : file, 19);
  memtbl[i].file[19] = 0;
  memused += size;
  lastused++;
#endif
  return x;
}

void *n_realloc(void *ptr, int size, const char *file, int line)
{
  void *x;
  int i = 0;

#ifdef DEBUG_MEM
  char *p;
#endif

  /* ptr == NULL is valid. Avoiding duplicate code further down */
  if (!ptr)
    return n_malloc(size, file, line);

  x = (void *) realloc(ptr, size);
  if (x == NULL && size > 0) {
    i = i;
    putlog(LOG_MISC, "*", "*** FAILED REALLOC %s (%d)", file, line);
    return NULL;
  }
#ifdef DEBUG_MEM
  for (i = 0; (i < lastused) && (memtbl[i].ptr != ptr); i++);
  if (i == lastused) {
    putlog(LOG_MISC, "*", "*** ATTEMPTING TO REALLOC NON-MALLOC'D PTR: %s (%d)",
           file, line);
    return NULL;
  }
  memused -= memtbl[i].size;
  memtbl[i].ptr = x;
  memtbl[i].line = line;
  memtbl[i].size = size;
  p = strrchr(file, '/');
  strncpy(memtbl[i].file, p ? p + 1 : file, 19);
  memtbl[i].file[19] = 0;
  memused += size;
#endif
  return x;
}

void n_free(void *ptr, const char *file, int line)
{
  int i = 0;

  if (ptr == NULL) {
    putlog(LOG_MISC, "*", "*** ATTEMPTING TO FREE NULL PTR: %s (%d)",
           file, line);
    i = i;
    return;
  }
#ifdef DEBUG_MEM
  /* Give tcl builtins an escape mechanism */
  if (line) {
    for (i = 0; (i < lastused) && (memtbl[i].ptr != ptr); i++);
    if (i == lastused) {
      putlog(LOG_MISC, "*", "*** ATTEMPTING TO FREE NON-MALLOC'D PTR: %s (%d)",
             file, line);
      return;
    }
    memused -= memtbl[i].size;
    lastused--;
    /* We don't want any holes, so if this wasn't the last entry, swap it. */
    if (i != lastused) {
      memtbl[i].ptr = memtbl[lastused].ptr;
      memtbl[i].size = memtbl[lastused].size;
      memtbl[i].line = memtbl[lastused].line;
      strcpy(memtbl[i].file, memtbl[lastused].file);
    }
  }
#endif
  free(ptr);
}

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us