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