diff --git a/src/mem/alloc.h b/src/mem/alloc.h index 6b95a15..c65c0cf 100644 --- a/src/mem/alloc.h +++ b/src/mem/alloc.h @@ -70,7 +70,15 @@ namespace snmalloc */ struct SuperslabMap { + /** + * Reference to the pagemap that we're using, allowing it to not be the + * default pagemap. + */ SuperslabPagemap& pagemap; + /** + * Constructor. Uses the global pagemap by default, but can use a + * different one if required. + */ SuperslabMap(SuperslabPagemap& pm = global_pagemap) : pagemap(pm) {} /** * Get the pagemap entry corresponding to a specific address. diff --git a/src/mem/pagemap.h b/src/mem/pagemap.h index 6a30772..eaf7795 100644 --- a/src/mem/pagemap.h +++ b/src/mem/pagemap.h @@ -198,8 +198,20 @@ namespace snmalloc } public: + /** + * The pagemap configuration describing this instantiation of the template. + */ static constexpr PagemapConfig config = { 1, false, GRANULARITY_BITS, sizeof(T)}; + + /** + * Cast a `void*` to a pointer to this template instantiation, given a + * config describing the configuration. Return null if the configuration + * passed does not correspond to this template instantiation. + * + * This intended to allow code that depends on the pagemap having a + * specific representation to fail gracefully. + */ static Pagemap* cast_to_pagemap(void* pm, const PagemapConfig* c) { if ( @@ -290,9 +302,20 @@ namespace snmalloc std::atomic top[ENTRIES]; public: + /** + * The pagemap configuration describing this instantiation of the template. + */ static constexpr PagemapConfig config = { 1, true, GRANULARITY_BITS, sizeof(T)}; + /** + * Cast a `void*` to a pointer to this template instantiation, given a + * config describing the configuration. Return null if the configuration + * passed does not correspond to this template instantiation. + * + * This intended to allow code that depends on the pagemap having a + * specific representation to fail gracefully. + */ static FlatPagemap* cast_to_pagemap(void* pm, PagemapConfig* c) { if ( diff --git a/src/override/malloc.cc b/src/override/malloc.cc index 61aa96e..95146c8 100644 --- a/src/override/malloc.cc +++ b/src/override/malloc.cc @@ -206,6 +206,14 @@ extern "C" } #ifdef SNMALLOC_EXPOSE_PAGEMAP + /** + * Export the pagemap. The return value is a pointer to the pagemap + * structure. The argument is used to return a pointer to a `PagemapConfig` + * structure describing the type of the pagemap. Static methods on the + * concrete pagemap templates can then be used to safely cast the return from + * this function to the correct type. This allows us to preserve some + * semblance of ABI safety via a pure C API. + */ SNMALLOC_EXPORT void* SNMALLOC_NAME_MANGLE(snmalloc_get_global_pagemap)( PagemapConfig const** config) {