From a822200166b394aa4de72e49bfd048fcb6389fa6 Mon Sep 17 00:00:00 2001 From: Matthew Parkinson Date: Tue, 20 Sep 2022 11:17:07 +0100 Subject: [PATCH 1/4] Add indirect range The ranges currently either completely own the parent range, or it is a static and thus completely shared. The indirect range allows sharing without it being global. --- .../backend_helpers/backend_helpers.h | 1 + src/snmalloc/backend_helpers/indirectrange.h | 53 +++++++++++++++++++ src/snmalloc/backend_helpers/range_helpers.h | 31 +++++++++++ 3 files changed, 85 insertions(+) create mode 100644 src/snmalloc/backend_helpers/indirectrange.h diff --git a/src/snmalloc/backend_helpers/backend_helpers.h b/src/snmalloc/backend_helpers/backend_helpers.h index fa4a708f7..a84a97f72 100644 --- a/src/snmalloc/backend_helpers/backend_helpers.h +++ b/src/snmalloc/backend_helpers/backend_helpers.h @@ -5,6 +5,7 @@ #include "defaultpagemapentry.h" #include "empty_range.h" #include "globalrange.h" +#include "indirectrange.h" #include "largebuddyrange.h" #include "logrange.h" #include "pagemap.h" diff --git a/src/snmalloc/backend_helpers/indirectrange.h b/src/snmalloc/backend_helpers/indirectrange.h new file mode 100644 index 000000000..ad551298b --- /dev/null +++ b/src/snmalloc/backend_helpers/indirectrange.h @@ -0,0 +1,53 @@ +#pragma once + +#include "../ds/ds.h" +#include "empty_range.h" + +namespace snmalloc +{ + /** + * Stores a references to the parent range so that it can be shared + * without `static` scope. + * + * This could be used to allow multiple allocators on a single region of memory. + */ + struct IndirectRange + { + template> + class Type : public RefParent + { + using RefParent::parent; + + public: + static constexpr bool Aligned = ParentRange::Aligned; + + static_assert( + ParentRange::ConcurrencySafe, + "StaticRange requires a concurrency safe parent."); + + static constexpr bool ConcurrencySafe = true; + + using ChunkBounds = typename ParentRange::ChunkBounds; + + constexpr Type() = default; + + CapPtr alloc_range(size_t size) + { + return parent->alloc_range(size); + } + + void dealloc_range(CapPtr base, size_t size) + { + parent->dealloc_range(base, size); + } + + /** + * Points the parent reference to the given range. + */ + void set_parent(ParentRange* p) + { + parent = p; + } + }; + }; +} // namespace snmalloc diff --git a/src/snmalloc/backend_helpers/range_helpers.h b/src/snmalloc/backend_helpers/range_helpers.h index 1534bb8df..5d434f324 100644 --- a/src/snmalloc/backend_helpers/range_helpers.h +++ b/src/snmalloc/backend_helpers/range_helpers.h @@ -130,4 +130,35 @@ namespace snmalloc } }; + /** + * Helper class for allowing a range to be navigated to find an + * ancestor of a specific type. The parent is a pointer to a range + * this allows the parent to be shared. + */ + template + class RefParent + { + protected: + Parent* parent{}; + + public: + /** + * Returns the outermost Ancestor with the correct type. + * + * Fails to compile if no such ancestor exists. + */ + template + Anc* ancestor() + { + if constexpr (std::is_same_v) + { + return parent; + } + else + { + return parent->template ancestor(); + } + } + }; + } // namespace snmalloc From e19777366a73b17f5cb150a2f05b594b140fd6c2 Mon Sep 17 00:00:00 2001 From: Matthew Parkinson Date: Tue, 20 Sep 2022 13:56:22 +0100 Subject: [PATCH 2/4] Update src/snmalloc/backend_helpers/indirectrange.h Co-authored-by: Nathaniel Filardo <105816689+nwf-msr@users.noreply.github.com> --- src/snmalloc/backend_helpers/indirectrange.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/snmalloc/backend_helpers/indirectrange.h b/src/snmalloc/backend_helpers/indirectrange.h index ad551298b..82ee0b95c 100644 --- a/src/snmalloc/backend_helpers/indirectrange.h +++ b/src/snmalloc/backend_helpers/indirectrange.h @@ -23,7 +23,7 @@ namespace snmalloc static_assert( ParentRange::ConcurrencySafe, - "StaticRange requires a concurrency safe parent."); + "IndirectRange requires a concurrency safe parent."); static constexpr bool ConcurrencySafe = true; From f4e39c51fb28c33534ad0cb5146d2304502efdc4 Mon Sep 17 00:00:00 2001 From: Matthew Parkinson Date: Tue, 20 Sep 2022 13:56:36 +0100 Subject: [PATCH 3/4] Update src/snmalloc/backend_helpers/range_helpers.h Co-authored-by: Nathaniel Filardo <105816689+nwf-msr@users.noreply.github.com> --- src/snmalloc/backend_helpers/range_helpers.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/snmalloc/backend_helpers/range_helpers.h b/src/snmalloc/backend_helpers/range_helpers.h index 5d434f324..9d97f0e3a 100644 --- a/src/snmalloc/backend_helpers/range_helpers.h +++ b/src/snmalloc/backend_helpers/range_helpers.h @@ -132,7 +132,7 @@ namespace snmalloc /** * Helper class for allowing a range to be navigated to find an - * ancestor of a specific type. The parent is a pointer to a range + * ancestor of a specific type. The parent is a pointer to a range; * this allows the parent to be shared. */ template From 4112ea8ed5372b158f7e42e709eb50e44d52b74d Mon Sep 17 00:00:00 2001 From: Matthew Parkinson Date: Tue, 20 Sep 2022 13:57:39 +0100 Subject: [PATCH 4/4] Clangformat --- src/snmalloc/backend_helpers/indirectrange.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/snmalloc/backend_helpers/indirectrange.h b/src/snmalloc/backend_helpers/indirectrange.h index ad551298b..126d35508 100644 --- a/src/snmalloc/backend_helpers/indirectrange.h +++ b/src/snmalloc/backend_helpers/indirectrange.h @@ -8,8 +8,9 @@ namespace snmalloc /** * Stores a references to the parent range so that it can be shared * without `static` scope. - * - * This could be used to allow multiple allocators on a single region of memory. + * + * This could be used to allow multiple allocators on a single region of + * memory. */ struct IndirectRange {