1 /* Standard debugging hooks for `malloc'.
2 Copyright 1990 Free Software Foundation
3 Written May 1989 by Mike Haertel.
5 The author may be reached (Email) at the address mike@ai.mit.edu,
6 or (US mail) as Mike Haertel c/o Free Software Foundation.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
25 /* Old hook values. */
26 static void EXFUN((*old_free_hook
), (PTR ptr
));
27 static PTR
EXFUN((*old_malloc_hook
), (size_t size
));
28 static PTR
EXFUN((*old_realloc_hook
), (PTR ptr
, size_t size
));
31 /* Function to call when something awful happens. */
33 static void EXFUN((*abortfunc
), (void)) = (void (*)()) abort
;
35 /* Arbitrary magical numbers. */
36 #define MAGICWORD 0xfedabeeb
37 #define MAGICBYTE ((char) 0xd7)
41 size_t size
; /* Exact size requested by user. */
42 unsigned int magic
; /* Magic number to check header integrity. */
46 DEFUN(checkhdr
, (hdr
), CONST
struct hdr
*hdr
)
48 if (hdr
->magic
!= MAGICWORD
|| ((char *) &hdr
[1])[hdr
->size
] != MAGICBYTE
)
53 DEFUN(freehook
, (ptr
), PTR ptr
)
55 struct hdr
*hdr
= ((struct hdr
*) ptr
) - 1;
58 __free_hook
= old_free_hook
;
60 __free_hook
= freehook
;
64 DEFUN(mallochook
, (size
), size_t size
)
68 __malloc_hook
= old_malloc_hook
;
69 hdr
= (struct hdr
*) malloc(sizeof(struct hdr
) + size
+ 1);
70 __malloc_hook
= mallochook
;
75 hdr
->magic
= MAGICWORD
;
76 ((char *) &hdr
[1])[size
] = MAGICBYTE
;
77 return (PTR
) (hdr
+ 1);
81 DEFUN(reallochook
, (ptr
, size
), PTR ptr AND
size_t size
)
83 struct hdr
*hdr
= ((struct hdr
*) ptr
) - 1;
86 __free_hook
= old_free_hook
;
87 __malloc_hook
= old_malloc_hook
;
88 __realloc_hook
= old_realloc_hook
;
89 hdr
= (struct hdr
*) realloc((PTR
) hdr
, sizeof(struct hdr
) + size
+ 1);
90 __free_hook
= freehook
;
91 __malloc_hook
= mallochook
;
92 __realloc_hook
= reallochook
;
97 ((char *) &hdr
[1])[size
] = MAGICBYTE
;
98 return (PTR
) (hdr
+ 1);
102 DEFUN(mcheck
, (func
), void EXFUN((*func
), (void)))
104 static int mcheck_used
= 0;
109 /* These hooks may not be safely inserted if malloc is already in use. */
110 if (!__malloc_initialized
&& !mcheck_used
)
112 old_free_hook
= __free_hook
;
113 __free_hook
= freehook
;
114 old_malloc_hook
= __malloc_hook
;
115 __malloc_hook
= mallochook
;
116 old_realloc_hook
= __realloc_hook
;
117 __realloc_hook
= reallochook
;
This page took 0.030644 seconds and 4 git commands to generate.