]> git.ipfire.org Git - thirdparty/squid.git/blob - src/LeakFinder.cc
Merged from trunk 13172.
[thirdparty/squid.git] / src / LeakFinder.cc
1 /*
2 * DEBUG: section 45 Callback Data Registry
3 * AUTHOR: Duane Wessels
4 *
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
7 *
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
30 *
31 */
32
33 /*
34 * Use these to find memory leaks
35 */
36
37 #include "squid.h"
38
39 #if USE_LEAKFINDER
40
41 #include "LeakFinder.h"
42 #include "SquidTime.h"
43 #include "Store.h"
44
45 /* ========================================================================= */
46
47 LeakFinderPtr::LeakFinderPtr(void *p , const char *f, const int l) :
48 file(f),
49 line(l),
50 when(squid_curtime)
51 {
52 // XXX: these bits should be done by hash_link()
53 key = p;
54 next = NULL;
55 }
56
57 /* ========================================================================= */
58
59 LeakFinder::LeakFinder() :
60 count(0),
61 last_dump(0)
62 {
63 debugs(45, 3, "LeakFinder constructed");
64 table = hash_create(cmp, 1 << 8, hash);
65 #if 0
66 /* if this is desired to reinstate, add a
67 * RegisterWithCacheManager method
68 */
69 cachemgrRegister("leaks",
70 "Memory Leak Tracking",
71 cachemgr_dump, 0, 1);
72 #endif
73 }
74
75 void *
76 LeakFinder::addSome(void *p, const char *file, int line)
77 {
78 assert(hash_lookup(table, p) == NULL);
79 LeakFinderPtr *c = new LeakFinderPtr(p, file, line);
80 hash_join(table, c);
81 ++count;
82 return p;
83 }
84
85 void *
86 LeakFinder::touch(void *p, const char *file, int line)
87 {
88 assert(p);
89 LeakFinderPtr *c = (LeakFinderPtr *) hash_lookup(table, p);
90 assert(c);
91 c->file = file;
92 c->line = line;
93 c->when = squid_curtime;
94 return p;
95 }
96
97 void *
98 LeakFinder::freeSome(void *p, const char *file, int line)
99 {
100 assert(p);
101 LeakFinderPtr *c = (LeakFinderPtr *) hash_lookup(table, p);
102 assert(c);
103 hash_remove_link(table, c);
104 --count;
105 delete c;
106 dump();
107 return p;
108 }
109
110 /* ========================================================================= */
111
112 int
113 LeakFinder::cmp(const void *p1, const void *p2)
114 {
115 return (char *) p1 - (char *) p2;
116 }
117
118 unsigned int
119 LeakFinder::hash(const void *p, unsigned int mod)
120 {
121 return ((unsigned long) p >> 8) % mod;
122 }
123
124 void
125 LeakFinder::dump()
126 {
127 if (0 == count)
128 return;
129
130 if (squid_curtime == last_dump)
131 return;
132
133 last_dump = squid_curtime;
134
135 debugs(45, DBG_IMPORTANT, "Tracking " << count << " pointers");
136
137 hash_first(table);
138
139 LeakFinderPtr *c;
140
141 while ((c = (LeakFinderPtr *)hash_next(table))) {
142 debugs(45, DBG_IMPORTANT, std::setw(20) << c->key << " last used " << std::setw(9) << (squid_curtime - c->when) <<
143 " seconds ago by " << c->file << ":" << c->line);
144 }
145 }
146
147 #endif /* USE_LEAKFINDER */