home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l440 / 2.ddi / CHAP5 / DOSSWAP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-21  |  5.7 KB  |  185 lines

  1. /* DOSSWAP.C - Functions to manage DOS swap areas */
  2.  
  3. #include <stdlib.h>
  4. #include <dos.h>
  5. #include <memory.h>
  6. #include "tsr.h"
  7.  
  8. #define GET_DOSSWAP3       0x5d06
  9. #define GET_DOSSWAP4       0x5d0b
  10.  
  11. #define SWAP_LIST_LIMIT    20     
  12.  
  13. struct  swap_list    /* format of DOS 4+ SDA list */
  14. {
  15.     void    far*    swap_ptr;
  16.             int     swap_size;
  17. };
  18.  
  19. /* variables for 3.x swap work */
  20. static   char  far *  swap_ptr;  /* pointer to dos swap area */
  21. static   char  far *  swap_save; /* pointer to our local save area */
  22. static   int   swap_size_indos;
  23. static   int   swap_size_always;
  24. static   int   size;
  25.  
  26. /* variables for 4.x swap work */
  27. static int swap_count;      /* count of swappable areas */
  28. static struct swap_list swp_list[SWAP_LIST_LIMIT]; /*list of swap areas*/
  29. static char far *swp_save[SWAP_LIST_LIMIT];   /* out save area */
  30. static int swp_flag[SWAP_LIST_LIMIT];   /* flags if has been swapped */
  31.  
  32. static int dos_level;    /* for level dependent code */
  33. int dos_critical;       /* in critical section, can't swap */
  34.  
  35. /*****
  36. Function: InitDosSwap
  37. Initialize pointers and sizes of DOS swap area. Return zero if success
  38. *****/
  39. int InitDosSwap(void)
  40.     union  REGS regs;
  41.     struct SREGS segregs;
  42.  
  43.     if ((_osmajor == 3) && (_osminor >= 10))
  44.         dos_level = 3;
  45.     else if (_osmajor >= 4)
  46.         dos_level = 4;
  47.     else
  48.         dos_level = 0;
  49.     
  50.     if (dos_level == 3)    /* use 215D06 */
  51.     {
  52.         regs.x.ax = GET_DOSSWAP3;   
  53.         intdosx(®s,®s,&segregs);
  54.         /* pointer to swap area is returned in DS:SI */
  55.         FP_SEG(swap_ptr) = segregs.ds;
  56.         FP_OFF(swap_ptr) = regs.x.si;
  57.    
  58.         swap_size_indos = regs.x.cx;
  59.         swap_size_always= regs.x.dx;
  60.  
  61.         size = 0;  /* initialize for later */
  62.         return ((swap_save = malloc(swap_size_indos)) == 0);
  63.     }
  64. /*
  65.     NOTE: Next line changed from version in UNDOCUMENTED DOS, p. 322
  66.     (first printing), where we tested DOS version number with == and
  67.     thereby lose the opportunity to run in DOS 5. This was just the
  68.     sort of thing we had warned against in Chapter 2 of the book!
  69. */
  70.     else if (dos_level >= 4)  /* use 5d0b */
  71.     {
  72.         struct swap_list far *ptr;
  73.         int far *iptr;
  74.         int i;
  75.         regs.x.ax = GET_DOSSWAP4;   
  76.         intdosx(®s,®s,&segregs);
  77.         /* pointer to swap list is returned in DS:SI */
  78.         FP_SEG(iptr) = segregs.ds;
  79.         FP_OFF(iptr) = regs.x.si;
  80.         swap_count = *iptr;                   /* get size of list */
  81.         iptr++;
  82.         ptr = (struct swap_list far *) iptr;  /* create point to list */
  83.  
  84.         if (swap_count > SWAP_LIST_LIMIT)     /* too many data areas */
  85.             return 2;
  86.  
  87.         /* get pointers and sizes of data areas */
  88.         for (i = 0; i < swap_count; i++)
  89.         {
  90.             swp_list[i].swap_ptr = ptr->swap_ptr;
  91.             swp_list[i].swap_size= ptr->swap_size;
  92.             if (! (swp_save[i] = malloc(swp_list[i].swap_size & 0x7fff)))
  93.                 return 3;   /* out of memory */
  94.             swp_flag[i] = 0;
  95.             ptr++;   /* point to next entry in the list */
  96.         }
  97.         return 0;
  98.     }
  99.     else
  100.         return 1;   /* unsupported DOS */
  101. }
  102.  
  103. /*****
  104. Function: SaveDosSwap
  105. This function will save the dos swap area to a local buffer
  106. It returns zero on success, non-zero meaning can't swap 
  107. *****/ 
  108. int SaveDosSwap(void)
  109. {
  110.     if (dos_level == 3)
  111.     {
  112.         if (swap_ptr && !dos_critical)   
  113.         {
  114.             /* if INDOS flag is zero, use smaller swap size */
  115.             size = (*indos_ptr) ? swap_size_indos : swap_size_always;
  116.               
  117.             movedata(FP_SEG(swap_ptr),  FP_OFF(swap_ptr), 
  118.                      FP_SEG(swap_save), FP_OFF(swap_save),
  119.                      size);
  120.         }
  121.         else       /* can't swap it */
  122.             return 1;
  123.     }
  124.     else if (dos_level == 4)
  125.     {
  126.         /* loop through pointer list and swap appropriate items */
  127.         int i;
  128.         for (i = 0; i < swap_count; i++)
  129.         {
  130.             if (swp_list[i].swap_size & 0x8000)  /* swap always */
  131.             {
  132.                 movedata(FP_SEG(swp_list[i].swap_ptr), 
  133.                          FP_OFF(swp_list[i].swap_ptr),
  134.                          FP_SEG(swp_save[i]),          
  135.                          FP_OFF(swp_save[i]),
  136.                          swp_list[i].swap_size & 0x7fff);
  137.             }
  138.             else if (*indos_ptr)     /* swap only if dos busy */
  139.             {
  140.                 movedata(FP_SEG(swp_list[i].swap_ptr), 
  141.                          FP_OFF(swp_list[i].swap_ptr),
  142.                          FP_SEG(swp_save[i]),          
  143.                          FP_OFF(swp_save[i]),
  144.                          swp_list[i].swap_size);
  145.             }
  146.         }
  147.     }
  148.     else 
  149.         return 1;
  150.    
  151.     return 0;
  152. }
  153.  
  154. /*****
  155. Function: RestoreDosSwap
  156. This function will restore a previously swapped dos data area
  157. *****/
  158. void RestoreDosSwap(void)
  159. {
  160.     if (dos_level == 3)
  161.     {
  162.         /* make sure its already saved and we have a good ptr */
  163.         if (size && swap_ptr)
  164.         {
  165.             movedata(FP_SEG(swap_save), FP_OFF(swap_save), 
  166.                      FP_SEG(swap_ptr),  FP_OFF(swap_ptr), size);
  167.             size = 0;
  168.         }
  169.     }
  170.     else if (dos_level == 4)
  171.     {
  172.         int i;
  173.         for (i = 0; i < swap_count; i++)
  174.         {
  175.             movedata(FP_SEG(swp_save[i]),          
  176.                      FP_OFF(swp_save[i]),
  177.                      FP_SEG(swp_list[i].swap_ptr), 
  178.                      FP_OFF(swp_list[i].swap_ptr),
  179.                      swp_list[i].swap_size);
  180.            swp_flag[i] = 0;   /* clear flag */
  181.         }
  182.     }
  183. }   
  184.