Commit e3033a22 by Paolo Carlini Committed by Benjamin Kosnik

fstream.tcc (_M_overflow): Rewrote to call _M_convert_to_external only once…

fstream.tcc (_M_overflow): Rewrote to call _M_convert_to_external only once (_M_buf_size is now the size...


2003-05-15  Paolo Carlini  <pcarlini@unitus.it>
	    Nathan Myers  <ncm@cantrip.org>

	* include/bits/fstream.tcc (_M_overflow): Rewrote to call
	_M_convert_to_external only once (_M_buf_size is now the size of
	the put area + 1 for the overflow char of a full area); call
	_M_set_buffer instead of _M_set_indeterminate.
	(setbuf): Don't accept a buffer smaller than 2 chars.
	(_M_underflow): Refill _M_buf_size - 1 chars; call _M_set_buffer,
	instead of _M_set_determinate.
	(open): Call _M_set_buffer, instead of _M_set_indeterminate.
	(seekoff): Likewise.
	* include/ext/stdio_filebuf.h (stdio_filebuf(int,
	std::ios_base::openmode, bool, size_t),
	stdio_filebuf(std::__c_file*, std::ios_base::openmode, size_t):
	Likewise.
	* include/std/std_fstream.h (_M_set_indeterminate): Remove.
	(_M_set_determinate): Rename as _M_set_buffer, _M_buf_size ->
	_M_buf_size - 1.
	* include/std/std_streambuf.h: Tweak _M_out_lim comment.
	* testsuite/27_io/basic_filebuf/sgetn/char/1.cc: Tweak, taking
	into account that, for _M_buf_size == BUFSIZ == 8192, the size of
	the put area is now BUFSIZ - 1.
	* testsuite/ext/stdio_filebuf_2.cc: Tweak, taking into account
	that now the smallest _M_buf_size is 2 (still fails, for the same
	reason, with 3.2.3)

Co-Authored-By: Nathan Myers <ncm@cantrip.org>

From-SVN: r66848
parent 36b9c271
2003-05-15 Paolo Carlini <pcarlini@unitus.it>
Nathan Myers <ncm@cantrip.org>
* include/bits/fstream.tcc (_M_overflow): Rewrote to call
_M_convert_to_external only once (_M_buf_size is now the size of
the put area + 1 for the overflow char of a full area); call
_M_set_buffer instead of _M_set_indeterminate.
(setbuf): Don't accept a buffer smaller than 2 chars.
(_M_underflow): Refill _M_buf_size - 1 chars; call _M_set_buffer,
instead of _M_set_determinate.
(open): Call _M_set_buffer, instead of _M_set_indeterminate.
(seekoff): Likewise.
* include/ext/stdio_filebuf.h (stdio_filebuf(int,
std::ios_base::openmode, bool, size_t),
stdio_filebuf(std::__c_file*, std::ios_base::openmode, size_t):
Likewise.
* include/std/std_fstream.h (_M_set_indeterminate): Remove.
(_M_set_determinate): Rename as _M_set_buffer, _M_buf_size ->
_M_buf_size - 1.
* include/std/std_streambuf.h: Tweak _M_out_lim comment.
* testsuite/27_io/basic_filebuf/sgetn/char/1.cc: Tweak, taking
into account that, for _M_buf_size == BUFSIZ == 8192, the size of
the put area is now BUFSIZ - 1.
* testsuite/ext/stdio_filebuf_2.cc: Tweak, taking into account
that now the smallest _M_buf_size is 2 (still fails, for the same
reason, with 3.2.3)
2003-05-14 Loren J. Rittle <ljrittle@acm.org> 2003-05-14 Loren J. Rittle <ljrittle@acm.org>
* testsuite/thread/pthread4.cc: Tweak test. * testsuite/thread/pthread4.cc: Tweak test.
......
...@@ -48,7 +48,7 @@ namespace std ...@@ -48,7 +48,7 @@ namespace std
basic_filebuf<_CharT, _Traits>:: basic_filebuf<_CharT, _Traits>::
_M_allocate_internal_buffer() _M_allocate_internal_buffer()
{ {
if (!this->_M_buf && this->_M_buf_size) if (!_M_buf_allocated && this->_M_buf_size)
{ {
// Allocate internal buffer. // Allocate internal buffer.
this->_M_buf = new char_type[this->_M_buf_size]; this->_M_buf = new char_type[this->_M_buf_size];
...@@ -101,9 +101,9 @@ namespace std ...@@ -101,9 +101,9 @@ namespace std
this->_M_mode = __mode; this->_M_mode = __mode;
// Setup initial position of buffer. // Setup initial position of buffer.
_M_set_indeterminate(); _M_set_buffer(0);
if ((__mode & ios_base::ate) if ((__mode & ios_base::ate)
&& this->seekoff(0, ios_base::end, __mode) < 0) && this->seekoff(0, ios_base::end, __mode) < 0)
// 27.8.1.3,4 // 27.8.1.3,4
this->close(); this->close();
...@@ -218,27 +218,28 @@ namespace std ...@@ -218,27 +218,28 @@ namespace std
ios_base::in); ios_base::in);
} }
if (_M_buf_size) if (_M_buf_size > 1)
{ {
streamsize __elen = 0; streamsize __elen = 0;
streamsize __ilen = 0; streamsize __ilen = 0;
if (__check_facet(_M_codecvt).always_noconv()) if (__check_facet(_M_codecvt).always_noconv())
{ {
__elen = _M_file.xsgetn(reinterpret_cast<char*>(this->_M_in_beg), _M_buf_size); __elen = _M_file.xsgetn(reinterpret_cast<char*>(this->_M_in_beg), _M_buf_size - 1);
__ilen = __elen; __ilen = __elen;
} }
else else
{ {
char* __buf = static_cast<char*>(__builtin_alloca(_M_buf_size)); char* __buf = static_cast<char*>(__builtin_alloca(_M_buf_size - 1));
__elen = _M_file.xsgetn(__buf, _M_buf_size); __elen = _M_file.xsgetn(__buf, _M_buf_size - 1);
const char* __eend; const char* __eend;
char_type* __iend; char_type* __iend;
codecvt_base::result __r; codecvt_base::result __r;
__r = _M_codecvt->in(_M_state_cur, __buf, __buf + __elen, __r = _M_codecvt->in(_M_state_cur, __buf, __buf + __elen,
__eend, this->_M_in_beg, __eend, this->_M_in_beg,
this->_M_in_beg + _M_buf_size, __iend); this->_M_in_beg + _M_buf_size - 1,
__iend);
if (__r == codecvt_base::ok) if (__r == codecvt_base::ok)
__ilen = __iend - this->_M_in_beg; __ilen = __iend - this->_M_in_beg;
else if (__r == codecvt_base::noconv) else if (__r == codecvt_base::noconv)
...@@ -258,7 +259,7 @@ namespace std ...@@ -258,7 +259,7 @@ namespace std
if (0 < __ilen) if (0 < __ilen)
{ {
_M_set_determinate(__ilen); _M_set_buffer(__ilen);
__ret = traits_type::to_int_type(*this->_M_in_cur); __ret = traits_type::to_int_type(*this->_M_in_cur);
if (__bump) if (__bump)
_M_move_in_cur(1); _M_move_in_cur(1);
...@@ -338,41 +339,30 @@ namespace std ...@@ -338,41 +339,30 @@ namespace std
_M_overflow(int_type __c) _M_overflow(int_type __c)
{ {
int_type __ret = traits_type::eof(); int_type __ret = traits_type::eof();
const bool __testeof = traits_type::eq_int_type(__c, __ret);
const bool __testput = this->_M_out_beg < this->_M_out_lim; const bool __testput = this->_M_out_beg < this->_M_out_lim;
if (__testput) if (__testput)
{ {
// Need to restore current position. The position of the external // Need to restore current position. The position of the
// byte sequence (_M_file) corresponds to _M_filepos, and we need // external byte sequence (_M_file) corresponds to
// to move it to _M_out_beg for the write. // _M_filepos, and we need to move it to _M_out_beg for the
if (_M_filepos && _M_filepos != this->_M_out_beg) // write.
{ if (_M_filepos != this->_M_out_beg)
off_type __off = this->_M_out_beg - _M_filepos; _M_file.seekoff(this->_M_out_beg - _M_filepos, ios_base::cur);
_M_file.seekoff(__off, ios_base::cur);
} // If appropriate, append the overflow char.
if (!__testeof)
// Convert internal buffer to external representation, output. *this->_M_out_lim++ = traits_type::to_char_type(__c);
if (_M_convert_to_external(this->_M_out_beg,
this->_M_out_lim - this->_M_out_beg)) // Convert pending sequence to external representation,
// output.
if (_M_convert_to_external(this->_M_out_beg,
this->_M_out_lim - this->_M_out_beg)
&& (!__testeof || (__testeof && !_M_file.sync())))
{ {
// Convert pending sequence to external representation, output. _M_set_buffer(0);
// If eof, then just attempt sync. __ret = traits_type::not_eof(__c);
if (!traits_type::eq_int_type(__c, traits_type::eof()))
{
// User code must flush when switching modes (thus
// don't sync).
char_type __pending = traits_type::to_char_type(__c);
if (_M_convert_to_external(&__pending, 1))
{
_M_set_indeterminate();
__ret = traits_type::not_eof(__c);
}
}
else if (!_M_file.sync())
{
_M_set_indeterminate();
__ret = traits_type::not_eof(__c);
}
} }
} }
_M_last_overflowed = true; _M_last_overflowed = true;
...@@ -388,20 +378,22 @@ namespace std ...@@ -388,20 +378,22 @@ namespace std
const bool __testput = this->_M_out_cur < this->_M_out_end; const bool __testput = this->_M_out_cur < this->_M_out_end;
const bool __testout = this->_M_mode & ios_base::out; const bool __testout = this->_M_mode & ios_base::out;
// Perhaps set below in _M_overflow.
_M_last_overflowed = false;
if (__testout) if (__testout)
{ {
if (traits_type::eq_int_type(__c, traits_type::eof())) if (traits_type::eq_int_type(__c, traits_type::eof()))
__ret = traits_type::not_eof(__c); __ret = traits_type::not_eof(__c);
else if (__testput) else if (__testput)
{ {
*this->_M_out_cur = traits_type::to_char_type(__c); *this->_M_out_cur = traits_type::to_char_type(__c);
_M_move_out_cur(1); _M_move_out_cur(1);
__ret = traits_type::not_eof(__c); __ret = traits_type::not_eof(__c);
} }
else else
__ret = this->_M_overflow(__c); __ret = this->_M_overflow(__c);
} }
_M_last_overflowed = false; // Set in _M_overflow, below.
return __ret; return __ret;
} }
...@@ -479,12 +471,15 @@ namespace std ...@@ -479,12 +471,15 @@ namespace std
{ {
if (!this->is_open() && __s == 0 && __n == 0) if (!this->is_open() && __s == 0 && __n == 0)
this->_M_buf_size = 0; this->_M_buf_size = 0;
else if (__s && __n) else if (__s && __n > 1)
{ {
// This is implementation-defined behavior, and assumes // This is implementation-defined behavior, and assumes that
// that an external char_type array of length (__s + __n) // an external char_type array of length (__s + __n) exists
// exists and has been pre-allocated. If this is not the // and has been pre-allocated. If this is not the case,
// case, things will quickly blow up. // things will quickly blow up. The length argument __n must
// be greater than 1 because __n - 1 positions will be used
// for the get and put areas, and 1 position is needed to
// host the overflow char of a full put area.
// Step 1: Destroy the current internal array. // Step 1: Destroy the current internal array.
_M_destroy_internal_buffer(); _M_destroy_internal_buffer();
...@@ -492,7 +487,7 @@ namespace std ...@@ -492,7 +487,7 @@ namespace std
// Step 2: Use the external array. // Step 2: Use the external array.
this->_M_buf = __s; this->_M_buf = __s;
this->_M_buf_size = __n; this->_M_buf_size = __n;
_M_set_indeterminate(); _M_set_buffer(0);
} }
_M_last_overflowed = false; _M_last_overflowed = false;
return this; return this;
...@@ -539,7 +534,7 @@ namespace std ...@@ -539,7 +534,7 @@ namespace std
// Return pos_type(off_type(-1)) in case of failure. // Return pos_type(off_type(-1)) in case of failure.
__ret = _M_file.seekoff(__computed_off, __way, __mode); __ret = _M_file.seekoff(__computed_off, __way, __mode);
_M_set_indeterminate(); _M_set_buffer(0);
} }
else else
{ {
......
...@@ -67,19 +67,23 @@ namespace __gnu_cxx ...@@ -67,19 +67,23 @@ namespace __gnu_cxx
* @param mode Same meaning as in a standard filebuf. * @param mode Same meaning as in a standard filebuf.
* @param del Whether to close the file on destruction. * @param del Whether to close the file on destruction.
* @param size Optimal or preferred size of internal buffer, in bytes. * @param size Optimal or preferred size of internal buffer, in bytes.
* Note that it includes a position for the overflow char,
* therefore, can't be smaller than 2.
* *
* This constructor associates a file stream buffer with an open * This constructor associates a file stream buffer with an open
* POSIX file descriptor. Iff @a del is true, then the associated * POSIX file descriptor. Iff @a del is true, then the associated
* file will be closed when the stdio_filebuf is closed/destroyed. * file will be closed when the stdio_filebuf is closed/destroyed.
*/ */
stdio_filebuf(int __fd, std::ios_base::openmode __mode, bool __del, stdio_filebuf(int __fd, std::ios_base::openmode __mode, bool __del,
size_t __size); size_t __size = static_cast<size_t>(BUFSIZ));
/** /**
* @param f An open @c FILE*. * @param f An open @c FILE*.
* @param mode Same meaning as in a standard filebuf. * @param mode Same meaning as in a standard filebuf.
* @param size Optimal or preferred size of internal buffer, in bytes. * @param size Optimal or preferred size of internal buffer, in bytes.
* Defaults to system's @c BUFSIZ. * Defaults to system's @c BUFSIZ. Note that it includes
* a position for the overflow char, therefore, can't be
* smaller than 2.
* *
* This constructor associates a file stream buffer with an open * This constructor associates a file stream buffer with an open
* C @c FILE*. The @c FILE* will not be automatically closed when the * C @c FILE*. The @c FILE* will not be automatically closed when the
...@@ -123,7 +127,7 @@ namespace __gnu_cxx ...@@ -123,7 +127,7 @@ namespace __gnu_cxx
this->_M_mode = __mode; this->_M_mode = __mode;
this->_M_buf_size = __size; this->_M_buf_size = __size;
_M_allocate_internal_buffer(); _M_allocate_internal_buffer();
_M_set_indeterminate(); _M_set_buffer(0);
} }
} }
...@@ -138,7 +142,7 @@ namespace __gnu_cxx ...@@ -138,7 +142,7 @@ namespace __gnu_cxx
this->_M_mode = __mode; this->_M_mode = __mode;
this->_M_buf_size = __size; this->_M_buf_size = __size;
_M_allocate_internal_buffer(); _M_allocate_internal_buffer();
_M_set_indeterminate(); _M_set_buffer(0);
} }
} }
} // namespace __gnu_cxx } // namespace __gnu_cxx
......
...@@ -123,7 +123,9 @@ namespace std ...@@ -123,7 +123,9 @@ namespace std
/** /**
* @if maint * @if maint
* Actual size of internal buffer. * Actual size of internal buffer. This number is equal to the size
* of the put area + 1 position, reserved for the overflow char of
* a full area.
* @endif * @endif
*/ */
size_t _M_buf_size; size_t _M_buf_size;
...@@ -452,41 +454,30 @@ namespace std ...@@ -452,41 +454,30 @@ namespace std
void void
_M_output_unshift(); _M_output_unshift();
// These two functions are used to clarify internal buffer // This function sets the pointers of the internal buffer, both get
// maintenance. After an overflow, or after a seekoff call that // and put areas. Typically, __off == _M_in_end - _M_in_beg upon
// started at beg or end, or possibly when the stream becomes // _M_underflow; __off == 0 upon _M_overflow, seekoff, open, setbuf.
// unbuffered, and a myrid other obscure corner cases, the //
// internal buffer does not truly reflect the contents of the // NB: _M_out_end - _M_out_beg == _M_buf_size - 1, since _M_buf_size
// external buffer. At this point, for whatever reason, it is in // reflects the actual allocated memory and the last cell is reserved
// an indeterminate state. // for the overflow char of a full put area.
/** void
* @if maint _M_set_buffer(streamsize __off)
* @doctodo {
* @endif const bool __testin = this->_M_mode & ios_base::in;
*/ const bool __testout = this->_M_mode & ios_base::out;
void if (_M_buf_size)
_M_set_indeterminate(void)
{ _M_set_determinate(off_type(0)); }
/**
* @if maint
* @doctodo
* @endif
*/
void
_M_set_determinate(off_type __off)
{
const bool __testin = this->_M_mode & ios_base::in;
const bool __testout = this->_M_mode & ios_base::out;
if (__testin)
this->setg(this->_M_buf, this->_M_buf, this->_M_buf + __off);
if (__testout)
{ {
this->setp(this->_M_buf, this->_M_buf + this->_M_buf_size); if (__testin)
this->_M_out_lim += __off; this->setg(this->_M_buf, this->_M_buf, this->_M_buf + __off);
if (__testout)
{
this->setp(this->_M_buf, this->_M_buf + this->_M_buf_size - 1);
this->_M_out_lim += __off;
}
_M_filepos = this->_M_buf + __off;
} }
_M_filepos = this->_M_buf + __off; }
}
}; };
// [27.8.1.5] Template class basic_ifstream // [27.8.1.5] Template class basic_ifstream
......
...@@ -187,9 +187,9 @@ namespace std ...@@ -187,9 +187,9 @@ namespace std
//@{ //@{
/** /**
* @if maint * @if maint
* _M_set_indeterminate and setp set it equal to _M_out_beg, then * setp (and _M_set_buffer(0) in basic_filebuf) set it equal to
* at each put operation it may be moved forward (toward _M_out_end) * _M_out_beg, then at each put operation it may be moved
* by _M_out_cur_move. * forward (toward _M_out_end) by _M_out_cur_move.
* @endif * @endif
*/ */
char_type* _M_out_lim; // End limit of used put area. char_type* _M_out_lim; // End limit of used put area.
......
...@@ -123,11 +123,11 @@ void test05() ...@@ -123,11 +123,11 @@ void test05()
VERIFY( c7 == c2 ); // n != i VERIFY( c7 == c2 ); // n != i
strmsz_1 = fb_03.sgetn(carray1, 10); strmsz_1 = fb_03.sgetn(carray1, 10);
VERIFY( !strmsz_1 ); //zero VERIFY( !strmsz_1 ); //zero
strmsz_1 = fb_01.in_avail(); strmsz_1 = fb_01.in_avail(); // N.B.: _M_in_end - _M_in_beg == BUFSIZ - 1
strmsz_2 = fb_01.sgetn(carray2, strmsz_1 + 5); strmsz_2 = fb_01.sgetn(carray2, strmsz_1 + 5);
VERIFY( strmsz_1 == strmsz_2 - 5 ); VERIFY( strmsz_1 == strmsz_2 - 5 );
c4 = fb_01.sgetc(); // buffer should have underflowed from above. c4 = fb_01.sgetc(); // buffer should have underflowed from above.
VERIFY( c4 == 'i' ); VERIFY( c4 == 'h' );
strmsz_1 = fb_01.in_avail(); strmsz_1 = fb_01.in_avail();
VERIFY( strmsz_1 > 0 ); VERIFY( strmsz_1 > 0 );
strmsz_2 = fb_01.sgetn(carray2, strmsz_1 + 5); strmsz_2 = fb_01.sgetn(carray2, strmsz_1 + 5);
......
...@@ -29,7 +29,6 @@ ...@@ -29,7 +29,6 @@
// out by _M_really_overflow upon overflow. // out by _M_really_overflow upon overflow.
void test01() void test01()
{ {
using namespace std; using namespace std;
bool test = true; bool test = true;
...@@ -37,11 +36,10 @@ void test01() ...@@ -37,11 +36,10 @@ void test01()
FILE* file = fopen(name, "w"); FILE* file = fopen(name, "w");
{ {
using namespace __gnu_cxx; using namespace __gnu_cxx;
stdio_filebuf<char> sbuf(file, ios_base::out, 2);
// One char big stack-based buffer.
stdio_filebuf<char> sbuf(file, ios_base::out, 1);
sbuf.sputc('T'); sbuf.sputc('T');
sbuf.sputc('S'); sbuf.sputc('S');
sbuf.sputc('P');
} }
fclose(file); fclose(file);
...@@ -51,8 +49,8 @@ void test01() ...@@ -51,8 +49,8 @@ void test01()
streamsize n = fbuf.sgetn(buf, sizeof(buf)); streamsize n = fbuf.sgetn(buf, sizeof(buf));
fbuf.close(); fbuf.close();
VERIFY( n == 2 ); VERIFY( n == 3 );
VERIFY( !memcmp(buf, "TS", 2) ); VERIFY( !memcmp(buf, "TSP", 3) );
} }
int main() int main()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment