home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / malloc / mcheck.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-20  |  3.5 KB  |  133 lines

  1. /* Standard debugging hooks for `malloc'.
  2.    Copyright 1990, 1991, 1992 Free Software Foundation
  3.    Written May 1989 by Mike Haertel.
  4.  
  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.
  9.  
  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.
  14.  
  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.
  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.  */
  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));
  32.  
  33. /* Function to call when something awful happens.  */
  34. extern void abort __P ((void));
  35. static void (*abortfunc) __P ((void)) = (void (*) __P ((void))) abort;
  36.  
  37. /* Arbitrary magical numbers.  */
  38. #define MAGICWORD    0xfedabeeb
  39. #define MAGICBYTE    ((char) 0xd7)
  40.  
  41. struct hdr
  42.   {
  43.     size_t size;        /* Exact size requested by user.  */
  44.     unsigned long int magic;    /* Magic number to check header integrity.  */
  45.   };
  46.  
  47. static void checkhdr __P ((__const struct hdr *));
  48. static void
  49. checkhdr (hdr)
  50.      __const struct hdr *hdr;
  51. {
  52.   if (hdr->magic != MAGICWORD || ((char *) &hdr[1])[hdr->size] != MAGICBYTE)
  53.     (*abortfunc) ();
  54. }
  55.  
  56. static void freehook __P ((__ptr_t));
  57. static void
  58. freehook (ptr)
  59.      __ptr_t ptr;
  60. {
  61.   struct hdr *hdr = ((struct hdr *) ptr) - 1;
  62.   checkhdr (hdr);
  63.   hdr->magic = 0;
  64.   __free_hook = old_free_hook;
  65.   free (hdr);
  66.   __free_hook = freehook;
  67. }
  68.  
  69. static __ptr_t mallochook __P ((size_t));
  70. static __ptr_t
  71. mallochook (size)
  72.      size_t size;
  73. {
  74.   struct hdr *hdr;
  75.  
  76.   __malloc_hook = old_malloc_hook;
  77.   hdr = (struct hdr *) malloc (sizeof (struct hdr) + size + 1);
  78.   __malloc_hook = mallochook;
  79.   if (hdr == NULL)
  80.     return NULL;
  81.  
  82.   hdr->size = size;
  83.   hdr->magic = MAGICWORD;
  84.   ((char *) &hdr[1])[size] = MAGICBYTE;
  85.   return (__ptr_t) (hdr + 1);
  86. }
  87.  
  88. static __ptr_t reallochook __P ((__ptr_t, size_t));
  89. static __ptr_t
  90. reallochook (ptr, size)
  91.      __ptr_t ptr;
  92.      size_t size;
  93. {
  94.   struct hdr *hdr = ((struct hdr *) ptr) - 1;
  95.  
  96.   checkhdr (hdr);
  97.   __free_hook = old_free_hook;
  98.   __malloc_hook = old_malloc_hook;
  99.   __realloc_hook = old_realloc_hook;
  100.   hdr = (struct hdr *) realloc ((__ptr_t) hdr, sizeof (struct hdr) + size + 1);
  101.   __free_hook = freehook;
  102.   __malloc_hook = mallochook;
  103.   __realloc_hook = reallochook;
  104.   if (hdr == NULL)
  105.     return NULL;
  106.  
  107.   hdr->size = size;
  108.   ((char *) &hdr[1])[size] = MAGICBYTE;
  109.   return (__ptr_t) (hdr + 1);
  110. }
  111.  
  112. void
  113. mcheck (func)
  114.      void (*func) __P ((void));
  115. {
  116.   static int mcheck_used = 0;
  117.  
  118.   if (func != NULL)
  119.     abortfunc = func;
  120.  
  121.   /* These hooks may not be safely inserted if malloc is already in use.  */
  122.   if (!__malloc_initialized && !mcheck_used)
  123.     {
  124.       old_free_hook = __free_hook;
  125.       __free_hook = freehook;
  126.       old_malloc_hook = __malloc_hook;
  127.       __malloc_hook = mallochook;
  128.       old_realloc_hook = __realloc_hook;
  129.       __realloc_hook = reallochook;
  130.       mcheck_used = 1;
  131.     }
  132. }
  133.