#include <test_helper.h>

struct mm *testmm;

void *alloc_frame(size_t bytes, size_t alignment,
                        struct capref *ram_cap_ret, struct capref *frame_cap_ret) {
  struct capref ram_cap;
  if (ram_cap_ret == NULL) ram_cap_ret = &ram_cap;
  struct capref frame_cap;
  if (frame_cap_ret == NULL) frame_cap_ret = &frame_cap;

  assert(bytes == ROUND_UP(bytes, BASE_PAGE_SIZE));
  CHECK_ERR(mm_alloc_aligned(testmm, bytes, alignment, ram_cap_ret));

  CHECK_ERR(slot_alloc(frame_cap_ret));
  CHECK_ERR(cap_retype(*frame_cap_ret, *ram_cap_ret, 0, ObjType_Frame, bytes, 1));

  struct paging_state *paging_state = get_current_paging_state();
  void *vaddr;
  CHECK_ERR(paging_map_frame_attr(
      paging_state, &vaddr,
      bytes, *frame_cap_ret, VREGION_FLAGS_READ_WRITE
  ));
  return vaddr;
}
