]>
Commit | Line | Data |
---|---|---|
5d620373 | 1 | /* |
5d620373 | 2 | * DEBUG: section 45 Callback Data Registry |
3 | * AUTHOR: Duane Wessels | |
4 | * | |
2b6662ba | 5 | * SQUID Web Proxy Cache http://www.squid-cache.org/ |
5d620373 | 6 | * ---------------------------------------------------------- |
7 | * | |
2b6662ba | 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. | |
5d620373 | 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. | |
26ac0430 | 21 | * |
5d620373 | 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. | |
26ac0430 | 26 | * |
5d620373 | 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 | ||
582c2af2 | 37 | #include "squid.h" |
3d41e53a FC |
38 | |
39 | #if USE_LEAKFINDER | |
40 | ||
ba126bee | 41 | #include "LeakFinder.h" |
e6ccf245 | 42 | #include "Store.h" |
a4452e04 | 43 | #include "SquidTime.h" |
5d620373 | 44 | |
ba126bee | 45 | /* ========================================================================= */ |
5d620373 | 46 | |
ba126bee | 47 | LeakFinderPtr::LeakFinderPtr(void *p , const char *f, const int l) : |
48 | file(f), line(l), when(squid_curtime) | |
62e76326 | 49 | { |
ba126bee | 50 | key = p; |
51 | next = NULL; | |
62e76326 | 52 | } |
53 | ||
5d620373 | 54 | /* ========================================================================= */ |
55 | ||
ba126bee | 56 | LeakFinder::LeakFinder() |
5d620373 | 57 | { |
bf8fe701 | 58 | debugs(45, 3, "LeakFinder constructed"); |
ba126bee | 59 | table = hash_create(cmp, 1 << 8, hash); |
60 | #if 0 | |
62ee09ca | 61 | /* if this is desired to reinstate, add a |
62 | * RegisterWithCacheManager method | |
63 | */ | |
5d620373 | 64 | cachemgrRegister("leaks", |
62e76326 | 65 | "Memory Leak Tracking", |
ba126bee | 66 | cachemgr_dump, 0, 1); |
67 | #endif | |
5d620373 | 68 | } |
69 | ||
70 | void * | |
c38a2377 | 71 | LeakFinder::addSome(void *p, const char *file, int line) |
5d620373 | 72 | { |
ba126bee | 73 | assert(hash_lookup(table, p) == NULL); |
74 | LeakFinderPtr *c = new LeakFinderPtr(p, file, line); | |
75 | hash_join(table, c); | |
95dc7ff4 | 76 | ++count; |
5d620373 | 77 | return p; |
78 | } | |
79 | ||
80 | void * | |
ba126bee | 81 | LeakFinder::touch(void *p, const char *file, int line) |
5d620373 | 82 | { |
5d620373 | 83 | assert(p); |
ba126bee | 84 | LeakFinderPtr *c = (LeakFinderPtr *) hash_lookup(table, p); |
5d620373 | 85 | assert(c); |
86 | c->file = file; | |
87 | c->line = line; | |
88 | c->when = squid_curtime; | |
89 | return p; | |
90 | } | |
91 | ||
92 | void * | |
c38a2377 | 93 | LeakFinder::freeSome(void *p, const char *file, int line) |
5d620373 | 94 | { |
5d620373 | 95 | assert(p); |
ba126bee | 96 | LeakFinderPtr *c = (LeakFinderPtr *) hash_lookup(table, p); |
97 | assert(c); | |
98 | hash_remove_link(table, c); | |
5e263176 | 99 | --count; |
ba126bee | 100 | delete c; |
101 | dump(); | |
5d620373 | 102 | return p; |
103 | } | |
104 | ||
105 | /* ========================================================================= */ | |
106 | ||
ba126bee | 107 | int |
108 | LeakFinder::cmp(const void *p1, const void *p2) | |
5d620373 | 109 | { |
110 | return (char *) p1 - (char *) p2; | |
111 | } | |
112 | ||
ba126bee | 113 | unsigned int |
114 | LeakFinder::hash(const void *p, unsigned int mod) | |
5d620373 | 115 | { |
116 | return ((unsigned long) p >> 8) % mod; | |
117 | } | |
118 | ||
ba126bee | 119 | void |
120 | LeakFinder::dump() | |
5d620373 | 121 | { |
ba126bee | 122 | if (0 == count) |
123 | return; | |
124 | ||
125 | if (squid_curtime == last_dump) | |
126 | return; | |
127 | ||
128 | last_dump = squid_curtime; | |
129 | ||
e0236918 | 130 | debugs(45, DBG_IMPORTANT, "Tracking " << count << " pointers"); |
ba126bee | 131 | |
132 | hash_first(table); | |
133 | ||
134 | LeakFinderPtr *c; | |
135 | ||
136 | while ((c = (LeakFinderPtr *)hash_next(table))) { | |
e0236918 | 137 | debugs(45, DBG_IMPORTANT, std::setw(20) << c->key << " last used " << std::setw(9) << (squid_curtime - c->when) << |
26ac0430 | 138 | " seconds ago by " << c->file << ":" << c->line); |
5d620373 | 139 | } |
140 | } | |
68488b4a | 141 | |
142 | #endif /* USE_LEAKFINDER */ |