#include "pls/internal/base/alignment.h"
#include "pls/internal/base/system_details.h"

namespace pls {
namespace internal {
namespace base {
namespace alignment {

void *allocate_aligned(size_t size) {
  return aligned_alloc(system_details::CACHE_LINE_SIZE, size);
}

system_details::pointer_t next_alignment(system_details::pointer_t size) {
  system_details::pointer_t miss_alignment = size % base::system_details::CACHE_LINE_SIZE;
  if (miss_alignment == 0) {
    return size;
  } else {
    return size + (base::system_details::CACHE_LINE_SIZE - miss_alignment);
  }
}

system_details::pointer_t previous_alignment(system_details::pointer_t size) {
  system_details::pointer_t miss_alignment = size % base::system_details::CACHE_LINE_SIZE;
  if (miss_alignment == 0) {
    return size;
  } else {
    return size - miss_alignment;
  }
}

char *next_alignment(char *pointer) {
  return reinterpret_cast<char *>(next_alignment(reinterpret_cast<system_details::pointer_t >(pointer)));
}

}
}
}
}
