Skip to content

Commit

Permalink
Fixing wrapper_heap alignment problems
Browse files Browse the repository at this point in the history
This patch ensures that the addresses returned from the wrapper_heap are
correctly aligned for the underlying objects.
  • Loading branch information
Thomas Heller committed Nov 15, 2017
1 parent 0982398 commit 075dfaf
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 79 deletions.
17 changes: 8 additions & 9 deletions hpx/runtime/components/server/wrapper_heap.hpp
Expand Up @@ -76,6 +76,7 @@ namespace hpx { namespace components { namespace detail
typedef one_size_heap_allocators::fixed_mallocator allocator_type;
typedef hpx::lcos::local::spinlock mutex_type;
typedef std::unique_lock<mutex_type> scoped_lock;
typedef wrapper_heap_base::heap_parameters heap_parameters;

#if HPX_DEBUG_WRAPPER_HEAP != 0
enum guard_value
Expand All @@ -87,7 +88,7 @@ namespace hpx { namespace components { namespace detail

public:
explicit wrapper_heap(char const* class_name, std::size_t count,
std::size_t heap_size, std::size_t step);
heap_parameters parameters);

wrapper_heap();
~wrapper_heap() override;
Expand Down Expand Up @@ -120,10 +121,9 @@ namespace hpx { namespace components { namespace detail
void tidy();

protected:
void* pool_;
void* first_free_;
std::size_t step_;
std::size_t size_;
char* pool_;
char* first_free_;
heap_parameters const parameters_;
std::size_t free_size_;

// these values are used for AGAS registration of all elements of this
Expand All @@ -140,8 +140,6 @@ namespace hpx { namespace components { namespace detail
std::size_t heap_count_;
#endif

std::size_t const element_size_; // size of one element in the heap

// make sure the ABI of this is stable across configurations
#if defined(HPX_DEBUG)
std::size_t heap_count() const override { return heap_count_; }
Expand All @@ -161,13 +159,14 @@ namespace hpx { namespace components { namespace detail
{
private:
typedef wrapper_heap base_type;
typedef base_type::heap_parameters heap_parameters;

public:
typedef T value_type;

explicit fixed_wrapper_heap(char const* class_name,
std::size_t count, std::size_t step, std::size_t element_size)
: base_type(class_name, count, step, element_size)
std::size_t count, heap_parameters parameters)
: base_type(class_name, count, parameters)
{}
};
}}} // namespace hpx::components::detail
Expand Down
17 changes: 11 additions & 6 deletions hpx/runtime/components/server/wrapper_heap_list.hpp
Expand Up @@ -23,22 +23,27 @@ namespace hpx { namespace components { namespace detail
class wrapper_heap_list : public util::one_size_heap_list
{
typedef util::one_size_heap_list base_type;
typedef typename Heap::value_type value_ype;
typedef typename Heap::value_type value_type;

typedef typename std::aligned_storage<
sizeof(value_ype), std::alignment_of<value_ype>::value
sizeof(value_type), std::alignment_of<value_type>::value
>::type storage_type;

enum
{
heap_step = 0xFFF, // default initial number of elements
heap_size = sizeof(storage_type) // size of one element in the heap
// default initial number of elements
heap_capacity = 0xFFF,
// Alignment of one element
heap_element_alignment = std::alignment_of<value_type>::value,
// size of one element in the heap
heap_element_size = sizeof(storage_type)
};

public:
wrapper_heap_list(component_type type)
: base_type(get_component_type_name(type), heap_step, heap_size,
(Heap*) nullptr)
: base_type(get_component_type_name(type),
base_type::heap_parameters{heap_capacity, heap_element_alignment,
heap_element_size}, (Heap*) nullptr)
, type_(type)
{}

Expand Down
27 changes: 12 additions & 15 deletions hpx/util/one_size_heap_list.hpp
Expand Up @@ -34,16 +34,17 @@ namespace hpx { namespace util

typedef std::unique_lock<mutex_type> unique_lock_type;

typedef wrapper_heap_base::heap_parameters heap_parameters;

private:
template <typename Heap>
static std::shared_ptr<util::wrapper_heap_base> create_heap(
char const* name, std::size_t counter, std::size_t step,
std::size_t heap_size)
char const* name, std::size_t counter, heap_parameters parameters)
{
#if defined(HPX_DEBUG)
return std::make_shared<Heap>(name, counter, step, heap_size);
return std::make_shared<Heap>(name, counter, parameters);
#else
return std::make_shared<Heap>(name, 0, step, heap_size);
return std::make_shared<Heap>(name, 0, parameters);
#endif
}

Expand All @@ -57,15 +58,14 @@ namespace hpx { namespace util
, max_alloc_count_(0)
#endif
, create_heap_(nullptr)
, heap_step_(0)
, heap_size_(0)
, parameters_({0, 0, 0})
{
HPX_ASSERT(false); // shouldn't ever be called
}

template <typename Heap>
explicit one_size_heap_list(char const* class_name,
std::size_t heap_step, std::size_t heap_size, Heap* = nullptr)
heap_parameters parameters, Heap* = nullptr)
: class_name_(class_name)
#if defined(HPX_DEBUG)
, alloc_count_(0L)
Expand All @@ -74,13 +74,12 @@ namespace hpx { namespace util
, max_alloc_count_(0L)
#endif
, create_heap_(&one_size_heap_list::create_heap<Heap>)
, heap_step_(heap_step)
, heap_size_(heap_size)
, parameters_(parameters)
{}

template <typename Heap>
explicit one_size_heap_list(std::string const& class_name,
std::size_t heap_step, std::size_t heap_size, Heap* = nullptr)
heap_parameters parameters, Heap* = nullptr)
: class_name_(class_name)
#if defined(HPX_DEBUG)
, alloc_count_(0L)
Expand All @@ -89,8 +88,7 @@ namespace hpx { namespace util
, max_alloc_count_(0L)
#endif
, create_heap_(&one_size_heap_list::create_heap<Heap>)
, heap_step_(heap_step)
, heap_size_(heap_size)
, parameters_(parameters)
{}

~one_size_heap_list() noexcept;
Expand Down Expand Up @@ -122,10 +120,9 @@ namespace hpx { namespace util
std::size_t max_alloc_count_;
#endif
std::shared_ptr<util::wrapper_heap_base> (*create_heap_)(
char const*, std::size_t, std::size_t, std::size_t);
char const*, std::size_t, heap_parameters);

std::size_t const heap_step_; // default grow step
std::size_t const heap_size_; // size of the object
heap_parameters const parameters_;
};
}}

Expand Down
7 changes: 7 additions & 0 deletions hpx/util/wrapper_heap_base.hpp
Expand Up @@ -17,6 +17,13 @@ namespace hpx { namespace util
{
struct wrapper_heap_base
{
struct heap_parameters
{
std::size_t capacity;
std::size_t element_alignment;
std::size_t element_size;
};

virtual ~wrapper_heap_base() {}

virtual bool alloc(void** result, std::size_t count = 1) = 0;
Expand Down

0 comments on commit 075dfaf

Please sign in to comment.