* stabs.texinfo: Document the format for C++ nested types.
[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"
ab1e12a2
PB
23#define size_t unsigned int
24#define ptrdiff_t int
25#define NULL 0
26#define __ONEFILE
dd3b648e
RP
27#include "gmalloc.h"
28
29/* Old hook values. */
30static void EXFUN((*old_free_hook), (PTR ptr));
31static PTR EXFUN((*old_malloc_hook), (size_t size));
32static PTR EXFUN((*old_realloc_hook), (PTR ptr, size_t size));
33
9fa28378 34
dd3b648e 35/* Function to call when something awful happens. */
9e7f3b6a 36extern void abort();
1ab3bf1b 37static void EXFUN((*abortfunc), (NOARGS)) = (void (*)()) abort;
dd3b648e
RP
38
39/* Arbitrary magical numbers. */
40#define MAGICWORD 0xfedabeeb
41#define MAGICBYTE ((char) 0xd7)
42
43struct hdr
44 {
45 size_t size; /* Exact size requested by user. */
46 unsigned int magic; /* Magic number to check header integrity. */
47 };
48
9e7f3b6a 49static void
dd3b648e
RP
50DEFUN(checkhdr, (hdr), CONST struct hdr *hdr)
51{
52 if (hdr->magic != MAGICWORD || ((char *) &hdr[1])[hdr->size] != MAGICBYTE)
53 (*abortfunc)();
54}
55
56static void
57DEFUN(freehook, (ptr), PTR ptr)
58{
59 struct hdr *hdr = ((struct hdr *) ptr) - 1;
60 checkhdr(hdr);
61 hdr->magic = 0;
62 __free_hook = old_free_hook;
63 free(hdr);
64 __free_hook = freehook;
65}
66
67static PTR
68DEFUN(mallochook, (size), size_t size)
69{
70 struct hdr *hdr;
71
72 __malloc_hook = old_malloc_hook;
73 hdr = (struct hdr *) malloc(sizeof(struct hdr) + size + 1);
74 __malloc_hook = mallochook;
75 if (hdr == NULL)
76 return NULL;
77
78 hdr->size = size;
79 hdr->magic = MAGICWORD;
80 ((char *) &hdr[1])[size] = MAGICBYTE;
81 return (PTR) (hdr + 1);
82}
83
84static PTR
85DEFUN(reallochook, (ptr, size), PTR ptr AND size_t size)
86{
87 struct hdr *hdr = ((struct hdr *) ptr) - 1;
88
89 checkhdr(hdr);
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;
97 if (hdr == NULL)
98 return NULL;
99
100 hdr->size = size;
101 ((char *) &hdr[1])[size] = MAGICBYTE;
102 return (PTR) (hdr + 1);
103}
104
105void
1ab3bf1b 106DEFUN(mcheck, (func), void EXFUN((*func), (NOARGS)))
dd3b648e
RP
107{
108 static int mcheck_used = 0;
109
110 if (func)
111 abortfunc = func;
112
113 /* These hooks may not be safely inserted if malloc is already in use. */
114 if (!__malloc_initialized && !mcheck_used)
115 {
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;
122 mcheck_used = 1;
123 }
124}
This page took 0.069222 seconds and 4 git commands to generate.