upd: [ipc] optimizes the return value of shm_close

This commit is contained in:
mutouyun 2023-07-23 18:33:44 +08:00
parent 746ee51584
commit f657fd1c7e
6 changed files with 15 additions and 15 deletions

View File

@ -17,6 +17,7 @@ LIBIMP_NAMESPACE_BEG_
using error_code_t = std::uint64_t; using error_code_t = std::uint64_t;
constexpr error_code_t error_number_limit = error_code_t(-1); constexpr error_code_t error_number_limit = error_code_t(-1);
constexpr error_code_t no_error = 0;
/** /**
* \brief Serves as the base class for specific error category types. * \brief Serves as the base class for specific error category types.

View File

@ -21,11 +21,15 @@ using shm_t = shm_handle *;
/// \brief Create a new shared memory handle with a name of the shared memory file. /// \brief Create a new shared memory handle with a name of the shared memory file.
LIBIMP_EXPORT ::LIBIMP::result<shm_t> shm_open(std::string name, LIBIMP_EXPORT ::LIBIMP::result<shm_t> shm_open(std::string name,
std::size_t size = 0, std::size_t size = 0,
mode::type = mode::create | mode::open) noexcept; mode::type = mode::create | mode::open) noexcept;
/// \brief Close the shared memory handle. /// \brief Close the shared memory handle.
LIBIMP_EXPORT ::LIBIMP::result_code shm_close(shm_t) noexcept; LIBIMP_EXPORT ::LIBIMP::result<void> shm_close(shm_t) noexcept;
/// \brief Gets the name of the shared memory file based on the shared memory handle.
/// \return empty string on failure.
LIBIMP_EXPORT std::string shm_name(shm_t) noexcept;
/// \brief Gets a memory pointer based on the shared memory handle. /// \brief Gets a memory pointer based on the shared memory handle.
/// \return nullptr on failure. /// \return nullptr on failure.
@ -39,10 +43,6 @@ LIBIMP_EXPORT std::size_t shm_size(shm_t) noexcept;
/// \remark [TBD] /// \remark [TBD]
LIBIMP_EXPORT ::LIBIMP::result_code shm_size(shm_t, std::size_t) noexcept; LIBIMP_EXPORT ::LIBIMP::result_code shm_size(shm_t, std::size_t) noexcept;
/// \brief Gets the name of the shared memory file based on the shared memory handle.
/// \return empty string on failure.
LIBIMP_EXPORT std::string shm_name(shm_t) noexcept;
/** /**
* \brief The shared memory object. * \brief The shared memory object.
*/ */

View File

@ -150,7 +150,7 @@ result<shm_t> shm_open(std::string name, std::size_t size, mode::type type) noex
/** /**
* \see https://man7.org/linux/man-pages/man2/mmap.2.html * \see https://man7.org/linux/man-pages/man2/mmap.2.html
*/ */
result_code shm_close(shm_t h) noexcept { result<void> shm_close(shm_t h) noexcept {
LIBIMP_LOG_(); LIBIMP_LOG_();
auto shm = valid(h); auto shm = valid(h);
if (shm == nullptr) return {}; if (shm == nullptr) return {};
@ -161,7 +161,7 @@ result_code shm_close(shm_t h) noexcept {
} }
/// \brief no unlink the file. /// \brief no unlink the file.
delete shm; delete shm;
return posix::succ; return no_error;
} }
LIBIPC_NAMESPACE_END_ LIBIPC_NAMESPACE_END_

View File

@ -33,7 +33,7 @@ namespace {
* \brief Closes an open object handle. * \brief Closes an open object handle.
* \see https://docs.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-closehandle * \see https://docs.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-closehandle
*/ */
result_code mmap_close(HANDLE h) { result<void> mmap_close(HANDLE h) {
LIBIMP_LOG_(); LIBIMP_LOG_();
if (h == NULL) { if (h == NULL) {
log.error("handle is null."); log.error("handle is null.");
@ -44,7 +44,7 @@ result_code mmap_close(HANDLE h) {
log.error("failed: CloseHandle(", h, "). error = ", err); log.error("failed: CloseHandle(", h, "). error = ", err);
return err; return err;
} }
return ERROR_SUCCESS; return no_error;
} }
/** /**
@ -161,7 +161,7 @@ result<SIZE_T> mmap_sizeof(LPCVOID mem) {
* \brief Unmaps a mapped view of a file from the calling process's address space. * \brief Unmaps a mapped view of a file from the calling process's address space.
* \see https://docs.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-unmapviewoffile * \see https://docs.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-unmapviewoffile
*/ */
result_code mmap_release(HANDLE h, LPCVOID mem) { result<void> mmap_release(HANDLE h, LPCVOID mem) {
LIBIMP_LOG_(); LIBIMP_LOG_();
if (h == NULL) { if (h == NULL) {
log.error("handle is null."); log.error("handle is null.");

View File

@ -39,7 +39,7 @@ result<shm_t> shm_open(std::string name, std::size_t size, mode::type type) noex
return new shm_handle{std::move(name), *sz, *mem, *h}; return new shm_handle{std::move(name), *sz, *mem, *h};
} }
result_code shm_close(shm_t h) noexcept { result<void> shm_close(shm_t h) noexcept {
LIBIMP_LOG_(); LIBIMP_LOG_();
if (h == nullptr) { if (h == nullptr) {
log.error("shm handle is null."); log.error("shm handle is null.");

View File

@ -3,7 +3,7 @@
#include "libipc/shm.h" #include "libipc/shm.h"
TEST(shm, create_close) { TEST(shm, open_close) {
EXPECT_FALSE(ipc::shm_open("hello-ipc-shm", 1024, ipc::mode::none)); EXPECT_FALSE(ipc::shm_open("hello-ipc-shm", 1024, ipc::mode::none));
auto shm1 = ipc::shm_open("hello-ipc-shm", 1024, ipc::mode::create | ipc::mode::open); auto shm1 = ipc::shm_open("hello-ipc-shm", 1024, ipc::mode::create | ipc::mode::open);
@ -77,7 +77,6 @@ TEST(shm, shared_memory) {
} }
#include <libimp/detect_plat.h> #include <libimp/detect_plat.h>
#if defined(LIBIMP_OS_LINUX) #if defined(LIBIMP_OS_LINUX)
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/un.h> #include <sys/un.h>