1 /* watch.c - watchpoint functions for malloc */
3 /* Copyright (C) 2001-2003 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
35 static PTR_T _malloc_watch_list[WATCH_MAX];
38 watch_warn (addr, file, line, type, data)
48 else if (type == W_FREE)
50 else if (type == W_REALLOC)
51 tag = "requesting resize";
52 else if (type == W_RESIZED)
55 tag = "bug: unknown operation";
57 fprintf (stderr, "malloc: watch alert: %p %s ", addr, tag);
58 if (data != (unsigned long)-1)
59 fprintf (stderr, "(size %lu) ", data);
60 fprintf (stderr, "from '%s:%d'\n", file ? file : "unknown", line);
64 _malloc_ckwatch (addr, file, line, type, data)
72 for (i = _malloc_nwatch - 1; i >= 0; i--)
74 if (_malloc_watch_list[i] == addr)
76 watch_warn (addr, file, line, type, data);
81 #endif /* MALLOC_WATCH */
95 for (i = _malloc_nwatch - 1; i >= 0; i--)
97 if (_malloc_watch_list[i] == addr)
102 if (_malloc_nwatch == WATCH_MAX) /* full, take out first */
104 ret = _malloc_watch_list[0];
106 for (i = 0; i < _malloc_nwatch; i++)
107 _malloc_watch_list[i] = _malloc_watch_list[i+1];
109 _malloc_watch_list[_malloc_nwatch++] = addr;
116 /* Remove a watchpoint set on ADDR. If ADDR is NULL, remove all
117 watchpoints. Returns ADDR if everything went OK, NULL if ADDR was
118 not being watched. */
120 malloc_unwatch (addr)
128 for (i = 0; i < _malloc_nwatch; i++)
129 _malloc_watch_list[i] = (PTR_T)0;
135 for (i = 0; i < _malloc_nwatch; i++)
137 if (_malloc_watch_list[i] == addr)
140 if (i == _malloc_nwatch)
141 return ((PTR_T)0); /* not found */
142 /* shuffle everything from i+1 to end down 1 */
144 for ( ; i < _malloc_nwatch; i++)
145 _malloc_watch_list[i] = _malloc_watch_list[i+1];