kHlpPage-iprt.cpp revision 07ca5489e6fa49b415eb5e0e42567833699a9e57
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync/* $Id$ */
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync/** @file
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync * kHlpPage - Page Memory Allocation, IPRT based implementation.
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync */
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync/*
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync * Copyright (c) 2007 knut st. osmundsen <bird-kStuff-spam@anduin.net>
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync *
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync * available from http://www.virtualbox.org. This file is free software;
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync * you can redistribute it and/or modify it under the terms of the GNU
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync * General Public License as published by the Free Software Foundation,
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync * distribution. VirtualBox OSE is distributed in the hope that it will
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync * be useful, but WITHOUT ANY WARRANTY of any kind.
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync *
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync */
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync/*******************************************************************************
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync* Header Files *
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync*******************************************************************************/
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync#include <k/kHlpAlloc.h>
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync#include <k/kErrors.h>
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync#include <iprt/mem.h>
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync#include <iprt/assert.h>
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync#include <iprt/err.h>
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsyncstatic unsigned kHlpPageProtToNative(KPROT enmProt)
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync{
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync switch (enmProt)
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync {
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync case KPROT_NOACCESS: return RTMEM_PROT_NONE;
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync case KPROT_READONLY: return RTMEM_PROT_READ;
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync case KPROT_READWRITE: return RTMEM_PROT_READ | RTMEM_PROT_WRITE;
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync case KPROT_EXECUTE: return RTMEM_PROT_EXEC;
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync case KPROT_EXECUTE_READ: return RTMEM_PROT_EXEC | RTMEM_PROT_READ;
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync case KPROT_EXECUTE_READWRITE: return RTMEM_PROT_EXEC | RTMEM_PROT_READ | RTMEM_PROT_WRITE;
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync default:
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync AssertFailed();
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync return ~0U;
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync }
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync}
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsync
5e91fc5e5ea9cccb7a40636f73253d489fbe340bvboxsyncKHLP_DECL(int) kHlpPageAlloc(void **ppv, KSIZE cb, KPROT enmProt, KBOOL fFixed)
{
AssertReturn(fFixed, KERR_NOT_IMPLEMENTED);
int rc = VINF_SUCCESS;
void *pv = RTMemPageAlloc(cb);
if (pv)
{
rc = RTMemProtect(pv, cb, kHlpPageProtToNative(enmProt));
if (RT_SUCCESS(rc))
*ppv = pv;
else
RTMemPageFree(pv);
}
return rc;
}
KHLP_DECL(int) kHlpPageProtect(void *pv, KSIZE cb, KPROT enmProt)
{
int rc = RTMemProtect(pv, cb, kHlpPageProtToNative(enmProt));
if (!rc)
return 0;
/** @todo convert iprt status code -> kErrors */
return rc;
}
KHLP_DECL(int) kHlpPageFree(void *pv, KSIZE cb)
{
RTMemPageFree(pv);
return 0;
}