home *** CD-ROM | disk | FTP | other *** search
- /* HeapGraph.SHStuff.c */
- /* © Julian Smith 1994 */
-
- /*
- This file contains just the HeapGraph things which use StubsHack.
- */
-
- #include <stdlib.h>
- #include <time.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdarg.h>
-
- #include "kernel.h"
-
- #include "StubsHack.StubsHack.h"
- #include "HeapGraph.OldMalloc.h"
-
- #define HEAPGRAPH
- #include "HeapGraph.HeapGraph.h"
-
-
-
-
- int HeapGraph_reference = 0;
-
- StubsHack_mallocfn HeapGraph_old_malloc = NULL;
- StubsHack_reallocfn HeapGraph_old_realloc = NULL;
- StubsHack_callocfn HeapGraph_old_calloc = NULL;
- StubsHack_freefn HeapGraph_old_free = NULL;
- /* These are inited to NULL to trap bugs... */
- /* They will point to the original malloc functions (usually the shared c lib */
- /* functions, unless StubsHack_ReplaceANSIAllocFns was called before */
- /* HeapGraph_RedirectAllocFns). */
-
-
-
- #define WriteByte( filehandle, byte) putc( byte, filehandle)
- #define WriteInt( filehandle, word) fwrite( &(word), sizeof( int), 1, filehandle)
-
-
-
- static void *HeapGraph_Malloc( size_t size)
- {
- void *newptr = HeapGraph_old_malloc( size);
- FILE *file = HeapGraph_OpenPipeFile();
-
- WriteByte( file, HeapGraph_MALLOC);
- WriteInt( file, newptr);
- WriteInt( file, size);
- WriteInt( file, HeapGraph_reference);
- /* !HeapDisp colours blocks according to the bottom 3 bits of ref. */
-
- HeapGraph_ClosePipeFile( file);
-
- return newptr;
- }
-
-
-
- static void *HeapGraph_Calloc( size_t num, size_t size)
- {
- void *newptr = HeapGraph_old_calloc( num, size);
- int blocksize = num*size;
- FILE *file = HeapGraph_OpenPipeFile();
-
- WriteByte( file, HeapGraph_MALLOC);
- WriteInt( file, newptr);
- WriteInt( file, blocksize);
- WriteInt( file, HeapGraph_reference); /* ref - see HeapGraph_Malloc */
- HeapGraph_ClosePipeFile( file);
-
- return newptr;
- }
-
-
-
- static void *HeapGraph_Realloc( void *oldptr, size_t newsize)
- {
- void *newptr = HeapGraph_old_realloc( oldptr, newsize);
- FILE *file = HeapGraph_OpenPipeFile();
-
- WriteByte( file, HeapGraph_REALLOC);
- WriteInt( file, oldptr);
- WriteInt( file, newptr);
- WriteInt( file, newsize);
- HeapGraph_ClosePipeFile( file);
- return newptr;
- }
-
-
-
-
- static void HeapGraph_Free( void *ptr)
- {
- FILE *file = HeapGraph_OpenPipeFile();
-
- WriteByte( file, HeapGraph_FREE);
- WriteInt( file, ptr);
- HeapGraph_ClosePipeFile( file);
-
- HeapGraph_old_free( ptr);
- return;
- }
-
-
-
-
-
-
- int HeapGraph_RedirectAllocFns( const char *filename)
- {
- static BOOL firsttime = TRUE;
- StubsHack_error e;
-
- if (!firsttime) return 1;
- firsttime = FALSE;
- /* Can only redirect to the HeapGraph veneers once. */
- /* Otherwise for eg., HeapGraph_MallocVeneer will end */
- /* calling itself, making an infinte loop... */
-
- if ( HeapGraph_filename[0]==0) HeapGraph_Init( filename);
-
- return (int) StubsHack_ReplaceANSIAllocFns(
- HeapGraph_Malloc, HeapGraph_Realloc, HeapGraph_Calloc, HeapGraph_Free,
- &HeapGraph_old_malloc, &HeapGraph_old_realloc, &HeapGraph_old_calloc, &HeapGraph_old_free,
- TRUE
- );
-
- return (int) e;
-
- }
-
-