home *** CD-ROM | disk | FTP | other *** search
- /* HeapGraph.HeapGraph.c */
- /* © Julian Smith 1994 */
-
- #include <stdlib.h>
- #include <time.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdarg.h>
-
- #include "kernel.h"
-
- #define HEAPGRAPH
- #include "HeapGraph.HeapGraph.h"
-
-
- #ifndef BOOL
- #define BOOL unsigned
- #define FALSE 0
- #define TRUE 1
- #endif
-
- char HeapGraph_filename[ 256] = "";
- static BOOL HeapGraph_we_are_a_taskwindow = TRUE;
-
-
-
- static char *LeafName(char *path) /* Nicked from DeskLib */
- {
- char *leaf=path, ch = '.';
-
- do {
- if ( ch == '.')
- leaf = path;
- }
- while ( ( ch = *( path++)) != '\0') ;
-
- return( leaf) ;
- }
-
-
- static int FileSize( const char *path)
- {
- int type;
- _kernel_osfile_block block;
-
- type = _kernel_osfile( 5, path, &block);
- /* DeskLib's SWI or swiv is much nicer, but using kernel makes */
- /* linking easier as it is included in Stubs. */
-
- if ( type==0) block.start=-1;
- return block.start;
- }
-
-
-
- FILE *HeapGraph_OpenPipeFile( void)
- {
- FILE *file;
-
- if ( HeapGraph_we_are_a_taskwindow) {
- /* Taskwindow apps don't seem to write to a pipe file which is more than 255 bytes long.*/
- /* But being a taskwindow, we can just loop until someone reads from the pipe file. */
- int size = FileSize( HeapGraph_filename);
-
- if ( size > 128) {
- clock_t tnew, told;
- told = clock();
- printf( "Waiting for pipefile to be read - size of '%s' is %i",
- HeapGraph_filename, size
- );
-
- do {
- tnew = clock();
- if ( tnew-told > 1*CLOCKS_PER_SEC) {
- told=tnew;
- printf( ".");
- }
- }
- while( FileSize( HeapGraph_filename) > 128);
-
- printf( "\n");
- }
- }
-
- file = fopen( HeapGraph_filename, "a");
- if (!file) {
- printf( "\nCouldn't open '%s'\n", HeapGraph_filename);
- exit(1);
- }
- return file;
- }
-
-
-
-
-
- void HeapGraph_ClosePipeFile( FILE *file)
- {
- fclose( file);
- }
-
-
-
-
-
- #ifndef SWI_TaskWindow_TaskInfo
- #define SWI_TaskWindow_TaskInfo 0x43380
- #endif
-
- static BOOL AreWeATaskWindow( void)
- /* returns non-zero if we are a taskwindow. */
- {
- _kernel_swi_regs in, out;
- in.r[0] = 0;
- _kernel_swi( SWI_TaskWindow_TaskInfo, &in, &out);
- return out.r[0];
- }
-
-
-
-
- void HeapGraph_Init( const char *filename)
- {
- HeapGraph_we_are_a_taskwindow = AreWeATaskWindow();
-
- if ( filename) strcpy( HeapGraph_filename, filename);
- else sprintf( HeapGraph_filename, "Pipe:$.C_Debug.%s", LeafName( tmpnam( NULL)));
-
- return;
- }
-
-
- void HeapGraph__Sendf( char *fmt, ...)
- {
- va_list args;
- FILE *file;
-
- if ( HeapGraph_filename[0]==0) HeapGraph_Init( NULL); /* Check we have a unique pipe filename */
-
- file = HeapGraph_OpenPipeFile();
-
- va_start( args, fmt);
- vfprintf( file, fmt, args);
- va_end( args);
- HeapGraph_ClosePipeFile( file);
- }
-
-
-
-
-