aligned_stack.cpp 980 Bytes
Newer Older
1
#include "pls/internal/data_structures/aligned_stack.h"
FritzFlorian committed
2 3 4
#include "pls/internal/base/system_details.h"

namespace pls {
5 6 7
namespace internal {
namespace data_structures {

8
aligned_stack::aligned_stack(pointer_t memory_region, const std::size_t size) :
9 10 11 12
    memory_start_{memory_region},
    memory_end_{memory_region + size},
    head_{base::alignment::next_alignment(memory_start_)} {}

13 14 15 16 17
aligned_stack::aligned_stack(char *memory_region, const std::size_t size) :
    memory_start_{(pointer_t) memory_region},
    memory_end_{(pointer_t) memory_region + size},
    head_{base::alignment::next_alignment(memory_start_)} {}

18 19 20 21 22 23 24 25 26 27 28 29
void *aligned_stack::push_bytes(size_t size) {
  void *result = reinterpret_cast<void *>(head_);

  // Move head to next aligned position after new object
  head_ = base::alignment::next_alignment(head_ + size);
  if (head_ >= memory_end_) {
    PLS_ERROR("Tried to allocate object on alligned_stack without sufficient memory!");
  }

  return result;
}

30 31
}
}
FritzFlorian committed
32
}