I am using an API that takes a FILE *
and am using that to create a data buffer in memory:
std::shared_ptr<FILE> f(tmpfile(), fclose);
write_to_file(f.get());
rewind(f.get());
auto data = make_file_buffer(f.get());
return data;
This works, but is slower than writing to a memory buffer.
Is it possible to get this to write to a memory file and avoid reading/writing to disk (like stdin/stdout/stderr read/write to the console)?
NOTE: I am using Linux, so have access to Linux and POSIX APIs.
Yes, this is possible, see fmemopen.
I wrote a comment above, but I'll flesh it out for an answer.
Since your API expects a FILE handle, the easiest way would be to create a file on an in-memory filesystem, such as something mounted with ramfs or tmpfs on Linux.
There's one of these created by default on most Linux systems under /dev/shm, so creating any file under there will still exist as an actual file, but will only exist in memory. Of course, if you reboot, the contents of the file will be lost. I also don't think it can be paged to disk, so try and avoid writing huge files or you'll take up all the space in your memory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With