diff --git a/ev.c b/ev.c index ae5abc9c9..238dae124 100644 --- a/ev.c +++ b/ev.c @@ -9302,6 +9302,44 @@ EV_LOCAL void ev__init_once_unix(void); // #line 62 "ev.c" //////////////////////////////////////////////////////////////////////////////// +// FILE: ev/unix/shmem_unix.h +// SIZE: 529 +// SHA-256: e24041beaa663c95af84e928b22a3bf2465fdaac380962ed7a2623a6f6bd3300 +//////////////////////////////////////////////////////////////////////////////// +// #line 1 "ev/unix/shmem_unix.h" +#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 + +// #line 63 "ev.c" +//////////////////////////////////////////////////////////////////////////////// // FILE: ev/unix/stream_unix.h // SIZE: 1796 // SHA-256: 9b706c88ae8e57a461ee12ee041bf7c37ffec9dfe4d567408a62f9908c7ba6fe @@ -9373,7 +9411,7 @@ EV_LOCAL void ev__nonblock_stream_cleanup(ev_nonblock_stream_t* stream, unsigned #endif #endif -// #line 63 "ev.c" +// #line 64 "ev.c" //////////////////////////////////////////////////////////////////////////////// // FILE: ev/unix/work.h // SIZE: 231 @@ -9395,7 +9433,7 @@ EV_LOCAL void ev__exit_work(ev_loop_t* loop); #endif #endif -// #line 64 "ev.c" +// #line 65 "ev.c" //////////////////////////////////////////////////////////////////////////////// // FILE: ev/unix/async_unix.c @@ -9587,7 +9625,7 @@ void ev_async_wakeup(ev_async_t *handle) ev__async_post(handle->backend.pipfd[1]); } -// #line 66 "ev.c" +// #line 67 "ev.c" //////////////////////////////////////////////////////////////////////////////// // FILE: ev/unix/fs_unix.c // SIZE: 11029 @@ -10004,7 +10042,7 @@ void ev_file_munmap(ev_file_map_t* view) view->size = 0; } -// #line 67 "ev.c" +// #line 68 "ev.c" //////////////////////////////////////////////////////////////////////////////// // FILE: ev/unix/io_unix.c // SIZE: 8754 @@ -10439,7 +10477,7 @@ EV_LOCAL int ev__send_unix(int fd, ev_write_t* req, return _ev_io_finalize_send_req_unix(req, (size_t)write_size); } -// #line 68 "ev.c" +// #line 69 "ev.c" //////////////////////////////////////////////////////////////////////////////// // FILE: ev/unix/loop_unix.c // SIZE: 4177 @@ -10619,7 +10657,7 @@ EV_LOCAL void ev__poll(ev_loop_t* loop, uint32_t timeout) } } -// #line 69 "ev.c" +// #line 70 "ev.c" //////////////////////////////////////////////////////////////////////////////// // FILE: ev/unix/misc_unix.c // SIZE: 356 @@ -10649,7 +10687,7 @@ void ev__backend_shutdown(void) ev__exit_process_unix(); } -// #line 70 "ev.c" +// #line 71 "ev.c" //////////////////////////////////////////////////////////////////////////////// // FILE: ev/unix/misc_random_unix.c // SIZE: 7547 @@ -11011,7 +11049,7 @@ EV_LOCAL int ev__random(void* buf, size_t len) #endif -// #line 71 "ev.c" +// #line 72 "ev.c" //////////////////////////////////////////////////////////////////////////////// // FILE: ev/unix/mutex_unix.c // SIZE: 2029 @@ -11143,7 +11181,7 @@ int ev_mutex_try_enter(ev_mutex_t *handle) return EV_EBUSY; } -// #line 72 "ev.c" +// #line 73 "ev.c" //////////////////////////////////////////////////////////////////////////////// // FILE: ev/unix/once_unix.c // SIZE: 157 @@ -11160,7 +11198,7 @@ void ev_once_execute(ev_once_t* guard, ev_once_cb cb) } } -// #line 73 "ev.c" +// #line 74 "ev.c" //////////////////////////////////////////////////////////////////////////////// // FILE: ev/unix/pipe_unix.c // SIZE: 27171 @@ -12163,7 +12201,7 @@ void ev_pipe_close(ev_os_pipe_t fd) } } -// #line 74 "ev.c" +// #line 75 "ev.c" //////////////////////////////////////////////////////////////////////////////// // FILE: ev/unix/process_unix.c // SIZE: 16851 @@ -12901,7 +12939,7 @@ ssize_t ev_exepath(char *buffer, size_t size) return errcode; } -// #line 75 "ev.c" +// #line 76 "ev.c" //////////////////////////////////////////////////////////////////////////////// // FILE: ev/unix/sem_unix.c // SIZE: 963 @@ -12983,7 +13021,7 @@ int ev_sem_try_wait(ev_sem_t *sem) return 0; } -// #line 76 "ev.c" +// #line 77 "ev.c" //////////////////////////////////////////////////////////////////////////////// // FILE: ev/unix/shdlib_unix.c // SIZE: 963 @@ -13046,22 +13084,22 @@ int ev_dlsym(ev_shdlib_t* lib, const char* name, void** ptr) return EV_ENOENT; } -// #line 77 "ev.c" +// #line 78 "ev.c" //////////////////////////////////////////////////////////////////////////////// // FILE: ev/unix/shmem_unix.c -// SIZE: 2334 -// SHA-256: 560380ad3b2ea591fe64178ae8936a7a49a38a947852229cd1d616f6ec7e72e1 +// SIZE: 3093 +// SHA-256: f13db8acf9960186196074e8a3c2158625f391024e76b40bbc991cd012a11d4e //////////////////////////////////////////////////////////////////////////////// // #line 1 "ev/unix/shmem_unix.c" #include #include -#include /* For mode constants */ -#include /* For O_* constants */ +#include /* For mode constants */ +#include /* For O_* constants */ #include #include #include -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; @@ -13073,7 +13111,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; @@ -13086,7 +13125,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; @@ -13101,7 +13141,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); @@ -13127,7 +13167,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; @@ -13142,7 +13183,45 @@ 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) { @@ -13150,12 +13229,14 @@ void ev_shm_exit(ev_shm_t* shm) } 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); } -// #line 78 "ev.c" +// #line 79 "ev.c" //////////////////////////////////////////////////////////////////////////////// // FILE: ev/unix/stream_unix.c // SIZE: 6160 @@ -13403,7 +13484,7 @@ EV_LOCAL void ev__nonblock_stream_cleanup(ev_nonblock_stream_t* stream, unsigned } } -// #line 79 "ev.c" +// #line 80 "ev.c" //////////////////////////////////////////////////////////////////////////////// // FILE: ev/unix/tcp_unix.c // SIZE: 14926 @@ -14000,7 +14081,7 @@ EV_LOCAL int ev__tcp_open(ev_tcp_t *tcp, int fd) return 0; } -// #line 80 "ev.c" +// #line 81 "ev.c" //////////////////////////////////////////////////////////////////////////////// // FILE: ev/unix/thread_unix.c // SIZE: 4496 @@ -14230,7 +14311,7 @@ void *ev_thread_key_get(ev_thread_key_t *key) return pthread_getspecific(key->tls); } -// #line 81 "ev.c" +// #line 82 "ev.c" //////////////////////////////////////////////////////////////////////////////// // FILE: ev/unix/threadpool_unix.c // SIZE: 942 @@ -14270,7 +14351,7 @@ EV_LOCAL void ev__exit_work(ev_loop_t* loop) loop->backend.threadpool.evtfd[1] = -1; } -// #line 82 "ev.c" +// #line 83 "ev.c" //////////////////////////////////////////////////////////////////////////////// // FILE: ev/unix/time_unix.c // SIZE: 284 @@ -14293,7 +14374,7 @@ uint64_t ev_hrtime(void) return t.tv_sec * (uint64_t) 1e9 + t.tv_nsec; } -// #line 83 "ev.c" +// #line 84 "ev.c" //////////////////////////////////////////////////////////////////////////////// // FILE: ev/unix/udp_unix.c // SIZE: 25305 @@ -15316,7 +15397,7 @@ int ev_udp_set_ttl(ev_udp_t *udp, int ttl) return _ev_udp_set_ttl_unix(udp, ttl, IP_TTL, IPV6_UNICAST_HOPS); } -// #line 84 "ev.c" +// #line 85 "ev.c" #endif @@ -15343,7 +15424,7 @@ EV_LOCAL void ev__assertion_failure(const char* exp, const char* file, int line, abort(); } -// #line 88 "ev.c" +// #line 89 "ev.c" //////////////////////////////////////////////////////////////////////////////// // FILE: ev/allocator.c // SIZE: 1108 @@ -15414,7 +15495,7 @@ char *ev__strdup(const char *s) return memcpy(m, s, len); } -// #line 89 "ev.c" +// #line 90 "ev.c" //////////////////////////////////////////////////////////////////////////////// // FILE: ev/atomic.c // SIZE: 5881 @@ -15735,7 +15816,7 @@ EV_LOCAL void ev__atomic_exit(void) #endif -// #line 90 "ev.c" +// #line 91 "ev.c" //////////////////////////////////////////////////////////////////////////////// // FILE: ev/errno.c // SIZE: 438 @@ -15761,7 +15842,7 @@ const char* ev_strerror(int err) #undef EV_EXPAND_ERRMAP } -// #line 91 "ev.c" +// #line 92 "ev.c" //////////////////////////////////////////////////////////////////////////////// // FILE: ev/fs.c // SIZE: 25615 @@ -16821,7 +16902,7 @@ EV_LOCAL int ev__fs_remove(const char* path, int recursive) return _ev_fs_remove(path); } -// #line 92 "ev.c" +// #line 93 "ev.c" //////////////////////////////////////////////////////////////////////////////// // FILE: ev/handle.c // SIZE: 3642 @@ -16972,7 +17053,7 @@ EV_LOCAL size_t ev__process_endgame(ev_loop_t* loop) return active_count; } -// #line 93 "ev.c" +// #line 94 "ev.c" //////////////////////////////////////////////////////////////////////////////// // FILE: ev/list.c // SIZE: 3572 @@ -17162,7 +17243,7 @@ void ev_list_migrate(ev_list_t* dst, ev_list_t* src) src->size = 0; } -// #line 94 "ev.c" +// #line 95 "ev.c" //////////////////////////////////////////////////////////////////////////////// // FILE: ev/log.c // SIZE: 1941 @@ -17258,7 +17339,7 @@ EV_LOCAL void ev__dump_hex(const void* data, size_t size, size_t width) } -// #line 95 "ev.c" +// #line 96 "ev.c" //////////////////////////////////////////////////////////////////////////////// // FILE: ev/loop.c // SIZE: 8737 @@ -17655,7 +17736,7 @@ void ev_loop_walk(ev_loop_t* loop, ev_walk_cb cb, void* arg) } } -// #line 96 "ev.c" +// #line 97 "ev.c" //////////////////////////////////////////////////////////////////////////////// // FILE: ev/map.c // SIZE: 23122 @@ -18435,7 +18516,7 @@ ev_map_node_t* ev_map_prev(const ev_map_node_t* node) return _ev_map_low_prev(node); } -// #line 97 "ev.c" +// #line 98 "ev.c" //////////////////////////////////////////////////////////////////////////////// // FILE: ev/misc.c // SIZE: 4675 @@ -18650,7 +18731,7 @@ EV_API int ev_random(ev_loop_t* loop, ev_random_req_t* req, void* buf, return ev_loop_queue_work(loop, &req->work, _ev_random_on_work, _ev_random_on_done); } -// #line 98 "ev.c" +// #line 99 "ev.c" //////////////////////////////////////////////////////////////////////////////// // FILE: ev/pipe.c // SIZE: 1714 @@ -18721,7 +18802,7 @@ EV_LOCAL int ev__pipe_write_init_ext(ev_pipe_write_req_t *req, return 0; } -// #line 99 "ev.c" +// #line 100 "ev.c" //////////////////////////////////////////////////////////////////////////////// // FILE: ev/queue.c // SIZE: 1816 @@ -18804,7 +18885,7 @@ int ev_queue_empty(const ev_queue_node_t* node) return EV_QUEUE_NEXT(node) == node; } -// #line 100 "ev.c" +// #line 101 "ev.c" //////////////////////////////////////////////////////////////////////////////// // FILE: ev/ringbuffer.c // SIZE: 17440 @@ -19359,25 +19440,25 @@ EV_LOCAL ring_buffer_token_t* ring_buffer_next(const ring_buffer_t* handler, con return &(node->token); } -// #line 101 "ev.c" +// #line 102 "ev.c" //////////////////////////////////////////////////////////////////////////////// // FILE: ev/shmem.c -// SIZE: 121 -// SHA-256: 9a619451978c80fa1da62cf9598d67b99e8e244bcdfdb33cefa709eaed8d1c28 +// SIZE: 129 +// SHA-256: f2d8bee7408dc4d79ec2b33c0f7fd2d1dc9d5d82f0e8df99fbe61a2b07f370ff //////////////////////////////////////////////////////////////////////////////// // #line 1 "ev/shmem.c" -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; } -// #line 102 "ev.c" +// #line 103 "ev.c" //////////////////////////////////////////////////////////////////////////////// // FILE: ev/threadpool.c // SIZE: 9221 @@ -19771,7 +19852,7 @@ EV_LOCAL void ev__threadpool_process(ev_loop_t *loop) } } -// #line 103 "ev.c" +// #line 104 "ev.c" //////////////////////////////////////////////////////////////////////////////// // FILE: ev/timer.c // SIZE: 2849 @@ -19898,7 +19979,7 @@ void ev_timer_stop(ev_timer_t *handle) ev_map_erase(&handle->base.loop->timer.heap, &handle->node); } -// #line 104 "ev.c" +// #line 105 "ev.c" //////////////////////////////////////////////////////////////////////////////// // FILE: ev/udp.c // SIZE: 3071 @@ -20032,7 +20113,7 @@ int ev_udp_send(ev_udp_t *udp, ev_buf_t *bufs, size_t nbuf, return ret; } -// #line 105 "ev.c" +// #line 106 "ev.c" //////////////////////////////////////////////////////////////////////////////// // FILE: ev/version.c // SIZE: 303 @@ -20056,5 +20137,5 @@ unsigned ev_version_code(void) return EV_VERSION_CODE; } -// #line 106 "ev.c" +// #line 107 "ev.c" diff --git a/ev.h b/ev.h index b561235aa..800e20584 100644 --- a/ev.h +++ b/ev.h @@ -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" /** @@ -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. */ @@ -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 @@ -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 @@ -3116,7 +3093,7 @@ 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 @@ -3124,27 +3101,27 @@ EV_API int ev_shm_init(ev_shm_t* shm, const char* key, size_t size); * @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 diff --git a/include/ev.h b/include/ev.h index 54aac77ce..28a125ae8 100644 --- a/include/ev.h +++ b/include/ev.h @@ -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" diff --git a/include/ev/shm.h b/include/ev/shmem.h similarity index 60% rename from include/ev/shm.h rename to include/ev/shmem.h index 03b6e7ca7..c68a09819 100644 --- a/include/ev/shm.h +++ b/include/ev/shmem.h @@ -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 @@ -27,7 +21,7 @@ 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 @@ -35,27 +29,27 @@ EV_API int ev_shm_init(ev_shm_t* shm, const char* key, size_t size); * @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 diff --git a/include/ev/unix.h b/include/ev/unix.h index 707e77566..e184395ef 100644 --- a/include/ev/unix.h +++ b/include/ev/unix.h @@ -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. */ diff --git a/src/ev.c b/src/ev.c index 3b793fcef..77b588105 100644 --- a/src/ev.c +++ b/src/ev.c @@ -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" diff --git a/src/ev/shmem.c b/src/ev/shmem.c index be4f08b1b..69a886b7f 100644 --- a/src/ev/shmem.c +++ b/src/ev/shmem.c @@ -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; } diff --git a/src/ev/unix/shmem_unix.c b/src/ev/unix/shmem_unix.c index 32e72adc0..96741daa1 100644 --- a/src/ev/unix/shmem_unix.c +++ b/src/ev/unix/shmem_unix.c @@ -1,12 +1,12 @@ #include #include -#include /* For mode constants */ -#include /* For O_* constants */ +#include /* For mode constants */ +#include /* For O_* constants */ #include #include #include -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; @@ -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; @@ -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; @@ -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); @@ -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; @@ -87,7 +90,45 @@ 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) { @@ -95,7 +136,9 @@ void ev_shm_exit(ev_shm_t* shm) } 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); } diff --git a/src/ev/unix/shmem_unix.h b/src/ev/unix/shmem_unix.h new file mode 100644 index 000000000..89167b43f --- /dev/null +++ b/src/ev/unix/shmem_unix.h @@ -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 diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 2463975b0..365e8b7ee 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -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" diff --git a/test/cases/shm.c b/test/cases/shm.c deleted file mode 100644 index 9e39be4c0..000000000 --- a/test/cases/shm.c +++ /dev/null @@ -1,37 +0,0 @@ -#include "ev.h" -#include "test.h" -#include - -struct test_3b5b -{ - ev_shm_t smt_token_1; - ev_shm_t smt_token_2; -}; - -struct test_3b5b g_test_3b5b; - -TEST_FIXTURE_SETUP(shm) -{ - const char* key = "/test"; - const size_t mem_size = 4096; - - memset(&g_test_3b5b, 0, sizeof(g_test_3b5b)); - - ASSERT_EQ_INT(ev_shm_init(&g_test_3b5b.smt_token_1, key, mem_size), 0); - ASSERT_EQ_INT(ev_shm_open(&g_test_3b5b.smt_token_2, key), 0); - - ASSERT_EQ_SIZE(ev_shm_size(&g_test_3b5b.smt_token_1), mem_size); - ASSERT_EQ_SIZE(ev_shm_size(&g_test_3b5b.smt_token_2), mem_size); -} - -TEST_FIXTURE_TEARDOWN(shm) -{ - ev_shm_exit(&g_test_3b5b.smt_token_1); - ev_shm_exit(&g_test_3b5b.smt_token_2); -} - -TEST_F(shm, memset) -{ - memset(ev_shm_addr(&g_test_3b5b.smt_token_1), 0, ev_shm_size(&g_test_3b5b.smt_token_1)); - memset(ev_shm_addr(&g_test_3b5b.smt_token_2), 0, ev_shm_size(&g_test_3b5b.smt_token_2)); -} diff --git a/test/cases/shmem.c b/test/cases/shmem.c new file mode 100644 index 000000000..57e7a81ea --- /dev/null +++ b/test/cases/shmem.c @@ -0,0 +1,49 @@ +#include "ev.h" +#include "test.h" +#include +#include + +#define TEST_SHMEM_KEY "/e35955c2" +#define TEST_SHMEM_SIZE (4096) + +struct test_3b5b +{ + ev_shmem_t *smt_token_1; + ev_shmem_t *smt_token_2; +}; + +struct test_3b5b *g_test_3b5b = NULL; + +TEST_FIXTURE_SETUP(shm) +{ + g_test_3b5b = ev_calloc(1, sizeof(*g_test_3b5b)); + + ASSERT_EQ_INT(ev_shmem_init(&g_test_3b5b->smt_token_1, TEST_SHMEM_KEY, + TEST_SHMEM_SIZE), + 0, "%s(%d)", ev_strerror(_L), _L); + ASSERT_EQ_INT(ev_shmem_open(&g_test_3b5b->smt_token_2, TEST_SHMEM_KEY), 0); + + ASSERT_EQ_SIZE(ev_shmem_size(g_test_3b5b->smt_token_1), TEST_SHMEM_SIZE); + ASSERT_EQ_SIZE(ev_shmem_size(g_test_3b5b->smt_token_2), TEST_SHMEM_SIZE); +} + +TEST_FIXTURE_TEARDOWN(shm) +{ + ev_shmem_exit(g_test_3b5b->smt_token_1); + ev_shmem_exit(g_test_3b5b->smt_token_2); + + ev_free(g_test_3b5b); + g_test_3b5b = NULL; + +#if defined(__linux__) + shm_unlink(TEST_SHMEM_KEY); +#endif +} + +TEST_F(shm, memset) +{ + memset(ev_shmem_addr(g_test_3b5b->smt_token_1), 0, + ev_shmem_size(g_test_3b5b->smt_token_1)); + memset(ev_shmem_addr(g_test_3b5b->smt_token_2), 0, + ev_shmem_size(g_test_3b5b->smt_token_2)); +}