Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template construction of Pool elements #641

Merged
merged 8 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
35 changes: 34 additions & 1 deletion src/snmalloc/mem/corealloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -1015,9 +1015,42 @@ namespace snmalloc
}
};

template<typename Config>
class ConstructCoreAlloc
{
using CA = CoreAllocator<Config>;

public:
static capptr::Alloc<CA> make(LocalCache* lc)
{
size_t size = sizeof(CA);
size_t round_sizeof = Aal::capptr_size_round(size);
size_t request_size = bits::next_pow2(round_sizeof);
size_t spare = request_size - round_sizeof;

auto raw =
Config::Backend::template alloc_meta_data<CA>(nullptr, request_size);

if (raw == nullptr)
{
Config::Pal::error("Failed to initialise thread local allocator.");
}

capptr::Alloc<void> spare_start = pointer_offset(raw, round_sizeof);
Range<capptr::bounds::Alloc> r{spare_start, spare};

auto p = capptr::Alloc<CA>::unsafe_from(new (raw.unsafe_ptr()) CA(r, lc));

// Remove excess from the permissions.
mjp41 marked this conversation as resolved.
Show resolved Hide resolved
p = Aal::capptr_bound<CA, capptr::bounds::Alloc>(p, round_sizeof);
return p;
}
};

