Skip to content

Commit 7421b95

Browse files
committed
Refactor handle closing in Windows platform code
1 parent 1fe8d93 commit 7421b95

File tree

4 files changed

+58
-35
lines changed

4 files changed

+58
-35
lines changed
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* \file libipc/platform/win/close_handle.h
3+
* \author mutouyun ([email protected])
4+
*/
5+
#pragma once
6+
7+
#include <Windows.h>
8+
9+
#include "libimp/log.h"
10+
#include "libimp/system.h"
11+
12+
#include "libipc/def.h"
13+
14+
LIBIPC_NAMESPACE_BEG_
15+
using namespace ::LIBIMP;
16+
17+
namespace winapi {
18+
19+
/**
20+
* \brief Closes an open object handle.
21+
* \see https://docs.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-closehandle
22+
*/
23+
inline result<void> close_handle(HANDLE h) noexcept {
24+
LIBIMP_LOG_();
25+
if (h == NULL) {
26+
log.error("handle is null.");
27+
return std::make_error_code(std::errc::invalid_argument);
28+
}
29+
if (!::CloseHandle(h)) {
30+
auto err = sys::error();
31+
log.error("failed: CloseHandle(", h, "). error = ", err);
32+
return err;
33+
}
34+
return std::error_code{};
35+
}
36+
37+
} // namespace winapi
38+
LIBIPC_NAMESPACE_END_

src/libipc/platform/win/event_impl.h

+8-10
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@
1212
#include <Windows.h>
1313

1414
#include "libimp/log.h"
15+
#include "libpmr/new.h"
1516
#include "libipc/event.h"
1617

1718
#include "get_sa.h"
1819
#include "to_tchar.h"
20+
#include "close_handle.h"
1921

2022
LIBIPC_NAMESPACE_BEG_
23+
2124
using namespace ::LIBIMP;
25+
using namespace ::LIBPMR;
2226

2327
struct evt_handle {
2428
std::string name;
@@ -51,26 +55,20 @@ result<evt_t> evt_open(std::string name) noexcept {
5155
log.error("failed: CreateEvent(FALSE, FALSE, ", name, "). error = ", err);
5256
return err;
5357
}
54-
return new evt_handle{std::move(name), h};
58+
return $new<evt_handle>(std::move(name), h);
5559
}
5660

5761
/**
58-
* \brief Closes an open object handle.
59-
* \see https://docs.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-closehandle
62+
* \brief Closes an open event object handle.
6063
*/
6164
result<void> evt_close(evt_t evt) noexcept {
6265
LIBIMP_LOG_();
63-
LIBIMP_UNUSED auto guard = std::unique_ptr<evt_handle>(evt);
66+
LIBIMP_UNUSED auto guard = std::unique_ptr<evt_handle, deleter>(evt);
6467
if (!is_valid(evt)) {
6568
log.error("handle is null.");
6669
return {};
6770
}
68-
if (!::CloseHandle(evt->h_event)) {
69-
auto err = sys::error();
70-
log.error("failed: CloseHandle(", evt->h_event, "). error = ", err);
71-
return err;
72-
}
73-
return std::error_code{};
71+
return winapi::close_handle(evt->h_event);
7472
}
7573

7674
/**

src/libipc/platform/win/mmap_impl.h

+4-20
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616

1717
#include "get_sa.h"
1818
#include "to_tchar.h"
19+
#include "close_handle.h"
1920

2021
LIBIPC_NAMESPACE_BEG_
22+
2123
using namespace ::LIBIMP;
2224

2325
struct shm_handle {
@@ -29,24 +31,6 @@ struct shm_handle {
2931

3032
namespace {
3133

32-
/**
33-
* \brief Closes an open object handle.
34-
* \see https://docs.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-closehandle
35-
*/
36-
result<void> mmap_close(HANDLE h) {
37-
LIBIMP_LOG_();
38-
if (h == NULL) {
39-
log.error("handle is null.");
40-
return std::make_error_code(std::errc::invalid_argument);
41-
}
42-
if (!::CloseHandle(h)) {
43-
auto err = sys::error();
44-
log.error("failed: CloseHandle(", h, "). error = ", err);
45-
return err;
46-
}
47-
return std::error_code{};
48-
}
49-
5034
/**
5135
* \brief Creates or opens a file mapping object for a specified file.
5236
*
@@ -113,7 +97,7 @@ result<HANDLE> mmap_open(std::string const &file, std::size_t size, mode::type t
11397
/// (with its current size, not the specified size), and GetLastError returns ERROR_ALREADY_EXISTS.
11498
if ((type == mode::create) && (::GetLastError() == ERROR_ALREADY_EXISTS)) {
11599
log.info("the file being created already exists. file = ", file, ", type = ", type);
116-
mmap_close(*h);
100+
winapi::close_handle(*h);
117101
return sys::error();
118102
}
119103
return h;
@@ -174,7 +158,7 @@ result<void> mmap_release(HANDLE h, LPCVOID mem) {
174158
if (!::UnmapViewOfFile(mem)) {
175159
log.warning("failed: UnmapViewOfFile. error = ", sys::error());
176160
}
177-
return mmap_close(h);
161+
return winapi::close_handle(h);
178162
}
179163

180164
} // namespace

src/libipc/platform/win/shm_impl.h

+8-5
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@
99

1010
#include "libimp/log.h"
1111
#include "libimp/system.h"
12-
12+
#include "libpmr/new.h"
1313
#include "libipc/shm.h"
1414

1515
#include "mmap_impl.h"
16+
#include "close_handle.h"
1617

1718
LIBIPC_NAMESPACE_BEG_
19+
1820
using namespace ::LIBIMP;
21+
using namespace ::LIBPMR;
1922

2023
result<shm_t> shm_open(std::string name, std::size_t size, mode::type type) noexcept {
2124
LIBIMP_LOG_();
@@ -27,16 +30,16 @@ result<shm_t> shm_open(std::string name, std::size_t size, mode::type type) noex
2730
auto mem = mmap_memof(*h);
2831
if (*mem == NULL) {
2932
log.error("failed: mmap_memof(", *h, ").");
30-
mmap_close(*h);
33+
winapi::close_handle(*h);
3134
return mem.error();
3235
}
3336
auto sz = mmap_sizeof(*mem);
3437
if (!sz) {
3538
log.error("failed: mmap_sizeof(", *mem, ").");
36-
mmap_close(*h);
39+
winapi::close_handle(*h);
3740
return sz.error();
3841
}
39-
return new shm_handle{std::move(name), *sz, *mem, *h};
42+
return $new<shm_handle>(std::move(name), *sz, *mem, *h);
4043
}
4144

4245
result<void> shm_close(shm_t h) noexcept {
@@ -47,7 +50,7 @@ result<void> shm_close(shm_t h) noexcept {
4750
}
4851
auto shm = static_cast<shm_handle *>(h);
4952
auto ret = mmap_release(shm->h_fmap, shm->memp);
50-
delete shm;
53+
$delete(shm);
5154
return ret;
5255
}
5356

0 commit comments

Comments
 (0)