Provide a generic PAL feature detection mechanism.

We can support up to 64 optional features, currently only one is used.
This commit is contained in:
David Chisnall
2019-02-21 14:00:04 +00:00
committed by David Chisnall
parent 80630a359f
commit a24e6270cc
8 changed files with 43 additions and 18 deletions

View File

@@ -1004,7 +1004,7 @@ namespace snmalloc
static_assert(
std::remove_reference_t<decltype(
large_allocator
.memory_provider)>::supports_low_memory_notification,
.memory_provider)>::pal_supports<LowMemoryNotification>(),
"A lazy decommit strategy cannot be implemented on platforms "
"without low memory notifications");
}

View File

@@ -47,7 +47,6 @@ namespace snmalloc
template<class PAL>
class MemoryProviderStateMixin : public PAL
{
using PAL::supports_low_memory_notification;
std::atomic_flag lock = ATOMIC_FLAG_INIT;
size_t bump;
size_t remaining;
@@ -117,13 +116,21 @@ namespace snmalloc
lazy_decommit_guard.clear();
}
public:
template<PalFeatures F, typename P = PAL>
constexpr static bool pal_supports()
{
return (P::pal_features & F) == F;
}
private:
/**
* Wrapper that is instantiated only if the memory provider supports low
* memory notifications and forwards the call to the memory provider.
*/
template<typename M>
ALWAYSINLINE uint64_t low_mem_epoch(
std::enable_if_t<M::supports_low_memory_notification, int> = 0)
std::enable_if_t<pal_supports<LowMemoryNotification, M>(), int> = 0)
{
return PAL::low_memory_epoch();
}
@@ -135,7 +142,7 @@ namespace snmalloc
*/
template<typename M>
ALWAYSINLINE uint64_t low_memory_epoch(
std::enable_if_t<!M::supports_low_memory_notification, int> = 0)
std::enable_if_t<!pal_supports<LowMemoryNotification, M>(), int> = 0)
{
return 0;
}

View File

@@ -3,6 +3,23 @@
namespace snmalloc
{
void error(const char* const str);
/**
* Flags in a bitfield of optional features that a PAL may support. These
* should be set in the PAL's `pal_features` static constexpr field.
*/
enum PalFeatures : uint64_t
{
/**
* This PAL supports low memory notifications. It must implement a
* `low_memory_epoch` method that returns a `uint64_t` of the number of
* times that a low-memory notification has been raised and an
* `expensive_low_memory_check()` method that returns a `bool` indicating
* whether low memory conditions are still in effect.
*/
LowMemoryNotification = (1 << 0)
};
}
// If simultating OE, then we need the underlying platform

View File

@@ -25,10 +25,10 @@ namespace snmalloc
public:
/**
* Flag indicating that this PAL does not support low pressure
* notifications.
* Bitmap of PalFeatures flags indicating the optional features that this
* PAL supports.
*/
static constexpr bool supports_low_memory_notification = false;
static constexpr uint64_t pal_features = 0;
void error(const char* const str)
{
panic("snmalloc error: %s", str);

View File

@@ -15,10 +15,10 @@ namespace snmalloc
{
public:
/**
* Flag indicating that this PAL does not support low pressure
* notifications.
* Bitmap of PalFeatures flags indicating the optional features that this
* PAL supports.
*/
static constexpr bool supports_low_memory_notification = false;
static constexpr uint64_t pal_features = 0;
static void error(const char* const str)
{
puts(str);

View File

@@ -14,10 +14,10 @@ namespace snmalloc
{
public:
/**
* Flag indicating that this PAL does not support low pressure
* notifications.
* Bitmap of PalFeatures flags indicating the optional features that this
* PAL supports.
*/
static constexpr bool supports_low_memory_notification = false;
static constexpr uint64_t pal_features = 0;
static void error(const char* const str)
{
puts(str);

View File

@@ -15,10 +15,10 @@ namespace snmalloc
public:
/**
* Flag indicating that this PAL does not support low pressure
* notifications.
* Bitmap of PalFeatures flags indicating the optional features that this
* PAL supports.
*/
static constexpr bool supports_low_memory_notification = false;
static constexpr uint64_t pal_features = 0;
static void error(const char* const str)
{
UNUSED(str);

View File

@@ -65,9 +65,10 @@ namespace snmalloc
}
}
/**
* Flag indicating that this PAL supports the low pressure notification.
* Bitmap of PalFeatures flags indicating the optional features that this
* PAL supports. This PAL supports low-memory notifications.
*/
static constexpr bool supports_low_memory_notification = true;
static constexpr uint64_t pal_features = LowMemoryNotification;
/**
* Counter values for the number of times that a low-pressure notification
* has been delivered. Callers should compare this with a previous value