]> Git Repo - binutils.git/blob - gdb/mcheck.c
ansi name abuse changes
[binutils.git] / gdb / mcheck.c
1 /* Standard debugging hooks for `malloc'.
2    Copyright 1990 Free Software Foundation
3                   Written May 1989 by Mike Haertel.
4
5    The author may be reached (Email) at the address [email protected],
6    or (US mail) as Mike Haertel c/o Free Software Foundation.
7
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.
12
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.
17
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.  */
21
22 #include "ansidecl.h"
23 #define size_t unsigned int
24 #define ptrdiff_t int
25 #define NULL 0
26 #define __ONEFILE
27 #include "gmalloc.h"
28
29 /* Old hook values.  */
30 static void EXFUN((*old_free_hook), (PTR ptr));
31 static PTR EXFUN((*old_malloc_hook), (size_t size));
32 static PTR EXFUN((*old_realloc_hook), (PTR ptr, size_t size));
33
34
35 /* Function to call when something awful happens. */
36 extern void abort();
37 static void EXFUN((*abortfunc), (void)) = (void (*)()) abort;
38
39 /* Arbitrary magical numbers.  */
40 #define MAGICWORD       0xfedabeeb
41 #define MAGICBYTE       ((char) 0xd7)
42
43 struct hdr
44   {
45     size_t size;                /* Exact size requested by user.  */
46     unsigned int magic;         /* Magic number to check header integrity.  */
47   };
48
49 static void
50 DEFUN(checkhdr, (hdr), CONST struct hdr *hdr)
51 {
52   if (hdr->magic != MAGICWORD || ((char *) &hdr[1])[hdr->size] != MAGICBYTE)
53     (*abortfunc)();
54 }
55
56 static void
57 DEFUN(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
67 static PTR
68 DEFUN(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
84 static PTR
85 DEFUN(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
105 void
106 DEFUN(mcheck, (func), void EXFUN((*func), (void)))
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.030768 seconds and 4 git commands to generate.