// vim:fileencoding=utf-8:foldmethod=marker

#include "../../../common/aliases/aliases.h"
#include "../../../common/platform/platform.h"

#ifdef WAPP_PLATFORM_WINDOWS

#include "mem_os_win.h"
#include "../mem_os_ops.h"

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <memoryapi.h>

wapp_intern const i32 access_types[] = {
    [WAPP_MEM_ACCESS_NONE]            = PAGE_NOACCESS,
    [WAPP_MEM_ACCESS_READ_ONLY]       = PAGE_READONLY,
    [WAPP_MEM_ACCESS_EXEC_ONLY]       = PAGE_EXECUTE,
    [WAPP_MEM_ACCESS_READ_WRITE]      = PAGE_READWRITE,
    [WAPP_MEM_ACCESS_READ_EXEC]       = PAGE_EXECUTE_READ,
    [WAPP_MEM_ACCESS_READ_WRITE_EXEC] = PAGE_EXECUTE_READWRITE,
};

void *mem_util_allocate(void *addr, u64 size, MemAccess access, MemAllocFlags flags, MemInitType type) {
  // Ensure memory is committed if it's meant to be initialised
  if (type == WAPP_MEM_INIT_INITIALISED) {
    flags |= WAPP_MEM_ALLOC_COMMIT;
  }

  return VirtualAlloc(addr, (SIZE_T)size, flags, access_types[access]);
}

void mem_util_free(void *ptr, u64 size) {
  VirtualFree(ptr, size, MEM_RELEASE);
}

#endif // !WAPP_PLATFORM_WINDOWS
