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

#include <unistd.h>
#include <sys/mman.h>

namespace pls::internal::base::system_details {

size_t get_page_size() {
  return sysconf(_SC_PAGESIZE);
}

void *memory_map_range(size_t size) {
  PLS_ASSERT(size % get_page_size() == 0, "Must only map memory regions in page_size chunks.");
  return mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
}

int memory_unmap_range(void *addr, size_t size) {
  PLS_ASSERT((pointer_t) addr % get_page_size() == 0, "Must only unmap memory page_size aligned memory regions.");
  PLS_ASSERT(size % get_page_size() == 0, "Must only map memory regions in page_size chunks.");

  return munmap(addr, size);
}

int memory_protect_range(void *addr, size_t size) {
  return mprotect(addr, size, PROT_NONE);
}

}
