File: leakfinder.c

package info (click to toggle)
squid 2.6.5-6etch5
  • links: PTS
  • area: main
  • in suites: etch
  • size: 12,540 kB
  • ctags: 13,801
  • sloc: ansic: 105,278; sh: 6,083; makefile: 1,297; perl: 1,245; awk: 40
file content (140 lines) | stat: -rw-r--r-- 3,596 bytes parent folder | download | duplicates (6)
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

/*
 * $Id: leakfinder.c,v 1.7 2005/05/17 16:56:38 hno Exp $
 *
 * DEBUG: section 45    Callback Data Registry
 * AUTHOR: Duane Wessels
 *
 * SQUID Web Proxy Cache          http://www.squid-cache.org/
 * ----------------------------------------------------------
 *
 *  Squid is the result of efforts by numerous individuals from
 *  the Internet community; see the CONTRIBUTORS file for full
 *  details.   Many organizations have provided support for Squid's
 *  development; see the SPONSORS file for full details.  Squid is
 *  Copyrighted (C) 2001 by the Regents of the University of
 *  California; see the COPYRIGHT file for full details.  Squid
 *  incorporates software developed and/or copyrighted by other
 *  sources; see the CREDITS file for full details.
 *
 *  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, USA.
 *
 */

/*
 * Use these to find memory leaks
 */

#include "squid.h"

static hash_table *htable = NULL;

static int leakCount = 0;

typedef struct _ptr {
    hash_link hash;		/* must be first */
    void *key;
    struct _ptr *next;
    const char *file;
    int line;
    time_t when;
} ptr;

static HASHCMP ptr_cmp;
static HASHHASH ptr_hash;
static OBJH ptrDump;

/* ========================================================================= */

void
leakInit(void)
{
    debug(45, 3) ("ptrInit\n");
    htable = hash_create(ptr_cmp, 1 << 8, ptr_hash);
    cachemgrRegister("leaks",
	"Memory Leak Tracking",
	ptrDump, 0, 1);
}

void *
leakAddFL(void *p, const char *file, int line)
{
    ptr *c;
    assert(p);
    assert(htable != NULL);
    assert(hash_lookup(htable, p) == NULL);
    c = xcalloc(1, sizeof(*c));
    c->key = p;
    c->file = file;
    c->line = line;
    c->when = squid_curtime;
    hash_join(htable, &c->hash);
    leakCount++;
    return p;
}

void *
leakTouchFL(void *p, const char *file, int line)
{
    ptr *c = (ptr *) hash_lookup(htable, p);
    assert(p);
    assert(htable != NULL);
    assert(c);
    c->file = file;
    c->line = line;
    c->when = squid_curtime;
    return p;
}

void *
leakFreeFL(void *p, const char *file, int line)
{
    ptr *c = (ptr *) hash_lookup(htable, p);
    assert(p);
    assert(c != NULL);
    hash_remove_link(htable, (hash_link *) c);
    leakCount--;
    xfree(c);
    return p;
}

/* ========================================================================= */

static int
ptr_cmp(const void *p1, const void *p2)
{
    return (char *) p1 - (char *) p2;
}

static unsigned int
ptr_hash(const void *p, unsigned int mod)
{
    return ((unsigned long) p >> 8) % mod;
}


static void
ptrDump(StoreEntry * sentry)
{
    hash_link *hptr;
    ptr *c;
    storeAppendPrintf(sentry, "Tracking %d pointers\n", leakCount);
    hash_first(htable);
    while ((hptr = hash_next(htable))) {
	c = (ptr *) hptr;
	storeAppendPrintf(sentry, "%20p last used %9d seconds ago by %s:%d\n",
	    c->key, squid_curtime - c->when, c->file, c->line);
    }
}