Skip to content

Commit bda4251

Browse files
committed
rx/Mappable: support 0 alignment, workaround ERROR_NO_SYSTEM_RESOURCES windows moments
1 parent 5e901ea commit bda4251

File tree

1 file changed

+38
-5
lines changed

1 file changed

+38
-5
lines changed

rx/src/Mappable.cpp

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,32 @@ rx::Mappable::CreateMemory(std::size_t size) {
2828
rx::Mappable result;
2929

3030
#ifdef _WIN32
31-
auto handle = CreateFileMapping2(INVALID_HANDLE_VALUE, nullptr,
32-
FILE_MAP_ALL_ACCESS, PAGE_EXECUTE_READWRITE,
33-
SEC_COMMIT, size, nullptr, nullptr, 0);
31+
HANDLE handle = nullptr;
3432

35-
if (!handle) {
36-
return {rx::Mappable{}, std::errc::invalid_argument};
33+
for (std::size_t i = 0; i < 100; ++i) {
34+
handle = CreateFileMapping2(INVALID_HANDLE_VALUE, nullptr,
35+
FILE_MAP_ALL_ACCESS, PAGE_EXECUTE_READWRITE,
36+
SEC_COMMIT, size, nullptr, nullptr, 0);
37+
38+
if (!handle) {
39+
int error = ::GetLastError();
40+
41+
if (error == ERROR_NO_SYSTEM_RESOURCES) {
42+
continue;
43+
}
44+
45+
if (error == ERROR_COMMITMENT_LIMIT) {
46+
return {rx::Mappable{}, std::errc::not_enough_memory};
47+
}
48+
49+
return {rx::Mappable{}, std::errc::invalid_argument};
50+
}
51+
52+
break;
53+
}
54+
55+
if (handle == nullptr) {
56+
return {rx::Mappable{}, std::errc::resource_unavailable_try_again};
3757
}
3858

3959
result.m_handle = handle;
@@ -97,6 +117,19 @@ std::errc rx::Mappable::map(rx::AddressRange virtualRange, std::size_t offset,
97117

98118
mem::release(virtualRange, alignment);
99119

120+
if (alignment == 0) {
121+
auto pointer = std::bit_cast<void *>(virtualRange.beginAddress());
122+
123+
auto result = MapViewOfFile3((HANDLE)m_handle, nullptr, pointer, offset,
124+
virtualRange.size(), MEM_REPLACE_PLACEHOLDER,
125+
prot, nullptr, 0);
126+
if (!result) {
127+
return std::errc::invalid_argument;
128+
}
129+
130+
return {};
131+
}
132+
100133
for (std::uintptr_t address = virtualRange.beginAddress();
101134
address < virtualRange.endAddress();
102135
address += alignment, offset += alignment) {

0 commit comments

Comments
 (0)