From fb29a5e08576bdf12ea18e83ff45b177441cf139 Mon Sep 17 00:00:00 2001 From: David CARLIER Date: Thu, 2 Jan 2025 14:11:58 +0000 Subject: [PATCH] PALSolaris AlignedAllocation feature implementation attempt. (#723) --- src/snmalloc/pal/pal_solaris.h | 38 +++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/src/snmalloc/pal/pal_solaris.h b/src/snmalloc/pal/pal_solaris.h index 5965760..a8d0739 100644 --- a/src/snmalloc/pal/pal_solaris.h +++ b/src/snmalloc/pal/pal_solaris.h @@ -17,13 +17,49 @@ namespace snmalloc * PAL supports. * */ - static constexpr uint64_t pal_features = PALPOSIX::pal_features; + static constexpr uint64_t pal_features = + AlignedAllocation | PALPOSIX::pal_features; + + static constexpr size_t page_size = + Aal::aal_name == Sparc ? 0x2000 : 0x1000; + static constexpr size_t minimum_alloc_size = page_size; /** * Solaris requires an explicit no-reserve flag in `mmap` to guarantee lazy * commit. */ static constexpr int default_mmap_flags = MAP_NORESERVE; + + /** + * Reserve memory at a specific alignment. + */ + template + static void* reserve_aligned(size_t size) noexcept + { + // Alignment must be a power of 2. + SNMALLOC_ASSERT(bits::is_pow2(size)); + SNMALLOC_ASSERT(size >= minimum_alloc_size); + UNUSED(state_using); + + uintptr_t alignment = + static_cast(bits::align_up(size, page_size)); + + auto prot = + !mitigations(pal_enforce_access) ? PROT_READ | PROT_WRITE : PROT_NONE; + + void* p = mmap( + (void*)alignment, + size, + prot, + MAP_PRIVATE | MAP_ANONYMOUS | MAP_ALIGN | default_mmap_flags, + -1, + 0); + + if (p == MAP_FAILED) + return nullptr; + + return p; + } }; } // namespace snmalloc #endif