home *** CD-ROM | disk | FTP | other *** search
/ Beijing Paradise BBS Backup / PARADISE.ISO / software / BBSDOORW / PEEK_222.ZIP / PEEKSRC.ZIP / PEEKSWAP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-30  |  4.8 KB  |  147 lines

  1. #include "stdlib.h"
  2. #include "alloc.h"
  3. #include "stdio.h"
  4. #include "conio.h"
  5. #include "dos.h"
  6. #include "string.h"
  7. #include "mem.h"
  8.  
  9. #ifdef __TURBOC__
  10.   #define _fastcall pascal
  11. #endif
  12.  
  13. void cdecl   bugprint(char *,...);
  14.  
  15. extern char   debug;
  16. char   SwapName[66];
  17. extern char   swapname[];
  18. extern char   useswapdisk;
  19. extern char   LIMEMS;
  20. unsigned long BytesSwapped;
  21. unsigned int  PrefixSeg;
  22. char          FileAllocated=0;
  23. unsigned int  EmsHandle;
  24. char          EmsAllocated;
  25. unsigned int  FrameSeg;
  26. unsigned int  FileHandle;
  27. unsigned      EmsPageSize=16384;
  28.  
  29. #define FALSE 0
  30.  
  31. extern unsigned cdecl _psp;
  32.  
  33.  
  34. /* NOTE!  If you're using this in a memory model other than any of the
  35. **  Large code size models, you'll need to make these all *far* calls!
  36. */
  37.  
  38. extern char far pascal ALLOCATESWAPFILE(), far pascal EMSINSTALLED();
  39. extern unsigned far pascal EMSPAGEFRAME(), far pascal ALLOCATEEMSPAGES (unsigned),
  40.   far pascal EXECWITHSWAP (char far *, char far *);
  41. extern void far pascal DEALLOCATEEMSHANDLE(unsigned), huge FIRSTTOSAVE(),
  42.   far pascal DEALLOCATESWAPFILE();
  43.  
  44.  
  45. int _fastcall doswap (char far *command_str, char far *args)
  46.  
  47. /*
  48. **    The first calling parameter *must* include the complete program name
  49. **  and path, including the program name's file extension!  You can easily
  50. **  get this with "searchpath()"    Example:  p = searchpath("PKZIP.EXE");
  51. **  and then pass "p" as the first parameter to "doswap()".
  52. **
  53. **    The second calling parameter above is a string of parameters to pass
  54. **  to the external program, or "" if none.  With the example given above,
  55. **  you might use "-ex arc-name filesone.ext filestwo.exe" to archive both
  56. **  the "filesone.ext" and "filestwo.exe" into ARC-NAME.ZIP using PKZip.
  57. */
  58.  
  59. {
  60.  char exec_tail[128], exec_file[128], far *ivt, huge *end_mem,
  61.    huge *start_mem, far *vptr;
  62.  int swapcode;
  63.  long freefarmem;
  64.  unsigned EMS_pagesneeded;
  65.  extern char **_heaptop;
  66.  
  67.   sprintf(SwapName, "%c:\\%s",useswapdisk,swapname);
  68.   vptr = MK_FP(0x0000, 0x0000);
  69.   ivt = farmalloc(1024);
  70.  
  71. /*
  72.  
  73. THIS IS THE REAL WAY TO DO THE FOLLOWING, BUT UNFORTUNATELY, TURBO C CHEATS
  74. AND CLAIMS *ALL* AVAILABLE RAM AT LOAD TIME, AND DOESN'T HAVE TO BOTHER WITH
  75. ADJUSTING THIS WHEN MEMORY IS DYNAMICALLY ALLOCATED.  THE ONLY TIME IT IS SET
  76. IS WHEN THE "spawnlp()" PROCEDURE SHELLS OUT or "system()" IS USED SO PROGRAMS
  77. HAVE MEMORY AVAILABLE.  Oh, well - so much for "doing it right" <grin>...
  78.         (the unsigned integer at _PSP+2 is supposed to be the
  79.          exact number of paragraphs the program has assigned)
  80.  
  81.  
  82.  unsigned last_seg;
  83.  long memallocated;
  84.  
  85.   last_seg = *((unsigned far *)(MK_FP(_psp, 2)));
  86.   memallocated = 16L * (long)(last_seg - _psp);
  87.  
  88.  
  89. Instead I compute the exact size of memory between the _FirstToSwap label in
  90. the SWAP.ASM code and **__heaptop, the end of available memory, then subtract
  91. the amount of available dynamic memory (far heap) and fudge it by 3k hopefully
  92. getting away with there being less than 3k worth of fragmentation from holes
  93. left in that area by freeing pointers.  If less than 3k of far RAM is free, I
  94. swap the whole thing - right to the top of memory but never beyond.
  95.  
  96. */
  97.  
  98.   start_mem = (char huge *) FIRSTTOSAVE;
  99.   end_mem = (char huge *) _heaptop;
  100.   freefarmem = farcoreleft();
  101.   if (freefarmem < 3072) freefarmem = 0;
  102.   else freefarmem -= 3072;
  103.   BytesSwapped = (unsigned long) (end_mem - start_mem) - freefarmem;
  104.  
  105. /* ----------------- */
  106.  
  107.   PrefixSeg = _psp;
  108.  
  109.   if ((LIMEMS) && (EMSINSTALLED ())) {
  110.     EMS_pagesneeded = (unsigned) ((BytesSwapped + EmsPageSize + 1) / EmsPageSize);
  111.     if ((EmsHandle = ALLOCATEEMSPAGES (EMS_pagesneeded)) != 0xffff) {
  112.       EmsAllocated = 1;
  113.       FrameSeg = EMSPAGEFRAME();
  114.       if(debug)bugprint("Swapping %lu bytes to EMS - %u pages (%dk)\r\n", BytesSwapped, EMS_pagesneeded, 16 * EMS_pagesneeded);
  115.     }
  116.   }
  117.   else EmsAllocated = FALSE;
  118.   if (!EmsAllocated && (FileAllocated = ALLOCATESWAPFILE ()) == 0) {
  119.     if (debug) bugprint("ERROR! Cannot Swap to EMS or Disk File `%s'\r\n", SwapName);
  120.     return(-1);
  121.   }
  122.   if (FileAllocated)
  123.     if (debug) bugprint("Swapping %lu bytes to disk file `%s'...\r\n", BytesSwapped, SwapName);
  124.   strcpy (exec_file, command_str);
  125.   strcpy (exec_tail, " ");
  126.   strcat (exec_tail, args);
  127.   strcat (exec_tail, "\r");
  128.   exec_tail[0] = (char) strlen (exec_tail) - 2;
  129.   disable();
  130.   memcpy((char far *) ivt, (char far *) vptr, 1024);
  131.   enable();
  132.   swapcode = EXECWITHSWAP ((char far *) exec_file, (char far *) exec_tail);
  133.   disable();
  134.   memcpy((char far *) vptr, (char far *) ivt, 1024);
  135.   enable();
  136.   if (EmsAllocated) {
  137.     DEALLOCATEEMSHANDLE (EmsHandle);
  138.     EmsAllocated = 0;
  139.   }
  140.   else if (FileAllocated) {
  141.     DEALLOCATESWAPFILE ();
  142.     FileAllocated = 0;
  143.   }
  144.   farfree ((char far *) ivt);
  145.   return (swapcode);
  146. }
  147.