home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 328_01 / demoheap.c < prev    next >
C/C++ Source or Header  |  1991-02-28  |  3KB  |  118 lines

  1. /* demoheap.c
  2.  *    this file shows how to allocate blocks on the heap,
  3.  *    free some of them, allocate more later, 
  4.  *    access them at different times, etc...
  5.  */
  6. #include <stdlib.h>
  7. #include <alloc.h>
  8. #include <string.h>
  9. #include <dos.h>
  10.  
  11. #include "wtwg.h"
  12.  
  13. #define farmemcpy(dest,src,num)  \
  14.     movedata (FP_SEG(src), FP_OFF(src), FP_SEG(dest), FP_OFF(dest), num )
  15.  
  16.  
  17. typedef WHEAP  *WHEAP_PTR;
  18.  
  19. main ()
  20.     {
  21.     #define HPMAX    50            /* number of heap items. */
  22.     #define ELSZ     100        /* bytes per item. */
  23.     WHEAP_PTR  hlist[HPMAX];    /* array of ptrs to WHEAP elements */
  24.  
  25.     int n;
  26.     int priority;
  27.     char far *ptr;
  28.  
  29.     char buff[ELSZ];
  30.  
  31.  
  32.     winit ('T' );
  33.     
  34.     /* The heap manager allocates first to expanded memory until that is full,
  35.      * then to far ram, and when that runs out, allocates to disk.
  36.      * 
  37.      * The amount of far ram used is limitted by the global variable
  38.      * wheap_ramlimit. 
  39.      * Setting wheap_ramlimit to 0 shuts off use of ram (only xms & disk used)
  40.      * Setting wheap_ramlimit to very large (1 MEG) allows use of all avail ram
  41.      * If you have data that MUST be stored in regular (far) ram,
  42.      * or in LARGE model want enough ram left over for important data, 
  43.      *        set wheap_limit large enough to hold that data.
  44.      *  
  45.      */
  46.      wheap_ramlimit = 1000000L; /* shuts off use of ram for demo prog. */
  47.     
  48.  
  49.     wclear ();
  50.  
  51.     for ( n= 0; n<HPMAX; ++n )
  52.         {
  53.         priority = n%3 +1;    /* use priorities that rotate 1-2-3 */
  54.         
  55.         
  56.         hlist[n] = wheap_alloc ( ELSZ, priority, "main" );
  57.         
  58.         wprintf("item #%3.3i. Stored %s. Remaining expanded memory = %0.7ul\n", 
  59.                 n,     ( hlist[n]->whp_flag == WHP_DSK )? "on disk": "in xram", 
  60.                 wheap_availxms() );
  61.  
  62.         ptr = wheap_access ( hlist[n] , 0);
  63.  
  64.         sprintf ( buff, "heap element #%i\n",n);
  65.  
  66.         farmemcpy ( ptr, buff, 1+strlen (buff) );
  67.  
  68.  
  69.         wheap_deaccess ( hlist[n], 1 );
  70.  
  71.         if ( n==3 )
  72.             {
  73.             /* Demo how to move a heap item from RAM/XMS to disk
  74.              */
  75.             wheap_swap2dsk ( hlist[n] );
  76.             }
  77.             
  78.  
  79.         if ( n==2  )
  80.             {
  81.             wheap_free ( hlist[n] );
  82.             }
  83.  
  84.         }
  85.     /* Demo how to swap all heap elements of a specified priority to disk
  86.      */
  87.     wheap_swapall2dsk ( 2 );        /* priority 2 items get swapped */
  88.  
  89.  
  90.  
  91.     /* read the items back from the heap
  92.      */
  93.     for ( n=0; n<HPMAX; ++n )
  94.         {
  95.         if (n !=2  )    /* number 2 was freed earlier */
  96.             {
  97.  
  98.             ptr = wheap_access ( hlist[n], 1 );
  99.             
  100.  
  101.             farmemcpy ( buff, ptr, ELSZ );
  102.  
  103.             wputs (buff);
  104.             wheap_free ( hlist[n] );
  105.  
  106.             }
  107.         }
  108.  
  109.  
  110.     wputs ( "\nthats all" );
  111.     
  112.     
  113.     /*    The disk file is automatically deleted 
  114.      *  and expanded memory returned to the system 
  115.      *  by the shutdown routine. 
  116.      */
  117.     return (0);
  118.     }