ReactOS 0.4.16-dev-927-g467dec4
setvbuf.cpp File Reference
Include dependency graph for setvbuf.cpp:

Go to the source code of this file.

Functions

static int __cdecl set_buffer (__crt_stdio_stream const stream, _In_reads_opt_(buffer_size_in_bytes) char *const buffer, size_t const buffer_size_in_bytes, int const new_flag_bits) throw ()
 
static int __cdecl _setvbuf_internal (FILE *const public_stream, char *const buffer, int const type, size_t const buffer_size_in_bytes, __crt_cached_ptd_host &ptd)
 
int __cdecl setvbuf (FILE *const public_stream, char *const buffer, int const type, size_t const buffer_size_in_bytes)
 

Function Documentation

◆ _setvbuf_internal()

static int __cdecl _setvbuf_internal ( FILE *const  public_stream,
char *const  buffer,
int const  type,
size_t const  buffer_size_in_bytes,
__crt_cached_ptd_host &  ptd 
)
static

Definition at line 39 of file setvbuf.cpp.

46{
47 __crt_stdio_stream const stream(public_stream);
48
50
51 // Make sure 'type' is one of the three allowed values, and if we are
52 // buffering, make sure the size is between 2 and INT_MAX:
54
55 if (type == _IOFBF || type == _IOLBF)
56 {
57 _UCRT_VALIDATE_RETURN(ptd, 2 <= buffer_size_in_bytes && buffer_size_in_bytes <= INT_MAX, EINVAL, -1);
58 }
59
60 return __acrt_lock_stream_and_call(stream.public_stream(), [&]
61 {
62 // Force the buffer size to be even by masking the low order bit:
63 size_t const usable_buffer_size = buffer_size_in_bytes & ~static_cast<size_t>(1);
64
65 // Flush the current buffer and free it, if it is ours:
66 __acrt_stdio_flush_nolock(stream.public_stream(), ptd);
67 __acrt_stdio_free_buffer_nolock(stream.public_stream());
68
69 // Clear the stream state bits related to buffering. Most of these
70 // should never be set when setvbuf() is called, but it doesn't cost
71 // anything to be safe.
72 stream.unset_flags(_IOBUFFER_CRT | _IOBUFFER_USER | _IOBUFFER_NONE |
73 _IOBUFFER_SETVBUF | _IOBUFFER_STBUF | _IOCTRLZ);
74
75 // Case 1: No buffering:
76 if (type & _IONBF)
77 {
78 return set_buffer(stream, reinterpret_cast<char*>(&stream->_charbuf), 2, _IOBUFFER_NONE);
79 }
80
81 // Cases 2 and 3 (below) cover the _IOFBF and _IOLBF types of buffering.
82 // Line buffering is treated the same as full buffering, so the _IOLBF
83 // bit in the flag is never set. Finally, since _IOFBF is defined to
84 // be zero, full buffering is simply assumed whenever _IONBF is not set.
85
86 // Case 2: Default buffering, CRT-allocated buffer:
87 if (buffer == nullptr)
88 {
89 char* const crt_buffer = _calloc_crt_t(char, usable_buffer_size).detach();
90 if (!crt_buffer)
91 {
92 #ifndef CRTDLL
93 // Force library pre-termination procedure (this is placed here
94 // because the code path should almost never be hit):
95 ++_cflush;
96 #endif
97
98 return -1;
99 }
100
101 return set_buffer(stream, crt_buffer, usable_buffer_size, _IOBUFFER_CRT | _IOBUFFER_SETVBUF);
102 }
103
104 // Case 3: Default buffering, user-provided buffer:
105 return set_buffer(stream, buffer, usable_buffer_size, _IOBUFFER_USER | _IOBUFFER_SETVBUF);
106 });
107}
int _cflush
Definition: _file.cpp:53
#define EINVAL
Definition: acclib.h:90
#define _UCRT_VALIDATE_RETURN(ptd, expr, errorcode, retexpr)
@ _IOBUFFER_CRT
@ _IOBUFFER_USER
@ _IOBUFFER_SETVBUF
auto __acrt_lock_stream_and_call(FILE *const stream, Action &&action) -> decltype(action())
_In_ size_t const _In_ int _In_ bool const _In_ unsigned const _In_ __acrt_rounding_mode const _Inout_ __crt_cached_ptd_host & ptd
Definition: cvt.cpp:355
GLuint GLuint GLsizei GLenum type
Definition: gl.h:1545
GLuint buffer
Definition: glext.h:5915
#define _IONBF
Definition: stdio.h:129
#define _IOLBF
Definition: stdio.h:128
#define _IOFBF
Definition: stdio.h:127
#define INT_MAX
Definition: intsafe.h:150
if(dx< 0)
Definition: linetemp.h:194
static int __cdecl set_buffer(__crt_stdio_stream const stream, _In_reads_opt_(buffer_size_in_bytes) char *const buffer, size_t const buffer_size_in_bytes, int const new_flag_bits)
Definition: setvbuf.cpp:14
Definition: parse.h:23

Referenced by setvbuf().

◆ set_buffer()

static int __cdecl set_buffer ( __crt_stdio_stream const  stream,
_In_reads_opt_(buffer_size_in_bytes) char *const  buffer,
size_t const  buffer_size_in_bytes,
int const  new_flag_bits 
)
throw (
)
static

Definition at line 14 of file setvbuf.cpp.

20{
21 stream.set_flags(new_flag_bits);
22 stream->_bufsiz = static_cast<int>(buffer_size_in_bytes);
23 stream->_ptr = buffer;
24 stream->_base = buffer;
25 stream->_cnt = 0;
26
27 return 0;
28}

Referenced by _setvbuf_internal().

◆ setvbuf()

int __cdecl setvbuf ( FILE *const  public_stream,
char *const  buffer,
int const  type,
size_t const  buffer_size_in_bytes 
)

Definition at line 109 of file setvbuf.cpp.

115{
116 __crt_cached_ptd_host ptd;
117 return _setvbuf_internal(public_stream, buffer, type, buffer_size_in_bytes, ptd);
118}
static int __cdecl _setvbuf_internal(FILE *const public_stream, char *const buffer, int const type, size_t const buffer_size_in_bytes, __crt_cached_ptd_host &ptd)
Definition: setvbuf.cpp:39