diff --git a/src/backend/backend.h b/src/backend/backend.h index 1c43dbf..42bb760 100644 --- a/src/backend/backend.h +++ b/src/backend/backend.h @@ -335,6 +335,12 @@ namespace snmalloc UNUSED(local_state); return concretePagemap.get_bounds(); } + + static bool is_initialised(LocalState* ls) + { + UNUSED(ls); + return concretePagemap.is_initialised(); + } }; private: diff --git a/src/backend/pagemap.h b/src/backend/pagemap.h index 48699cd..cbd32b9 100644 --- a/src/backend/pagemap.h +++ b/src/backend/pagemap.h @@ -278,6 +278,14 @@ namespace snmalloc return body[p >> SHIFT]; } + /** + * Check if the pagemap has been initialised. + */ + [[nodiscard]] bool is_initialised() const + { + return body_opt != nullptr; + } + /** * Return the starting address corresponding to a given entry within the * Pagemap. Also checks that the reference actually points to a valid entry. diff --git a/src/mem/localalloc.h b/src/mem/localalloc.h index 5cd80da..d8e1b13 100644 --- a/src/mem/localalloc.h +++ b/src/mem/localalloc.h @@ -772,6 +772,16 @@ namespace snmalloc #endif } + bool check_bounds(const void* p, size_t s) + { + auto ls = core_alloc->backend_state_ptr(); + if (SNMALLOC_LIKELY(SharedStateHandle::Pagemap::is_initialised(ls))) + { + return remaining_bytes(p) >= s; + } + return true; + } + /** * Returns the byte offset into an object. * diff --git a/src/override/memcpy.cc b/src/override/memcpy.cc index 884e1c2..6b51bc5 100644 --- a/src/override/memcpy.cc +++ b/src/override/memcpy.cc @@ -137,7 +137,7 @@ namespace auto& alloc = ThreadAlloc::get(); void* p = const_cast(ptr); - if (SNMALLOC_UNLIKELY(alloc.remaining_bytes(ptr) < len)) + if (SNMALLOC_UNLIKELY(!alloc.check_bounds(ptr, len))) { if constexpr (FailFast) { diff --git a/src/test/perf/memcpy/memcpy.cc b/src/test/perf/memcpy/memcpy.cc index 9bfb985..bfa6cb4 100644 --- a/src/test/perf/memcpy/memcpy.cc +++ b/src/test/perf/memcpy/memcpy.cc @@ -112,7 +112,7 @@ int main(int argc, char** argv) }; std::vector sizes; - for (size_t size = 1; size < 64; size++) + for (size_t size = 0; size < 64; size++) { sizes.push_back(size); }