File: runmalloc.c

package info (click to toggle)
ircd 2.10.07-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,232 kB
  • ctags: 2,253
  • sloc: ansic: 27,541; makefile: 742; sh: 327; perl: 18
file content (456 lines) | stat: -rw-r--r-- 11,494 bytes parent folder | download
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*
 * Run's malloc/realloc/calloc/free DEBUG tools v2.0
 *
 * (c) Copyright 1996, 1997
 *
 * Author:
 *
 * 1024/624ACAD5 1997/01/26 Carlo Wood, Run on IRC <carlo@runaway.xs4all.nl>
 * Key fingerprint = 32 EC A7 B6 AC DB 65 A6  F6 F6 55 DD 1C DC FF 61
 * Get key from pgp-public-keys server or
 * finger carlo@runaway.xs4all.nl for public key (dialin, try at 21-22h GMT).
 *
 * 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, 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "sys.h"

#ifdef DEBUGMALLOC
#include <stdlib.h>
#include "h.h"

RCSTAG_CC("$Id: runmalloc.c,v 1.23 1999/10/04 15:08:15 foxxe Exp $");

#define MALLOC_HASHTABLE_SIZE 16384
#define MallocHash(x) \
    ((unsigned int)(((((long int)(x) >> 4) * 0xDEECE66D) >> 16) & (long int)0x3fff))
#define MAGIC_PREFIX 0xe4c483a1
#define MAGIC_POSTFIX 0x435bd0fa

#ifdef MEMLEAKSTATS
typedef struct {
  const char *filename;
  int line;
  int number_of_allocations;
#ifdef MEMSIZESTATS
  size_t size;
#endif
} location_st;

#define LOCSIZE 1024		/* Maximum of 256 different locations */
static location_st location[LOCSIZE];
static unsigned int locations;	/* Counter */

static unsigned int find_location(const char *filename, int line)
{
  register unsigned int hash;
  hash = line & 0xff;
  while (location[hash].filename && (location[hash].line != line ||
      location[hash].filename != filename))
    if (++hash == LOCSIZE)
      hash = 0;
  if (!location[hash].filename)
  {
    /* New location */
    ++locations;
    location[hash].filename = filename;
    location[hash].line = line;
  }
  return hash;
}
#endif

#ifdef MEMMAGICNUMS
/* The size of this struct should be a multiple of 4 bytes, just in case... */
typedef struct {
#ifdef MEMMAGICNUMS
  unsigned int prefix_magicnumber;
#endif
} prefix_blk_st;

#define SIZEOF_PREFIX sizeof(prefix_blk_st)
#else
typedef void prefix_blk_st;
#define SIZEOF_PREFIX 0
#endif

#ifdef MEMMAGICNUMS
typedef struct {
  unsigned int postfix_magicnumber;
} postfix_blk_st;

#define SIZEOF_POSTFIX sizeof(postfix_blk_st)
#define HAS_POSTFIX
#else
#define SIZEOF_POSTFIX 0
#endif

typedef struct hash_entry_st {
  struct hash_entry_st *next;
  prefix_blk_st *ptr;
#ifdef MEMSIZESTATS
  size_t size;
#endif
#ifdef MEMLEAKSTATS
  unsigned int location;
#ifdef MEMTIMESTATS
  time_t when;
#endif
#endif
} hash_entry_st;

#define memblkp(prefix_ptr) \
    ((void *)((size_t)prefix_ptr + SIZEOF_PREFIX))
#define prefixp(memblk_ptr) \
    ((prefix_blk_st *)((size_t)memblk_ptr - SIZEOF_PREFIX))
#define postfixp(memblk_ptr, size) \
    ((postfix_blk_st *)((size_t)memblk_ptr + size))

static hash_entry_st *hashtable[MALLOC_HASHTABLE_SIZE];
#ifdef MEMSIZESTATS
static size_t mem_size = 0;	/* Number of allocated bytes  */
static unsigned int alloc_cnt = 0;	/* Number of allocated blocks */
#endif

#ifdef MEMLEAKSTATS
#include "struct.h"
#include "send.h"
#include "numeric.h"
#include "s_err.h"
#include "ircd.h"
#include "s_serv.h"
#include "numnicks.h"

