home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / clib / progs / stubshack / HeapGraph_Sources_c_HeapGraph < prev    next >
Encoding:
Text File  |  1994-11-24  |  2.5 KB  |  151 lines

  1. /* HeapGraph.HeapGraph.c    */
  2. /* © Julian Smith 1994        */
  3.  
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <stdarg.h>
  9.  
  10. #include "kernel.h"
  11.  
  12. #define HEAPGRAPH
  13. #include "HeapGraph.HeapGraph.h"
  14.  
  15.  
  16. #ifndef BOOL
  17. #define BOOL  unsigned
  18. #define FALSE 0
  19. #define TRUE  1
  20. #endif
  21.  
  22. char        HeapGraph_filename[ 256]    = "";
  23. static BOOL    HeapGraph_we_are_a_taskwindow    = TRUE;
  24.  
  25.  
  26.  
  27. static char    *LeafName(char *path)    /* Nicked from DeskLib    */
  28. {
  29. char *leaf=path, ch = '.';
  30.  
  31. do    {
  32.     if ( ch == '.')
  33.     leaf = path;
  34.     }
  35.     while ( ( ch = *( path++)) != '\0') ;
  36.  
  37. return( leaf) ;
  38. }
  39.  
  40.  
  41. static int    FileSize( const char *path)
  42. {
  43. int            type;
  44. _kernel_osfile_block    block;
  45.  
  46. type = _kernel_osfile( 5, path, &block);
  47.     /* DeskLib's SWI or swiv is much nicer, but using kernel makes    */
  48.     /* linking easier as it is included in Stubs.            */
  49.  
  50. if ( type==0)    block.start=-1;
  51. return block.start;
  52. }
  53.  
  54.  
  55.  
  56. FILE    *HeapGraph_OpenPipeFile( void)
  57. {
  58. FILE    *file;
  59.  
  60. if ( HeapGraph_we_are_a_taskwindow)    {
  61.     /* Taskwindow apps don't seem to write to a pipe file which is more than 255 bytes long.*/
  62.     /* But being a taskwindow, we can just loop until someone reads from the pipe file.    */
  63.     int    size = FileSize( HeapGraph_filename);
  64.  
  65.     if ( size > 128)    {
  66.         clock_t    tnew, told;
  67.         told = clock();
  68.         printf( "Waiting for pipefile to be read - size of '%s' is %i",
  69.             HeapGraph_filename, size
  70.             );
  71.  
  72.         do    {
  73.             tnew = clock();
  74.             if ( tnew-told > 1*CLOCKS_PER_SEC)    {
  75.                 told=tnew;
  76.                 printf( ".");
  77.                 }
  78.             }
  79.             while( FileSize( HeapGraph_filename) > 128);
  80.  
  81.         printf( "\n");
  82.         }
  83.     }
  84.  
  85. file = fopen( HeapGraph_filename, "a");
  86. if (!file)    {
  87.     printf( "\nCouldn't open '%s'\n", HeapGraph_filename);
  88.     exit(1);
  89.     }
  90. return file;
  91. }
  92.  
  93.  
  94.  
  95.  
  96.  
  97. void    HeapGraph_ClosePipeFile( FILE *file)
  98. {
  99. fclose( file);
  100. }
  101.  
  102.  
  103.  
  104.  
  105.  
  106. #ifndef    SWI_TaskWindow_TaskInfo
  107. #define SWI_TaskWindow_TaskInfo    0x43380
  108. #endif
  109.  
  110. static BOOL    AreWeATaskWindow( void)
  111.     /* returns non-zero if we are a taskwindow.    */
  112. {
  113. _kernel_swi_regs    in, out;
  114. in.r[0] = 0;
  115. _kernel_swi( SWI_TaskWindow_TaskInfo, &in, &out);
  116. return out.r[0];
  117. }
  118.  
  119.  
  120.  
  121.  
  122. void    HeapGraph_Init( const char *filename)
  123. {
  124. HeapGraph_we_are_a_taskwindow = AreWeATaskWindow();
  125.  
  126. if ( filename)    strcpy( HeapGraph_filename, filename);
  127. else        sprintf( HeapGraph_filename, "Pipe:$.C_Debug.%s", LeafName( tmpnam( NULL)));
  128.  
  129. return;
  130. }
  131.  
  132.  
  133. void    HeapGraph__Sendf( char *fmt, ...)
  134. {
  135. va_list    args;
  136. FILE    *file;
  137.  
  138. if ( HeapGraph_filename[0]==0)    HeapGraph_Init( NULL);    /* Check we have a unique pipe filename    */
  139.  
  140. file    = HeapGraph_OpenPipeFile();
  141.  
  142. va_start( args, fmt);
  143. vfprintf( file, fmt, args);
  144. va_end( args);
  145. HeapGraph_ClosePipeFile( file);
  146. }
  147.  
  148.  
  149.  
  150.  
  151.