Address review comments.

Move slow_allocator into a separate header in the snmalloc namespace and
rename it for consistency with the rest of the codebase.  Delete its
copy and move constructors / assignment operators.
This commit is contained in:
David Chisnall
2019-01-18 15:57:28 +00:00
parent e81dacc1c5
commit c3d46a9d8e
3 changed files with 75 additions and 46 deletions

68
src/mem/slowalloc.h Normal file
View File

@@ -0,0 +1,68 @@
#include "globalalloc.h"
namespace snmalloc
{
/**
* RAII wrapper around an `Alloc`. This class gets an allocator from the
* global pool and wraps it so that `Alloc` methods can be called
* directly via the `->` operator on this class. When this object is
* destroyed, it returns the allocator to the global pool.
*
* This does not depend on thread-local storage working, so can be used for
* bootstrapping.
*/
struct SlowAllocator
{
/**
* The allocator that this wrapper will use.
*/
Alloc* alloc;
/**
* Constructor. Claims an allocator from the global pool
*/
SlowAllocator() : alloc(current_alloc_pool()->acquire()) {}
/**
* Copying is not supported, it could easily lead to accidental sharing of
* allocators.
*/
SlowAllocator(const SlowAllocator&) = delete;
/**
* Moving is not supported, though it would be easy to add if there's a use
* case for it.
*/
SlowAllocator(SlowAllocator&&) = delete;
/**
* Copying is not supported, it could easily lead to accidental sharing of
* allocators.
*/
SlowAllocator& operator=(const SlowAllocator&) = delete;
/**
* Moving is not supported, though it would be easy to add if there's a use
* case for it.
*/
SlowAllocator& operator=(SlowAllocator&&) = delete;
/**
* Destructor. Returns the allocator to the pool.
*/
~SlowAllocator()
{
current_alloc_pool()->release(alloc);
}
/**
* Arrow operator, allows methods exposed by `Alloc` to be called on the
* wrapper.
*/
Alloc* operator->()
{
return alloc;
}
};
/**
* Returns a new slow allocator. When the `SlowAllocator` goes out of scope,
* the underlying `Alloc` will be returned to the pool.
*/
inline SlowAllocator get_slow_allocator()
{
return SlowAllocator{};
}
}

View File

@@ -1,3 +1,4 @@
#include "../mem/slowalloc.h"
#include "../snmalloc.h"
#include <errno.h>
@@ -12,46 +13,6 @@ using namespace snmalloc;
# define SNMALLOC_NAME_MANGLE(a) a
#endif
namespace
{
/**
* RAII wrapper around an `Alloc`. This class gets an allocator from the
* global pool and wraps it so that `Alloc` methods can be called
* directly via the `->` operator on this class. When this object is
* destroyed, it returns the allocator to the global pool.
*/
struct slow_allocator
{
/**
* The allocator that this wrapper will use.
*/
Alloc* alloc;
/**
* Constructor. Claims an allocator from the global pool
*/
slow_allocator() : alloc(current_alloc_pool()->acquire()) {}
/**
* Destructor. Returns the allocator to the pool.
*/
~slow_allocator()
{
current_alloc_pool()->release(alloc);
}
/**
* Arrow operator, allows methods exposed by `Alloc` to be called on the
* wrapper.
*/
Alloc* operator->()
{
return alloc;
}
};
slow_allocator bootstrap_alloc()
{
return slow_allocator{};
}
}
extern "C"
{
SNMALLOC_EXPORT void* SNMALLOC_NAME_MANGLE(__malloc_end_pointer)(void* ptr)
@@ -247,7 +208,7 @@ extern "C"
void* __je_bootstrap_malloc(size_t size)
{
return bootstrap_alloc()->alloc(size);
return get_slow_allocator()->alloc(size);
}
void* __je_bootstrap_calloc(size_t nmemb, size_t size)
{
@@ -260,11 +221,11 @@ extern "C"
}
// Include size 0 in the first sizeclass.
sz = ((sz - 1) >> (bits::BITS - 1)) + sz;
return bootstrap_alloc()->alloc<ZeroMem::YesZero>(sz);
return get_slow_allocator()->alloc<ZeroMem::YesZero>(sz);
}
void __je_bootstrap_free(void* ptr)
{
bootstrap_alloc()->dealloc(ptr);
get_slow_allocator()->dealloc(ptr);
}
#endif
}

View File

@@ -4,10 +4,10 @@
# include "../ds/bits.h"
# include "../mem/allocconfig.h"
# include <sys/mman.h>
# include <strings.h>
# include <stdio.h>
# include <pthread.h>
# include <stdio.h>
# include <strings.h>
# include <sys/mman.h>
namespace snmalloc
{