Skip to content

Commit

Permalink
Hide ev_shm_t
Browse files Browse the repository at this point in the history
  • Loading branch information
qgymib committed Jan 6, 2025
1 parent 8dc80b1 commit 77c8656
Show file tree
Hide file tree
Showing 12 changed files with 290 additions and 169 deletions.
191 changes: 136 additions & 55 deletions ev.c

Large diffs are not rendered by default.

47 changes: 12 additions & 35 deletions ev.h
Original file line number Diff line number Diff line change
Expand Up @@ -1822,8 +1822,8 @@ typedef struct ev_file_map_backend
#else
////////////////////////////////////////////////////////////////////////////////
// FILE: ev/unix.h
// SIZE: 11722
// SHA-256: 96ab076458d7e4fb1c296a4c13bac59ae2212fb06355d0d2ba0cef805048cea4
// SIZE: 11287
// SHA-256: 1056acdf3ff770b9d6c5f6c1c182af2df13ee2b881f5b38e17e2586c31d28a4a
////////////////////////////////////////////////////////////////////////////////
// #line 1 "ev/unix.h"
/**
Expand Down Expand Up @@ -2216,23 +2216,6 @@ struct ev_nonblock_stream
*/
#define EV_PIPE_BACKEND_INVALID { 0 }

/**
* @brief Unix backend for #ev_shm_t.
*/
#define EV_SHM_BACKEND \
struct ev_shm_backend {\
char name[256];\
int map_file;\
struct {\
unsigned is_open : 1;\
}mask;\
}

/**
* @brief Initialize #EV_SHM_BACKEND to Unix specific invalid value.
*/
#define EV_SHM_BACKEND_INVALID { 0 }

/**
* @brief Unix backend for #ev_process_t.
*/
Expand Down Expand Up @@ -3082,11 +3065,11 @@ EV_API void ev_once_execute(ev_once_t* guard, ev_once_cb cb);

