Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
4.1 KiB

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