home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 2 / RISC_DISC_2.iso / pd_share / program / code / stubshack / HeapGraph / Sources / c / SHStuff < prev   
Encoding:
Text File  |  1994-11-24  |  2.8 KB  |  134 lines

  1. /* HeapGraph.SHStuff.c        */
  2. /* © Julian Smith 1994        */
  3.  
  4. /* 
  5. This file contains just the HeapGraph things which use StubsHack.
  6. */
  7.  
  8. #include <stdlib.h>
  9. #include <time.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdarg.h>
  13.  
  14. #include "kernel.h"
  15.  
  16. #include "StubsHack.StubsHack.h"
  17. #include "HeapGraph.OldMalloc.h"
  18.  
  19. #define HEAPGRAPH
  20. #include "HeapGraph.HeapGraph.h"
  21.  
  22.  
  23.  
  24.  
  25. int    HeapGraph_reference = 0;
  26.  
  27. StubsHack_mallocfn    HeapGraph_old_malloc    = NULL;
  28. StubsHack_reallocfn    HeapGraph_old_realloc    = NULL;
  29. StubsHack_callocfn    HeapGraph_old_calloc    = NULL;
  30. StubsHack_freefn    HeapGraph_old_free    = NULL;
  31.     /* These are inited to NULL to trap bugs...    */
  32.     /* They will point to the original malloc functions (usually the shared c lib    */
  33.     /* functions, unless StubsHack_ReplaceANSIAllocFns was called before         */
  34.     /* HeapGraph_RedirectAllocFns).                            */
  35.  
  36.  
  37.  
  38. #define WriteByte( filehandle, byte)    putc( byte, filehandle)
  39. #define WriteInt(  filehandle, word)    fwrite( &(word), sizeof( int), 1, filehandle)
  40.  
  41.  
  42.  
  43. static void    *HeapGraph_Malloc( size_t size)
  44. {
  45. void    *newptr = HeapGraph_old_malloc( size);
  46. FILE    *file    = HeapGraph_OpenPipeFile();
  47.  
  48. WriteByte( file, HeapGraph_MALLOC);
  49. WriteInt(  file, newptr);
  50. WriteInt(  file, size);
  51. WriteInt(  file, HeapGraph_reference);
  52.     /* !HeapDisp colours blocks according to the bottom 3 bits of ref.    */
  53.     
  54. HeapGraph_ClosePipeFile( file);
  55.  
  56. return newptr;
  57. }
  58.  
  59.  
  60.  
  61. static void    *HeapGraph_Calloc( size_t num, size_t size)
  62. {
  63. void    *newptr     = HeapGraph_old_calloc( num, size);
  64. int    blocksize    = num*size;
  65. FILE    *file        = HeapGraph_OpenPipeFile();
  66.  
  67. WriteByte( file, HeapGraph_MALLOC);
  68. WriteInt(  file, newptr);
  69. WriteInt(  file, blocksize);
  70. WriteInt(  file, HeapGraph_reference);    /* ref - see HeapGraph_Malloc    */
  71. HeapGraph_ClosePipeFile( file);
  72.  
  73. return newptr;
  74. }
  75.  
  76.  
  77.  
  78. static void    *HeapGraph_Realloc( void *oldptr, size_t newsize)
  79. {
  80. void    *newptr = HeapGraph_old_realloc( oldptr, newsize);
  81. FILE    *file    = HeapGraph_OpenPipeFile();
  82.  
  83. WriteByte( file, HeapGraph_REALLOC);
  84. WriteInt(  file, oldptr);
  85. WriteInt(  file, newptr);
  86. WriteInt(  file, newsize);
  87. HeapGraph_ClosePipeFile( file);
  88. return newptr;
  89. }
  90.  
  91.  
  92.  
  93.  
  94. static void    HeapGraph_Free( void *ptr)
  95. {
  96. FILE    *file    = HeapGraph_OpenPipeFile();
  97.  
  98. WriteByte( file, HeapGraph_FREE);
  99. WriteInt(  file, ptr);
  100. HeapGraph_ClosePipeFile( file);
  101.  
  102. HeapGraph_old_free( ptr);
  103. return;
  104. }
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111. int    HeapGraph_RedirectAllocFns( const char *filename)
  112. {
  113. static BOOL    firsttime    = TRUE;
  114. StubsHack_error    e;
  115.  
  116. if (!firsttime)    return 1;
  117. firsttime = FALSE;
  118.     /* Can only redirect to the HeapGraph veneers once.    */
  119.     /* Otherwise for eg., HeapGraph_MallocVeneer will end    */
  120.     /* calling itself, making an infinte loop...        */
  121.  
  122. if ( HeapGraph_filename[0]==0)    HeapGraph_Init( filename);
  123.  
  124. return    (int)    StubsHack_ReplaceANSIAllocFns(
  125.     HeapGraph_Malloc,    HeapGraph_Realloc,    HeapGraph_Calloc,    HeapGraph_Free,
  126.     &HeapGraph_old_malloc,    &HeapGraph_old_realloc,    &HeapGraph_old_calloc,    &HeapGraph_old_free,
  127.     TRUE
  128.     );
  129.  
  130. return (int) e;
  131.  
  132. }
  133.  
  134.