Source code of Windows XP (NT5)
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.

159 lines
4.9 KiB

  1. /***
  2. *stream.c - find a stream not in use
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * defines _getstream() - find a stream not in use
  8. *
  9. *Revision History:
  10. * 09-02-83 RN initial version
  11. * 11-01-87 JCR Multi-thread support
  12. * 05-24-88 PHG Merged DLL and normal versions
  13. * 06-10-88 JCR Use near pointer to reference _iob[] entries
  14. * 08-17-89 GJF Removed _NEAR_, fixed copyright and indenting.
  15. * 02-16-90 GJF Fixed copyright
  16. * 03-19-90 GJF Made calling type _CALLTYPE1, added #include
  17. * <cruntime.h> and removed #include <register.h>.
  18. * 10-03-90 GJF New-style function declarator.
  19. * 12-31-91 GJF Improved multi-thread lock usage [_WIN32_].
  20. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  21. * 09-06-94 CFW Replace MTHREAD with _MT.
  22. * 03-07-95 GJF Changes to manage streams via __piob[], rather than
  23. * _iob[].
  24. * 05-12-95 CFW Bug fix: set _tmpfname field to NULL.
  25. * 03-02-98 GJF Exception-safe locking.
  26. * 05-13-99 PML Remove Win32s
  27. * 05-17-99 PML Remove all Macintosh support.
  28. * 10-14-99 PML Replace InitializeCriticalSection with wrapper function
  29. * __crtInitCritSecAndSpinCount
  30. * 02-20-01 PML vs7#172586 Avoid _RT_LOCK by preallocating all locks
  31. * that will be required, and returning failure back on
  32. * inability to allocate a lock.
  33. *
  34. *******************************************************************************/
  35. #include <cruntime.h>
  36. #include <windows.h>
  37. #include <malloc.h>
  38. #include <stdio.h>
  39. #include <file2.h>
  40. #include <internal.h>
  41. #include <mtdll.h>
  42. #include <dbgint.h>
  43. /***
  44. *FILE *_getstream() - find a stream not in use
  45. *
  46. *Purpose:
  47. * Find a stream not in use and make it available to caller. Intended
  48. * for use inside library only
  49. *
  50. *Entry:
  51. * None. Scans __piob[]
  52. *
  53. *Exit:
  54. * Returns a pointer to a free stream, or NULL if all are in use. A
  55. * stream becomes allocated if the caller decided to use it by setting
  56. * any r, w, r/w mode.
  57. *
  58. * [Multi-thread note: If a free stream is found, it is returned in a
  59. * LOCKED state. It is the caller's responsibility to unlock the stream.]
  60. *
  61. *Exceptions:
  62. *
  63. *******************************************************************************/
  64. FILE * __cdecl _getstream (
  65. void
  66. )
  67. {
  68. REG2 FILE *retval = NULL;
  69. REG1 int i;
  70. #ifdef _MT
  71. /* Get the iob[] scan lock */
  72. _mlock(_IOB_SCAN_LOCK);
  73. __try {
  74. #endif
  75. /*
  76. * Loop through the __piob table looking for a free stream, or the
  77. * first NULL entry.
  78. */
  79. for ( i = 0 ; i < _nstream ; i++ ) {
  80. if ( __piob[i] != NULL ) {
  81. /*
  82. * if the stream is not inuse, return it.
  83. */
  84. if ( !inuse( (FILE *)__piob[i] ) ) {
  85. #ifdef _MT
  86. /*
  87. * Allocate the FILE lock, in case it hasn't already been
  88. * allocated (only necessary for the first _IOB_ENTRIES
  89. * locks, not including stdin/stdout/stderr). Return
  90. * failure if lock can't be allocated.
  91. */
  92. if ( i > 2 && i < _IOB_ENTRIES )
  93. if ( !_mtinitlocknum( _STREAM_LOCKS + i ) )
  94. break;
  95. _lock_str2(i, __piob[i]);
  96. if ( inuse( (FILE *)__piob[i] ) ) {
  97. _unlock_str2(i, __piob[i]);
  98. continue;
  99. }
  100. #endif
  101. retval = (FILE *)__piob[i];
  102. break;
  103. }
  104. }
  105. else {
  106. /*
  107. * allocate a new _FILEX, set _piob[i] to it and return a
  108. * pointer to it.
  109. */
  110. if ( (__piob[i] = _malloc_crt( sizeof(_FILEX) )) != NULL ) {
  111. #if defined(_MT)
  112. if ( !__crtInitCritSecAndSpinCount(
  113. &(((_FILEX *)__piob[i])->lock), _CRT_SPINCOUNT ))
  114. {
  115. /*
  116. * Failed to initialize the critical section because
  117. * of lack of memory, clean up and return failure.
  118. */
  119. _free_crt( __piob[i] );
  120. __piob[i] = NULL;
  121. break;
  122. }
  123. EnterCriticalSection( &(((_FILEX *)__piob[i])->lock) );
  124. #endif
  125. retval = (FILE *)__piob[i];
  126. }
  127. break;
  128. }
  129. }
  130. /*
  131. * Initialize the return stream.
  132. */
  133. if ( retval != NULL ) {
  134. retval->_flag = retval->_cnt = 0;
  135. retval->_tmpfname = retval->_ptr = retval->_base = NULL;
  136. retval->_file = -1;
  137. }
  138. #ifdef _MT
  139. }
  140. __finally {
  141. _munlock(_IOB_SCAN_LOCK);
  142. }
  143. #endif
  144. return(retval);
  145. }