home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / setmaxf.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  4KB  |  138 lines

  1. /***
  2. *setmaxf.c - Set the maximum number of streams
  3. *
  4. *       Copyright (c) 1995-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       Defines _setmaxstdio(), a function which changes the maximum number
  8. *       of streams (stdio-level files) which can be open simultaneously.
  9. *
  10. *******************************************************************************/
  11.  
  12. #include <cruntime.h>
  13. #include <stdio.h>
  14. #include <malloc.h>
  15. #include <internal.h>
  16. #include <file2.h>
  17. #include <mtdll.h>
  18. #include <dbgint.h>
  19.  
  20. /***
  21. *int _setmaxstdio(maxnum) - sets the maximum number of streams to maxnum
  22. *
  23. *Purpose:
  24. *       Sets the maximum number of streams which may be simultaneously open
  25. *       to maxnum. This is done by resizing the __piob[] array and updating
  26. *       _nstream. Note that maxnum may be either larger or smaller than the
  27. *       current _nstream value.
  28. *
  29. *Entry:
  30. *       maxnum = new maximum number of streams
  31. *
  32. *Exit:
  33. *       Returns maxnum, if successful, and -1 otherwise.
  34. *
  35. *Exceptions:
  36. *
  37. *******************************************************************************/
  38.  
  39. int __cdecl _setmaxstdio (
  40.         int maxnum
  41.         )
  42. {
  43.         void **newpiob;
  44.         int i;
  45.         int retval;
  46.  
  47.         /*
  48.          * Make sure the request is reasonable.
  49.          */
  50.         if ( (maxnum < _IOB_ENTRIES) || (maxnum > _NHANDLE_) )
  51.             return -1;
  52.  
  53.         _mlock(_IOB_SCAN_LOCK);
  54.  
  55.         /*
  56.          * Try to reallocate the __piob array.
  57.          */
  58.         if ( maxnum > _nstream ) {
  59.             if ( (newpiob = _realloc_crt( __piob, maxnum * sizeof(void *) ))
  60.                  != NULL )
  61.             {
  62.                 /*
  63.                  * Initialize new __piob entries to NULL
  64.                  */
  65.                 for ( i = _nstream ; i < maxnum ; i++ )
  66.                     newpiob[i] = NULL;
  67.  
  68.                 retval = _nstream = maxnum;
  69.                 __piob = newpiob;
  70.             }
  71.             else
  72.                 retval = -1;
  73.         }
  74.         else if ( maxnum == _nstream )
  75.             retval = _nstream;
  76.         else {  /* maxnum < _nstream */
  77.             retval = maxnum;
  78.             /*
  79.              * Clean up the portion of the __piob[] to be freed.
  80.              */
  81.             for ( i = _nstream - 1 ; i >= maxnum ; i-- )
  82.                 /*
  83.                  * If __piob[i] is non-NULL, free up the _FILEX struct it
  84.                  * points to.
  85.                  */
  86.                 if ( __piob[i] != NULL )
  87.                     if ( !inuse( (FILE *)__piob[i] ) ) {
  88.                         _free_crt( __piob[i] );
  89.                     }
  90.                     else {
  91.                         /*
  92.                          * _FILEX is still inuse! Don't free any anything and
  93.                          * return failure to the caller.
  94.                          */
  95.                         retval = -1;
  96.                         break;
  97.                     }
  98.  
  99.             if ( retval != -1 )
  100.                 if ( (newpiob = _realloc_crt( __piob, maxnum * sizeof(void *) ))
  101.                      != NULL )
  102.                 {
  103.                     _nstream = maxnum;      /* retval already set to maxnum */
  104.                     __piob = newpiob;
  105.                 }
  106.                 else
  107.                     retval = -1;
  108.         }
  109.  
  110.         _munlock(_IOB_SCAN_LOCK);
  111.  
  112.         return retval;
  113. }
  114.  
  115.  
  116. /***
  117. *int _getmaxstdio() - gets the maximum number of stdio files
  118. *
  119. *Purpose:
  120. *       Returns the maximum number of simultaneously open stdio-level files.
  121. *       This is the current value of _nstream.
  122. *
  123. *Entry:
  124. *
  125. *Exit:
  126. *       Returns current value of _nstream.
  127. *
  128. *Exceptions:
  129. *
  130. *******************************************************************************/
  131.  
  132. int __cdecl _getmaxstdio (
  133.         void
  134.         )
  135. {
  136.         return _nstream;
  137. }
  138.