// #line 88 "ev.h"
////////////////////////////////////////////////////////////////////////////////
// FILE: ev/shm.h
// SIZE: 1487
// SHA-256: 05ee79576229bd508f77925351f441e9258257235501bf38571784df54f22659
// FILE: ev/shmem.h
// SIZE: 1252
// SHA-256: 8af4ad69977271692db5c5c1a93933d2cf66db5b76dbcf5b6fdcca07e12dac3f
////////////////////////////////////////////////////////////////////////////////
// #line 1 "ev/shm.h"
// #line 1 "ev/shmem.h"
#ifndef __EV_SHARD_MEMORY_H__
#define __EV_SHARD_MEMORY_H__
#ifdef __cplusplus
Expand All @@ -3101,13 +3084,7 @@ extern "C" {
/**
* @brief Shared memory type.
*/
typedef struct ev_shm
{
void* addr; /**< Shared memory address */
size_t size; /**< Shared memory size */
EV_SHM_BACKEND backend; /**< Backend */
} ev_shm_t;
#define EV_SHM_INIT { NULL, 0, EV_SHM_BACKEND_INVALID }
typedef struct ev_shmem ev_shmem_t;

/**
* @brief Create a new shared memory
Expand All @@ -3116,35 +3093,35 @@ typedef struct ev_shm
* @param[in] size Shared memory size
* @return #ev_errno_t
*/
EV_API int ev_shm_init(ev_shm_t* shm, const char* key, size_t size);
EV_API int ev_shmem_init(ev_shmem_t **shm, const char *key, size_t size);

/**
* @brief Open a existing shared memory
* @param[out] shm Shared memory token
* @param[in] key Shared memory key
* @return #ev_errno_t
*/
EV_API int ev_shm_open(ev_shm_t* shm, const char* key);
EV_API int ev_shmem_open(ev_shmem_t **shm, const char *key);

/**
* @brief Close shared memory
* @param[in] shm Shared memory token
*/
EV_API void ev_shm_exit(ev_shm_t* shm);
EV_API void ev_shmem_exit(ev_shmem_t *shm);

/**
* @brief Get shared memory address
* @param[in] shm Shared memory token
* @return Shared memory address
*/
EV_API void* ev_shm_addr(ev_shm_t* shm);
EV_API void *ev_shmem_addr(ev_shmem_t *shm);

/**
* @brief Get shared memory size
* @param[in] shm Shared memory token
* @return Shared memory size
*/
EV_API size_t ev_shm_size(ev_shm_t* shm);
EV_API size_t ev_shmem_size(ev_shmem_t *shm);

/**
* @} EV_SHARED_MEMORY
Expand Down
2 changes: 1 addition & 1 deletion include/ev.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
#include "ev/mutex.h"
#include "ev/sem.h"
#include "ev/once.h"
#include "ev/shm.h"
#include "ev/shmem.h"
#include "ev/shdlib.h"
#include "ev/time.h"
#include "ev/handle.h"
Expand Down
18 changes: 6 additions & 12 deletions include/ev/shm.h → include/ev/shmem.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@ extern "C" {
/**
* @brief Shared memory type.
*/
typedef struct ev_shm
{
void* addr; /**< Shared memory address */
size_t size; /**< Shared memory size */
EV_SHM_BACKEND backend; /**< Backend */
} ev_shm_t;
#define EV_SHM_INIT { NULL, 0, EV_SHM_BACKEND_INVALID }
typedef struct ev_shmem ev_shmem_t;

/**
* @brief Create a new shared memory
Expand All @@ -27,35 +21,35 @@ typedef struct ev_shm
* @param[in] size Shared memory size
* @return #ev_errno_t
*/
EV_API int ev_shm_init(ev_shm_t* shm, const char* key, size_t size);
EV_API int ev_shmem_init(ev_shmem_t **shm, const char *key, size_t size);

/**
* @brief Open a existing shared memory
* @param[out] shm Shared memory token
* @param[in] key Shared memory key
* @return #ev_errno_t
*/
EV_API int ev_shm_open(ev_shm_t* shm, const char* key);
EV_API int ev_shmem_open(ev_shmem_t **shm, const char *key);

/**
* @brief Close shared memory
* @param[in] shm Shared memory token
*/
EV_API void ev_shm_exit(ev_shm_t* shm);
EV_API void ev_shmem_exit(ev_shmem_t *shm);

/**
* @brief Get shared memory address
* @param[in] shm Shared memory token
* @return Shared memory address
*/
EV_API void* ev_shm_addr(ev_shm_t* shm);
EV_API void *ev_shmem_addr(ev_shmem_t *shm);

/**
* @brief Get shared memory size
* @param[in] shm Shared memory token
* @return Shared memory size
*/
EV_API size_t ev_shm_size(ev_shm_t* shm);
EV_API size_t ev_shmem_size(ev_shmem_t *shm);

/**
* @} EV_SHARED_MEMORY
Expand Down
17 changes: 0 additions & 17 deletions include/ev/unix.h
Original file line number Diff line number Diff line change
Expand Up @@ -388,23 +388,6 @@ struct ev_nonblock_stream
*/
#define EV_PIPE_BACKEND_INVALID { 0 }

/**
* @brief Unix backend for #ev_shm_t.
*/
#define EV_SHM_BACKEND \
struct ev_shm_backend {\
char name[256];\
int map_file;\
struct {\
unsigned is_open : 1;\
}mask;\
}

/**
* @brief Initialize #EV_SHM_BACKEND to Unix specific invalid value.
*/
#define EV_SHM_BACKEND_INVALID { 0 }

/**
* @brief Unix backend for #ev_process_t.
*/
Expand Down
1 change: 1 addition & 0 deletions src/ev.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
# include "ev/unix/process_unix.h"
# include "ev/unix/tcp_unix.h"
# include "ev/unix/loop_unix.h"
# include "ev/unix/shmem_unix.h"
# include "ev/unix/stream_unix.h"
# include "ev/unix/work.h"

Expand Down
4 changes: 2 additions & 2 deletions src/ev/shmem.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

void* ev_shm_addr(ev_shm_t* shm)
void* ev_shmem_addr(ev_shmem_t* shm)
{
return shm->addr;
}

size_t ev_shm_size(ev_shm_t* shm)
size_t ev_shmem_size(ev_shmem_t* shm)
{
return shm->size;
}
61 changes: 52 additions & 9 deletions src/ev/unix/shmem_unix.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include <assert.h>
#include <sys/mman.h>
#include <sys/stat.h> /* For mode constants */
#include <fcntl.h> /* For O_* constants */
#include <sys/stat.h> /* For mode constants */
#include <fcntl.h> /* For O_* constants */
#include <stdio.h>
#include <string.h>
#include <unistd.h>

int ev_shm_init(ev_shm_t* shm, const char* key, size_t size)
static int s_ev_shm_init(ev_shmem_t *shm, const char *key, size_t size)
{
int err;
shm->size = size;
Expand All @@ -18,7 +18,8 @@ int ev_shm_init(ev_shm_t* shm, const char* key, size_t size)
}
memset(&shm->backend.mask, 0, sizeof(shm->backend.mask));

shm->backend.map_file = shm_open(key, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
shm->backend.map_file =
shm_open(key, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
if (shm->backend.map_file == -1)
{
err = errno;
Expand All @@ -31,7 +32,8 @@ int ev_shm_init(ev_shm_t* shm, const char* key, size_t size)
goto err_ftruncate;
}

shm->addr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, shm->backend.map_file, 0);
shm->addr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED,
shm->backend.map_file, 0);
if (shm->addr == NULL)
{
err = errno;
Expand All @@ -46,7 +48,7 @@ int ev_shm_init(ev_shm_t* shm, const char* key, size_t size)
return ev__translate_sys_error(err);
}

int ev_shm_open(ev_shm_t* shm, const char* key)
static int s_ev_shm_open(ev_shmem_t *shm, const char *key)
{
int err;
int ret = snprintf(shm->backend.name, sizeof(shm->backend.name), "%s", key);
Expand All @@ -72,7 +74,8 @@ int ev_shm_open(ev_shm_t* shm, const char* key)
}
shm->size = statbuf.st_size;

shm->addr = mmap(NULL, shm->size, PROT_READ | PROT_WRITE, MAP_SHARED, shm->backend.map_file, 0);
shm->addr = mmap(NULL, shm->size, PROT_READ | PROT_WRITE, MAP_SHARED,
shm->backend.map_file, 0);
if (shm->addr == NULL)
{
err = errno;
Expand All @@ -87,15 +90,55 @@ int ev_shm_open(ev_shm_t* shm, const char* key)
return ev__translate_sys_error(err);
}

void ev_shm_exit(ev_shm_t* shm)
int ev_shmem_init(ev_shmem_t **shm, const char *key, size_t size)
{
ev_shmem_t *handle = ev_malloc(sizeof(ev_shmem_t));
if (handle == NULL)
{
return EV_ENOMEM;
}

int ret = s_ev_shm_init(handle, key, size);
if (ret != 0)
{
ev_free(handle);
return ret;
}

*shm = handle;
return 0;
}

int ev_shmem_open(ev_shmem_t **shm, const char *key)
{
ev_shmem_t *handle = ev_malloc(sizeof(ev_shmem_t));
if (handle == NULL)
{
return EV_ENOMEM;
}

int ret = s_ev_shm_open(handle, key);
if (ret != 0)
{
ev_free(handle);
return ret;
}

*shm = handle;
return 0;
}

void ev_shmem_exit(ev_shmem_t *shm)
{
if (!shm->backend.mask.is_open)
{
shm_unlink(shm->backend.name);
}

int ret = munmap(shm->addr, shm->size);
assert(ret == 0); (void)ret;
assert(ret == 0);
(void)ret;

close(shm->backend.map_file);
ev_free(shm);
}
30 changes: 30 additions & 0 deletions src/ev/unix/shmem_unix.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef __EV_SHARED_MEMORY_UNIX_H__
#define __EV_SHARED_MEMORY_UNIX_H__
#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief Unix backend for #ev_shm_t.
*/
typedef struct ev_shm_backend
{
char name[256];
int map_file;
struct
{
unsigned is_open : 1;
} mask;
} ev_shm_backend_t;

struct ev_shmem
{
void *addr; /**< Shared memory address */
size_t size; /**< Shared memory size */
ev_shm_backend_t backend; /**< Backend */
};

#ifdef __cplusplus
}
#endif
#endif
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ add_library(ev_test_lib SHARED
"test/cases/process.redirect_file.c"
"test/cases/queue.c"
"test/cases/shdlib.c"
"test/cases/shm.c"
"test/cases/shmem.c"
"test/cases/tcp_close_in_middle.c"
"test/cases/tcp_connect_non_exist.c"
"test/cases/tcp_idle_client.c"
Expand Down
Loading

0 comments on commit 77c8656

Please sign in to comment.