Logo ROOT  
Reference Guide
mmcheck.c
Go to the documentation of this file.
1/* @(#)root/clib:$Id$ */
2/* Author: */
3
4/* Standard debugging hooks for `mmalloc'.
5 Copyright 1990, 1991, 1992 Free Software Foundation
6
7 Written May 1989 by Mike Haertel.
8 Heavily modified Mar 1992 by Fred Fish (fnf@cygnus.com)
9
10The GNU C Library is free software; you can redistribute it and/or
11modify it under the terms of the GNU Library General Public License as
12published by the Free Software Foundation; either version 2 of the
13License, or (at your option) any later version.
14
15The GNU C Library is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18Library General Public License for more details.
19
20You should have received a copy of the GNU Library General Public
21License along with the GNU C Library; see the file COPYING.LIB. If
22not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23Boston, MA 02111-1307, USA.
24
25 The author may be reached (Email) at the address mike@ai.mit.edu,
26 or (US mail) as Mike Haertel c/o Free Software Foundation. */
27
28#include "mmprivate.h"
29
30/* Default function to call when something awful happens. The application
31 can specify an alternate function to be called instead (and probably will
32 want to). */
33
34extern void abort PARAMS ((void));
35
36/* Arbitrary magical numbers. */
37
38#define MAGICWORD (unsigned int) 0xfedabeeb /* Active chunk */
39#define MAGICWORDFREE (unsigned int) 0xdeadbeef /* Inactive chunk */
40#define MAGICBYTE ((char) 0xd7)
41
42/* Each memory allocation is bounded by a header structure and a trailer
43 byte. I.E.
44
45 <size><magicword><user's allocation><magicbyte>
46
47 The pointer returned to the user points to the first byte in the
48 user's allocation area. The magic word can be tested to detect
49 buffer underruns and the magic byte can be tested to detect overruns. */
50
51struct hdr
52 {
53 size_t size; /* Exact size requested by user. */
54 unsigned long int magic; /* Magic number to check header integrity. */
55 };
56
57typedef void (*mmfree_fun_t) PARAMS ((PTR, PTR));
58typedef PTR (*mmalloc_fun_t) PARAMS ((PTR, size_t));
59typedef PTR (*mmrealloc_fun_t) PARAMS ((PTR, PTR, size_t));
60
61/* Check the magicword and magicbyte, and if either is corrupted then
62 call the emergency abort function specified for the heap in use. */
63
64static void checkhdr(struct mdesc *mdp, const struct hdr *hdr)
65{
66 if (hdr -> magic != MAGICWORD ||
67 ((char *) &hdr[1])[hdr -> size] != MAGICBYTE)
68 {
69 (*mdp -> abortfunc)();
70 }
71}
72
73static void mfree_check(PTR md, PTR ptr)
74{
75 struct hdr *hdr = ((struct hdr *) ptr) - 1;
76 struct mdesc *mdp;
77
78 mdp = MD_TO_MDP (md);
79 checkhdr (mdp, hdr);
81 mdp -> mfree_hook = NULL;
82 mfree (md, (PTR)hdr);
83 mdp -> mfree_hook = (mmfree_fun_t) mfree_check;
84}
85
86static PTR mmalloc_check(PTR md, size_t size)
87{
88 struct hdr *hdr;
89 struct mdesc *mdp;
90 size_t nbytes;
91
92 mdp = MD_TO_MDP (md);
93 mdp -> mmalloc_hook = NULL;
94 nbytes = sizeof (struct hdr) + size + 1;
95 hdr = (struct hdr *) mmalloc (md, nbytes);
96 mdp -> mmalloc_hook = (mmalloc_fun_t) mmalloc_check;
97 if (hdr != NULL)
98 {
99 hdr -> size = size;
100 hdr -> magic = MAGICWORD;
101 hdr++;
102 *((char *) hdr + size) = MAGICBYTE;
103 }
104 return ((PTR) hdr);
105}
106
107static PTR mrealloc_check(PTR md, PTR ptr, size_t size)
108{
109 struct hdr *hdr = ((struct hdr *) ptr) - 1;
110 struct mdesc *mdp;
111 size_t nbytes;
112
113 mdp = MD_TO_MDP (md);
114 checkhdr (mdp, hdr);
115 mdp -> mfree_hook = NULL;
116 mdp -> mmalloc_hook = NULL;
117 mdp -> mrealloc_hook = NULL;
118 nbytes = sizeof (struct hdr) + size + 1;
119 hdr = (struct hdr *) mrealloc (md, (PTR) hdr, nbytes);
120 mdp -> mfree_hook = (mmfree_fun_t) mfree_check;
121 mdp -> mmalloc_hook = (mmalloc_fun_t) mmalloc_check;
122 mdp -> mrealloc_hook = (mmrealloc_fun_t) mrealloc_check;
123 if (hdr != NULL)
124 {
125 hdr -> size = size;
126 hdr++;
127 *((char *) hdr + size) = MAGICBYTE;
128 }
129 return ((PTR) hdr);
130}
131
132/* Turn on default checking for mmalloc/mrealloc/mfree, for the heap specified
133 by MD. If FUNC is non-NULL, it is a pointer to the function to call
134 to abort whenever memory corruption is detected. By default, this is the
135 standard library function abort().
136
137 Note that we disallow installation of initial checking hooks if mmalloc
138 has been called at any time for this particular heap, since if any region
139 that is allocated prior to installation of the hooks is subsequently
140 reallocated or freed after installation of the hooks, it is guaranteed
141 to trigger a memory corruption error. We do this by checking the state
142 of the MMALLOC_INITIALIZED flag.
143
144 However, we can call this function at any time after the initial call,
145 to update the function pointers to the checking routines and to the
146 user defined corruption handler routine, as long as these function pointers
147 have been previously extablished by the initial call. Note that we
148 do this automatically when remapping an previously used heap, to ensure
149 that the hooks get updated to the correct values, although the corruption
150 handler pointer gets set back to the default. The application can then
151 call mmcheck to use a different corruption handler if desired.
152
153 Returns non-zero if checking is successfully enabled, zero otherwise. */
154
155int mmcheck(PTR md, void(*func) PARAMS((void)))
156{
157 struct mdesc *mdp;
158 int rtnval;
159
160 mdp = MD_TO_MDP (md);
161
162 /* We can safely set or update the abort function at any time, regardless
163 of whether or not we successfully do anything else. */
164
165 mdp -> abortfunc = (func != NULL ? func : abort);
166
167 /* If we haven't yet called mmalloc the first time for this heap, or if we
168 have hooks that were previously installed, then allow the hooks to be
169 initialized or updated. */
170
171 if (1 /* FIXME: Always allow installation for now. */ ||
172 !(mdp -> flags & MMALLOC_INITIALIZED) ||
173 (mdp -> mfree_hook != NULL))
174 {
175 mdp -> mfree_hook = (mmfree_fun_t) mfree_check;
176 mdp -> mmalloc_hook = (mmalloc_fun_t) mmalloc_check;
177 mdp -> mrealloc_hook = (mmrealloc_fun_t) mrealloc_check;
178 mdp -> flags |= MMALLOC_MMCHECK_USED;
179 rtnval = 1;
180 }
181 else
182 {
183 rtnval = 0;
184 }
185
186 return (rtnval);
187}
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
#define void
Definition Tailor.h:179
#define NULL
Definition ZInflate.c:15
void mfree(PTR md, PTR ptr)
Definition mfree.c:206
PTR mmalloc(PTR md, size_t size)
Definition mmalloc.c:125
#define PTR
Definition mmalloc.h:29
static void mfree_check(PTR md, PTR ptr)
Definition mmcheck.c:73
static PTR mmalloc_check(PTR md, size_t size)
Definition mmcheck.c:86
int mmcheck(PTR md, void(*func) PARAMS((void)))
Definition mmcheck.c:155
static PTR mrealloc_check(PTR md, PTR ptr, size_t size)
Definition mmcheck.c:107
#define MAGICWORDFREE
Definition mmcheck.c:39
#define MAGICWORD
Definition mmcheck.c:38
void mmfree_fun_t PARAMS((PTR, PTR))
Definition mmcheck.c:57
static void checkhdr(struct mdesc *mdp, const struct hdr *hdr)
Definition mmcheck.c:64
#define MAGICBYTE
Definition mmcheck.c:40
#define MMALLOC_MMCHECK_USED
Definition mmprivate.h:323
#define MD_TO_MDP(md)
Definition mmprivate.h:375
#define MMALLOC_INITIALIZED
Definition mmprivate.h:322
PTR mrealloc(PTR md, PTR ptr, size_t size)
Definition mrealloc.c:37
Definition mmcheck.c:52
size_t size
Definition mmcheck.c:53
unsigned long int magic
Definition mmcheck.c:54
char magic[MMALLOC_MAGIC_SIZE]
Definition mmprivate.h:182
unsigned int flags
Definition mmprivate.h:195