ReactOS 0.4.16-dev-959-g2ec3a19
setmaxf.cpp
Go to the documentation of this file.
1//
2// setmaxf.cpp
3//
4// Copyright (c) Microsoft Corporation. All rights reserved.
5//
6// Defines _setmaxstdio() and _getmaxstdio(), which control the maximum number
7// of stdio streams that may be open simultaneously.
8//
10
11
12
13// Sets the maximum number of stdio streams that may be simultaneously open.
14// Note that the maximum must be at least _IOB_ENTRIES, and must be no larger
15// than _N_HANDLE_. However, it may be either larger or smaller than the current
16// maximum.
17//
18// Returns the new maximum value on success; returns -1 on failure.
19extern "C" int __cdecl _setmaxstdio(int const new_maximum)
20{
21 // Make sure the request is reasonable:
22 _VALIDATE_RETURN(new_maximum >= _IOB_ENTRIES && new_maximum <= _NHANDLE_, EINVAL, -1);
23
24 return __acrt_lock_and_call(__acrt_stdio_index_lock, [&]
25 {
26 // If the new maximum is the same as our current maximum, no work to do:
27 if (new_maximum == _nstream)
28 return new_maximum;
29
30 // If the new maximum is smaller than the current maximum, attempt to
31 // free up any entries that are beyond the new maximum:
32 if (new_maximum < _nstream)
33 {
34 __crt_stdio_stream_data** const first_to_remove = __piob + new_maximum;
35 __crt_stdio_stream_data** const last_to_remove = __piob + _nstream;
36 for (__crt_stdio_stream_data** rit = last_to_remove; rit != first_to_remove; --rit)
37 {
38 __crt_stdio_stream_data* const entry = *(rit - 1);
39 if (entry == nullptr)
40 continue;
41
42 // If the entry is still in use, stop freeing entries and return
43 // failure to the caller:
44 if (__crt_stdio_stream(entry).is_in_use())
45 return -1;
46
48 }
49 }
50
51 // Enlarge or shrink the array, as required:
52 __crt_stdio_stream_data** const new_piob = _recalloc_crt_t(__crt_stdio_stream_data*, __piob, new_maximum).detach();
53 if (new_piob == nullptr)
54 return -1;
55
56 _nstream = new_maximum;
57 __piob = new_piob;
58 return new_maximum;
59 });
60}
61
62
63
64// Gets the maximum number of stdio streams that may be open at any one time.
65extern "C" int __cdecl _getmaxstdio()
66{
67 return __acrt_lock_and_call(__acrt_stdio_index_lock, []
68 {
69 return _nstream;
70 });
71}
#define EINVAL
Definition: acclib.h:90
#define __cdecl
Definition: accygwin.h:79
@ __acrt_stdio_index_lock
#define _NHANDLE_
int _nstream
__crt_stdio_stream_data ** __piob
#define _VALIDATE_RETURN(expr, errorcode, retexpr)
#define _IOB_ENTRIES
Definition: stdio.h:23
#define _free_crt
uint32_t entry
Definition: isohybrid.c:63
int __cdecl _setmaxstdio(int const new_maximum)
Definition: setmaxf.cpp:19
int __cdecl _getmaxstdio()
Definition: setmaxf.cpp:65