I have a piece of code for reading smaps
into a buffer like this (for simplicity, I have removed error-control code and I have also assumed for this example that the buffer is big enough to fit the file entirely):
char* buff = (char*)malloc(2 * 1024 * 1024); // 2MB of buffer
int fd = open("/proc/self/smaps", O_RDONLY);
char* buff_it = buff;
char* buff_end = buff_it + buff_sz - 1;
ssize_t read_sz;
while ((read_sz = read(fd, buff_it, buff_end - buff_it)) > 0)
buff_it += read_sz;
close(fd);
// Parse buff
free(buff);
However, if I want to move to using ifstream
in C++, the code would look like this:
auto buff = std::make_unique<char[]>(2 * 1024 * 1024);
std::ifstream ifs("/proc/self/smaps", std::ios::binary);
ifs.read(buff.get(), 2 * 1024 * 1024);
buff[ifs.gcount()] = '\0';
but my question is, do read
internally use another private buffer to store intermediate input that is then copied to my supplied buffer? In other words, is that snippet of code using double buffering? Because that feels like a waste to be honest.
The kind of functionality I want is to say to ifstream
: hey, take this 2 MB buffer as your internal buffer, and fill it as much as you can, and then close the file (that I will parse the buffer later).