#define _GNU_SOURCE #include #include #include #include #include #include #include #include #include "network.h" int make_swap(void *message) { rbt_msg *msg = (rbt_msg*)message; printf("gid=%s uid=%s nick=%s raw=%s type=%c\n", msg->gid, msg->uid, msg->nickname, msg->raw_message, msg->message_type); } int create_swap(const char *name) { int fd = memfd_create(name,0); //申请共享内存 ftruncate(fd, sizeof(rbt_msg)); //调整大小 rbt_msg *init_msg = (rbt_msg*)mmap(NULL, sizeof(rbt_msg), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); char buf[MAX_MESSAGE_BUF] = {'\0'}; memcpy(init_msg->raw_message,buf,MAX_MESSAGE_BUF); memcpy(init_msg->nickname,buf,64); munmap((void*)init_msg,sizeof(rbt_msg)); //初始化 return fd; }