home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / msdos / c / djgpp / go32 / dalloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-30  |  3.2 KB  |  154 lines

  1. /* This is file DALLOC.C */
  2. /*
  3. ** Copyright (C) 1991 DJ Delorie, 24 Kirsten Ave, Rochester NH 03867-2954
  4. **
  5. ** This file is distributed under the terms listed in the document
  6. ** "copying.dj", available from DJ Delorie at the address above.
  7. ** A copy of "copying.dj" should accompany this file; if not, a copy
  8. ** should be available from where this file was obtained.  This file
  9. ** may not be distributed without a verbatim copy of "copying.dj".
  10. **
  11. ** This file is distributed WITHOUT ANY WARRANTY; without even the implied
  12. ** warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. */
  14.  
  15. /* History:22,22 */
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <dos.h>
  20. #include <io.h>
  21. #include <fcntl.h>
  22. #include <sys/stat.h>
  23.  
  24. #include "build.h"
  25. #include "types.h"
  26. #include "valloc.h"
  27. #include "dalloc.h"
  28. #include "mono.h"
  29.  
  30. #define DA_FREE    0
  31. #define DA_USED    1
  32.  
  33. #define MAX_DBLOCK 32760 /* 4095 * 8 -> 128 Mb */
  34.  
  35. static int dalloc_initted = 0;
  36. static word8 map[4096];
  37. static first_avail, last_avail;
  38. static int dfile = -1;
  39. static disk_used = 0;
  40.  
  41. extern int debug_mode;
  42.  
  43. static void dset(unsigned i, int b)
  44. {
  45.   unsigned o, m;
  46.   o = i>>3;
  47.   m = 1<<(i&7);
  48.   if (b)
  49.     map[o] |= m;
  50.   else
  51.     map[o] &= ~m;
  52. }
  53.  
  54. static int dtest(unsigned i)
  55. {
  56.   unsigned o, m;
  57.   o = i>>3;
  58.   m = 1<<(i&7);
  59.   return map[o] & m;
  60. }
  61.  
  62. static char dfilename[80];
  63.  
  64. dalloc_init()
  65. {
  66.   int i;
  67.   char *tmp;
  68.   tmp = getenv("GCCTMP");
  69.   if (!tmp) tmp = getenv("TMP");
  70.   if (!tmp) tmp = getenv("TEMP");
  71.   if (!tmp) tmp = "/";
  72.   if ((tmp[strlen(tmp)-1] == '/') || (tmp[strlen(tmp)-1] == '\\'))
  73.     sprintf(dfilename, "%spage%04x.386", tmp, _CS);
  74.   else
  75.     sprintf(dfilename, "%s/page%04x.386", tmp, _CS);
  76.   dfile = open(dfilename, O_RDWR|O_BINARY|O_CREAT|O_TRUNC, S_IWRITE|S_IREAD);
  77.   if (dfile < 0)
  78.   {
  79.     printf("Fatal! cannot open swap file %s\n", dfilename);
  80.     exit(1);
  81.   }
  82.   for (i=0; i<4096; i++)
  83.     map[i] = 0;
  84.   first_avail = last_avail = 0;
  85.   dalloc_initted = 1;
  86. }
  87.  
  88. dalloc_uninit()
  89. {
  90.   if (dfile == -1)
  91.     return;
  92.   close(dfile);
  93.   unlink(dfilename);
  94. }
  95.  
  96. unsigned dalloc()
  97. {
  98.   char buf[8];
  99.   int i;
  100.   unsigned pn;
  101.   if (!dalloc_initted)
  102.     dalloc_init();
  103.   for (pn=first_avail; pn<=MAX_DBLOCK; pn++)
  104.     if (dtest(pn) == DA_FREE)
  105.     {
  106.       dset(pn, DA_USED);
  107.       first_avail = pn+1;
  108. #if TOPLINEINFO
  109.       if (pn >= last_avail)
  110.         last_avail = pn+1;
  111.       disk_used++;
  112.       sprintf(buf, "%5dk", disk_used*4);
  113.       for (i=0; i<6; i++)
  114.         poke(screen_seg, (54+i)*2, buf[i]|0x0c00);
  115. #endif
  116.       return pn;
  117.     }
  118.   printf("Fatal: out of swap space!\n");
  119.   return 0;
  120. }
  121.  
  122. void dfree(unsigned pn)
  123. {
  124.   char buf[8];
  125.   int i;
  126.   dset(pn, DA_FREE);
  127.   if (pn < first_avail)
  128.     first_avail = pn;
  129. #if TOPLINEINFO
  130.   disk_used--;
  131.   sprintf(buf, "%5dk", disk_used*4);
  132.   for (i=0; i<6; i++)
  133.     poke(screen_seg, (54+i)*2, buf[i]|0x0c00);
  134. #endif
  135. }
  136.  
  137. dwrite(word8 *buf, unsigned block)
  138. {
  139.   int c;
  140.   lseek(dfile, (long)block*4096L, 0);
  141.   c = write(dfile, buf, 4096);
  142.   if (c < 4096)
  143.   {
  144.     printf("Fatal! disk full writing to swap file\n");
  145.     exit(1);
  146.   }
  147. }
  148.  
  149. dread(word8 *buf, unsigned block)
  150. {
  151.   lseek(dfile, (long)block*4096L, 0);
  152.   read(dfile, buf, 4096);
  153. }
  154.