Moved SharedStateHandle to Pool class instead of methods since all of them use it

This commit is contained in:
Istvan Haller
2021-08-23 20:08:27 +01:00
parent c89a085c90
commit 769c61e716
4 changed files with 40 additions and 36 deletions

View File

@@ -11,8 +11,8 @@ namespace snmalloc
static_assert(
SharedStateHandle::Options.CoreAllocIsPoolAllocated,
"Global statistics are available only for pool-allocated configurations");
auto* alloc = Pool<CoreAllocator<SharedStateHandle>>::template iterate<
SharedStateHandle>();
auto* alloc =
Pool<CoreAllocator<SharedStateHandle>, SharedStateHandle>::iterate();
while (alloc != nullptr)
{
@@ -20,8 +20,9 @@ namespace snmalloc
if (a != nullptr)
stats.add(*a);
stats.add(alloc->stats());
alloc = Pool<CoreAllocator<SharedStateHandle>>::template iterate<
SharedStateHandle>(alloc);
alloc =
Pool<CoreAllocator<SharedStateHandle>, SharedStateHandle>::iterate(
alloc);
}
}
@@ -32,16 +33,17 @@ namespace snmalloc
static_assert(
SharedStateHandle::Options.CoreAllocIsPoolAllocated,
"Global statistics are available only for pool-allocated configurations");
auto alloc = Pool<CoreAllocator<SharedStateHandle>>::template iterate<
SharedStateHandle>();
auto alloc =
Pool<CoreAllocator<SharedStateHandle>, SharedStateHandle>::iterate();
while (alloc != nullptr)
{
auto stats = alloc->stats();
if (stats != nullptr)
stats->template print<decltype(alloc)>(o, dumpid, alloc->id());
alloc = Pool<CoreAllocator<SharedStateHandle>>::template iterate<
SharedStateHandle>(alloc);
alloc =
Pool<CoreAllocator<SharedStateHandle>, SharedStateHandle>::iterate(
alloc);
}
}
#else
@@ -64,7 +66,8 @@ namespace snmalloc
// allocators that are not currently in use by any thread.
// One atomic operation to extract the stack, another to restore it.
// Handling the message queue for each stack is non-atomic.
auto* first = Pool<CoreAllocator<SharedStateHandle>>::extract();
auto* first =
Pool<CoreAllocator<SharedStateHandle>, SharedStateHandle>::extract();
auto* alloc = first;
decltype(alloc) last;
@@ -74,10 +77,13 @@ namespace snmalloc
{
alloc->flush();
last = alloc;
alloc = Pool<CoreAllocator<SharedStateHandle>>::extract(alloc);
alloc =
Pool<CoreAllocator<SharedStateHandle>, SharedStateHandle>::extract(
alloc);
}
Pool<CoreAllocator<SharedStateHandle>>::restore(first, last);
Pool<CoreAllocator<SharedStateHandle>, SharedStateHandle>::restore(
first, last);
}
#endif
}
@@ -96,8 +102,8 @@ namespace snmalloc
"Global status is available only for pool-allocated configurations");
// This is a debugging function. It checks that all memory from all
// allocators has been freed.
auto* alloc = Pool<CoreAllocator<SharedStateHandle>>::template iterate<
SharedStateHandle>();
auto* alloc =
Pool<CoreAllocator<SharedStateHandle>, SharedStateHandle>::iterate();
# ifdef SNMALLOC_TRACING
std::cout << "debug check empty: first " << alloc << std::endl;
@@ -111,8 +117,8 @@ namespace snmalloc
std::cout << "debug_check_empty: Check all allocators!" << std::endl;
# endif
done = true;
alloc = Pool<CoreAllocator<SharedStateHandle>>::template iterate<
SharedStateHandle>();
alloc =
Pool<CoreAllocator<SharedStateHandle>, SharedStateHandle>::iterate();
okay = true;
while (alloc != nullptr)
@@ -134,8 +140,9 @@ namespace snmalloc
# ifdef SNMALLOC_TRACING
std::cout << "debug check empty: okay = " << okay << std::endl;
# endif
alloc = Pool<CoreAllocator<SharedStateHandle>>::template iterate<
SharedStateHandle>(alloc);
alloc =
Pool<CoreAllocator<SharedStateHandle>, SharedStateHandle>::iterate(
alloc);
}
}
@@ -148,13 +155,14 @@ namespace snmalloc
// Redo check so abort is on allocator with allocation left.
if (!okay)
{
alloc = Pool<CoreAllocator<SharedStateHandle>>::template iterate<
SharedStateHandle>();
alloc =
Pool<CoreAllocator<SharedStateHandle>, SharedStateHandle>::iterate();
while (alloc != nullptr)
{
alloc->debug_is_empty(nullptr);
alloc = Pool<CoreAllocator<SharedStateHandle>>::template iterate<
SharedStateHandle>(alloc);
alloc =
Pool<CoreAllocator<SharedStateHandle>, SharedStateHandle>::iterate(
alloc);
}
}
#else
@@ -168,8 +176,8 @@ namespace snmalloc
static_assert(
SharedStateHandle::Options.CoreAllocIsPoolAllocated,
"Global status is available only for pool-allocated configurations");
auto alloc = Pool<CoreAllocator<SharedStateHandle>>::template iterate<
SharedStateHandle>();
auto alloc =
Pool<CoreAllocator<SharedStateHandle>, SharedStateHandle>::iterate();
while (alloc != nullptr)
{
if (alloc->debug_is_in_use())
@@ -180,8 +188,9 @@ namespace snmalloc
}
count--;
}
alloc = Pool<CoreAllocator<SharedStateHandle>>::template iterate<
SharedStateHandle>(alloc);
alloc =
Pool<CoreAllocator<SharedStateHandle>, SharedStateHandle>::iterate(
alloc);
if (count != 0)
{

View File

@@ -380,8 +380,7 @@ namespace snmalloc
// Initialise the global allocator structures
ensure_init();
// Grab an allocator for this thread.
init(Pool<CoreAlloc>::template acquire<SharedStateHandle>(
&(this->local_cache)));
init(Pool<CoreAlloc, SharedStateHandle>::acquire(&(this->local_cache)));
}
// Return all state in the fast allocator and release the underlying
@@ -403,7 +402,7 @@ namespace snmalloc
// Return underlying allocator to the system.
if constexpr (SharedStateHandle::Options.CoreAllocOwnsLocalState)
{
Pool<CoreAlloc>::template release<SharedStateHandle>(core_alloc);
Pool<CoreAlloc, SharedStateHandle>::release(core_alloc);
}
// Set up thread local allocator to look like

