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 mike@ai.mit.edu, |
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" | |
23 | #include <stdlib.h> | |
24 | #include "gmalloc.h" | |
25 | ||
26 | /* Old hook values. */ | |
27 | static void EXFUN((*old_free_hook), (PTR ptr)); | |
28 | static PTR EXFUN((*old_malloc_hook), (size_t size)); | |
29 | static PTR EXFUN((*old_realloc_hook), (PTR ptr, size_t size)); | |
30 | ||
5e5215eb JG |
31 | /* FIXME. We cannot *declare* abort() as either being void or being |
32 | int, because if the system declares it as the other, we get a fatal | |
33 | error. It's senseless to configure the system for whether abort is | |
34 | void or int. So we simply fail to declare it, which works on all | |
35 | systems, but might produce a warning on yours. Please ignore the warning | |
36 | and raise your middle finger in the general direction of the ANSI C | |
37 | committee in tribute. */ | |
dd3b648e | 38 | /* Function to call when something awful happens. */ |
5e5215eb | 39 | static void EXFUN((*abortfunc), (void)) = (void (*)()) abort; |
dd3b648e RP |
40 | |
41 | /* Arbitrary magical numbers. */ | |
42 | #define MAGICWORD 0xfedabeeb | |
43 | #define MAGICBYTE ((char) 0xd7) | |
44 | ||
45 | struct hdr | |
46 | { | |
47 | size_t size; /* Exact size requested by user. */ | |
48 | unsigned int magic; /* Magic number to check header integrity. */ | |
49 | }; | |
50 | ||
51 | static void | |
52 | DEFUN(checkhdr, (hdr), CONST struct hdr *hdr) | |
53 | { | |
54 | if (hdr->magic != MAGICWORD || ((char *) &hdr[1])[hdr->size] != MAGICBYTE) | |
55 | (*abortfunc)(); | |
56 | } | |
57 | ||
58 | static void | |
59 | DEFUN(freehook, (ptr), PTR ptr) | |
60 | { | |
61 | struct hdr *hdr = ((struct hdr *) ptr) - 1; | |
62 | checkhdr(hdr); | |
63 | hdr->magic = 0; | |
64 | __free_hook = old_free_hook; | |
65 | free(hdr); | |
66 | __free_hook = freehook; | |
67 | } | |
68 | ||
69 | static PTR | |
70 | DEFUN(mallochook, (size), size_t size) | |
71 | { | |
72 | struct hdr *hdr; | |
73 | ||
74 | __malloc_hook = old_malloc_hook; | |
75 | hdr = (struct hdr *) malloc(sizeof(struct hdr) + size + 1); | |
76 | __malloc_hook = mallochook; | |
77 | if (hdr == NULL) | |
78 | return NULL; | |
79 | ||
80 | hdr->size = size; | |
81 | hdr->magic = MAGICWORD; | |
82 | ((char *) &hdr[1])[size] = MAGICBYTE; | |
83 | return (PTR) (hdr + 1); | |
84 | } | |
85 | ||
86 | static PTR | |
87 | DEFUN(reallochook, (ptr, size), PTR ptr AND size_t size) | |
88 | { | |
89 | struct hdr *hdr = ((struct hdr *) ptr) - 1; | |
90 | ||
91 | checkhdr(hdr); | |
92 | __free_hook = old_free_hook; | |
93 | __malloc_hook = old_malloc_hook; | |
94 | __realloc_hook = old_realloc_hook; | |
95 | hdr = (struct hdr *) realloc((PTR) hdr, sizeof(struct hdr) + size + 1); | |
96 | __free_hook = freehook; | |
97 | __malloc_hook = mallochook; | |
98 | __realloc_hook = reallochook; | |
99 | if (hdr == NULL) | |
100 | return NULL; | |
101 | ||
102 | hdr->size = size; | |
103 | ((char *) &hdr[1])[size] = MAGICBYTE; | |
104 | return (PTR) (hdr + 1); | |
105 | } | |
106 | ||
107 | void | |
108 | DEFUN(mcheck, (func), void EXFUN((*func), (void))) | |
109 | { | |
110 | static int mcheck_used = 0; | |
111 | ||
112 | if (func) | |
113 | abortfunc = func; | |
114 | ||
115 | /* These hooks may not be safely inserted if malloc is already in use. */ | |
116 | if (!__malloc_initialized && !mcheck_used) | |
117 | { | |
118 | old_free_hook = __free_hook; | |
119 | __free_hook = freehook; | |
120 | old_malloc_hook = __malloc_hook; | |
121 | __malloc_hook = mallochook; | |
122 | old_realloc_hook = __realloc_hook; | |
123 | __realloc_hook = reallochook; | |
124 | mcheck_used = 1; | |
125 | } | |
126 | } |