Change GDB over to GNU General Public License version 2.
[deliverable/binutils-gdb.git] / gdb / mcheck.c
CommitLineData
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
8This program is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2 of the License, or
11(at your option) any later version.
dd3b648e 12
99a7de40
JG
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
dd3b648e 17
99a7de40
JG
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, 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. */
27static void EXFUN((*old_free_hook), (PTR ptr));
28static PTR EXFUN((*old_malloc_hook), (size_t size));
29static PTR EXFUN((*old_realloc_hook), (PTR ptr, size_t size));
30
31/* Function to call when something awful happens. */
32static void EXFUN((*abortfunc), (void)) = abort;
33
34/* Arbitrary magical numbers. */
35#define MAGICWORD 0xfedabeeb
36#define MAGICBYTE ((char) 0xd7)
37
38struct hdr
39 {
40 size_t size; /* Exact size requested by user. */
41 unsigned int magic; /* Magic number to check header integrity. */
42 };
43
44static void
45DEFUN(checkhdr, (hdr), CONST struct hdr *hdr)
46{
47 if (hdr->magic != MAGICWORD || ((char *) &hdr[1])[hdr->size] != MAGICBYTE)
48 (*abortfunc)();
49}
50
51static void
52DEFUN(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
62static PTR
63DEFUN(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
79static PTR
80DEFUN(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
100void
101DEFUN(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}
This page took 0.030856 seconds and 4 git commands to generate.