1 /* Standard debugging hooks for `malloc'.
2 Copyright 1990, 1991, 1992, 1993 Free Software Foundation
3 Written May 1989 by Mike Haertel.
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; see the file COPYING.LIB. If
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, USA.
20 The author may be reached (Email) at the address mike@ai.mit.edu,
21 or (US mail) as Mike Haertel c/o Free Software Foundation. */
23 #ifndef _MALLOC_INTERNAL
24 #define _MALLOC_INTERNAL
28 /* Old hook values. */
29 static void (*old_free_hook
) __P ((__ptr_t ptr
));
30 static __ptr_t (*old_malloc_hook
) __P ((size_t size
));
31 static __ptr_t (*old_realloc_hook
) __P ((__ptr_t ptr
, size_t size
));
33 /* Function to call when something awful happens. */
34 static void (*abortfunc
) __P ((void));
36 /* Arbitrary magical numbers. */
37 #define MAGICWORD 0xfedabeeb
38 #define MAGICBYTE ((char) 0xd7)
42 size_t size
; /* Exact size requested by user. */
43 unsigned long int magic
; /* Magic number to check header integrity. */
46 static void checkhdr
__P ((const struct hdr
*));
49 const struct hdr
*hdr
;
51 if (hdr
->magic
!= MAGICWORD
|| ((char *) &hdr
[1])[hdr
->size
] != MAGICBYTE
)
55 static void freehook
__P ((__ptr_t
));
60 struct hdr
*hdr
= ((struct hdr
*) ptr
) - 1;
63 __free_hook
= old_free_hook
;
65 __free_hook
= freehook
;
68 static __ptr_t mallochook
__P ((size_t));
75 __malloc_hook
= old_malloc_hook
;
76 hdr
= (struct hdr
*) malloc (sizeof (struct hdr
) + size
+ 1);
77 __malloc_hook
= mallochook
;
82 hdr
->magic
= MAGICWORD
;
83 ((char *) &hdr
[1])[size
] = MAGICBYTE
;
84 return (__ptr_t
) (hdr
+ 1);
87 static __ptr_t reallochook
__P ((__ptr_t
, size_t));
89 reallochook (ptr
, size
)
93 struct hdr
*hdr
= ((struct hdr
*) ptr
) - 1;
96 __free_hook
= old_free_hook
;
97 __malloc_hook
= old_malloc_hook
;
98 __realloc_hook
= old_realloc_hook
;
99 hdr
= (struct hdr
*) realloc ((__ptr_t
) hdr
, sizeof (struct hdr
) + size
+ 1);
100 __free_hook
= freehook
;
101 __malloc_hook
= mallochook
;
102 __realloc_hook
= reallochook
;
107 ((char *) &hdr
[1])[size
] = MAGICBYTE
;
108 return (__ptr_t
) (hdr
+ 1);
113 void (*func
) __P ((void));
115 extern void abort
__P ((void));
116 static int mcheck_used
= 0;
118 abortfunc
= (func
!= NULL
) ? func
: abort
;
120 /* These hooks may not be safely inserted if malloc is already in use. */
121 if (!__malloc_initialized
&& !mcheck_used
)
123 old_free_hook
= __free_hook
;
124 __free_hook
= freehook
;
125 old_malloc_hook
= __malloc_hook
;
126 __malloc_hook
= mallochook
;
127 old_realloc_hook
= __realloc_hook
;
128 __realloc_hook
= reallochook
;
132 return mcheck_used
? 0 : -1;