#include "pls/internal/data_structures/aligned_stack.h"
#include "pls/internal/base/system_details.h"

namespace pls {
namespace internal {
namespace data_structures {

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

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_)} {}

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;
}

}
}
}
