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

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

#ifdef WP_PLATFORM_WINDOWS

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

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

wp_intern const i32 access_types[] = {
	[WP_MEM_ACCESS_NONE]            = PAGE_NOACCESS,
	[WP_MEM_ACCESS_READ_ONLY]       = PAGE_READONLY,
	[WP_MEM_ACCESS_EXEC_ONLY]       = PAGE_EXECUTE,
	[WP_MEM_ACCESS_READ_WRITE]      = PAGE_READWRITE,
	[WP_MEM_ACCESS_READ_EXEC]       = PAGE_EXECUTE_READ,
	[WP_MEM_ACCESS_READ_WRITE_EXEC] = PAGE_EXECUTE_READWRITE,
};

void *_osMemAllocate(void *addr, u64 size, WpMemAccess access, WpMemAllocFlags flags, WpMemInitType type) {
	// Ensure memory is committed if it's meant to be initialised
	if (type == WP_MEM_INIT_INITIALISED) {
		flags |= WP_MEM_ALLOC_COMMIT;
	}

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

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

#endif // !WP_PLATFORM_WINDOWS
