|
| 1 | +#pragma once |
| 2 | + |
| 3 | +// Cross-platform RAII wrapper for large temporary buffers. |
| 4 | +// |
| 5 | +// Uses mmap/VirtualAlloc for large allocations with two key features: |
| 6 | +// 1. Page-aligned allocation that keeps a stable virtual address range for |
| 7 | +// reuse across calls (no munmap/VirtualFree between uses). |
| 8 | +// 2. mark_reclaimable() issues MADV_FREE (Linux/macOS) or MEM_RESET (Windows) |
| 9 | +// to tell the OS the physical pages may be reclaimed under memory pressure, |
| 10 | +// without releasing the virtual address range. If pages are not reclaimed, |
| 11 | +// the next use is essentially free; if they are, the OS transparently |
| 12 | +// zero-fills them on the next fault — no user-visible error. |
| 13 | +// |
| 14 | +// Platform support: |
| 15 | +// Linux / macOS — mmap + MADV_FREE |
| 16 | +// Windows — VirtualAlloc + MEM_RESET |
| 17 | +// Other — std::aligned_alloc fallback (no reclaimable support) |
| 18 | +// |
| 19 | +// This relies only on POSIX mmap (available since 4.4BSD / POSIX.1-2001) and |
| 20 | +// Win32 VirtualAlloc — both stable OS-level APIs with decades of support. |
| 21 | + |
| 22 | +#include <cstddef> |
| 23 | +#include <cstdio> |
| 24 | +#include <cstdlib> |
| 25 | + |
| 26 | +#if defined(_WIN32) |
| 27 | +#ifndef WIN32_LEAN_AND_MEAN |
| 28 | +#define WIN32_LEAN_AND_MEAN // Exclude rarely-used Windows headers to speed compilation |
| 29 | +#endif |
| 30 | +#ifndef NOMINMAX |
| 31 | +#define NOMINMAX // Prevent Windows <windows.h> from defining min/max macros |
| 32 | +#endif |
| 33 | +#include <windows.h> |
| 34 | +#elif defined(__unix__) || defined(__APPLE__) |
| 35 | +#include <sys/mman.h> |
| 36 | +#include <unistd.h> |
| 37 | +#else |
| 38 | +#include <cstring> // memset fallback |
| 39 | +#endif |
| 40 | + |
| 41 | +namespace finufft { |
| 42 | + |
| 43 | +// Large-buffer allocator that returns page-aligned memory and supports |
| 44 | +// marking pages as reclaimable between uses. |
| 45 | +class ReclaimableMemory { |
| 46 | +public: |
| 47 | + ReclaimableMemory() = default; |
| 48 | + |
| 49 | + // Non-copyable, movable |
| 50 | + ReclaimableMemory(const ReclaimableMemory &) = delete; |
| 51 | + ReclaimableMemory &operator=(const ReclaimableMemory &) = delete; |
| 52 | + ReclaimableMemory(ReclaimableMemory &&o) noexcept : ptr_(o.ptr_), nbytes_(o.nbytes_) { |
| 53 | + o.ptr_ = nullptr; |
| 54 | + o.nbytes_ = 0; |
| 55 | + } |
| 56 | + ReclaimableMemory &operator=(ReclaimableMemory &&o) noexcept { |
| 57 | + if (this != &o) { |
| 58 | + deallocate(); |
| 59 | + ptr_ = o.ptr_; |
| 60 | + nbytes_ = o.nbytes_; |
| 61 | + o.ptr_ = nullptr; |
| 62 | + o.nbytes_ = 0; |
| 63 | + } |
| 64 | + return *this; |
| 65 | + } |
| 66 | + |
| 67 | + ~ReclaimableMemory() { deallocate(); } |
| 68 | + |
| 69 | + // Allocate nbytes of memory while leaving physical pages to be faulted in on |
| 70 | + // first use. Returns true on success. |
| 71 | + bool allocate(size_t nbytes) { |
| 72 | + if (nbytes == 0) return true; |
| 73 | + if (ptr_ && nbytes_ == nbytes) return true; // already the right size |
| 74 | + deallocate(); |
| 75 | + nbytes_ = nbytes; |
| 76 | +#if defined(_WIN32) |
| 77 | + ptr_ = VirtualAlloc(nullptr, nbytes, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); |
| 78 | + if (!ptr_) { |
| 79 | + nbytes_ = 0; |
| 80 | + return false; |
| 81 | + } |
| 82 | +#elif defined(__linux__) |
| 83 | + ptr_ = |
| 84 | + mmap(nullptr, nbytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 85 | + if (ptr_ == MAP_FAILED) { |
| 86 | + ptr_ = nullptr; |
| 87 | + nbytes_ = 0; |
| 88 | + return false; |
| 89 | + } |
| 90 | +#elif defined(__APPLE__) || defined(__unix__) |
| 91 | + // macOS and other Unix: no MAP_POPULATE, use plain mmap |
| 92 | + ptr_ = |
| 93 | + mmap(nullptr, nbytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 94 | + if (ptr_ == MAP_FAILED) { |
| 95 | + ptr_ = nullptr; |
| 96 | + nbytes_ = 0; |
| 97 | + return false; |
| 98 | + } |
| 99 | +#else |
| 100 | + // Fallback: aligned allocation |
| 101 | + ptr_ = std::aligned_alloc(4096, ((nbytes + 4095) / 4096) * 4096); |
| 102 | + if (!ptr_) { |
| 103 | + nbytes_ = 0; |
| 104 | + return false; |
| 105 | + } |
| 106 | + std::memset(ptr_, 0, nbytes); |
| 107 | +#endif |
| 108 | + return true; |
| 109 | + } |
| 110 | + |
| 111 | + // Mark pages as reclaimable by the OS. The virtual address range is kept, |
| 112 | + // and pages may remain resident if there is no memory pressure. |
| 113 | + // After this call, the contents are undefined until the next write. |
| 114 | + void mark_reclaimable() { |
| 115 | + if (!ptr_ || !nbytes_) return; |
| 116 | +#if defined(_WIN32) |
| 117 | + // MEM_RESET tells Windows the pages are no longer needed. |
| 118 | + // Pages remain committed but can be discarded under pressure. |
| 119 | + VirtualAlloc(ptr_, nbytes_, MEM_RESET, PAGE_READWRITE); |
| 120 | +#elif defined(__linux__) || defined(__APPLE__) |
| 121 | + madvise(ptr_, nbytes_, MADV_FREE); |
| 122 | +#endif |
| 123 | + // Other platforms: no-op, pages stay resident |
| 124 | + } |
| 125 | + |
| 126 | + void *data() const { return ptr_; } |
| 127 | + size_t size() const { return nbytes_; } |
| 128 | + |
| 129 | +private: |
| 130 | + void deallocate() { |
| 131 | + if (!ptr_) return; |
| 132 | +#if defined(_WIN32) |
| 133 | + VirtualFree(ptr_, 0, MEM_RELEASE); |
| 134 | +#elif defined(__unix__) || defined(__APPLE__) |
| 135 | + munmap(ptr_, nbytes_); |
| 136 | +#else |
| 137 | + std::free(ptr_); |
| 138 | +#endif |
| 139 | + ptr_ = nullptr; |
| 140 | + nbytes_ = 0; |
| 141 | + } |
| 142 | + |
| 143 | + void *ptr_ = nullptr; |
| 144 | + size_t nbytes_ = 0; |
| 145 | +}; |
| 146 | + |
| 147 | +} // namespace finufft |
0 commit comments