void report_memleak_stats(aClient *sptr, int parc, char *parv[])
{
  unsigned int hash;
  location_st *loc = location;

#ifdef MEMTIMESTATS
  time_t till = now;
  time_t from = me.since;
  if (parc > 3)
  {
    location_st tmp_loc[LOCSIZE];
    hash_entry_st **start;
    memset(tmp_loc, 0, sizeof(tmp_loc));
    if (parc > 3)
      till -= atoi(parv[3]);
    if (parc > 4)
      from += atoi(parv[4]);
    for (start = &hashtable[0];
	start < &hashtable[MALLOC_HASHTABLE_SIZE]; ++start)
    {
      hash_entry_st *hash_entry;
      for (hash_entry = *start; hash_entry; hash_entry = hash_entry->next)
	if (hash_entry->when >= from && hash_entry->when <= till)
	{
#ifdef MEMSIZESTATS
	  tmp_loc[hash_entry->location].size += hash_entry->size;
#endif
	  tmp_loc[hash_entry->location].number_of_allocations++;
	}
    }
    loc = tmp_loc;
    if (MyUser(sptr) || Protocol(sptr->from) < 10)
      sendto_one(sptr, ":%s NOTICE %s :Memory allocated between " TIME_T_FMT
	  " (server start + %s s) and " TIME_T_FMT " (now - %s s):",
	  me.name, parv[0], from, parc > 4 ? parv[4] : "0", till,
	  parc > 3 ? parv[3] : "0");
    else
      sendto_one(sptr, "%s NOTICE %s%s :Memory allocated between " TIME_T_FMT
	  " (server start + %s s) and " TIME_T_FMT " (now - %s s):",
	  NumServ(&me), NumNick(sptr), from, parc > 4 ? parv[4] : "0", till,
	  parc > 3 ? parv[3] : "0");
  }
#endif
  for (hash = 0; hash < LOCSIZE; ++hash)
    if (loc[hash].number_of_allocations > 0)
      sendto_one(sptr, rpl_str(RPL_STATMEM), me.name, parv[0],
	  loc[hash].number_of_allocations,
	  location[hash].line, location[hash].filename
#ifdef MEMSIZESTATS
	  , loc[hash].size
#endif
	  );
}

void *RunMalloc_memleak(size_t size, int line, const char *filename)
#else
void *RunMalloc(size_t size)
#endif
{
  register prefix_blk_st *ptr;
  register hash_entry_st *hash_entry;
  register hash_entry_st **hashtablep;

#ifdef HAS_POSTFIX
  size += 3;
  size &= ~3;
#endif

  if (!((ptr = (prefix_blk_st *)
      malloc(SIZEOF_PREFIX + size + SIZEOF_POSTFIX)) &&
      (hash_entry = (hash_entry_st *) malloc(sizeof(hash_entry_st)))))
  {
    if (ptr)
      free(ptr);
    Debug((DEBUG_FATAL, "Out of memory !"));
    return NULL;
  }

  hashtablep = &hashtable[MallocHash(ptr)];
  hash_entry->next = *hashtablep;
  *hashtablep = hash_entry;
  hash_entry->ptr = ptr;
#ifdef MEMLEAKSTATS
#ifdef MEMTIMESTATS
  hash_entry->when = now;
#endif
  location[(hash_entry->location =
      find_location(filename, line))].number_of_allocations++;
#endif
#ifdef MEMSIZESTATS
  hash_entry->size = size;
#ifdef MEMLEAKSTATS
  location[hash_entry->location].size += size;
#endif
  mem_size += size;
  ++alloc_cnt;
#endif
#ifdef MEMMAGICNUMS
  ptr->prefix_magicnumber = MAGIC_PREFIX;
  postfixp(memblkp(ptr), size)->postfix_magicnumber = MAGIC_POSTFIX;
#endif

  Debug((DEBUG_DEBUG, "RunMalloc(%u) = %p", size, memblkp(ptr)));

  return memblkp(ptr);
}

#ifdef MEMLEAKSTATS
void *RunCalloc_memleak(size_t nmemb, size_t size,
    int line, const char *filename)
#else
void *RunCalloc(size_t nmemb, size_t size)
#endif
{
  void *ptr;
  size *= nmemb;
#ifdef MEMLEAKSTATS
  if ((ptr = RunMalloc_memleak(size, line, filename)))
#else
  if ((ptr = RunMalloc(size)))
#endif
    memset(ptr, 0, size);
  return ptr;
}

int RunFree_test(void *memblk_ptr)
{
  register prefix_blk_st *prefix_ptr = prefixp(memblk_ptr);
  register hash_entry_st *hash_entry;
  for (hash_entry = hashtable[MallocHash(prefix_ptr)];
      hash_entry && hash_entry->ptr != prefix_ptr;
      hash_entry = hash_entry->next);
  return hash_entry ? 1 : 0;
}

