Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/snmalloc/ds/pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ namespace snmalloc
* Pool of a particular type of object.
*
* This pool will never return objects to the OS. It maintains a list of all
* objects ever allocated that can be iterated (not concurrency safe). Pooled
* types can be acquired from the pool, and released back to the pool. This is
* concurrency safe.
* objects ever allocated that can be iterated. Pooled types can be acquired
* from the pool, and released back to the pool. This is concurrency safe.
*
* This is used to bootstrap the allocation of allocators.
*/
Expand Down Expand Up @@ -210,7 +209,15 @@ namespace snmalloc
static T* iterate(T* p = nullptr)
{
if (p == nullptr)
return get_state().list.unsafe_ptr();
{
// Read the head under the lock that `acquire` publishes it with. A
// node's `list_next` never changes after that, so the rest of the
// walk needs no lock.
PoolState<T>& pool = get_state();
T* result;
with(pool.lock, [&]() { result = pool.list.unsafe_ptr(); });
return result;
}

return p->list_next.unsafe_ptr();
}
Expand Down
73 changes: 71 additions & 2 deletions src/snmalloc/global/globalalloc.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "../mem/mem.h"
#include "../mem/secondary/optional.h"
#include "threadalloc.h"

namespace snmalloc
Expand Down Expand Up @@ -32,6 +33,36 @@ namespace snmalloc
AllocPool<Config_>::restore(first, last);
}

namespace detail
{
/**
* If a secondary allocator that tracks object bounds owns `p`, set
* `start` and `size` to its object. Main-heap pointers return false
* after one pagemap read.
*/
template<SNMALLOC_CONCEPT(IsConfig) Config_>
SNMALLOC_FAST_PATH_INLINE bool secondary_object(
address_t p,
const typename Config_::PagemapEntry& entry,
address_t& start,
size_t& size)
{
if constexpr (Config_::SecondaryAllocator::pass_through)
{
UNUSED(p, entry, start, size);
return false;
}
else
{
// Backend-owned chunks belong to snmalloc's backend, never to a
// secondary allocator, and `is_owned()` asserts `!is_backend_owned()`.
return !entry.is_backend_owned() && !entry.is_owned() &&
secondary::lookup<typename Config_::SecondaryAllocator>(
p, start, size, 0);
}
}
} // namespace detail

