Skip to content

Commit adb8d9f

Browse files
DinoVmeta-codesync[bot]
authored andcommitted
Fix slab allocator for Windows
Summary: Use `_aligned_malloc` on Windows and don't support locking. Reviewed By: mpage Differential Revision: D91526693 fbshipit-source-id: 375631fbfc4d9387ae6dbc7dd330e2e11ebfdcb1
1 parent a6fc304 commit adb8d9f

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

cinderx/Common/slab.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
#include "cinderx/Common/log.h"
66
#include "cinderx/Common/util.h"
7+
#ifndef WIN32
78
#include "sys/mman.h"
9+
#endif
810

911
#include <cstddef>
1012
#include <cstdlib>
@@ -64,8 +66,13 @@ class Slab {
6466
increment >= sizeof(T),
6567
"Trying to fit a slab object into too little memory");
6668
void* ptr;
69+
#ifndef WIN32
6770
int result = posix_memalign(&ptr, kPageSize, kSlabSize);
6871
JIT_CHECK(result == 0, "Failed to allocate {} bytes", kSlabSize);
72+
#else
73+
ptr = _aligned_malloc(kSlabSize, kPageSize);
74+
JIT_CHECK(ptr != nullptr, "Failed to allocate {} bytes", kSlabSize);
75+
#endif
6976
base_.reset(static_cast<char*>(ptr));
7077
fill_ = base_.get();
7178
}
@@ -96,6 +103,7 @@ class Slab {
96103
return ptr;
97104
}
98105

106+
#ifndef WIN32
99107
void mlock() {
100108
if (::mlock(base_.get(), kSlabSize) < 0) {
101109
JIT_LOG("Failed to mlock slab at {}", base_.get());
@@ -117,6 +125,7 @@ class Slab {
117125
}
118126
mlocks_--;
119127
}
128+
#endif
120129

121130
iterator begin() const {
122131
return iterator{base_.get(), increment_};

cinderx/Common/slab_arena.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,23 +123,28 @@ class SlabArena {
123123
T* allocate(Args&&... args) {
124124
std::lock_guard<std::mutex> guard{mutex_};
125125

126+
#ifndef WIN32
126127
if (mlocked_) {
127128
// It's not necessarily an error to allocate after locking but it's
128129
// probably not what we expect to happen in the common forking case.
129130
JIT_DLOG("Allocating while locked");
130131
}
132+
#endif
131133

132134
void* mem = slabs_.back().allocate();
133135
if (mem == nullptr) {
134136
mem = slabs_.emplace_back(SizeTrait::size()).allocate();
135137
JIT_CHECK(mem != nullptr, "Empty slab failed to allocate");
138+
#ifndef WIN32
136139
if (mlocked_) {
137140
slabs_.back().mlock();
138141
}
142+
#endif
139143
}
140144
return new (mem) T(std::forward<Args>(args)...);
141145
}
142146

147+
#ifndef WIN32
143148
// Pin the contents to physical memory.
144149
void mlock() {
145150
std::lock_guard<std::mutex> guard{mutex_};
@@ -155,6 +160,7 @@ class SlabArena {
155160
slab.munlock();
156161
}
157162
}
163+
#endif
158164

159165
iterator begin() {
160166
return iterator{&slabs_};
@@ -167,7 +173,9 @@ class SlabArena {
167173
private:
168174
std::vector<Slab<T, kSlabSize>> slabs_;
169175
std::mutex mutex_;
176+
#ifndef WIN32
170177
bool mlocked_{false};
178+
#endif
171179
};
172180

173181
} // namespace jit

0 commit comments

Comments
 (0)