void RunFree(void *memblk_ptr)
{
  register prefix_blk_st *prefix_ptr = prefixp(memblk_ptr);
  register hash_entry_st *hash_entry, *prev_hash_entry = NULL;
  unsigned int hash = MallocHash(prefix_ptr);

  Debug((DEBUG_DEBUG, "RunFree(%p)", memblk_ptr));

  if (!memblk_ptr)
    return;

  for (hash_entry = hashtable[hash];
      hash_entry && hash_entry->ptr != prefix_ptr;
      prev_hash_entry = hash_entry, hash_entry = hash_entry->next);
  if (!hash_entry)
  {
    Debug((DEBUG_FATAL, "FREEING NON MALLOC PTR !!!"));
    MyCoreDump;
  }
#ifdef MEMMAGICNUMS
  if (prefix_ptr->prefix_magicnumber != MAGIC_PREFIX)
  {
    Debug((DEBUG_FATAL, "MAGIC_PREFIX CORRUPT !"));
    MyCoreDump;
  }
  prefix_ptr->prefix_magicnumber = 12345678;
  if (postfixp(memblk_ptr, hash_entry->size)->postfix_magicnumber
      != MAGIC_POSTFIX)
  {
    Debug((DEBUG_FATAL, "MAGIC_POSTFIX CORRUPT !"));
    MyCoreDump;
  }
  postfixp(memblk_ptr, hash_entry->size)->postfix_magicnumber = 87654321;
#endif

  if (prev_hash_entry)
    prev_hash_entry->next = hash_entry->next;
  else
    hashtable[hash] = hash_entry->next;

#ifdef MEMLEAKSTATS
  location[hash_entry->location].number_of_allocations--;
#endif

#ifdef MEMSIZESTATS
  mem_size -= hash_entry->size;
  --alloc_cnt;
#ifdef MEMLEAKSTATS
  location[hash_entry->location].size -= hash_entry->size;
#endif
#ifdef DEBUGMODE
  /* Put 0xfefefefe.. in freed memory */
#ifndef memset
  memset(prefix_ptr, 0xfe, hash_entry->size + SIZEOF_PREFIX);
#else
  {
    register char *p = prefix_ptr;
    size_t len = hash_entry->size + SIZEOF_PREFIX;
    for (; len; --len)
      *p++ = 0xfe;
  }
#endif
#endif
#endif

  free(hash_entry);
  free(prefix_ptr);
}

#ifdef MEMLEAKSTATS
void *RunRealloc_memleak(void *memblk_ptr, size_t size,
    int line, const char *filename)
#else
void *RunRealloc(void *memblk_ptr, size_t size)
#endif
{
  register prefix_blk_st *ptr;
  register prefix_blk_st *prefix_ptr = prefixp(memblk_ptr);
  register hash_entry_st *hash_entry, *prev_hash_entry = NULL;
  register hash_entry_st **hashtablep;
  unsigned int hash;

  if (!memblk_ptr)
#ifdef MEMLEAKSTATS
    return RunMalloc_memleak(size, line, filename);
#else
    return RunMalloc(size);
#endif
  if (!size)
  {
    RunFree(memblk_ptr);
    return NULL;
  }

  for (hash_entry = hashtable[(hash = MallocHash(prefix_ptr))];
      hash_entry && hash_entry->ptr != prefix_ptr;
      prev_hash_entry = hash_entry, hash_entry = hash_entry->next);
  if (!hash_entry)
  {
    Debug((DEBUG_FATAL, "REALLOCATING NON MALLOC PTR !!!"));
    MyCoreDump;
  }

#ifdef MEMMAGICNUMS
  if (prefix_ptr->prefix_magicnumber != MAGIC_PREFIX)
  {
    Debug((DEBUG_FATAL, "MAGIC_PREFIX CORRUPT !"));
    MyCoreDump;
  }
  if (postfixp(memblk_ptr, hash_entry->size)->postfix_magicnumber
      != MAGIC_POSTFIX)
  {
    Debug((DEBUG_FATAL, "MAGIC_POSTFIX CORRUPT !"));
    MyCoreDump;
  }
#endif

#ifdef HAS_POSTFIX
  size += 3;
  size &= ~3;
#endif

#ifdef MEMMAGICNUMS
  postfixp(memblkp(prefix_ptr), hash_entry->size)->postfix_magicnumber = 123456;
#endif
#ifdef MEMLEAKSTATS
  location[hash_entry->location].number_of_allocations--;
#ifdef MEMSIZESTATS
  location[hash_entry->location].size -= hash_entry->size;
#endif
#endif

  if (!(ptr =
      (prefix_blk_st *) realloc(prefix_ptr,
      SIZEOF_PREFIX + size + SIZEOF_POSTFIX)))
  {
    Debug((DEBUG_FATAL, "RunRealloc: Out of memory :"));
    return NULL;
  }

  if (prev_hash_entry)
    prev_hash_entry->next = hash_entry->next;
  else
    hashtable[hash] = hash_entry->next;

  hashtablep = &hashtable[MallocHash(ptr)];
  hash_entry->next = *hashtablep;
  *hashtablep = hash_entry;
  hash_entry->ptr = ptr;
#ifdef MEMLEAKSTATS
#ifdef MEMTIMESTATS
  hash_entry->when = now;
#endif
  location[(hash_entry->location =
      find_location(filename, line))].number_of_allocations++;
#endif
#ifdef MEMSIZESTATS
  mem_size += size - hash_entry->size;
  hash_entry->size = size;
#ifdef MEMLEAKSTATS
  location[hash_entry->location].size += size;
#endif
#endif
#ifdef MEMMAGICNUMS
  postfixp(memblkp(ptr), size)->postfix_magicnumber = MAGIC_POSTFIX;
#endif

  Debug((DEBUG_DEBUG, ": RunRealloc(%p, %u) = %p",
      memblk_ptr, size, memblkp(ptr)));

  return memblkp(ptr);
}

#ifdef MEMSIZESTATS
unsigned int get_alloc_cnt(void)
{
  return alloc_cnt;
}

size_t get_mem_size(void)
{
  return mem_size;
}
#endif

#endif /* DEBUGMALLOC */