home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / c / cl5sr386.zip / GO32 / DALLOC.C < prev    next >
C/C++ Source or Header  |  1992-04-13  |  3KB  |  156 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("GO32TMP");
  69.   if (!tmp) tmp = getenv("GCCTMP");
  70.   if (!tmp) tmp = getenv("TMP");
  71.   if (!tmp) tmp = getenv("TEMP");
  72.   if (!tmp) tmp = "/";
  73.   if ((tmp[strlen(tmp)-1] == '/') || (tmp[strlen(tmp)-1] == '\\'))
  74.     sprintf(dfilename, "%spg%04xXXXXXX", tmp, _CS);
  75.   else
  76.     sprintf(dfilename, "%s/pg%04xXXXXXX", tmp, _CS);
  77.   mktemp(dfilename);
  78.   dfile = open(dfilename, O_RDWR|O_BINARY|O_CREAT|O_TRUNC, S_IWRITE|S_IREAD);
  79.   if (dfile < 0)
  80.   {
  81.     printf("Fatal! cannot open swap file %s\n", dfilename);
  82.     exit(1);
  83.   }
  84.   for (i=0; i<4096; i++)
  85.     map[i] = 0;
  86.   first_avail = last_avail = 0;
  87.   dalloc_initted = 1;
  88. }
  89.  
  90. dalloc_uninit()
  91. {
  92.   if (dfile == -1)
  93.     return;
  94.   close(dfile);
  95.   unlink(dfilename);
  96. }
  97.  
  98. unsigned dalloc()
  99. {
  100.   char buf[8];
  101.   int i;
  102.   unsigned pn;
  103.   if (!dalloc_initted)
  104.     dalloc_init();
  105.   for (pn=first_avail; pn<=MAX_DBLOCK; pn++)
  106.     if (dtest(pn) == DA_FREE)
  107.     {
  108.       dset(pn, DA_USED);
  109.       first_avail = pn+1;
  110. #if TOPLINEINFO
  111.       if (pn >= last_avail)
  112.         last_avail = pn+1;
  113.       disk_used++;
  114.       sprintf(buf, "%5dk", disk_used*4);
  115.       for (i=0; i<6; i++)
  116.         poke(screen_seg, (54+i)*2, buf[i]|0x0c00);
  117. #endif
  118.       return pn;
  119.     }
  120.   printf("Fatal: out of swap space!\n");
  121.   return 0;
  122. }
  123.  
  124. void dfree(unsigned pn)
  125. {
  126.   char buf[8];
  127.   int i;
  128.   dset(pn, DA_FREE);
  129.   if (pn < first_avail)
  130.     first_avail = pn;
  131. #if TOPLINEINFO
  132.   disk_used--;
  133.   sprintf(buf, "%5dk", disk_used*4);
  134.   for (i=0; i<6; i++)
  135.     poke(screen_seg, (54+i)*2, buf[i]|0x0c00);
  136. #endif
  137. }
  138.  
  139. dwrite(word8 *buf, unsigned block)
  140. {
  141.   int c;
  142.   lseek(dfile, (long)block*4096L, 0);
  143.   c = write(dfile, buf, 4096);
  144.   if (c < 4096)
  145.   {
  146.     printf("Fatal! disk full writing to swap file\n");
  147.     exit(1);
  148.   }
  149. }
  150.  
  151. dread(word8 *buf, unsigned block)
  152. {
  153.   lseek(dfile, (long)block*4096L, 0);
  154.   read(dfile, buf, 4096);
  155. }
  156.