Skip to content

Commit 67306b1

Browse files
committed
Fix windows build
1 parent 0562407 commit 67306b1

File tree

1 file changed

+53
-12
lines changed

1 file changed

+53
-12
lines changed

ydb/library/actors/interconnect/rdma/mem_pool.cpp

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,21 @@ struct ibv_mr {
3030
#include <vector>
3131
#include <list>
3232

33-
#include <unistd.h>
34-
#include <sys/syscall.h>
3533
#include <mutex>
3634
#include <thread>
3735

3836
#if defined(_linux_)
3937
#include <sys/mman.h>
38+
#include <unistd.h>
39+
#include <sys/syscall.h>
40+
#endif
41+
42+
#include <cstdlib>
43+
#include <cstring>
44+
#include <cerrno>
45+
46+
#if defined(_WIN32)
47+
#include <malloc.h> // _aligned_malloc, _aligned_free
4048
#endif
4149

4250
static constexpr size_t HPageSz = (1 << 21);
@@ -45,6 +53,10 @@ using ::NMonitoring::TDynamicCounters;
4553

4654
namespace NInterconnect::NRdma {
4755

56+
// Cross-platform memory management
57+
static void* allocateMemory(size_t size, size_t alignment, bool hp);
58+
static void freeMemory(void* ptr) noexcept;
59+
4860
class TChunk: public NNonCopyable::TMoveOnly, public TAtomicRefCount<TChunk> {
4961
public:
5062

@@ -67,7 +79,7 @@ namespace NInterconnect::NRdma {
6779
#else
6880
free(MRs.front());
6981
#endif
70-
std::free(addr);
82+
freeMemory(addr);
7183
MRs.clear();
7284
}
7385

@@ -204,26 +216,55 @@ namespace NInterconnect::NRdma {
204216
);
205217
}
206218

207-
void* allocateMemory(size_t size, size_t alignment, bool hp) {
219+
static void* allocateMemory(size_t size, size_t alignment, bool hp) {
208220
if (size % alignment != 0) {
209221
return nullptr;
210222
}
211-
void* buf = std::aligned_alloc(alignment, size);
223+
224+
void* buf = nullptr;
225+
226+
#if defined(_WIN32)
227+
// Windows: use _aligned_malloc
228+
buf = _aligned_malloc(size, alignment);
229+
if (!buf) {
230+
fprintf(stderr, "Failed to allocate aligned memory on Windows\n");
231+
return nullptr;
232+
}
233+
#else
234+
// POSIX/C++: std::aligned_alloc (C++17)
235+
buf = std::aligned_alloc(alignment, size);
236+
if (!buf) {
237+
fprintf(stderr, "Failed to allocate aligned memory on Unix\n");
238+
return nullptr;
239+
}
240+
#endif
241+
212242
if (hp) {
213-
#if defined(_linux_)
243+
#if defined(_linux_)
214244
if (madvise(buf, size, MADV_HUGEPAGE) < 0) {
215-
fprintf(stderr, "Unable to madvice to use THP, %d (%d)",
216-
strerror(errno), errno);
245+
fprintf(stderr, "Unable to madvise to use THP: %s (%d)\n", strerror(errno), errno);
217246
}
218-
#endif
247+
#endif
219248
for (size_t i = 0; i < size; i += HPageSz) {
220-
// We use THP right now. We need to touch each page to promote it to HUGE.
221-
((char*)buf)[i] = 0;
249+
// touch pages to promote to huge pages
250+
static_cast<char*>(buf)[i] = 0;
222251
}
223252
}
253+
224254
return buf;
225255
}
226256

257+
static void freeMemory(void* ptr) noexcept {
258+
if (!ptr) {
259+
return;
260+
}
261+
#if defined(_WIN32)
262+
_aligned_free(ptr);
263+
#else
264+
std::free(ptr);
265+
#endif
266+
}
267+
227268
std::vector<ibv_mr*> registerMemory(void* addr, size_t size, const NInterconnect::NRdma::NLinkMgr::TCtxsMap& ctxs) {
228269
std::vector<ibv_mr*> res;
229270
#ifndef MEM_POOL_DISABLE_RDMA_SUPPORT
@@ -307,7 +348,7 @@ namespace NInterconnect::NRdma {
307348

308349
auto mrs = registerMemory(ptr, size, Ctxs);
309350
if (mrs.empty()) {
310-
std::free(ptr);
351+
freeMemory(ptr);
311352
return nullptr;
312353
}
313354

0 commit comments

Comments
 (0)