Create shared memory object

This commit is contained in:
Alex Kotov 2020-12-13 16:32:17 +05:00
parent 2d609d4783
commit 8095a02029
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
3 changed files with 16 additions and 0 deletions

View File

@ -30,9 +30,12 @@ AC_FUNC_MALLOC
AC_FUNC_MMAP
AC_CHECK_HEADER_STDBOOL
AC_CHECK_HEADERS([fcntl.h])
AC_CHECK_HEADERS([stddef.h])
AC_CHECK_HEADERS([stdlib.h])
AC_CHECK_HEADERS([string.h])
AC_CHECK_HEADERS([sys/mman.h])
AC_CHECK_HEADERS([sys/stat.h])
AC_CHECK_FUNCS([ftruncate])
AC_CHECK_FUNCS([munmap])

View File

@ -20,6 +20,7 @@ enum Shmemq_Error {
SHMEMQ_ERROR_NONE = 0,
SHMEMQ_ERROR_MALLOC = 1,
SHMEMQ_ERROR_INVALID_NAME = 2,
SHMEMQ_ERROR_SHARED_MEMORY = 3,
};
struct Shmemq_FrameHeader {
@ -45,6 +46,7 @@ struct Shmemq_Buffer {
struct Shmemq {
char name[SHMEMQ_NAME_SIZE_MAX];
bool is_consumer;
int shm_id;
struct Shmemq_Buffer *buffer;
};

View File

@ -4,8 +4,11 @@
#include <shmemq.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
struct Shmemq *shmemq_new(
const char *const name,
@ -48,6 +51,14 @@ enum Shmemq_Error shmemq_init(
shmemq->is_consumer = size == 0;
shmemq->shm_id = shm_open(
shmemq->name,
O_CREAT | O_RDWR,
S_IRUSR | S_IWUSR
);
if (shmemq->shm_id == -1) return SHMEMQ_ERROR_SHARED_MEMORY;
shmemq->buffer = NULL;
return SHMEMQ_ERROR_NONE;