]> git.ipfire.org Git - thirdparty/squid.git/blame - src/LeakFinder.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / LeakFinder.cc
CommitLineData
5d620373 1/*
bde978a6 2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
5d620373 3 *
bbc27441
AJ
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
5d620373 7 */
8
bbc27441
AJ
9/* DEBUG: section 45 Callback Data Registry */
10
5d620373 11/*
12 * Use these to find memory leaks
13 */
14
582c2af2 15#include "squid.h"
3d41e53a
FC
16
17#if USE_LEAKFINDER
18
ba126bee 19#include "LeakFinder.h"
a4452e04 20#include "SquidTime.h"
602d9612 21#include "Store.h"
5d620373 22
ba126bee 23/* ========================================================================= */
5d620373 24
ba126bee 25LeakFinderPtr::LeakFinderPtr(void *p , const char *f, const int l) :
f53969cc
SM
26 file(f),
27 line(l),
28 when(squid_curtime)
62e76326 29{
e110d013 30 // XXX: these bits should be done by hash_link()
ba126bee 31 key = p;
32 next = NULL;
62e76326 33}
34
5d620373 35/* ========================================================================= */
36
e110d013 37LeakFinder::LeakFinder() :
f53969cc
SM
38 count(0),
39 last_dump(0)
5d620373 40{
bf8fe701 41 debugs(45, 3, "LeakFinder constructed");
ba126bee 42 table = hash_create(cmp, 1 << 8, hash);
43#if 0
62ee09ca 44 /* if this is desired to reinstate, add a
45 * RegisterWithCacheManager method
46 */
5d620373 47 cachemgrRegister("leaks",
62e76326 48 "Memory Leak Tracking",
ba126bee 49 cachemgr_dump, 0, 1);
50#endif
5d620373 51}
52
53void *
c38a2377 54LeakFinder::addSome(void *p, const char *file, int line)
5d620373 55{
ba126bee 56 assert(hash_lookup(table, p) == NULL);
57 LeakFinderPtr *c = new LeakFinderPtr(p, file, line);
58 hash_join(table, c);
95dc7ff4 59 ++count;
5d620373 60 return p;
61}
62
63void *
ba126bee 64LeakFinder::touch(void *p, const char *file, int line)
5d620373 65{
5d620373 66 assert(p);
ba126bee 67 LeakFinderPtr *c = (LeakFinderPtr *) hash_lookup(table, p);
5d620373 68 assert(c);
69 c->file = file;
70 c->line = line;
71 c->when = squid_curtime;
72 return p;
73}
74
75void *
c38a2377 76LeakFinder::freeSome(void *p, const char *file, int line)
5d620373 77{
5d620373 78 assert(p);
ba126bee 79 LeakFinderPtr *c = (LeakFinderPtr *) hash_lookup(table, p);
80 assert(c);
81 hash_remove_link(table, c);
5e263176 82 --count;
ba126bee 83 delete c;
84 dump();
5d620373 85 return p;
86}
87
88/* ========================================================================= */
89
ba126bee 90int
91LeakFinder::cmp(const void *p1, const void *p2)
5d620373 92{
93 return (char *) p1 - (char *) p2;
94}
95
ba126bee 96unsigned int
97LeakFinder::hash(const void *p, unsigned int mod)
5d620373 98{
99 return ((unsigned long) p >> 8) % mod;
100}
101
ba126bee 102void
103LeakFinder::dump()
5d620373 104{
ba126bee 105 if (0 == count)
106 return;
107
108 if (squid_curtime == last_dump)
109 return;
110
111 last_dump = squid_curtime;
112
e0236918 113 debugs(45, DBG_IMPORTANT, "Tracking " << count << " pointers");
ba126bee 114
115 hash_first(table);
116
117 LeakFinderPtr *c;
118
119 while ((c = (LeakFinderPtr *)hash_next(table))) {
e0236918 120 debugs(45, DBG_IMPORTANT, std::setw(20) << c->key << " last used " << std::setw(9) << (squid_curtime - c->when) <<
26ac0430 121 " seconds ago by " << c->file << ":" << c->line);
5d620373 122 }
123}
68488b4a 124
125#endif /* USE_LEAKFINDER */
f53969cc 126