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 | ||
5 | This program is free software; you can redistribute it and/or modify | |
6 | it under the terms of the GNU General Public License as published by | |
7 | the Free Software Foundation; either version 1, or (at your option) | |
8 | any later version. | |
9 | ||
10 | This program 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 | |
13 | GNU General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
16 | along with this program; if not, write to the Free Software | |
17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
18 | ||
19 | The author may be reached (Email) at the address mike@ai.mit.edu, | |
20 | or (US mail) as Mike Haertel c/o Free Software Foundation. */ | |
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 | ||
31 | /* Function to call when something awful happens. */ | |
32 | static void EXFUN((*abortfunc), (void)) = abort; | |
33 | ||
34 | /* Arbitrary magical numbers. */ | |
35 | #define MAGICWORD 0xfedabeeb | |
36 | #define MAGICBYTE ((char) 0xd7) | |
37 | ||
38 | struct hdr | |
39 | { | |
40 | size_t size; /* Exact size requested by user. */ | |
41 | unsigned int magic; /* Magic number to check header integrity. */ | |
42 | }; | |
43 | ||
44 | static void | |
45 | DEFUN(checkhdr, (hdr), CONST struct hdr *hdr) | |
46 | { | |
47 | if (hdr->magic != MAGICWORD || ((char *) &hdr[1])[hdr->size] != MAGICBYTE) | |
48 | (*abortfunc)(); | |
49 | } | |
50 | ||
51 | static void | |
52 | DEFUN(freehook, (ptr), PTR ptr) | |
53 | { | |
54 | struct hdr *hdr = ((struct hdr *) ptr) - 1; | |
55 | checkhdr(hdr); | |
56 | hdr->magic = 0; | |
57 | __free_hook = old_free_hook; | |
58 | free(hdr); | |
59 | __free_hook = freehook; | |
60 | } | |
61 | ||
62 | static PTR | |
63 | DEFUN(mallochook, (size), size_t size) | |
64 | { | |
65 | struct hdr *hdr; | |
66 | ||
67 | __malloc_hook = old_malloc_hook; | |
68 | hdr = (struct hdr *) malloc(sizeof(struct hdr) + size + 1); | |
69 | __malloc_hook = mallochook; | |
70 | if (hdr == NULL) | |
71 | return NULL; | |
72 | ||
73 | hdr->size = size; | |
74 | hdr->magic = MAGICWORD; | |
75 | ((char *) &hdr[1])[size] = MAGICBYTE; | |
76 | return (PTR) (hdr + 1); | |
77 | } | |
78 | ||
79 | static PTR | |
80 | DEFUN(reallochook, (ptr, size), PTR ptr AND size_t size) | |
81 | { | |
82 | struct hdr *hdr = ((struct hdr *) ptr) - 1; | |
83 | ||
84 | checkhdr(hdr); | |
85 | __free_hook = old_free_hook; | |
86 | __malloc_hook = old_malloc_hook; | |
87 | __realloc_hook = old_realloc_hook; | |
88 | hdr = (struct hdr *) realloc((PTR) hdr, sizeof(struct hdr) + size + 1); | |
89 | __free_hook = freehook; | |
90 | __malloc_hook = mallochook; | |
91 | __realloc_hook = reallochook; | |
92 | if (hdr == NULL) | |
93 | return NULL; | |
94 | ||
95 | hdr->size = size; | |
96 | ((char *) &hdr[1])[size] = MAGICBYTE; | |
97 | return (PTR) (hdr + 1); | |
98 | } | |
99 | ||
100 | void | |
101 | DEFUN(mcheck, (func), void EXFUN((*func), (void))) | |
102 | { | |
103 | static int mcheck_used = 0; | |
104 | ||
105 | if (func) | |
106 | abortfunc = func; | |
107 | ||
108 | /* These hooks may not be safely inserted if malloc is already in use. */ | |
109 | if (!__malloc_initialized && !mcheck_used) | |
110 | { | |
111 | old_free_hook = __free_hook; | |
112 | __free_hook = freehook; | |
113 | old_malloc_hook = __malloc_hook; | |
114 | __malloc_hook = mallochook; | |
115 | old_realloc_hook = __realloc_hook; | |
116 | __realloc_hook = reallochook; | |
117 | mcheck_used = 1; | |
118 | } | |
119 | } |