diff --git a/src/libipc/platform/win/close_handle.h b/src/libipc/platform/win/close_handle.h new file mode 100644 index 0000000..b7c51e7 --- /dev/null +++ b/src/libipc/platform/win/close_handle.h @@ -0,0 +1,38 @@ +/** + * \file libipc/platform/win/close_handle.h + * \author mutouyun (orz@orzz.org) + */ +#pragma once + +#include + +#include "libimp/log.h" +#include "libimp/system.h" + +#include "libipc/def.h" + +LIBIPC_NAMESPACE_BEG_ +using namespace ::LIBIMP; + +namespace winapi { + +/** + * \brief Closes an open object handle. + * \see https://docs.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-closehandle + */ +inline result close_handle(HANDLE h) noexcept { + LIBIMP_LOG_(); + if (h == NULL) { + log.error("handle is null."); + return std::make_error_code(std::errc::invalid_argument); + } + if (!::CloseHandle(h)) { + auto err = sys::error(); + log.error("failed: CloseHandle(", h, "). error = ", err); + return err; + } + return std::error_code{}; +} + +} // namespace winapi +LIBIPC_NAMESPACE_END_ diff --git a/src/libipc/platform/win/event_impl.h b/src/libipc/platform/win/event_impl.h index 9ca9c44..be441fc 100644 --- a/src/libipc/platform/win/event_impl.h +++ b/src/libipc/platform/win/event_impl.h @@ -12,13 +12,17 @@ #include #include "libimp/log.h" +#include "libpmr/new.h" #include "libipc/event.h" #include "get_sa.h" #include "to_tchar.h" +#include "close_handle.h" LIBIPC_NAMESPACE_BEG_ + using namespace ::LIBIMP; +using namespace ::LIBPMR; struct evt_handle { std::string name; @@ -51,26 +55,20 @@ result evt_open(std::string name) noexcept { log.error("failed: CreateEvent(FALSE, FALSE, ", name, "). error = ", err); return err; } - return new evt_handle{std::move(name), h}; + return $new(std::move(name), h); } /** - * \brief Closes an open object handle. - * \see https://docs.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-closehandle + * \brief Closes an open event object handle. */ result evt_close(evt_t evt) noexcept { LIBIMP_LOG_(); - LIBIMP_UNUSED auto guard = std::unique_ptr(evt); + LIBIMP_UNUSED auto guard = std::unique_ptr(evt); if (!is_valid(evt)) { log.error("handle is null."); return {}; } - if (!::CloseHandle(evt->h_event)) { - auto err = sys::error(); - log.error("failed: CloseHandle(", evt->h_event, "). error = ", err); - return err; - } - return std::error_code{}; + return winapi::close_handle(evt->h_event); } /** diff --git a/src/libipc/platform/win/mmap_impl.h b/src/libipc/platform/win/mmap_impl.h index e911a98..de73f14 100644 --- a/src/libipc/platform/win/mmap_impl.h +++ b/src/libipc/platform/win/mmap_impl.h @@ -16,8 +16,10 @@ #include "get_sa.h" #include "to_tchar.h" +#include "close_handle.h" LIBIPC_NAMESPACE_BEG_ + using namespace ::LIBIMP; struct shm_handle { @@ -29,24 +31,6 @@ struct shm_handle { namespace { -/** - * \brief Closes an open object handle. - * \see https://docs.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-closehandle - */ -result mmap_close(HANDLE h) { - LIBIMP_LOG_(); - if (h == NULL) { - log.error("handle is null."); - return std::make_error_code(std::errc::invalid_argument); - } - if (!::CloseHandle(h)) { - auto err = sys::error(); - log.error("failed: CloseHandle(", h, "). error = ", err); - return err; - } - return std::error_code{}; -} - /** * \brief Creates or opens a file mapping object for a specified file. * @@ -113,7 +97,7 @@ result mmap_open(std::string const &file, std::size_t size, mode::type t /// (with its current size, not the specified size), and GetLastError returns ERROR_ALREADY_EXISTS. if ((type == mode::create) && (::GetLastError() == ERROR_ALREADY_EXISTS)) { log.info("the file being created already exists. file = ", file, ", type = ", type); - mmap_close(*h); + winapi::close_handle(*h); return sys::error(); } return h; @@ -174,7 +158,7 @@ result mmap_release(HANDLE h, LPCVOID mem) { if (!::UnmapViewOfFile(mem)) { log.warning("failed: UnmapViewOfFile. error = ", sys::error()); } - return mmap_close(h); + return winapi::close_handle(h); } } // namespace diff --git a/src/libipc/platform/win/shm_impl.h b/src/libipc/platform/win/shm_impl.h index 243df96..e6b0296 100644 --- a/src/libipc/platform/win/shm_impl.h +++ b/src/libipc/platform/win/shm_impl.h @@ -9,13 +9,16 @@ #include "libimp/log.h" #include "libimp/system.h" - +#include "libpmr/new.h" #include "libipc/shm.h" #include "mmap_impl.h" +#include "close_handle.h" LIBIPC_NAMESPACE_BEG_ + using namespace ::LIBIMP; +using namespace ::LIBPMR; result shm_open(std::string name, std::size_t size, mode::type type) noexcept { LIBIMP_LOG_(); @@ -27,16 +30,16 @@ result shm_open(std::string name, std::size_t size, mode::type type) noex auto mem = mmap_memof(*h); if (*mem == NULL) { log.error("failed: mmap_memof(", *h, ")."); - mmap_close(*h); + winapi::close_handle(*h); return mem.error(); } auto sz = mmap_sizeof(*mem); if (!sz) { log.error("failed: mmap_sizeof(", *mem, ")."); - mmap_close(*h); + winapi::close_handle(*h); return sz.error(); } - return new shm_handle{std::move(name), *sz, *mem, *h}; + return $new(std::move(name), *sz, *mem, *h); } result shm_close(shm_t h) noexcept { @@ -47,7 +50,7 @@ result shm_close(shm_t h) noexcept { } auto shm = static_cast(h); auto ret = mmap_release(shm->h_fmap, shm->memp); - delete shm; + $delete(shm); return ret; }