View File

@@ -21,7 +21,7 @@ namespace snmalloc
template<class T>
class PoolState
{
template<typename TT>
template<typename TT, typename SharedStateHandle>
friend class Pool;
private:
@@ -55,11 +55,11 @@ namespace snmalloc
}
};
template<typename T>
template<typename T, typename SharedStateHandle>
class Pool
{
public:
template<typename SharedStateHandle, typename... Args>
template<typename... Args>
static T* acquire(Args&&... args)
{
PoolState<T>& pool = SharedStateHandle::pool();
@@ -93,7 +93,6 @@ namespace snmalloc
*
* Do not return objects from `extract`.
*/
template<typename SharedStateHandle>
static void release(T* p)
{
// The object's destructor is not run. If the object is "reallocated", it
@@ -103,7 +102,6 @@ namespace snmalloc
SharedStateHandle::pool().stack.push(p);
}
template<typename SharedStateHandle>
static T* extract(T* p = nullptr)
{
// Returns a linked list of all objects in the stack, emptying the stack.
@@ -118,7 +116,6 @@ namespace snmalloc
*
* Do not return objects from `acquire`.
*/
template<typename SharedStateHandle>
static void restore(T* first, T* last)
{
// Pushes a linked list of objects onto the stack. Use to put a linked
@@ -126,7 +123,6 @@ namespace snmalloc
SharedStateHandle::pool().stack.push(first, last);
}
template<typename SharedStateHandle>
static T* iterate(T* p = nullptr)
{
if (p == nullptr)

View File

@@ -8,7 +8,7 @@ namespace snmalloc
class Pooled
{
private:
template<class TT>
template<class TT, typename SharedStateHandle>
friend class Pool;
template<class a, Construction c>
friend class MPMCStack;