Skip to content

Commit

Permalink
improve macos native code to create shm
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosuc3m committed Dec 14, 2023
1 parent 7999e52 commit 95e1be9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions lib/ShmCreate.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,25 @@
#include <unistd.h>
#include <sys/stat.h>

// Function to get the size of a shared memory segment given its file descriptor
long get_shared_memory_size(int fd) {
struct stat shm_stat;
if (fstat(fd, &shm_stat) == -1) {
perror("fstat");
return -1;
}
return (long)shm_stat.st_size;
}

// Function to create a shared memory segment, modified to accept a long for size
int create_shared_memory(const char *name, long size) {
int fd = shm_open(name, O_CREAT | O_RDWR, 0666);
if (fd < 0) {
perror("shm_open");
return -1;
}
int size = get_shared_memory_size(fd);
if (size > 0) {
long already_size = get_shared_memory_size(fd);
if (already_size > 0) {
return fd;
}

Expand All @@ -33,16 +43,6 @@ void unlink_shared_memory(const char *name) {
}
}

// Function to get the size of a shared memory segment given its file descriptor
long get_shared_memory_size(int fd) {
struct stat shm_stat;
if (fstat(fd, &shm_stat) == -1) {
perror("fstat");
return -1;
}
return (long)shm_stat.st_size;
}

int main() {
const char *shm_name = "/myshm";
size_t shm_size = 1024;
Expand Down
Binary file modified src/main/resources/ShmCreate.dylib
Binary file not shown.

0 comments on commit 95e1be9

Please sign in to comment.