home *** CD-ROM | disk | FTP | other *** search
/ Media Share 13 / mediashare_13.zip / mediashare_13 / ZIPPED / PROGRAM / SNIP9404.ZIP / STRAT.C < prev    next >
C/C++ Source or Header  |  1994-04-03  |  4KB  |  171 lines

  1. /*
  2. **  strat.c  10-5-91  Robert Mashlan, public domain
  3. **
  4. **   Interface functions to DOS 3.0+ set allocation strategy
  5. **   and get allocation strategy functions via int 21h,
  6. **   function 58h.
  7. **
  8. **   By setting the dos allocation strategy to LAST_FIT_LOW
  9. **   before using DOS the set handle count function int 21h,
  10. **   function 67h, DOS will allocate memory for the extended
  11. **   file handle table at the end of free memory instead of
  12. **   after the last heap allocation, with the benefit of
  13. **   allowing the heap manager make further contiguous
  14. **   allocations from the operating system.
  15. **
  16. */
  17.  
  18. #include <stdlib.h>
  19. #include <dos.h>
  20. #include "strat.h"
  21.  
  22. /*
  23. **   Gets dos memory allocation strategy via function 0x58.
  24. **   Returns  allocation strategy, else returns -1 and sets
  25. **   _doserrno on error.
  26. */
  27.  
  28. int get_alloc_strat(void)
  29. {
  30.       union REGS r;
  31.  
  32.       r.x.ax = 0x5800;              /* DOS "get allocation strategy" */
  33.       int86(0x21,&r,&r);
  34.       if (r.x.cflag)                /* error? */
  35.       {
  36.             _doserrno = r.x.ax;     /* save error code */
  37.             return -1;
  38.       }
  39.       else  return r.x.ax;
  40. }
  41.  
  42. /*
  43. **   Sets DOS memory allocation strategy
  44. **   returns allocation strategy on success,
  45. **   else returns -1 and sets _doserrno on error
  46. */
  47.  
  48. int set_alloc_strat( int strat )
  49. {
  50.       union REGS r;
  51.  
  52.       r.x.ax = 0x5801;              /* DOS "set allocation strategy" */
  53.       r.x.bx = strat;
  54.       int86(0x21,&r,&r);
  55.       if (r.x.cflag)                /* error? */
  56.       {
  57.             _doserrno = r.x.ax;     /* save error code */
  58.             return -1;
  59.       }
  60.       _doserrno = 0;
  61.       return strat;
  62. }
  63.  
  64. /*
  65. **   Uses dos function 67h to increase open file handle count.
  66. **   Returns -1 and sets _doserrno on error, 0 otherwise.
  67. */
  68.  
  69. int set_handle_count( unsigned nhandles )
  70. {
  71.       union REGS r;
  72.  
  73.       r.x.ax = 0x6700;
  74.       r.x.bx = nhandles;
  75.       int86(0x21,&r,&r);
  76.       if(r.x.cflag)
  77.       {
  78.             _doserrno = r.x.ax;
  79.             return -1;
  80.       }
  81.       _doserrno = 0;
  82.       return 0;
  83. }
  84.  
  85. #ifdef DEMO
  86.  
  87. #include <stdio.h>
  88. #include <stdlib.h>
  89. #include <io.h>
  90. #include <fcntl.h>
  91.  
  92. /*
  93. **  returns maximum number of files that can be open
  94. */
  95.  
  96. int handle_count(void)
  97. {
  98.       int handles[500];
  99.       int i, result;
  100.  
  101.       /* try allocating as many file handles as possible */
  102.  
  103.       for (i = 0; i < 500; i++)
  104.       {
  105.             if( (handles[i]=open("NUL",O_WRONLY)) == -1 )
  106.                   break;
  107.       }
  108.       result = i;
  109.  
  110.       /* close all files opened */
  111.  
  112.       for (i--; i >= 0; i--)
  113.             close(handles[i]);
  114.       return result;
  115. }
  116.  
  117.  
  118. /*
  119. **   Memory test, returns number of kilobytes that
  120. **   can be allocated before failure.
  121. */
  122.  
  123. int memtest(void)
  124. {
  125.       static void *mem[1024];
  126.       int i,result;
  127.  
  128.       /* try allocating as many 1K blocks as possible */
  129.  
  130.       for(i=0;i<1024;i++)
  131.       {
  132.             if( (mem[i]=malloc(1024)) == NULL )
  133.                   break;
  134.       }
  135.       result = i;                               /* save result */
  136.  
  137.       /* free memory allocated */
  138.  
  139.       for(i--; i >= 0; i--)
  140.             free(mem[i]);
  141.       return result;
  142. }
  143.  
  144. #define checkdoserror(f) \
  145.    ((f==-1)?printf("%s failed, doserror = %#02x\n",#f,_doserrno):(void)0)
  146.  
  147. int main(void)
  148. {
  149.       int strat;
  150.  
  151.       /* do pre-test diagnostics */
  152.  
  153.       printf("allocated %d Kbytes before failure\n",memtest());
  154.       printf("opened %d files\n",handle_count());
  155.  
  156.       strat = get_alloc_strat();  /* save current allocation strategy */
  157.       checkdoserror(set_alloc_strat(LAST_FIT_LOW));
  158.       puts("setting handle count to 50, with changed allocation strategy");
  159.       checkdoserror(set_handle_count(50));
  160.       checkdoserror(set_alloc_strat(strat)); /* restore strategy */
  161.  
  162.       /* do post-test diagnostics */
  163.  
  164.       printf("allocated %d Kbytes before failure\n",memtest());
  165.       printf("opened %d files\n",handle_count());
  166.  
  167.       return 0;
  168. }
  169.  
  170. #endif
  171.