No empty .Rs/.Re
[netbsd-mini2440.git] / gnu / lib / libmalloc / mcheck.c
blobf7d9d4f892e278ac08fde3461152d34f35bdac72
1 /* Standard debugging hooks for `malloc'.
2 Copyright 1990, 1991, 1992, 1993 Free Software Foundation
3 Written May 1989 by Mike Haertel.
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This library 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 GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; see the file COPYING.LIB. If
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, USA.
20 The author may be reached (Email) at the address mike@ai.mit.edu,
21 or (US mail) as Mike Haertel c/o Free Software Foundation. */
23 #ifndef _MALLOC_INTERNAL
24 #define _MALLOC_INTERNAL
25 #include <malloc.h>
26 #endif
28 /* Old hook values. */
29 static void (*old_free_hook) __P ((__ptr_t ptr));
30 static __ptr_t (*old_malloc_hook) __P ((size_t size));
31 static __ptr_t (*old_realloc_hook) __P ((__ptr_t ptr, size_t size));
33 /* Function to call when something awful happens. */
34 static void (*abortfunc) __P ((void));
36 /* Arbitrary magical numbers. */
37 #define MAGICWORD 0xfedabeeb
38 #define MAGICBYTE ((char) 0xd7)
40 struct hdr
42 size_t size; /* Exact size requested by user. */
43 unsigned long int magic; /* Magic number to check header integrity. */
46 static void checkhdr __P ((const struct hdr *));
47 static void
48 checkhdr (hdr)
49 const struct hdr *hdr;
51 if (hdr->magic != MAGICWORD || ((char *) &hdr[1])[hdr->size] != MAGICBYTE)
52 (*abortfunc) ();
55 static void freehook __P ((__ptr_t));
56 static void
57 freehook (ptr)
58 __ptr_t ptr;
60 struct hdr *hdr = ((struct hdr *) ptr) - 1;
61 checkhdr (hdr);
62 hdr->magic = 0;
63 __free_hook = old_free_hook;
64 free (hdr);
65 __free_hook = freehook;
68 static __ptr_t mallochook __P ((size_t));
69 static __ptr_t
70 mallochook (size)
71 size_t size;
73 struct hdr *hdr;
75 __malloc_hook = old_malloc_hook;
76 hdr = (struct hdr *) malloc (sizeof (struct hdr) + size + 1);
77 __malloc_hook = mallochook;
78 if (hdr == NULL)
79 return NULL;
81 hdr->size = size;
82 hdr->magic = MAGICWORD;
83 ((char *) &hdr[1])[size] = MAGICBYTE;
84 return (__ptr_t) (hdr + 1);
87 static __ptr_t reallochook __P ((__ptr_t, size_t));
88 static __ptr_t
89 reallochook (ptr, size)
90 __ptr_t ptr;
91 size_t size;
93 struct hdr *hdr = ((struct hdr *) ptr) - 1;
95 checkhdr (hdr);
96 __free_hook = old_free_hook;
97 __malloc_hook = old_malloc_hook;
98 __realloc_hook = old_realloc_hook;
99 hdr = (struct hdr *) realloc ((__ptr_t) hdr, sizeof (struct hdr) + size + 1);
100 __free_hook = freehook;
101 __malloc_hook = mallochook;
102 __realloc_hook = reallochook;
103 if (hdr == NULL)
104 return NULL;
106 hdr->size = size;
107 ((char *) &hdr[1])[size] = MAGICBYTE;
108 return (__ptr_t) (hdr + 1);
112 mcheck (func)
113 void (*func) __P ((void));
115 extern void abort __P ((void));
116 static int mcheck_used = 0;
118 abortfunc = (func != NULL) ? func : abort;
120 /* These hooks may not be safely inserted if malloc is already in use. */
121 if (!__malloc_initialized && !mcheck_used)
123 old_free_hook = __free_hook;
124 __free_hook = freehook;
125 old_malloc_hook = __malloc_hook;
126 __malloc_hook = mallochook;
127 old_realloc_hook = __realloc_hook;
128 __realloc_hook = reallochook;
129 mcheck_used = 1;
132 return mcheck_used ? 0 : -1;