home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / tt / vmem11 / vmem_prg / vm_save.c < prev    next >
C/C++ Source or Header  |  1990-10-02  |  2KB  |  86 lines

  1. #include "vmem.h"
  2.  
  3. /****************************************************/
  4. /* Speichern eines Bereiches in den virt. Speicher: */
  5. /****************************************************/
  6.  
  7. long vm_save (source, destination, count)
  8.     char    *source;
  9.     V_ADR    destination;
  10.     long    count;
  11. {
  12.     BYTE    *page_ptr;
  13.     VPAGE    vmempage;
  14.     WORD    start_offset;
  15.     long    entry_size;
  16.  
  17.     vmempage = GET_PAGE (destination);
  18.     start_offset = GET_OFFSET (destination);
  19.  
  20. #ifdef DEBUG
  21.     printf ("VM_SAVE: page: %x  offset: %x\n", vmempage, start_offset);
  22. #endif
  23.  
  24.     if ((vmempage >= info.count_page) || (flags [vmempage] == FREE))
  25.         return (ILLEGAL_ADDRESS);
  26.  
  27.     if ((count == 0) || (count < -1))
  28.         return (ILLEGAL_COUNT);
  29.  
  30.     entry_size = PAGE_TO_ADDR (MD_COUNT (md_find (vmempage)));
  31.  
  32.     if (count == -1)
  33.         count = entry_size - start_offset;
  34.     else
  35.         entry_size = count = MIN (count, (entry_size - start_offset));
  36.  
  37.     if (start_offset > 0)
  38.     {
  39.         long    size = info.page_size - start_offset;
  40.         
  41.         page_ptr = load_page (vmempage++, CACHE_WRITE) + start_offset;
  42.         if (count <= size)
  43.         {
  44.             memcpy (page_ptr, source, count);
  45.             return (count);
  46.         }
  47.         else
  48.         {
  49.             memcpy (page_ptr, source, size);
  50.             source += size;
  51.             entry_size -= size;
  52.         }
  53.     }
  54.  
  55.     if (entry_size >= info.page_size)
  56.     {
  57.         WORD    wcount;
  58.         long    size;
  59.  
  60.         start_offset = GET_PAGE (entry_size);
  61.         cache_clr (0, MIN (start_offset, info.cache_count));
  62.  
  63.         while (start_offset > 0)
  64.         {
  65.             wcount = MIN (start_offset, info.cache_count);
  66.             size = PAGE_TO_ADDR (wcount);
  67.  
  68.             memcopy (cache, source, size);
  69.             write_sequence (0, wcount, vmempage);
  70.  
  71.             source += size;
  72.             entry_size -= size;
  73.             vmempage += wcount;
  74.             start_offset -= wcount;
  75.         }
  76.     }
  77.  
  78.     if (entry_size > 0)
  79.     {
  80.         page_ptr = load_page (vmempage, CACHE_WRITE);
  81.         memcpy (page_ptr, source, entry_size);
  82.     }
  83.  
  84.     return (count);
  85. }
  86.