]>
Commit | Line | Data |
---|---|---|
dd3b648e RP |
1 | /* Standard debugging hooks for `malloc'. |
2 | Copyright 1990 Free Software Foundation | |
3 | Written May 1989 by Mike Haertel. | |
4 | ||
99a7de40 JG |
5 | The author may be reached (Email) at the address [email protected], |
6 | or (US mail) as Mike Haertel c/o Free Software Foundation. | |
dd3b648e | 7 | |
99a7de40 JG |
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. | |
dd3b648e | 12 | |
99a7de40 JG |
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. | |
dd3b648e | 17 | |
99a7de40 JG |
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. */ | |
dd3b648e RP |
21 | |
22 | #include "ansidecl.h" | |
dd3b648e RP |
23 | #include "gmalloc.h" |
24 | ||
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)); | |
29 | ||
9fa28378 | 30 | |
dd3b648e | 31 | /* Function to call when something awful happens. */ |
9e7f3b6a SG |
32 | extern void abort(); |
33 | static void EXFUN((*abortfunc), (void)) = (void (*)()) abort; | |
dd3b648e RP |
34 | |
35 | /* Arbitrary magical numbers. */ | |
36 | #define MAGICWORD 0xfedabeeb | |
37 | #define MAGICBYTE ((char) 0xd7) | |
38 | ||
39 | struct hdr | |
40 | { | |
41 | size_t size; /* Exact size requested by user. */ | |
42 | unsigned int magic; /* Magic number to check header integrity. */ | |
43 | }; | |
44 | ||
9e7f3b6a | 45 | static void |
dd3b648e RP |
46 | DEFUN(checkhdr, (hdr), CONST struct hdr *hdr) |
47 | { | |
48 | if (hdr->magic != MAGICWORD || ((char *) &hdr[1])[hdr->size] != MAGICBYTE) | |
49 | (*abortfunc)(); | |
50 | } | |
51 | ||
52 | static void | |
53 | DEFUN(freehook, (ptr), PTR ptr) | |
54 | { | |
55 | struct hdr *hdr = ((struct hdr *) ptr) - 1; | |
56 | checkhdr(hdr); | |
57 | hdr->magic = 0; | |
58 | __free_hook = old_free_hook; | |
59 | free(hdr); | |
60 | __free_hook = freehook; | |
61 | } | |
62 | ||
63 | static PTR | |
64 | DEFUN(mallochook, (size), size_t size) | |
65 | { | |
66 | struct hdr *hdr; | |
67 | ||
68 | __malloc_hook = old_malloc_hook; | |
69 | hdr = (struct hdr *) malloc(sizeof(struct hdr) + size + 1); | |
70 | __malloc_hook = mallochook; | |
71 | if (hdr == NULL) | |
72 | return NULL; | |
73 | ||
74 | hdr->size = size; | |
75 | hdr->magic = MAGICWORD; | |
76 | ((char *) &hdr[1])[size] = MAGICBYTE; | |
77 | return (PTR) (hdr + 1); | |
78 | } | |
79 | ||
80 | static PTR | |
81 | DEFUN(reallochook, (ptr, size), PTR ptr AND size_t size) | |
82 | { | |
83 | struct hdr *hdr = ((struct hdr *) ptr) - 1; | |
84 | ||
85 | checkhdr(hdr); | |
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; | |
93 | if (hdr == NULL) | |
94 | return NULL; | |
95 | ||
96 | hdr->size = size; | |
97 | ((char *) &hdr[1])[size] = MAGICBYTE; | |
98 | return (PTR) (hdr + 1); | |
99 | } | |
100 | ||
101 | void | |
9e7f3b6a | 102 | DEFUN(mcheck, (func), void EXFUN((*func), (void))) |
dd3b648e RP |
103 | { |
104 | static int mcheck_used = 0; | |
105 | ||
106 | if (func) | |
107 | abortfunc = func; | |
108 | ||
109 | /* These hooks may not be safely inserted if malloc is already in use. */ | |
110 | if (!__malloc_initialized && !mcheck_used) | |
111 | { | |
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; | |
118 | mcheck_used = 1; | |
119 | } | |
120 | } |