@@ -33,18 +33,16 @@ struct ibv_mr {
3333#include < mutex>
3434#include < thread>
3535
36- #if defined(_linux_)
37- #include < sys/mman.h>
38- #include < unistd.h>
39- #include < sys/syscall.h>
40- #endif
41-
4236#include < cstdlib>
4337#include < cstring>
4438#include < cerrno>
4539
46- #if defined(_WIN32 )
40+ #if defined(_win_ )
4741#include < malloc.h> // _aligned_malloc, _aligned_free
42+ #else
43+ #include < sys/mman.h> // madvise
44+ #include < unistd.h>
45+ #include < sys/syscall.h>
4846#endif
4947
5048static constexpr size_t HPageSz = (1 << 21 );
@@ -223,46 +221,46 @@ namespace NInterconnect::NRdma {
223221
224222 void * buf = nullptr ;
225223
226- #if defined(_WIN32 )
224+ #if defined(_win_ )
227225 // Windows: use _aligned_malloc
228226 buf = _aligned_malloc (size, alignment);
229227 if (!buf) {
230228 fprintf (stderr, " Failed to allocate aligned memory on Windows\n " );
231229 return nullptr ;
232230 }
233- #else
231+ #else
234232 // POSIX/C++: std::aligned_alloc (C++17)
235233 buf = std::aligned_alloc (alignment, size);
236234 if (!buf) {
237235 fprintf (stderr, " Failed to allocate aligned memory on Unix\n " );
238236 return nullptr ;
239237 }
240- #endif
238+ #endif
241239
242240 if (hp) {
243- #if defined(_linux_)
241+ #if defined(_linux_)
244242 if (madvise (buf, size, MADV_HUGEPAGE) < 0 ) {
245- fprintf (stderr, " Unable to madvise to use THP: %s (%d)\n " , strerror (errno), errno);
243+ fprintf (stderr, " Unable to madvice to use THP, %d (%d)" ,
244+ strerror (errno), errno);
246245 }
247- #endif
246+ #endif
248247 for (size_t i = 0 ; i < size; i += HPageSz) {
249- // touch pages to promote to huge pages
248+ // We use THP right now. We need to touch each page to promote it to HUGE.
250249 static_cast <char *>(buf)[i] = 0 ;
251250 }
252251 }
253-
254252 return buf;
255253 }
256254
257255 static void freeMemory (void * ptr) noexcept {
258256 if (!ptr) {
259257 return ;
260258 }
261- #if defined(_WIN32 )
259+ #if defined(_win_ )
262260 _aligned_free (ptr);
263- #else
261+ #else
264262 std::free (ptr);
265- #endif
263+ #endif
266264 }
267265
268266 std::vector<ibv_mr*> registerMemory (void * addr, size_t size, const NInterconnect::NRdma::NLinkMgr::TCtxsMap& ctxs) {
0 commit comments