We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f014495 commit dd5850dCopy full SHA for dd5850d
cp-algo/util/big_alloc.hpp
@@ -26,18 +26,28 @@ namespace cp_algo {
26
27
#if CP_ALGO_USE_MMAP
28
[[nodiscard]] T* allocate(std::size_t n) {
29
- auto *raw = base::allocate(n);
30
- if(n * sizeof(T) >= (1 << 20)) {
31
- madvise(raw, n, MADV_HUGEPAGE);
32
- madvise(raw, n, MADV_POPULATE_WRITE);
+ if(n * sizeof(T) < 1024 * 1024) {
+ return base::allocate(n);
33
}
34
- return raw;
+ n *= sizeof(T);
+ void* raw = mmap(nullptr, n,
+ PROT_READ | PROT_WRITE,
35
+ MAP_PRIVATE | MAP_ANONYMOUS,
36
+ -1, 0);
37
+ madvise(raw, n, MADV_HUGEPAGE);
38
+ madvise(raw, n, MADV_POPULATE_WRITE);
39
+ return static_cast<T*>(raw);
40
41
#endif
42
43
44
void deallocate(T* p, std::size_t n) noexcept {
- return base::deallocate(p, n);
45
46
+ return base::deallocate(p, n);
47
+ }
48
+ if(p) {
49
+ munmap(p, n * sizeof(T));
50
51
52
53
};
0 commit comments