[NFC] Separate out support for aligned allocation from the FreeBSD PAL.
NetBSD also supports `MAP_ALIGNED()` in `mmap` (according to the man page, at least).
This commit is contained in:
55
src/pal/pal_bsd_aligned.h
Normal file
55
src/pal/pal_bsd_aligned.h
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if defined(__FreeBSD__) && !defined(_KERNEL)
|
||||||
|
# include "pal_bsd.h"
|
||||||
|
|
||||||
|
namespace snmalloc
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* FreeBSD-specific platform abstraction layer.
|
||||||
|
*
|
||||||
|
* This adds aligned allocation using `MAP_ALIGNED` to the generic BSD
|
||||||
|
* implementation. This flag is supported by NetBSD and FreeBSD.
|
||||||
|
*/
|
||||||
|
template<class OS>
|
||||||
|
class PALBSD_Aligned : public PALBSD<OS>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* Bitmap of PalFeatures flags indicating the optional features that this
|
||||||
|
* PAL supports.
|
||||||
|
*
|
||||||
|
* This class adds support for aligned allocation.
|
||||||
|
*/
|
||||||
|
static constexpr uint64_t pal_features =
|
||||||
|
AlignedAllocation | PALBSD<OS>::pal_features;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reserve memory at a specific alignment.
|
||||||
|
*/
|
||||||
|
template<bool committed>
|
||||||
|
void* reserve(const size_t* size, size_t align) noexcept
|
||||||
|
{
|
||||||
|
// Alignment must be a power of 2.
|
||||||
|
assert(align == bits::next_pow2(align));
|
||||||
|
|
||||||
|
align = bits::max<size_t>(4096, align);
|
||||||
|
|
||||||
|
size_t log2align = bits::next_pow2_bits(align);
|
||||||
|
|
||||||
|
void* p = mmap(
|
||||||
|
nullptr,
|
||||||
|
*size,
|
||||||
|
PROT_READ | PROT_WRITE,
|
||||||
|
MAP_PRIVATE | MAP_ANONYMOUS | MAP_ALIGNED(log2align),
|
||||||
|
-1,
|
||||||
|
0);
|
||||||
|
|
||||||
|
if (p == MAP_FAILED)
|
||||||
|
error("Out of memory");
|
||||||
|
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} // namespace snmalloc
|
||||||
|
#endif
|
||||||
@@ -1,13 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#if defined(__FreeBSD__) && !defined(_KERNEL)
|
#if defined(__FreeBSD__) && !defined(_KERNEL)
|
||||||
# include "../ds/bits.h"
|
# include "pal_bsd_aligned.h"
|
||||||
# include "../mem/allocconfig.h"
|
|
||||||
# include "pal_bsd.h"
|
|
||||||
|
|
||||||
# include <stdio.h>
|
|
||||||
# include <strings.h>
|
|
||||||
# include <sys/mman.h>
|
|
||||||
|
|
||||||
namespace snmalloc
|
namespace snmalloc
|
||||||
{
|
{
|
||||||
@@ -17,45 +11,19 @@ namespace snmalloc
|
|||||||
* This adds FreeBSD-specific aligned allocation to the generic BSD
|
* This adds FreeBSD-specific aligned allocation to the generic BSD
|
||||||
* implementation.
|
* implementation.
|
||||||
*/
|
*/
|
||||||
class PALFBSD : public PALBSD<PALFBSD>
|
class PALFBSD : public PALBSD_Aligned<PALFBSD>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* Bitmap of PalFeatures flags indicating the optional features that this
|
* Bitmap of PalFeatures flags indicating the optional features that this
|
||||||
* PAL supports.
|
* PAL supports.
|
||||||
*/
|
|
||||||
static constexpr uint64_t pal_features =
|
|
||||||
AlignedAllocation | PALBSD::pal_features;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reserve memory.
|
|
||||||
*
|
*
|
||||||
* FreeBSD supports requesting pages at an arbitrary alignment, which
|
* The FreeBSD PAL does not currently add any features beyond those of a
|
||||||
* improves efficiency of snmalloc.
|
* generic BSD with support for arbitrary alignment from `mmap`. This
|
||||||
|
* field is declared explicitly to remind anyone modifying this class to
|
||||||
|
* add new features that they should add any required feature flags.
|
||||||
*/
|
*/
|
||||||
template<bool committed>
|
static constexpr uint64_t pal_features = PALBSD_Aligned::pal_features;
|
||||||
void* reserve(const size_t* size, size_t align) noexcept
|
|
||||||
{
|
|
||||||
// Alignment must be a power of 2.
|
|
||||||
assert(align == bits::next_pow2(align));
|
|
||||||
|
|
||||||
align = bits::max<size_t>(4096, align);
|
|
||||||
|
|
||||||
size_t log2align = bits::next_pow2_bits(align);
|
|
||||||
|
|
||||||
void* p = mmap(
|
|
||||||
nullptr,
|
|
||||||
*size,
|
|
||||||
PROT_READ | PROT_WRITE,
|
|
||||||
MAP_PRIVATE | MAP_ANONYMOUS | MAP_ALIGNED(log2align),
|
|
||||||
-1,
|
|
||||||
0);
|
|
||||||
|
|
||||||
if (p == MAP_FAILED)
|
|
||||||
error("Out of memory");
|
|
||||||
|
|
||||||
return p;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
} // namespace snmalloc
|
} // namespace snmalloc
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user