home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / DOOM / EDITOR / DEU51 / SOURCE / SWAPMEM.C < prev    next >
C/C++ Source or Header  |  1994-04-24  |  8KB  |  292 lines

  1. /*
  2.    Memory swapping by Raphaël Quinet <quinet@montefiore.ulg.ac.be>
  3.           and Christian Johannes Schladetsch <s924706@yallara.cs.rmit.OZ.AU>
  4.  
  5.    You are allowed to use any parts of this code in another program, as
  6.    long as you give credits to the authors in the documentation and in
  7.    the program itself.  Read the file README.1ST for more information.
  8.  
  9.    This program comes with absolutely no warranty.
  10.  
  11.    SWAPMEM.C - When the memory is low....
  12.  
  13.    Note from RQ:
  14.       Yuck!  I don't like this horrible thing.  The program should be
  15.       able to swap almost anything to XMS or to disk, not only the
  16.       five objects used here (Things, LineDefs, SideDefs, Vertexes and
  17.       Sectors).  That was a quick and dirty hack...  I didn't have the
  18.       time to write a cleaner code...
  19.  
  20.    Note2 from RQ:
  21.       After having tested these routines, I see that they are not very
  22.       useful...  I'm still getting "out of memory" errors while editing
  23.       E2M7 and other huge levels.  I should rewrite all this for GCC,
  24.       use a flat memory model and a DOS extender, then just delete all
  25.       this code...  I will have to do that anyway if I want to port it
  26.       to other systems (Unix, Linux), so why not?
  27.       Moral of the story: never waste long hours writing high-level
  28.       memory swapping routines on a deficient OS.  Use a real OS with
  29.       a better memory management instead.
  30.  
  31.    Note for CJS:
  32.       It should be easy to include your XMS code in this file.  Just
  33.       add the necessary lines in InitSwap(), SwapIn() and SwapOut().
  34.       You won't need to edit any other file.  Put all your routines
  35.       in XMS.C, with the necessary includes in XMS.H.  Please keep it
  36.       short and simple... :-)
  37.       ... And delete this note once you're done.
  38. */
  39.  
  40. /* the includes and typedefs */
  41. #include "deu.h"
  42. #include "levels.h"
  43. #ifdef SWAP_TO_XMS
  44. #include "xms.h"
  45. typedef XMSHandle SwapHandle;    /* XMS handle */
  46. #define INVALID_HANDLE        -1
  47. #else
  48. typedef char SwapHandle[ 128];    /* name of the temporary disk file */
  49. #define INVALID_HANDLE        "..."
  50. #endif /* SWAP_TO_XMS */
  51.  
  52. /* global variables */
  53. Bool NeedThings = FALSE;
  54. Bool NeedLineDefs = FALSE;
  55. Bool NeedSideDefs = FALSE;
  56. Bool NeedVertexes = FALSE;
  57. Bool NeedSectors = FALSE;
  58. SwapHandle ThingsH;
  59. SwapHandle LineDefsH;
  60. SwapHandle SideDefsH;
  61. SwapHandle VertexesH;
  62. SwapHandle SectorsH;
  63.  
  64.  
  65. /*
  66.    do the necessary initialisation for the secondary storage
  67. */
  68.  
  69. void InitSwap()
  70. {
  71. #ifdef SWAP_TO_XMS
  72.    /* Init XMS */
  73.    ...
  74. #else
  75.    strcpy(ThingsH, INVALID_HANDLE);
  76.    strcpy(LineDefsH, INVALID_HANDLE);
  77.    strcpy(SideDefsH, INVALID_HANDLE);
  78.    strcpy(VertexesH, INVALID_HANDLE);
  79.    strcpy(SectorsH, INVALID_HANDLE);
  80. #endif /* SWAP_TO_XMS */
  81. }
  82.  
  83.  
  84.  
  85. /*
  86.    moves an object from secondary storage to lower RAM
  87. */
  88.  
  89. void huge *SwapIn( SwapHandle handle, unsigned long size)
  90. {
  91.    void huge *ptr;
  92. #ifdef SWAP_TO_XMS
  93.    /* allocate a new memory block (in lower RAM) */
  94.    ptr = GetFarMemory( size);
  95.    /* read the data from XMS */
  96.    ...
  97.    /* free the XMS memory block */
  98.    ...
  99.    /* delete the handle */
  100.    ...
  101. #else
  102.    FILE      *file;
  103.    char huge *data;
  104.    SwapHandle oldhandle;
  105.  
  106.    /* Note from RQ:
  107.      the following test is there to prevent an infinite loop when
  108.      SwapIn calls GetFarMemory, which calls FreeSomeMemory, which
  109.      in turn calls SwapOut, then SwapIn...
  110.     */
  111.    if (! strcmp( handle, INVALID_HANDLE))
  112.       return NULL;
  113. #ifdef DEBUG
  114.    LogMessage( "swapping in %lu bytes from %s\n", size, handle);
  115. #endif /* DEBUG */
  116.    strcpy( oldhandle, handle);
  117.    /* invalidate the handle (must be before "GetFarMemory") */
  118.    strcpy( handle, INVALID_HANDLE);
  119.    /* allocate a new memory block (in lower RAM) */
  120.    ptr = GetFarMemory( size);
  121.    /* read the data from the temporary file */
  122.    file = fopen( oldhandle, "rb");
  123.    data = ptr;
  124.    if (file == NULL)
  125.       ProgError( "error opening temporary file \"%s\"", handle);
  126.    while (size > 0x8000)
  127.    {
  128.       if (fread( data, 1, 0x8000, file) != 0x8000)
  129.      ProgError( "error reading from temporary file \"%s\"", handle);
  130.       data = data + 0x8000;
  131.       size -= 0x8000;
  132.    }
  133.    if (fread( data, 1, size, file) != size)
  134.       ProgError( "error reading from temporary file \"%s\"", handle);
  135.    fclose( file);
  136.    /* delete the file */
  137.    unlink( oldhandle);
  138. #endif /* !SWAP_TO_XMS */
  139.    return ptr;
  140. }
  141.  
  142.  
  143.  
  144. /*
  145.    moves an object from lower RAM to secondary storage
  146. */
  147.  
  148. void SwapOut( void huge *ptr, SwapHandle handle, unsigned long size)
  149. {
  150. #ifdef SWAP_TO_XMS
  151.    /* get a new XMS handle */
  152.    ...
  153.    /* write the data to XMS */
  154.    ...
  155. #else
  156.    FILE      *file;
  157.    char huge *data;
  158.    char      *tmp;
  159.  
  160.    /* get a new (unique) file name */
  161.    tmp = tempnam( NULL, "{DEU}");
  162.    if (tmp == NULL)
  163.       ProgError( "cannot create a temporary file name (out of memory)");
  164.    strcpy( handle, tmp);
  165.    free( tmp);
  166. #ifdef DEBUG
  167.    LogMessage( "swapping out %lu bytes to %s\n", size, handle);
  168. #endif /* DEBUG */
  169.    /* write the data to the temporary file */
  170.    data = ptr;
  171.    file = fopen( handle, "wb");
  172.    if (file == NULL)
  173.       ProgError( "error creating temporary file \"%s\"", handle);
  174.    while (size > 0x8000)
  175.    {
  176.       if (fwrite( data, 1, 0x8000, file) != 0x8000)
  177.      ProgError( "error writing to temporary file \"%s\"", handle);
  178.       data = data + 0x8000;
  179.       size -= 0x8000;
  180.    }
  181.    if (fwrite( data, 1, size, file) != size)
  182.       ProgError( "error writing to temporary file \"%s\"", handle);
  183.    fclose( file);
  184. #endif /* !SWAP_TO_XMS */
  185.    /* free the data block (in lower RAM) */
  186.    FreeFarMemory( ptr);
  187. }
  188.  
  189.  
  190.  
  191. /*
  192.    get the objects needed (if they aren't already in memory)
  193. */
  194.  
  195. void SwapInObjects()
  196. {
  197.    if (NeedThings && NumThings > 0 && Things == NULL)
  198.       Things = SwapIn( ThingsH, (unsigned long) NumThings * sizeof (struct Thing));
  199.    if (NeedLineDefs && NumLineDefs > 0 && LineDefs == NULL)
  200.       LineDefs = SwapIn( LineDefsH, (unsigned long) NumLineDefs * sizeof (struct LineDef));
  201.    if (NeedSideDefs && NumSideDefs > 0 && SideDefs == NULL)
  202.       SideDefs = SwapIn( SideDefsH, (unsigned long) NumSideDefs * sizeof (struct SideDef));
  203.    if (NeedVertexes && NumVertexes > 0 && Vertexes == NULL)
  204.       Vertexes = SwapIn( VertexesH, (unsigned long) NumVertexes * sizeof (struct Vertex));
  205.    if (NeedSectors && NumSectors > 0 && Sectors == NULL)
  206.       Sectors = SwapIn( SectorsH, (unsigned long) NumSectors * sizeof (struct Sector));
  207. }
  208.  
  209.  
  210. /*
  211.    mark the objects that should be in lower RAM
  212. */
  213.  
  214. void ObjectsNeeded( int objtype, ...)
  215. {
  216.    va_list args;
  217.  
  218.    /* get the list of objects */
  219.    NeedThings = FALSE;
  220.    NeedLineDefs = FALSE;
  221.    NeedSideDefs = FALSE;
  222.    NeedVertexes = FALSE;
  223.    NeedSectors = FALSE;
  224.    va_start( args, objtype);
  225.    while (objtype > 0)
  226.    {
  227.       switch (objtype)
  228.       {
  229.       case OBJ_THINGS:
  230.      NeedThings = TRUE;
  231.      break;
  232.       case OBJ_LINEDEFS:
  233.      NeedLineDefs = TRUE;
  234.      break;
  235.       case OBJ_SIDEDEFS:
  236.      NeedSideDefs = TRUE;
  237.      break;
  238.       case OBJ_VERTEXES:
  239.      NeedVertexes = TRUE;
  240.      break;
  241.       case OBJ_SECTORS:
  242.      NeedSectors = TRUE;
  243.      break;
  244.       }
  245.       objtype = va_arg( args, int);
  246.    }
  247.    va_end( args);
  248.    /* get the objects if they aren't already in memory */
  249.    SwapInObjects();
  250. }
  251.  
  252.  
  253.  
  254. /*
  255.    free as much memory as possible by moving some objects out of lower RAM
  256. */
  257.  
  258. void FreeSomeMemory()
  259. {
  260.    /* move everything to secondary storage */
  261.    if (NumSectors > 0 && Sectors != NULL)
  262.    {
  263.       SwapOut( Sectors, SectorsH, (unsigned long) NumSectors * sizeof (struct Sector));
  264.       Sectors = NULL;
  265.    }
  266.    if (NumVertexes > 0 && Vertexes != NULL)
  267.    {
  268.       SwapOut( Vertexes, VertexesH, (unsigned long) NumVertexes * sizeof (struct Vertex));
  269.       Vertexes = NULL;
  270.    }
  271.    if (NumSideDefs > 0 && SideDefs != NULL)
  272.    {
  273.       SwapOut( SideDefs, SideDefsH, (unsigned long) NumSideDefs * sizeof (struct SideDef));
  274.       SideDefs = NULL;
  275.    }
  276.    if (NumLineDefs > 0 && LineDefs != NULL)
  277.    {
  278.       SwapOut( LineDefs, LineDefsH, (unsigned long) NumLineDefs * sizeof (struct LineDef));
  279.       LineDefs = NULL;
  280.    }
  281.    if (NumThings > 0 && Things != NULL)
  282.    {
  283.       SwapOut( Things, ThingsH, (unsigned long) NumThings * sizeof (struct Thing));
  284.       Things = NULL;
  285.    }
  286.    /* re-load the objects that are needed */
  287.    SwapInObjects();
  288. }
  289.  
  290.  
  291. /* end of file */
  292.