home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / tt / vmem11 / vmem_prg / vmem.c < prev    next >
C/C++ Source or Header  |  1991-06-08  |  6KB  |  231 lines

  1. #include "vmem.h"
  2.  
  3. /**********************/
  4. /* Globale Variablen: */
  5. /**********************/
  6.  
  7. V_INFO        info;        /* Informationsblock */
  8.  
  9. WORD    max_page;        /* bisher größte Datei-Seiten-Nummer */
  10.  
  11. BYTE    *flags;            /* Flags für den virtuellen Speicher */
  12.  
  13. CPAGE    *cache_page;    /* Umsetztabelle Cache-Seite => Speicher-Seite */
  14. BYTE    *cache_flags;    /* Flags für den Cache */
  15. ULONG    *cache_age;        /* LRU-Age für den Cache */
  16. WORD    age_count;        /* Anzahl der Age-LONGS */
  17. ULONG    lru_count;        /* Timer für LRU-Aging */
  18.  
  19. BYTE    *cache;            /* eigentlicher Seiten-Cache */
  20. BYTE    *buffer;        /* Puffer für eine Seite */
  21.  
  22. int        sector_flag;    /* Flag für die Verwendung von BIOS-Routinen */
  23. WORD    *sector_no;        /* Sektor-Nummern der jeweiligen Seiten */
  24.  
  25. BPB        *bpb_block;        /* Zeiger auf aktuellen BPB-Block */
  26. WORD    size_bits;
  27. WORD    record_bits;
  28. WORD    cluster_bits;
  29. WORD    cluster_size;
  30. WORD    sector_mask;
  31. WORD    sectors;
  32. WORD    drive;
  33.  
  34. WORD    offset_bits;    /* Anzahl der Bits, die den Offset bestimmen */
  35. WORD    offset_mask;    /* Maske für die Offset-Bits */
  36.  
  37. MD        used_list;        /* Belegtliste */
  38. MD        free_list;        /* Freiliste */
  39.  
  40. MEMORY    md_array [MAX_BLOCKS];    /* Speicher für Memory Deskriptoren */
  41.  
  42. int        tmp_handle;                /* Handle für die temporäre Datei */
  43. char    tmp_file [] = TMP_FILE;    /* Name und Pfad der temporären Datei */
  44.  
  45. /********************************************************/
  46. /* Konfiguration des Caches sowie der nötigen Tabellen: */
  47. /********************************************************/
  48.  
  49. int vm_config (parameter)
  50.     TAB    *parameter;
  51. {
  52.     DISKINFO    dbuffer;
  53.     int            i;
  54.     long        drivemap;
  55.  
  56.     if (info.version != 0)                /* schon mal konfiguriert ? */
  57.         vm_close ();
  58.  
  59.     if (parameter->cache_size < 4)        /* mindestens 4 Seiten */
  60.         return (WRONG_CACHE_SIZE);
  61.  
  62.     if (parameter->page_type > SIZE_32768)
  63.         return (WRONG_PAGE_TYPE);
  64.  
  65.     drivemap = Drvmap ();
  66.     info.drive_no = parameter->drive_no;
  67.     drive = parameter->drive_no - 1;
  68.  
  69.     if ((drive < 2)
  70.     || ((drivemap & (1 << drive)) == 0))
  71.         return (ILLEGAL_DRIVE);
  72.  
  73.     bpb_block = Getbpb (drive);
  74.     if (bpb_block->bflags != 1)
  75.         return (ILLEGAL_FATSIZE);
  76.  
  77.     record_bits = 0;
  78.     i = bpb_block->recsiz;
  79.     while (i >>= 1)
  80.         ++record_bits;
  81.  
  82.     cluster_bits = 0;
  83.     i = bpb_block->clsiz;
  84.     while (i >>= 1)
  85.         ++cluster_bits;
  86.  
  87.     sector_mask = (1 << (record_bits - cluster_bits)) - 1;
  88.     sectors = 1 + bpb_block->fsiz * 2 + bpb_block->rdlen
  89.             + (bpb_block->numcl * bpb_block->clsiz);
  90.  
  91.     Dfree (&dbuffer, info.drive_no);
  92.     info.drive_free = dbuffer.b_free << 10;
  93.  
  94.     offset_bits = 10 + parameter->page_type;
  95.     offset_mask = (1 << offset_bits) - 1;
  96.  
  97.     lru_count = 0;
  98.  
  99.     age_count = parameter->cache_size;
  100.  
  101.     info.version = VERSION;
  102.     info.count_page = parameter->count_page;
  103.     info.count_blocks = info.free_blocks = MAX_BLOCKS;
  104.     info.fill_value = parameter->fill_value;
  105.     info.page_size = PAGE_TO_ADDR (1);
  106.     info.cache_size = PAGE_TO_ADDR (parameter->cache_size);
  107.     info.cache_count = parameter->cache_size;
  108.     info.max_size = PAGE_TO_ADDR (info.count_page);
  109.     info.max_alloc = PAGE_TO_ADDR (info.count_page);
  110.  
  111.     cluster_size = bpb_block->clsiz;
  112.     if (info.page_size == bpb_block->clsizb)
  113.     {
  114.         size_bits = 0;
  115.         sector_flag = 1;
  116.     }
  117.     else if (info.page_size == (bpb_block->clsizb + bpb_block->clsizb))
  118.     {
  119.         cluster_size += cluster_size;
  120.         size_bits = 1;
  121.         sector_flag = 1;
  122.     }
  123.     else if (info.page_size == (bpb_block->clsizb << 2))
  124.     {
  125.         cluster_size <<= 2;
  126.         size_bits = 2;
  127.         sector_flag = 1;
  128.     }
  129. /*    else*/
  130.     {
  131.         tmp_file [0] = 'C' - 3 + info.drive_no;
  132.         Fdelete (tmp_file);
  133.         if ((tmp_handle = Fcreate (tmp_file, 0)) < 0)
  134.             return (tmp_handle);
  135.         sector_flag = 0;
  136.     }
  137.  
  138. /* Speicheranforderung der benötigten Arrays: */
  139.  
  140.     MALLOC (flags, BYTE, info.count_page+1);
  141.     if (sector_flag)
  142.     {
  143.         MALLOC (sector_no, SECTOR, info.count_page+1);
  144.     }
  145.     else
  146.         sector_no = NULL;
  147.     MALLOC (cache_page, VPAGE, info.cache_count);
  148.     MALLOC (cache_flags, BYTE, info.cache_count);
  149.     MALLOC (cache_age, ULONG, info.cache_count);
  150.     MALLOC (cache, BYTE, info.cache_size);
  151.     MALLOC (buffer, BYTE, info.page_size);
  152.  
  153. /* Initialisierung der benötigten Arrays: */
  154.  
  155.     max_page = 1;
  156.  
  157.     if (!sector_flag)
  158.     {
  159.         if ((i = init_pages (1, 1)) != OK)
  160.             return (i);
  161.  
  162.         Fclose (tmp_handle);
  163.  
  164.         if ((tmp_handle = Fopen (tmp_file, 2)) < 0)
  165.             return (tmp_handle);
  166.     }
  167.  
  168.     for (i = 0; i < info.count_page; i++)        /* Datei-Parameter */
  169.         flags [i] = FREE;
  170.  
  171.     if (sector_flag)
  172.         for (i = 0; i < info.count_page; i++)    /* Sektor-Daten */
  173.             sector_no [i] = EMPTY_ENTRY;
  174.  
  175.     for (i = 0; i < info.cache_count; i++)        /* Cache-Parameter */
  176.         FREE_CACHE (i);
  177.  
  178.     for (i = 0; i < (age_count + 4); i++)
  179.         cache_age [i] = 0;
  180.  
  181.     used_list = NIL;
  182.     free_list = 0;
  183.     INSERT_MD (0, 1, info.count_page, NIL);
  184.  
  185.     for (i = 1; i < MAX_BLOCKS; i++)
  186.         DELETE_MD (i);
  187.  
  188.     return (OK);
  189. }
  190.  
  191.  
  192. /***********************************/
  193. /* Schließen der temporären Datei: */
  194. /***********************************/
  195.  
  196. void vm_close ()
  197. {
  198.     if (!sector_flag)
  199.     {
  200.         Fclose (tmp_handle);    /* Datei schließen, */
  201.         Fdelete (tmp_file);
  202.     }
  203.  
  204.     Mfree (buffer);
  205.     Mfree (cache);
  206.     Mfree (cache_age);
  207.     Mfree (cache_flags);
  208.     Mfree (cache_page);
  209.     if (sector_flag)
  210.         Mfree (sector_no);
  211.     Mfree (flags);
  212. }
  213.  
  214. /*********************************/
  215. /* Übergabe der V_INFO-Struktur: */
  216. /*********************************/
  217.  
  218. V_INFO *vm_info (void)
  219. {
  220.     return (&info);
  221. }
  222.  
  223. /********************************************/
  224. /* Neu-Initialisierung des gesamten Caches: */
  225. /********************************************/
  226.  
  227. void vm_clrcache (void)
  228. {
  229.     cache_clr (0, info.cache_count);
  230. }
  231.