/**
If you pass a pointer to a bool, then it returns whether all the
allocators are empty. If you don't pass a pointer to a bool, then will
Expand Down Expand Up @@ -84,12 +115,19 @@ namespace snmalloc
}
}

const bool secondary_okay =
secondary::debug_is_empty<typename Config_::SecondaryAllocator>(0);
okay = okay && secondary_okay;

if (result != nullptr)
{
*result = okay;
return;
}

if (!secondary_okay)
error("debug_check_empty: secondary allocator still has live objects");

// Redo check so abort is on allocator with allocation left.
if (!okay)
{
Expand Down Expand Up @@ -138,6 +176,10 @@ namespace snmalloc
size_t SNMALLOC_FAST_PATH_INLINE remaining_bytes(address_t p)
{
const auto& entry = Config_::Backend::template get_metaentry<true>(p);
address_t start;
size_t size;
if (detail::secondary_object<Config_>(p, entry, start, size))
return start + size - p;

auto sizeclass = entry.get_sizeclass();
return snmalloc::remaining_bytes(sizeclass, p);
Expand All @@ -159,6 +201,10 @@ namespace snmalloc
static inline size_t index_in_object(address_t p)
{
const auto& entry = Config_::Backend::template get_metaentry<true>(p);
address_t start;
size_t size;
if (detail::secondary_object<Config_>(p, entry, start, size))
return p - start;

auto sizeclass = entry.get_sizeclass();
return snmalloc::index_in_object(sizeclass, p);
Expand Down Expand Up @@ -302,15 +348,38 @@ namespace snmalloc
UNUSED(p, size);
}

template<SNMALLOC_CONCEPT(IsConfig) Config_ = Config>
SNMALLOC_FAST_PATH_INLINE bool secondary_owns(const void* p)
{
if constexpr (Config_::SecondaryAllocator::pass_through)
{
UNUSED(p);
return false;
}
else
{
if (p == nullptr)
return false;
// Main-heap (frontend or backend) pointers never belong to the secondary
// allocator. Check `is_backend_owned()` first because `is_owned()`
// asserts `!is_backend_owned()`.
const auto& entry =
Config_::Backend::template get_metaentry<true>(address_cast(p));
if (entry.is_backend_owned() || entry.is_owned())
return false;
return Config_::SecondaryAllocator::owns(p);
}
}

template<SNMALLOC_CONCEPT(IsConfig) Config_ = Config>
SNMALLOC_FAST_PATH_INLINE size_t alloc_size(const void* p_raw)
{
const auto& entry = Config_::Backend::get_metaentry(address_cast(p_raw));

if (SNMALLOC_UNLIKELY(
!Config::SecondaryAllocator::pass_through && !entry.is_owned() &&
!Config_::SecondaryAllocator::pass_through && !entry.is_owned() &&
p_raw != nullptr))
return Config::SecondaryAllocator::alloc_size(p_raw);
return Config_::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
2 changes: 1 addition & 1 deletion src/snmalloc/global/libc.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ namespace snmalloc::libc

size_t sz = alloc_size(ptr);
// Keep the current allocation if the given size is in the same sizeclass.
if (sz == round_size(size))
if (sz == round_size(size) && !secondary_owns(ptr))
{
return ptr;
}
Expand Down
73 changes: 73 additions & 0 deletions src/snmalloc/mem/secondary/chain.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#pragma once

#include "snmalloc/ds_core/defines.h"
#include "snmalloc/mem/secondary/optional.h"

#include <stddef.h>

namespace snmalloc
{
template<typename First, typename Second>
class SecondaryAllocatorChain
{
public:
static constexpr inline bool pass_through = false;

static void initialize() noexcept
{
First::initialize();
Second::initialize();
}

template<class SizeAlign>
SNMALLOC_FAST_PATH static void* allocate(SizeAlign&& getter)
{
// A member holding work for this allocation goes first, so the other
// cannot take the allocation and drop that work.
if (SNMALLOC_UNLIKELY(secondary::has_pending<Second>(0)))
{
if (void* p = Second::allocate(getter))
return p;
return First::allocate(getter);
}
if (void* p = First::allocate(getter))
{
secondary::cancel_pending<Second>(0);
return p;
}
return Second::allocate(getter);
}

SNMALLOC_FAST_PATH static bool owns(const void* pointer)
{
return First::owns(pointer) || Second::owns(pointer);
}

SNMALLOC_FAST_PATH static void deallocate(void* pointer)
{
if (First::owns(pointer))
First::deallocate(pointer);
else
Second::deallocate(pointer);
}

SNMALLOC_FAST_PATH static size_t alloc_size(const void* pointer)
{
if (First::owns(pointer))
return First::alloc_size(pointer);
return Second::alloc_size(pointer);
}

static bool lookup(address_t a, address_t& start, size_t& size)
{
return secondary::lookup<First>(a, start, size, 0) ||
secondary::lookup<Second>(a, start, size, 0);
}

static bool debug_is_empty()
{
return secondary::debug_is_empty<First>(0) &&
secondary::debug_is_empty<Second>(0);
}
};
} // namespace snmalloc
6 changes: 6 additions & 0 deletions src/snmalloc/mem/secondary/default.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,11 @@ namespace snmalloc
"secondary alloc_size should never be invoked with default setup");
return 0;
}

SNMALLOC_FAST_PATH
static bool owns(const void*)
{
return false;
}
};
} // namespace snmalloc
6 changes: 6 additions & 0 deletions src/snmalloc/mem/secondary/gwp_asan.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,11 @@ namespace snmalloc
{
return singleton.getSize(pointer);
}

SNMALLOC_FAST_PATH
static bool owns(const void* pointer)
{
return singleton.pointerIsMine(pointer);
}
};
} // namespace snmalloc
59 changes: 59 additions & 0 deletions src/snmalloc/mem/secondary/optional.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#pragma once

#include "../../aal/aal.h"

#include <stddef.h>

namespace snmalloc::secondary
{
// Optional secondary-allocator members. Each helper calls the member when
// `SA` has it and otherwise returns the default.

template<typename SA>
auto cancel_pending(int) noexcept -> decltype(SA::cancel_pending(), void())
{
SA::cancel_pending();
}

template<typename>
void cancel_pending(long) noexcept
{}

template<typename SA>
auto has_pending(int) noexcept -> decltype(SA::has_pending())
{
return SA::has_pending();
}

template<typename>
bool has_pending(long) noexcept
{
return false;
}

/// Bounds of the object containing `a`, for allocators that track them.
template<typename SA>
auto lookup(address_t a, address_t& start, size_t& size, int)
-> decltype(SA::lookup(a, start, size))
{
return SA::lookup(a, start, size);
}

template<typename>
bool lookup(address_t, address_t&, size_t&, long)
{
return false;
}

template<typename SA>
auto debug_is_empty(int) -> decltype(SA::debug_is_empty())
{
return SA::debug_is_empty();
}

template<typename>
bool debug_is_empty(long)
{
return true;
}
} // namespace snmalloc::secondary
3 changes: 2 additions & 1 deletion src/snmalloc/override/rust.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ extern "C" SNMALLOC_EXPORT void* SNMALLOC_NAME_MANGLE(rust_realloc)(
aligned_new_size = aligned_size(alignment, new_size);
if (
size_to_sizeclass_full(aligned_old_size).raw() ==
size_to_sizeclass_full(aligned_new_size).raw())
size_to_sizeclass_full(aligned_new_size).raw() &&
!secondary_owns(ptr))
return ptr;
void* p = alloc(aligned_new_size);
if (p)
Expand Down
10 changes: 8 additions & 2 deletions src/snmalloc/stl/gnu/atomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ namespace snmalloc
return __builtin_addressof(ref);
}

SNMALLOC_FAST_PATH static const T* addressof(const T& ref)
{
return __builtin_addressof(ref);
}

// From libc++:
// require types that are 1, 2, 4, 8, or 16 bytes in length to be aligned
// to at least their size to be potentially
Expand All @@ -84,12 +89,13 @@ namespace snmalloc
SNMALLOC_FAST_PATH Atomic& operator=(const Atomic&) = delete;

// Atomic load.
SNMALLOC_FAST_PATH operator T()
SNMALLOC_FAST_PATH operator T() const
{
return load();
}

SNMALLOC_FAST_PATH T load(MemoryOrder mem_ord = MemoryOrder::SEQ_CST)
SNMALLOC_FAST_PATH T
load(MemoryOrder mem_ord = MemoryOrder::SEQ_CST) const
{
T res;
__atomic_load(addressof(val), addressof(res), order(mem_ord));
Expand Down
Loading
Loading