为C++流自定义流缓冲区
Customize streambuffer for C++ ostream
我正在为输出流实现自己的流缓冲区。基本上,它是一个类似矢量的流缓冲区,其中每次溢出函数都只是将缓冲区重新分配为两倍大。同步功能会将所有数据写出到文件描述符fd
指定的设备。
class MyStreamBuf : public ::std::streambuf {
constexpr static size_t INIT_BUFFER_SIZE {1024};
public:
MyStreamBuf();
~MyStreamBuf();
void fd(const int);
int sync() override;
int_type overflow(int_type ch = traits_type::eof()) override;
private:
int _fd {-1};
size_t _size;
char_type* _base;
void _resize(const size_t);
};
MyStreamBuf::MyStreamBuf() {
_size = INIT_BUFFER_SIZE;
_base = static_cast<char_type*>(malloc(_size * sizeof(char_type)));
setp(_base, _base + _size - 1); // -1 to make overflow easier.
}
// Destructor.
MyStreamBuf::~MyStreamBuf() {
::free(_base);
}
// Procedure: fd
// Change the underlying device.
void MyStreamBuf::fd(const int fd) {
_fd = fd;
}
// Procedure: _resize
// Resize the underlying buffer to fit at least "tgt_size" items of type char_type.
void MyStreamBuf::_resize(const size_t tgt_size) {
// Nothing has to be done if the capacity can accommodate the file descriptor.
if(_size >= tgt_size) return;
// Adjust the cap to the next highest power of 2 larger than num_fds
for(_size = (_size ? _size : 1); _size < tgt_size; _size *= 2);
// Adjust and reset the memory chunk.
_base = static_cast<char_type*>(::realloc(_base, _size*sizeof(char_type)));
setp(_base, _base + _size - 1); // -1 to make overflow easier.
}
int MyStreamBuf::sync() {
int res = 0;
::std::ptrdiff_t remain = pptr() - pbase();
while(remain) {
issue_write:
auto ret = ::write(_fd, pptr() - remain, remain);
if(ret == -1) {
if(errno == EINTR) {
goto issue_write;
}
else if(errno == EAGAIN) {
break;
}
else {
res = -1;
break;
}
}
remain -= ret;
}
if(remain) {
::memcpy(pbase(), pptr() - remain, remain*sizeof(char_type));
}
pbump(pbase() + remain - pptr());
return res;
}
typename MyStreamBuf::int_type MyStreamBuf::overflow(int_type ch) {
assert(traits_type::eq_int_type(ch, traits_type::eof()) == false);
_resize(_size * 2);
return ch;
}
但是,在用我自己的缓冲区替换cout
时,我遇到了段错误。在与GDB斗争后,我找不到错误在哪里。
// Function: main
int main() {
auto fd = open("./test.txt", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
MyStreamBuf d;
d.fd(fd);
::std::cout.rdbuf(&d);
::std::cout << 1 << " " << 2 << ::std::endl;
close(fd);
return 0;
}
这个实现有什么问题吗?我看到许多文章通常覆盖sync
并且需要overflow
。
问题似乎在于您的对象d
在std::cout
之前被销毁,因此最终调用销毁全局对象,其中包括刷新缓冲区,并且在main()
结束后采取 palce(记住它是一个全局对象),尝试对不再存在的streambuf
对象执行操作。 您的缓冲区对象绝对应该比您与之关联的流存活。
在程序中使用它的一种方法是将d
变成一个指针,您永远不会删除它。 或者,您可以保留本地对象,但调用 std::cout.flush()
,然后在超出范围之前将 cout
的缓冲区分配给其他内容(甚至nullptr
)。
在使用您的程序进行测试时(在我发现问题之前),我做了一些对我来说有意义的小更改。 例如,成功写入描述符后,您可以简单地bump(ret)
(您已经知道ret!=-1
,因此可以安全使用)。
我没有做过但您可以考虑的其他更改是由构造函数本身设置描述符,让析构函数关闭悬空描述符,并可能将动态分配从面向 C 的 malloc()
/realloc()
/free()
更改为面向C++的std::vector
。
说到分配,您在使用realloc()
时犯了一个非常常见的错误。 如果重新分配失败,realloc()
将保持原始指针不变,并通过返回空指针来指示失败。 由于使用相同的指针来获取返回值,因此可能会丢失对仍分配的内存的引用。 因此,如果您根本无法使用C++容器而不是 C 指针,则应将代码更改为更像这样的东西:
char *newptr;
newptr=static_cast<char *>(realloc(ptr, newsize));
if(newptr)
ptr=newptr;
else {
// Any treatment you want. I wrote some fatal failure code, but
// you might even prefer to go on with current buffer.
perror("ralloc()");
exit(1);
}
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- C++自定义比较函数
- 如何比较自定义类的std::变体
- std::设置自定义比较器
- 如何正确实现和访问运算符的各种自定义枚举器
- flutter:即使shouldRepaint()返回true,自定义画家也不会重新绘制
- std::带有自定义缓冲区的 iostream 不允许我写入
- istream std::cin如何修改自定义istream缓冲区
- 是否可以将 std::basic_ifstream 和 std::basic_ofstream 与自定义缓冲区一起使用?
- CGI程序的自定义输出缓冲区,运算符重载
- 自定义 OpenGL 缓冲区类不显示任何内容
- 在 C++ 中填充重复的自定义协议缓冲区字段
- 谷歌协议缓冲区,如何设置自定义类型的字段
- 为C++流自定义流缓冲区
- C++由自定义缓冲区支持的固定大小矢量
- 为什么_splitpath_s()使用自定义大小的字符串缓冲区来返回驱动器号
- 可以在程序退出时和请求输入时自动刷新自定义流缓冲区
- 自定义分配器,用于将stl矢量存储在OpenGL缓冲区对象中
- 在std::string中使用自定义分配器来重用已分配的字符缓冲区