Files
snmalloc/src/pal/pal_bsd.h
Matthew Parkinson 55a7ad2d58 Introduce PalEnforceAccess
The various Pals were given different meanings in CHECK_CLIENT and
non-CHECK_CLIENT builds.  This was because it is essential
that in the CHECK_CLIENT builds access is prevented, when not requested.

This PR separates the CHECK_CLIENT concept from how the Pal should be
implemented.
2021-09-28 09:23:52 +01:00

50 lines
1.3 KiB
C++

#pragma once
#include "pal_posix.h"
#include <sys/mman.h>
namespace snmalloc
{
/**
* Generic *BSD PAL mixin. This provides features that are common to the BSD
* family.
*/
template<typename OS>
class PALBSD : public PALPOSIX<OS>
{
public:
/**
* Bitmap of PalFeatures flags indicating the optional features that this
* PAL supports.
*
* The generic BSD PAL does not add any features that are not supported by
* generic POSIX systems, but explicitly declares this variable to remind
* anyone who extends this class that they may need to modify this field.
*/
static constexpr uint64_t pal_features = PALPOSIX<OS>::pal_features;
/**
* Notify platform that we will not be using these pages.
*
* BSD systems provide the `MADV_FREE` flag to `madvise`, which allows the
* operating system to replace the pages with CoW copies of a zero page at
* any point between the call and the next write to that page.
*/
static void notify_not_using(void* p, size_t size) noexcept
{
SNMALLOC_ASSERT(is_aligned_block<OS::page_size>(p, size));
#if !defined(NDEBUG)
memset(p, 0x5a, size);
#endif
madvise(p, size, MADV_FREE);
if constexpr (PalEnforceAccess)
{
mprotect(p, size, PROT_NONE);
}
}
};
} // namespace snmalloc