"shm_open" Gives "Variadic function is unavailable" in swift

Viewed 85

Is shm_open supported in swift?

I can use shm_unlink but shm_open doesn't exist.

shm_open("/myregion", O_CREAT | O_RDWR)

If shm_open not supported in Swift is there an equivalent way to create a shared memory file descriptor for mapping?

I have managed to create shared memory in C but still not in Swift.

1 Answers

Turned out that shm_open works fine, it is just that swift can't use Variadic C functions.

I solved the problem by creating a new c file with the function:

int shmopen(const char *path) {
    return shm_open(path, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
}

Place the function in the "Project-Bridging-Header.h" file:

int shmopen(const char *path);

Call the function from swift:

var fd = shmopen("/myregion")
print(fd)

And now it works fine.

Related