Does MacOS – Does Mac OS have a way to create an anonymous file mapping?

In the UNIX world, the standard way to create a file mapping object supported by RAM or page files instead of disk files is to call shm_open. This will create a memory map with a name, and Return a file handle that can be passed to mmap.

The problem is that it creates a name. It would be great if I could create an anonymous memory map. This would solve two problems:

>It can avoid the problem that two instances of the same program may step on each other’s mapping files.
>If the program crashes or terminates suddenly, no shared memory objects will be left. Call shm_unlink immediately after shm_open Is a possibility, but this leaves a small window in which abrupt termination will leave the object until the next restart.

In Linux, there is memfd_create to solve this problem. Similarly, Windows allows an empty name to be passed to CreateFileMappingW to create an anonymous mapping.

Does Mac OS have the same function?

You can use open to create a file, and then immediately delete it from the file system. File description The character will remain open, and the file itself will remain active until its last descriptor is closed. If you need to share file descriptors between processes, this can solve the two problems you listed.

Alternatively, passing MAP_ANON and MAP_SHARED to mmap will create a shared memory area that is not supported by any files but will be shared between this process and all its branches. However, as with any memory mapping, It is deleted on exec.

In the UNIX world, the standard way to create a file mapping object backed by RAM or page files instead of disk files is to call shm_open. This Will create a memory map with a name and return a file handle that can be passed to mmap.

The problem is that it creates a name. It would be great if I can create an anonymous memory map. This will solve two problems:

>It can avoid the problem that two instances of the same program may step on each other’s mapping files.
>If the program crashes or terminates suddenly, it will not stay Under the shared memory object. It is a possibility to call shm_unlink immediately after shm_open, but this leaves a small window in which abrupt termination will cause the object to leave until the next restart.

In Linux In, there is memfd_create to solve this problem. Similarly, Windows allows an empty name to be passed to CreateFileMappingW to create an anonymous mapping.

Does Mac OS have the same function?

You can use open to create a file and then immediately delete it from the file system. The file descriptor will remain open and the file itself will remain active Until its last descriptor is closed. If you need to share file descriptors between processes, this can solve the two problems you listed.

Or, pass MAP_ANON and MAP_SHARED to mmap creates a shared memory area that is not supported by any files, but will be shared between this process and all its branches. However, like any memory mapping, it is deleted on exec.

< /p>

Leave a Comment

Your email address will not be published.