1 /* Standard debugging hooks for `malloc'.
2 Copyright 1990 Free Software Foundation
3 Written May 1989 by Mike Haertel.
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. */
23 #define size_t unsigned int
29 /* Old hook values. */
30 static void EXFUN((*old_free_hook), (PTR ptr));
31 static PTR EXFUN((*old_malloc_hook), (size_t size));
32 static PTR EXFUN((*old_realloc_hook), (PTR ptr, size_t size));
35 /* Function to call when something awful happens. */
37 static void EXFUN((*abortfunc), (void)) = (void (*)()) abort;
39 /* Arbitrary magical numbers. */
40 #define MAGICWORD 0xfedabeeb
41 #define MAGICBYTE ((char) 0xd7)
45 size_t size; /* Exact size requested by user. */
46 unsigned int magic; /* Magic number to check header integrity. */
50 DEFUN(checkhdr, (hdr), CONST struct hdr *hdr)
52 if (hdr->magic != MAGICWORD || ((char *) &hdr[1])[hdr->size] != MAGICBYTE)
57 DEFUN(freehook, (ptr), PTR ptr)
59 struct hdr *hdr = ((struct hdr *) ptr) - 1;
62 __free_hook = old_free_hook;
64 __free_hook = freehook;
68 DEFUN(mallochook, (size), size_t size)
72 __malloc_hook = old_malloc_hook;
73 hdr = (struct hdr *) malloc(sizeof(struct hdr) + size + 1);
74 __malloc_hook = mallochook;
79 hdr->magic = MAGICWORD;
80 ((char *) &hdr[1])[size] = MAGICBYTE;
81 return (PTR) (hdr + 1);
85 DEFUN(reallochook, (ptr, size), PTR ptr AND size_t size)
87 struct hdr *hdr = ((struct hdr *) ptr) - 1;
90 __free_hook = old_free_hook;
91 __malloc_hook = old_malloc_hook;
92 __realloc_hook = old_realloc_hook;
93 hdr = (struct hdr *) realloc((PTR) hdr, sizeof(struct hdr) + size + 1);
94 __free_hook = freehook;
95 __malloc_hook = mallochook;
96 __realloc_hook = reallochook;
101 ((char *) &hdr[1])[size] = MAGICBYTE;
102 return (PTR) (hdr + 1);
106 DEFUN(mcheck, (func), void EXFUN((*func), (void)))
108 static int mcheck_used = 0;
113 /* These hooks may not be safely inserted if malloc is already in use. */
114 if (!__malloc_initialized && !mcheck_used)
116 old_free_hook = __free_hook;
117 __free_hook = freehook;
118 old_malloc_hook = __malloc_hook;
119 __malloc_hook = mallochook;
120 old_realloc_hook = __realloc_hook;
121 __realloc_hook = reallochook;