Morning Musings

I'm not ready to wake up yet...

Aligned Heap

| Comments

In the previous post, I introduced an allocator framework that supports pluggable policies. An example heap policy was given. This post will expand on the heap to give it alignment abilities. You may want to refer to another previous post on alignment.

The code modifications are simple; allocate a little more data, align the pointer when allocating, then unalign it before deallocating:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
template<typename T,
       std::size_t align = 0>
class heap
{
public:

  ALLOCATOR_TRAITS(T)

  template<typename U>
  struct rebind
  {
      typedef heap<U, align> other;
  };

  // Default Constructor
  heap(void){}

  // Copy Constructor
  template<typename U>
  heap(heap<U, align> const& other){}

  // Allocate memory
  pointer allocate(size_type count, const_pointer hint = 0)
  {
      // Request additional memory to perform alignment
      void* ptr = ::operator new((count * sizeof(type)) + boundary<align>::value, ::std::nothrow);

      // Align the pointer
      return static_cast<pointer>(boundary<align>::align(ptr));
  }

  // Deallocate memory
  void deallocate(pointer ptr, size_type count)
  {
      // Unalign the pointer and delete the memory
      ::operator delete(boundary<align>::unalign(ptr));
  }

  // Max number of objects that can be allocated in one call
  size_type max_size(void) const {return max_allocations<T>::value;}
};

Using the aligned heap no different than the previous version. If you leave out the alignment template parameter, it will act the same as before.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <set>

struct Example
{
  int value;

  Example(int v) :
      value(v)
  {}

  bool operator<(Example const& other) const
  {
      return value < other.value;
  }
};

int main(int argc, char* argv[])
{
  // Increase scope
  {
     std::set<Example, std::less<Example>, allocator<Example, heap<Example, 16> > > foo;

      foo.insert(Example(3));
      foo.insert(Example(1));
      foo.insert(Example(4));
      foo.insert(Example(2));
  }
  // Leaving scope
}

Avatar

About The Author

Joe Ruether is the lead software engineer for the Multisensor Aircraft Tracking system at SRC Inc. He is an expert in C++ template metaprogramming and is interested in cryptography and open source software.


« C++ Allocators Google Test »

Comments