1/* Standard debugging hooks for `malloc'.
2   Copyright 1990, 1991, 1992, 1993 Free Software Foundation
3   Written May 1989 by Mike Haertel.
4
5This library is free software; you can redistribute it and/or
6modify it under the terms of the GNU Library General Public License as
7published by the Free Software Foundation; either version 2 of the
8License, or (at your option) any later version.
9
10This library is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13Library General Public License for more details.
14
15You should have received a copy of the GNU Library General Public
16License along with this library; see the file COPYING.LIB.  If
17not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18Cambridge, MA 02139, USA.
19
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.  */
22
23#ifndef	_MALLOC_INTERNAL
24#define	_MALLOC_INTERNAL
25#include <malloc.h>
26#endif
27
28/* Old hook values.  */
29static void (*old_free_hook) __P ((__ptr_t ptr));
30static __ptr_t (*old_malloc_hook) __P ((size_t size));
31static __ptr_t (*old_realloc_hook) __P ((__ptr_t ptr, size_t size));
32
33/* Function to call when something awful happens.  */
34static void (*abortfunc) __P ((void));
35
36/* Arbitrary magical numbers.  */
37#define MAGICWORD	0xfedabeeb
38#define MAGICBYTE	((char) 0xd7)
39
40struct hdr
41  {
42    size_t size;		/* Exact size requested by user.  */
43    unsigned long int magic;	/* Magic number to check header integrity.  */
44  };
45
46static void checkhdr __P ((const struct hdr *));
47static void
48checkhdr (hdr)
49     const struct hdr *hdr;
50{
51  if (hdr->magic != MAGICWORD || ((char *) &hdr[1])[hdr->size] != MAGICBYTE)
52    (*abortfunc) ();
53}
54
55static void freehook __P ((__ptr_t));
56static void
57freehook (ptr)
58     __ptr_t ptr;
59{
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;
66}
67
68static __ptr_t mallochook __P ((size_t));
69static __ptr_t
70mallochook (size)
71     size_t size;
72{
73  struct hdr *hdr;
74
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;
80
81  hdr->size = size;
82  hdr->magic = MAGICWORD;
83  ((char *) &hdr[1])[size] = MAGICBYTE;
84  return (__ptr_t) (hdr + 1);
85}
86
87static __ptr_t reallochook __P ((__ptr_t, size_t));
88static __ptr_t
89reallochook (ptr, size)
90     __ptr_t ptr;
91     size_t size;
92{
93  struct hdr *hdr = ((struct hdr *) ptr) - 1;
94
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;
105
106  hdr->size = size;
107  ((char *) &hdr[1])[size] = MAGICBYTE;
108  return (__ptr_t) (hdr + 1);
109}
110
111int
112mcheck (func)
113     void (*func) __P ((void));
114{
115  extern void abort __P ((void));
116  static int mcheck_used = 0;
117
118  abortfunc = (func != NULL) ? func : abort;
119
120  /* These hooks may not be safely inserted if malloc is already in use.  */
121  if (!__malloc_initialized && !mcheck_used)
122    {
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;
130    }
131
132  return mcheck_used ? 0 : -1;
133}
134