/**
* Use this alias to access the pool of allocators throughout snmalloc.
*/
template<typename Config>
using AllocPool = Pool<CoreAllocator<Config>, Config, Config::pool>;
using AllocPool =
Pool<CoreAllocator<Config>, ConstructCoreAlloc<Config>, Config::pool>;
} // namespace snmalloc
91 changes: 23 additions & 68 deletions src/snmalloc/mem/pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ namespace snmalloc
template<class T>
class PoolState
{
template<
typename TT,
SNMALLOC_CONCEPT(IsConfig) Config,
PoolState<TT>& get_state()>
template<typename TT, typename Construct, PoolState<TT>& get_state()>
friend class Pool;

private:
Expand All @@ -45,50 +42,10 @@ namespace snmalloc
* SingletonPoolState::pool is the default provider for the PoolState within
* the Pool class.
*/
template<typename T, SNMALLOC_CONCEPT(IsConfig) Config>
template<typename T>
class SingletonPoolState
{
/**
* SFINAE helper. Matched only if `T` implements `ensure_init`. Calls it
* if it exists.
*/
template<typename SharedStateHandle_>
SNMALLOC_FAST_PATH static auto call_ensure_init(SharedStateHandle_*, int)
-> decltype(SharedStateHandle_::ensure_init())
{
static_assert(
std::is_same<Config, SharedStateHandle_>::value,
"SFINAE parameter, should only be used with Config");
SharedStateHandle_::ensure_init();
}

/**
* SFINAE helper. Matched only if `T` does not implement `ensure_init`.
* Does nothing if called.
*/
template<typename SharedStateHandle_>
SNMALLOC_FAST_PATH static auto call_ensure_init(SharedStateHandle_*, long)
{
static_assert(
std::is_same<Config, SharedStateHandle_>::value,
"SFINAE parameter, should only be used with Config");
}

/**
* Call `Config::ensure_init()` if it is implemented, do nothing
* otherwise.
*/
SNMALLOC_FAST_PATH static void ensure_init()
{
call_ensure_init<Config>(nullptr, 0);
}

static void make_pool(PoolState<T>*) noexcept
{
ensure_init();
// Default initializer already called on PoolState, no need to use
// placement new.
}
static void make_pool(PoolState<T>*) noexcept {}

public:
/**
Expand All @@ -101,6 +58,23 @@ namespace snmalloc
}
};

/**
* @brief Default construct helper for the pool. Just uses `new`. This can't
* be used by the allocator pool as it has not created memory yet.
*
* @tparam T
*/
template<typename T>
class DefaultConstruct
{
public:
template<typename... Args>
static capptr::Alloc<T> make(Args&&... args)
{
return capptr::Alloc<T>::unsafe_from(new T(std::forward<Args>(args)...));
}
};

/**
* Wrapper class to access a pool of a particular type of object.
*
Expand All @@ -116,8 +90,8 @@ namespace snmalloc
*/
template<
typename T,
SNMALLOC_CONCEPT(IsConfig) Config,
PoolState<T>& get_state() = SingletonPoolState<T, Config>::pool>
typename ConstructT = DefaultConstruct<T>,
nwf-msr marked this conversation as resolved.
Show resolved Hide resolved
PoolState<T>& get_state() = SingletonPoolState<T>::pool>
class Pool
{
public:
Expand All @@ -141,26 +115,7 @@ namespace snmalloc
}
}

size_t request_size = bits::next_pow2(sizeof(T));
size_t round_sizeof = Aal::capptr_size_round(sizeof(T));
size_t spare = request_size - round_sizeof;

auto raw =
Config::Backend::template alloc_meta_data<T>(nullptr, request_size);

if (raw == nullptr)
{
Config::Pal::error("Failed to initialise thread local allocator.");
}

capptr::Alloc<void> spare_start = pointer_offset(raw, round_sizeof);
Range<capptr::bounds::Alloc> r{spare_start, spare};

auto p = capptr::Alloc<T>::unsafe_from(
new (raw.unsafe_ptr()) T(r, std::forward<Args>(args)...));

// Remove excess from the permissions.
p = Aal::capptr_bound<T, capptr::bounds::Alloc>(p, round_sizeof);
auto p = ConstructT::make(std::forward<Args>(args)...);

FlagLock f(pool.lock);
p->list_next = pool.list;
Expand Down
5 changes: 1 addition & 4 deletions src/snmalloc/mem/pooled.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ namespace snmalloc
class Pooled
{
public:
template<
typename TT,
SNMALLOC_CONCEPT(IsConfig) Config,
PoolState<TT>& get_state()>
template<typename TT, typename Construct, PoolState<TT>& get_state()>
friend class Pool;

/// Used by the pool for chaining together entries when not in use.
Expand Down
18 changes: 9 additions & 9 deletions src/test/func/pool/pool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,26 @@ struct PoolAEntry : Pooled<PoolAEntry>
{
int field;

PoolAEntry(Range<capptr::bounds::Alloc>&) : field(1){};
PoolAEntry() : field(1){};
};

using PoolA = Pool<PoolAEntry, Alloc::Config>;
using PoolA = Pool<PoolAEntry>;

struct PoolBEntry : Pooled<PoolBEntry>
{
int field;

PoolBEntry(Range<capptr::bounds::Alloc>&) : field(0){};
PoolBEntry(Range<capptr::bounds::Alloc>&, int f) : field(f){};
PoolBEntry() : field(0){};
PoolBEntry(int f) : field(f){};
};

using PoolB = Pool<PoolBEntry, Alloc::Config>;
using PoolB = Pool<PoolBEntry>;

struct PoolLargeEntry : Pooled<PoolLargeEntry>
{
std::array<int, 2'000'000> payload;

PoolLargeEntry(Range<capptr::bounds::Alloc>&)
PoolLargeEntry()
{
printf(".");
fflush(stdout);
Expand All @@ -41,18 +41,18 @@ struct PoolLargeEntry : Pooled<PoolLargeEntry>
};
};

using PoolLarge = Pool<PoolLargeEntry, Alloc::Config>;
using PoolLarge = Pool<PoolLargeEntry>;

template<bool order>
struct PoolSortEntry : Pooled<PoolSortEntry<order>>
{
int field;

PoolSortEntry(Range<capptr::bounds::Alloc>&, int f) : field(f){};
PoolSortEntry(int f) : field(f){};
};

template<bool order>
using PoolSort = Pool<PoolSortEntry<order>, Alloc::Config>;
using PoolSort = Pool<PoolSortEntry<order>>;

void test_alloc()
{
Expand Down