I'm backporting a piece of code that uses a virtual memory trick involving a file descriptor that gets passed to mmap
, but doesn't have a mount point. Having the physical file would be an unnecessary overhead in this application. The original code uses memfd_create
which is great.
Since Linux 2.6 doesn't have memfd_create
or the O_TMPFILE
flag for open
, I'm currently creating a file with mkstemp
and then unlinking it without closing it first. This works, but it doesn't please me at all.
Is there a better way to get a file descriptor for mmap
purposes without ever touching the file system in 2.6?
Before somebody says "XY problem," what I really need is two different virtual memory addresses to the same data in memory. This is implemented by mmap
'ing the same anonymous file to two different addresses. Any other "Y" to my "X" also welcome.
Thanks
I considered two approaches:
/dev/shm/
rather than /tmp/
shm_open
to get a file descriptor. Although irrelevant to the specific problem at hand, /dev/shm/
is not guaranteed to exist on all distributions, so #2 felt more correct to me.
In order to not have to worry about unique names of the shared memory objects, I just generate UUIDs.
I think I'm happy with this.
Shout out to @NominalAnimal.