home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / STRAT.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  4KB  |  173 lines

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