home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / System / Swatch / Development / swatch 1.7 / heap_list.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-09  |  6.1 KB  |  335 lines  |  [TEXT/KAHL]

  1. /**
  2.  
  3.     heap.list.c
  4.     Copyright (c) 1990-1992, joe holt
  5.  
  6.  **/
  7.  
  8.  
  9. /**-----------------------------------------------------------------------------
  10.  **
  11.  **    Headers
  12.  **
  13.  **/
  14.  
  15. #include <GestaltEqu.h>
  16. #include <Packages.h>
  17.  
  18. #ifndef __ctypes__
  19. #include "ctypes.h"
  20. #endif
  21. #ifndef __heap__
  22. #include "heap.h"
  23. #endif
  24. #ifndef __heap_list__
  25. #include "heap_list.h"
  26. #endif
  27. #ifndef __linked_list__
  28. #include "linked_list.h"
  29. #endif
  30. #ifndef __pstring__
  31. #include "pstring.h"
  32. #endif
  33. #ifndef __resources__
  34. #include "resources.h"
  35. #endif
  36. #ifndef __swatch__
  37. #include "swatch.h"
  38. #endif
  39.  
  40. /**-----------------------------------------------------------------------------
  41.  **
  42.  ** Private Variables
  43.  **
  44.  **/
  45.  
  46. static Boolean system_heap_is_32_bit, process_heaps_are_32_bit;
  47. static Boolean heap_list_inited = false;
  48.  
  49. static Handle MF_handle;
  50.  
  51.  
  52. /**-----------------------------------------------------------------------------
  53.  **
  54.  ** Private Functions
  55.  **
  56.  **/
  57.  
  58. static void init_one( Heap_info_handle_t h, ProcessSerialNumber *psn );
  59.  
  60.  
  61. /*******************************************************************************
  62.  **
  63.  **    Public Variables
  64.  **
  65.  **/
  66.  
  67. Boolean Memory_Manager_is_32_bit;
  68. l_anchor_t heaps;
  69. Heap_info_handle_t multifinder, system;
  70.  
  71.  
  72. /*******************************************************************************
  73.  **
  74.  **    Public Functions
  75.  **
  76.  **/
  77.  
  78. void Heap_list_init( void )
  79. {
  80.     OSErr err;
  81.     int32 response;
  82.     Heap_info_t *hi;
  83.  
  84.     if ( heap_list_inited ) return;
  85.     heap_list_inited = true;
  86.  
  87.     if ( Gestalt( gestaltAddressingModeAttr, &response ) ) Bail( BAD_SYSTEM_VERSION_ALRT );
  88.     system_heap_is_32_bit = (response & (1<<gestalt32BitSysZone)) != 0;
  89.     Memory_Manager_is_32_bit =
  90.             process_heaps_are_32_bit = (response & (1<<gestalt32BitAddressing)) != 0;
  91.  
  92.     MF_handle = TempNewHandle( 0, &err );
  93.     if ( err ) Bail( OUT_OF_MEMORY_ALRT );
  94.  
  95.     heaps = l_new_list();
  96.  
  97.     system = l_new( heaps, sizeof(Heap_info_t) );
  98.     init_one( system, nil );
  99.  
  100.     multifinder = l_new( heaps, sizeof(Heap_info_t) );
  101.     init_one( multifinder, nil );
  102. }
  103.  
  104.  
  105. void Heap_list_exit( void )
  106. {
  107.     OSErr err;
  108.     
  109.     MFTempDisposHandle( MF_handle, &err );
  110.     l_old_list( heaps );
  111. }
  112.  
  113.  
  114. int16 Heap_list_count( void )
  115. {
  116.     l_elem_t h = nil;
  117.     int count = 0;
  118.  
  119.     while ( h = l_next( heaps, h ) ) ++count;
  120.     return count;
  121. }
  122.  
  123.  
  124. int32 Heap_list_largest_heap_size( void )
  125. {
  126.     int32 largest_heap_size = 0;
  127.     Heap_info_handle_t h = nil;
  128.  
  129.     while ( (h = l_next( heaps, h )) ) {
  130.         Heap_info_t *hi = l_access( h );
  131.         if ( hi->updating && hi->current_size > largest_heap_size )
  132.             largest_heap_size = hi->current_size;
  133.         l_release( h );
  134.     }
  135.  
  136.     return largest_heap_size;
  137. }
  138.  
  139.  
  140. Boolean Heap_list_update( void )
  141. {
  142.     Heap_info_handle_t h;
  143.     ProcessSerialNumber psn;
  144.     Boolean changed = false;
  145.  
  146.  
  147.     psn.highLongOfPSN = 0;
  148.     psn.lowLongOfPSN = kNoProcess;
  149.     while ( !GetNextProcess( &psn ) ) {
  150.  
  151.         Boolean found = false;
  152.  
  153.         h = nil;
  154.         while ( !found && (h = l_next( heaps, h )) ) {
  155.             Boolean same;
  156.             Heap_info_t *hi;
  157.  
  158.             if ( h == multifinder || h == system ) continue;
  159.  
  160.             hi = l_access( h );
  161.             SameProcess( &psn, &hi->info.processNumber, &same );
  162.             if ( same ) {
  163.                 hi->touched = true;
  164.                 found = true;
  165.             }
  166.  
  167.             l_release( h );
  168.         }
  169.  
  170.         if ( !found ) {
  171.             Heap_info_t *hi;
  172.  
  173.             h = l_new( heaps, sizeof(Heap_info_t) );
  174.             init_one( h, &psn );
  175.  
  176.             hi = l_access( h );
  177.             hi->touched = true;
  178.             l_release( h );
  179.  
  180.             changed = true;
  181.         }
  182.  
  183.     }
  184.  
  185.  
  186.     h = l_next( heaps, nil );
  187.     while ( h ) {
  188.  
  189.         Boolean touched;
  190.         Heap_info_handle_t h_this;        
  191.         Heap_info_t *hi;
  192.  
  193.         h_this = h;
  194.         h = l_next( heaps, h );
  195.  
  196.         if ( h_this == multifinder || h_this == system ) continue;    // never removed
  197.  
  198.         hi = l_access( h_this );
  199.         touched = hi->touched;
  200.         hi->touched = false;
  201.         l_release( h_this );
  202.  
  203.         if ( !touched ) {
  204.             l_old( h_this );
  205.             changed = true;
  206.         }
  207.  
  208.     }
  209.  
  210.  
  211.     if ( changed ) {
  212.         if ( !Heap_list_resort() ) Display_update_row_tops();
  213.     }
  214.  
  215.     return changed;
  216. }
  217.  
  218.  
  219. Boolean Heap_list_resort( void )
  220. {
  221.     Heap_info_handle_t h, last_h;
  222.     Heap_info_t *hi;
  223.     int16 last_weight;
  224.     Boolean changed = false;
  225.  
  226. #if 0
  227.     last_h = h = nil;
  228.     while ( (h = Heap_list_next( h )) ) {
  229.  
  230.         int16 weight = 0;
  231.         Boolean swapped = false;
  232.  
  233.         hi = Heap_list_access( h );
  234.  
  235.         if ( hi->updating ) --weight;
  236.  
  237.         if ( last_h ) {
  238.             Heap_info_t *last_hi = Heap_list_access( last_h );
  239.  
  240.             if ( weight < last_weight ) swapped = true;
  241.             else if ( IUCompString( (StringPtr) hi->appname,
  242.                     (StringPtr) last_hi->appname ) == 1 ) swapped = true;
  243.  
  244.             Heap_list_release( last_h );
  245.         }
  246.  
  247.         Heap_list_release( h );
  248.  
  249.         if ( swapped ) {
  250.             Linked_list_swap( last_h, h );
  251.             last_h = h = nil;        // restart list
  252.             changed = true;
  253.         }
  254.         else {
  255.             last_weight = weight;
  256.             last_h = h;
  257.         }
  258.  
  259.     }
  260. #endif
  261.  
  262.     if ( changed ) Display_update_row_tops();
  263.  
  264.     return changed;
  265. }
  266.  
  267.  
  268. void Heap_list_update_one( Heap_info_handle_t h )
  269. {
  270.     Heap_info_t *hi = l_access( h );
  271.  
  272.     if ( hi->updating ) {
  273.  
  274.         if ( h == multifinder ) hi->zone = HandleZone( MF_handle );
  275.  
  276.         hi->current_size = (int32) hi->zone->bkLim - (int32) &hi->zone->heapData;
  277.         hi->current_free = hi->zone->zcbFree;
  278.         Heap_update_current( h );
  279.     }
  280.  
  281.     l_release( h );
  282. }
  283.  
  284.  
  285.  
  286. /**-----------------------------------------------------------------------------
  287.  **
  288.  ** Private Functions
  289.  **
  290.  **/
  291.  
  292.  
  293. static void init_one( Heap_info_handle_t h, ProcessSerialNumber *psn )
  294. {
  295.     Heap_info_t *hi = l_access( h );
  296.  
  297.     hi->updating = true;
  298.  
  299.     if ( h == multifinder ) {
  300.         pstrcopy( pstr(MultiFinder_STR_x), hi->appname );
  301.         hi->zone = HandleZone( MF_handle );
  302.         hi->heap_is_32_bit = process_heaps_are_32_bit;
  303.         hi->info.processNumber.highLongOfPSN = 0;
  304.         hi->info.processNumber.lowLongOfPSN = kNoProcess;
  305.     }
  306.  
  307.     else if ( h == system ) {
  308.         pstrcopy( pstr(System_STR_x), hi->appname );
  309.         hi->zone = SysZone;
  310.         hi->heap_is_32_bit = system_heap_is_32_bit;
  311.         hi->info.processNumber.highLongOfPSN = 0;
  312.         hi->info.processNumber.lowLongOfPSN = kNoProcess;
  313.     }
  314.  
  315.     else {
  316.         hi->info.processNumber = *psn;
  317.         hi->info.processInfoLength = sizeof( ProcessInfoRec );
  318.         hi->info.processName = (StringPtr) hi->appname;
  319.         hi->info.processAppSpec = nil;
  320.         hi->heap_is_32_bit = process_heaps_are_32_bit;
  321.         if ( GetProcessInformation( &hi->info.processNumber, &hi->info ) ) {
  322.             DebugStr( (unsigned char *)"\pinit_one(): GetProcessInformation() failed." );
  323.             return;
  324.         }
  325.         hi->zone = (THz) hi->info.processLocation;
  326.     }
  327.  
  328.     l_release( h );
  329.  
  330.     Display_new_row( h );
  331.  
  332.     Heap_list_update_one( h );
  333. }
  334.  
  335.