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