Skip to content

Commit

Permalink
override alloc_size
Browse files Browse the repository at this point in the history
  • Loading branch information
SchrodingerZhu committed Jan 27, 2025
1 parent 9a98bc1 commit 6d0b985
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
3 changes: 3 additions & 0 deletions src/snmalloc/mem/localalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,9 @@ namespace snmalloc
#ifdef SNMALLOC_PASS_THROUGH
return external_alloc::malloc_usable_size(const_cast<void*>(p_raw));
#else

if (SecondaryAllocator::has_secondary_ownership(p_raw))
return SecondaryAllocator::alloc_size(p_raw);
// TODO What's the domestication policy here? At the moment we just
// probe the pagemap with the raw address, without checks. There could
// be implicit domestication through the `Config::Pagemap` or
Expand Down
11 changes: 10 additions & 1 deletion src/snmalloc/mem/secondary/default.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,18 @@ namespace snmalloc
}

SNMALLOC_FAST_PATH
static bool has_secondary_ownership([[maybe_unused]] void* pointer)
static bool has_secondary_ownership([[maybe_unused]] const void* pointer)
{
return false;
}

SNMALLOC_FAST_PATH
static size_t alloc_size([[maybe_unused]] const void* pointer)
{
SNMALLOC_ASSERT(
false &&
"secondary alloc_size should never be invoked with default setup");
return 0;
}
};
} // namespace snmalloc
25 changes: 12 additions & 13 deletions src/snmalloc/mem/secondary/gwp_asan.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ namespace snmalloc
static inline gwp_asan::GuardedPoolAllocator singleton;
static inline size_t max_allocation_size;

static gwp_asan::GuardedPoolAllocator& get()
{
return singleton;
}

public:
static void initialize() noexcept
{
Expand All @@ -38,13 +33,12 @@ namespace snmalloc

SNMALLOC_FAST_PATH static void* allocate(size_t size)
{
auto& inner = get();
if (SNMALLOC_UNLIKELY(inner.shouldSample()))
if (SNMALLOC_UNLIKELY(singleton.shouldSample()))
{
if (size > max_allocation_size)
return nullptr;
auto alignment = natural_alignment(size);
return get().allocate(size, alignment);
return singleton.allocate(size, alignment);
}
return nullptr;
}
Expand All @@ -55,19 +49,24 @@ namespace snmalloc
if (SNMALLOC_LIKELY(pointer == nullptr))
return;

auto& inner = get();
snmalloc_check_client(
mitigations(sanity_checks),
inner.pointerIsMine(pointer),
singleton.pointerIsMine(pointer),
"Not allocated by snmalloc or secondary allocator");

inner.deallocate(pointer);
singleton.deallocate(pointer);
}

SNMALLOC_FAST_PATH
static bool has_secondary_ownership([[maybe_unused]] const void* pointer)
{
return singleton.pointerIsMine(pointer);
}

SNMALLOC_FAST_PATH
static bool has_secondary_ownership([[maybe_unused]] void* pointer)
static size_t alloc_size(const void* pointer)
{
return get().pointerIsMine(pointer);
return singleton.getSize(pointer);
}
};
} // namespace snmalloc

0 comments on commit 6d0b985

Please sign in to comment.