home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / System / Swatch / Development / swatch 1.2 / swatch.heap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-11  |  7.0 KB  |  310 lines  |  [TEXT/KAHL]

  1. /**
  2.  
  3.     swatch.heap.c
  4.     Copyright (c) 1990, Adobe Systems, Inc.
  5.  
  6.  **/
  7.  
  8.  
  9. /**-----------------------------------------------------------------------------
  10.  **
  11.  **    Headers
  12.  **
  13.  **/
  14.  
  15. #include "swatch.h"
  16. #include "swatch.prefs.h"
  17. #include "swatch.heap.h"
  18.  
  19.  
  20. /**-----------------------------------------------------------------------------
  21.  **
  22.  ** Private Constants
  23.  **
  24.  **/
  25.  
  26. #define BLOCK_TYPE_MASK                    0xC0
  27. #define BLOCK_FREE                        0x00
  28. #define BLOCK_NON_RELOCATABLE            0x40
  29. #define BLOCK_RELOCATABLE                0x80
  30.  
  31. #define MASTER_POINTER_LOCK_MASK        0x80
  32. #define MASTER_POINTER_PURGE_MASK        0x40
  33. #define MASTER_POINTER_RESOURCE_MASK    0x20
  34.  
  35.  
  36. /**-----------------------------------------------------------------------------
  37.  **
  38.  ** Private Functions
  39.  **
  40.  **/
  41.  
  42. void make_24_bit_heap( Display_stat_t *d );
  43.  
  44. void make_32_bit_heap( Display_stat_t *d );
  45.  
  46.  
  47. /*******************************************************************************
  48.  **
  49.  **    Public Functions
  50.  **
  51.  **/
  52.  
  53. /*******************************************************************************
  54.  ***
  55.  *** prototype
  56.  ***
  57.  *** summary
  58.  ***
  59.  *** History:
  60.  ***
  61.  *** To Do:
  62.  ***
  63.  ***/
  64.  
  65.  
  66. void make_current_heap( Display_stat_t *d )
  67. {
  68.     if ( d->heap_is_32_bit )
  69.         make_32_bit_heap( d );
  70.     else
  71.         make_24_bit_heap( d );
  72. }
  73.  
  74.  
  75. void make_24_bit_heap( Display_stat_t *d )
  76. {
  77.     register Ptr bh, tp;
  78.     register THz zone;
  79.     register int32 chunk_size, size;
  80.     register uns16 run_type, block_type;
  81.     uns16 last_run_type;
  82.     register int32 this_run;
  83.     int32 *p;
  84.     int32 i;
  85.  
  86.     zone = d->app.zone;
  87.     bh = (Ptr) &zone->heapData;
  88.  
  89.     d->current_heap_transitions = 1;
  90.     p = &d->current_heap[0];
  91.     run_type = last_run_type = HEAP_LOCKED_RUN;
  92.     this_run = (int32) run_type << 24;
  93.     chunk_size = 0;
  94.  
  95.     for (;;) {
  96.         if ( bh >= *(Ptr *) BufPtr || ( (int32) bh & 1 ) )
  97.             goto heap_bad;
  98.  
  99.         size = *(int32 *) bh & 0x00FFFFFF;
  100.         block_type = *(unsigned char *) bh & BLOCK_TYPE_MASK;
  101.         if ( (chunk_size += size) >= Swatch_prefs.heap_scale ) {
  102.             if ( run_type == last_run_type )
  103.                 ++this_run;
  104.             else {
  105.                 *p++ = this_run;
  106.                 if ( d->current_heap_transitions == MAX_HEAP_TRANSITIONS )
  107.                     break;
  108.                 ++d->current_heap_transitions;
  109.                 this_run = ((int32) run_type << 24) + 1;
  110.                 last_run_type = run_type;
  111.             }
  112.  
  113.             if ( !block_type )
  114.                 run_type = HEAP_FREE_RUN;
  115.             else if ( block_type == BLOCK_NON_RELOCATABLE )
  116.                 run_type = HEAP_LOCKED_RUN;
  117.             else {
  118.                 tp = (Ptr) zone + *(int32 *) (bh + 4);
  119.                 if ( tp >= *(Ptr *) BufPtr || ( (int32) tp & 1 ) )
  120.                     goto heap_bad;
  121.                 if ( *tp & MASTER_POINTER_LOCK_MASK )
  122.                     run_type = HEAP_LOCKED_RUN;
  123.                 else if ( *tp & MASTER_POINTER_PURGE_MASK )
  124.                     run_type = HEAP_PURGEABLE_RUN;
  125.                 else
  126.                     run_type = HEAP_UNLOCKED_RUN;
  127.             }
  128.             chunk_size -= Swatch_prefs.heap_scale;
  129.             if ( chunk_size >= Swatch_prefs.heap_scale ) {
  130.                 if ( run_type != last_run_type ) {
  131.                     *p++ = this_run;
  132.                     if ( d->current_heap_transitions == MAX_HEAP_TRANSITIONS )
  133.                         break;
  134.                     ++d->current_heap_transitions;
  135.                     this_run = (int32) run_type << 24;
  136.                     last_run_type = run_type;
  137.                 }
  138.                 i = chunk_size >> Swatch_prefs.heap_scale_2n;
  139.                 this_run += i;
  140.                 chunk_size -= (int32) i << Swatch_prefs.heap_scale_2n;
  141.             }
  142.         }
  143.         else if ( run_type != HEAP_LOCKED_RUN ) {
  144.             if ( !block_type )
  145.                 ;
  146.             else if ( block_type == BLOCK_NON_RELOCATABLE )
  147.                 run_type = HEAP_LOCKED_RUN;
  148.             else {
  149.                 tp = (Ptr) zone + *(int32 *) (bh + 4);
  150.                 if ( tp >= *(Ptr *) BufPtr || ( (int32) tp & 1 ) )
  151.                     goto heap_bad;
  152.                 if ( *tp & MASTER_POINTER_LOCK_MASK )
  153.                     run_type = HEAP_LOCKED_RUN;
  154.                 else if ( *tp & MASTER_POINTER_PURGE_MASK )
  155.                     run_type = HEAP_PURGEABLE_RUN;
  156.                 else
  157.                     run_type = HEAP_UNLOCKED_RUN;
  158.             }
  159.         }
  160.  
  161.         bh += size;
  162.         if ( bh < zone->bkLim )
  163.             continue;
  164.  
  165.         if ( bh > zone->bkLim )
  166.             goto heap_bad;
  167.  
  168.         if ( chunk_size )
  169.             ++this_run;
  170.         *p = this_run;
  171.         break;
  172.     }
  173.  
  174.     d->current_heap_ok = TRUE;
  175.     return;
  176.  
  177. heap_bad:
  178.     d->current_heap_ok = FALSE;
  179. }
  180.  
  181.  
  182. /*
  183.  
  184.     A 32-bit block header is:
  185.  
  186.     1 byte block type ($40 = non-relo, etc)
  187.     1 byte block flags ($80 = locked, etc)
  188.     2 bytes alignment
  189.     1 long size of block
  190.     1 long relative handle, etc
  191.  
  192.     The physical size of the block is in the second long word. To get to the next block
  193.     header, just add the size of the block.
  194.  
  195. */
  196.  
  197. typedef struct {
  198.     unsigned char type;
  199.     unsigned char flags;
  200.     int16 alignment;
  201.     int32 size;
  202.     int32 relative_handle;
  203. } header_32_t;
  204.  
  205. void make_32_bit_heap( Display_stat_t *d )
  206. {
  207.     register Ptr tp, bh;
  208.     register THz zone;
  209.     register int32 chunk_size, size;
  210.     register uns16 run_type, block_type;
  211.     uns16 last_run_type;
  212.     register int32 this_run;
  213.     int32 *p;
  214.     int32 i;
  215.  
  216.     zone = d->app.zone;
  217.     bh = (Ptr) &zone->heapData;
  218.  
  219.     d->current_heap_transitions = 1;
  220.     p = &d->current_heap[0];
  221.     run_type = last_run_type = HEAP_LOCKED_RUN;
  222.     this_run = (int32) run_type << 24;
  223.     chunk_size = 0;
  224.  
  225.     for (;;) {
  226.         if ( bh >= *(Ptr *) BufPtr || ( (int32) bh & 1 ) )
  227.             goto heap_bad;
  228.  
  229.         size = ((header_32_t *) bh)->size;
  230.         block_type = ((header_32_t *) bh)->type;
  231.         if ( (chunk_size += size) >= Swatch_prefs.heap_scale ) {
  232.             if ( run_type == last_run_type )
  233.                 ++this_run;
  234.             else {
  235.                 *p++ = this_run;
  236.                 if ( d->current_heap_transitions == MAX_HEAP_TRANSITIONS )
  237.                     break;
  238.                 ++d->current_heap_transitions;
  239.                 this_run = ((int32) run_type << 24) + 1;
  240.                 last_run_type = run_type;
  241.             }
  242.  
  243.             if ( block_type == BLOCK_FREE )
  244.                 run_type = HEAP_FREE_RUN;
  245.             else if ( block_type == BLOCK_NON_RELOCATABLE )
  246.                 run_type = HEAP_LOCKED_RUN;
  247.             else {
  248.                 tp = (Ptr) zone + ((header_32_t *) bh)->relative_handle;
  249.                 if ( tp >= *(Ptr *) BufPtr || ( (int32) tp & 1 ) )
  250.                     goto heap_bad;
  251.                 if ( ((header_32_t *) bh)->flags & MASTER_POINTER_LOCK_MASK )
  252.                     run_type = HEAP_LOCKED_RUN;
  253.                 else if ( ((header_32_t *) bh)->flags & MASTER_POINTER_PURGE_MASK )
  254.                     run_type = HEAP_PURGEABLE_RUN;
  255.                 else
  256.                     run_type = HEAP_UNLOCKED_RUN;
  257.             }
  258.             chunk_size -= Swatch_prefs.heap_scale;
  259.             if ( chunk_size >= Swatch_prefs.heap_scale ) {
  260.                 if ( run_type != last_run_type ) {
  261.                     *p++ = this_run;
  262.                     if ( d->current_heap_transitions == MAX_HEAP_TRANSITIONS )
  263.                         break;
  264.                     ++d->current_heap_transitions;
  265.                     this_run = (int32) run_type << 24;
  266.                     last_run_type = run_type;
  267.                 }
  268.                 i = chunk_size >> Swatch_prefs.heap_scale_2n;
  269.                 this_run += i;
  270.                 chunk_size -= (int32) i << Swatch_prefs.heap_scale_2n;
  271.             }
  272.         }
  273.         else if ( run_type != HEAP_LOCKED_RUN ) {
  274.             if ( block_type == BLOCK_FREE )
  275.                 ;
  276.             else if ( block_type == BLOCK_NON_RELOCATABLE )
  277.                 run_type = HEAP_LOCKED_RUN;
  278.             else {
  279.                 tp = (Ptr) zone + ((header_32_t *) bh)->relative_handle;
  280.                 if ( tp >= *(Ptr *) BufPtr || ( (int32) tp & 1 ) )
  281.                     goto heap_bad;
  282.                 if ( ((header_32_t *) bh)->flags & MASTER_POINTER_LOCK_MASK )
  283.                     run_type = HEAP_LOCKED_RUN;
  284.                 else if ( ((header_32_t *) bh)->flags & MASTER_POINTER_PURGE_MASK )
  285.                     run_type = HEAP_PURGEABLE_RUN;
  286.                 else
  287.                     run_type = HEAP_UNLOCKED_RUN;
  288.             }
  289.         }
  290.  
  291.         bh += size;
  292.         if ( bh < zone->bkLim )
  293.             continue;
  294.  
  295.         if ( bh > zone->bkLim )
  296.             goto heap_bad;
  297.  
  298.         if ( chunk_size )
  299.             ++this_run;
  300.         *p = this_run;
  301.         break;
  302.     }
  303.  
  304.     d->current_heap_ok = TRUE;
  305.     return;
  306.  
  307. heap_bad:
  308.     d->current_heap_ok = FALSE;
  309. }
  310.