|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#ifdef __cpp_concepts |
| 4 | +# include "../ds/concept.h" |
| 5 | +# include "pal_consts.h" |
| 6 | + |
| 7 | +# include <utility> |
| 8 | + |
| 9 | +namespace snmalloc |
| 10 | +{ |
| 11 | + /** |
| 12 | + * PALs must advertize the bit vector of their supported features and the |
| 13 | + * platform's page size. This concept enforces that these are indeed |
| 14 | + * constants that fit in the desired types. (This is subtly different from |
| 15 | + * saying that they are the required types; C++ may handle constants without |
| 16 | + * much regard for their claimed type.) |
| 17 | + */ |
| 18 | + template<typename PAL> |
| 19 | + concept ConceptPAL_static_members = requires() |
| 20 | + { |
| 21 | + typename std::integral_constant<uint64_t, PAL::pal_features>; |
| 22 | + typename std::integral_constant<size_t, PAL::page_size>; |
| 23 | + }; |
| 24 | + |
| 25 | + /** |
| 26 | + * PALs expose an error reporting function which takes a const C string. |
| 27 | + */ |
| 28 | + template<typename PAL> |
| 29 | + concept ConceptPAL_error = requires(const char* const str) |
| 30 | + { |
| 31 | + { PAL::error(str) } -> ConceptSame<void>; |
| 32 | + }; |
| 33 | + |
| 34 | + /** |
| 35 | + * PALs expose a basic library of memory operations. |
| 36 | + */ |
| 37 | + template<typename PAL> |
| 38 | + concept ConceptPAL_memops = requires(PAL p, void* vp, size_t sz) |
| 39 | + { |
| 40 | + { p.notify_not_using(vp, sz) } noexcept -> ConceptSame<void>; |
| 41 | + |
| 42 | + /* For reasons unknown, these seem to tickle some bug in MSVC */ |
| 43 | +# if !defined(_MSC_VER) |
| 44 | + { p.template notify_using<NoZero>(vp, sz) } noexcept |
| 45 | + -> ConceptSame<void>; |
| 46 | + { p.template notify_using<YesZero>(vp, sz) } noexcept |
| 47 | + -> ConceptSame<void>; |
| 48 | +# endif |
| 49 | + |
| 50 | + { p.template zero<false>(vp, sz) } noexcept -> ConceptSame<void>; |
| 51 | + { p.template zero<true>(vp, sz) } noexcept -> ConceptSame<void>; |
| 52 | + }; |
| 53 | + |
| 54 | + /** |
| 55 | + * Absent any feature flags, the PAL must support a crude primitive allocator |
| 56 | + */ |
| 57 | + template<typename PAL> |
| 58 | + concept ConceptPAL_reserve_at_least = requires(PAL p, void* vp, size_t sz) |
| 59 | + { |
| 60 | + { p.reserve_at_least(sz) } noexcept |
| 61 | + -> ConceptSame<std::pair<void*, size_t>>; |
| 62 | + }; |
| 63 | + |
| 64 | + /** |
| 65 | + * Some PALs expose a richer allocator which understands aligned allocations |
| 66 | + */ |
| 67 | + template<typename PAL> |
| 68 | + concept ConceptPAL_reserve_aligned = requires(PAL p, size_t sz) |
| 69 | + { |
| 70 | + { p.template reserve_aligned<false>(sz) } noexcept -> ConceptSame<void*>; |
| 71 | + { p.template reserve_aligned<true>(sz) } noexcept -> ConceptSame<void*>; |
| 72 | + }; |
| 73 | + |
| 74 | + /** |
| 75 | + * Some PALs can provide memory pressure callbacks. |
| 76 | + */ |
| 77 | + template<typename PAL> |
| 78 | + concept ConceptPAL_mem_low_notify = |
| 79 | + requires(PAL p, PalNotificationObject* pno) |
| 80 | + { |
| 81 | + { p.expensive_low_memory_check() } -> ConceptSame<bool>; |
| 82 | + { p.register_for_low_memory_callback(pno) } -> ConceptSame<void>; |
| 83 | + }; |
| 84 | + |
| 85 | + /** |
| 86 | + * PALs ascribe to the conjunction of several concepts. These are broken |
| 87 | + * out by the shape of the requires() quantifiers required and by any |
| 88 | + * requisite claimed pal_features. PALs not claiming particular features |
| 89 | + * are, naturally, not bound by the corresponding concept. |
| 90 | + */ |
| 91 | + template<typename PAL> |
| 92 | + concept ConceptPAL = |
| 93 | + ConceptPAL_static_members<PAL> && |
| 94 | + ConceptPAL_error<PAL> && |
| 95 | + ConceptPAL_memops<PAL> && |
| 96 | + (!(PAL::pal_features & LowMemoryNotification) || |
| 97 | + ConceptPAL_mem_low_notify<PAL>) && |
| 98 | + (!!(PAL::pal_features & AlignedAllocation) || |
| 99 | + ConceptPAL_reserve_at_least<PAL>) && |
| 100 | + (!(PAL::pal_features & AlignedAllocation) || |
| 101 | + ConceptPAL_reserve_aligned<PAL>); |
| 102 | + |
| 103 | +} // namespace snmalloc |
| 104 | +#endif |
0 commit comments