summaryrefslogtreecommitdiff
path: root/src/libs/kStuff/iprt/kHlpPage-iprt.cpp
blob: a9a1c69bd542a586bdd4ed2954c509479f4c0815 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* $Id: kHlpPage-iprt.cpp $ */
/** @file
 * kHlpPage - Page Memory Allocation, IPRT based implementation.
 */

/*
 * Copyright (C) 2007-2010 Oracle Corporation
 *
 * This file is part of VirtualBox Open Source Edition (OSE), as
 * available from http://www.virtualbox.org. This file is free software;
 * you can redistribute it and/or modify it under the terms of the GNU
 * General Public License (GPL) as published by the Free Software
 * Foundation, in version 2 as it comes in the "COPYING" file of the
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
 */

/*******************************************************************************
*   Header Files                                                               *
*******************************************************************************/
#include <k/kHlpAlloc.h>
#include <k/kErrors.h>
#include <iprt/mem.h>
#include <iprt/assert.h>
#include <iprt/err.h>



static unsigned kHlpPageProtToNative(KPROT enmProt)
{
    switch (enmProt)
    {
        case KPROT_NOACCESS:             return RTMEM_PROT_NONE;
        case KPROT_READONLY:             return RTMEM_PROT_READ;
        case KPROT_READWRITE:            return RTMEM_PROT_READ | RTMEM_PROT_WRITE;
        case KPROT_EXECUTE:              return RTMEM_PROT_EXEC;
        case KPROT_EXECUTE_READ:         return RTMEM_PROT_EXEC | RTMEM_PROT_READ;
        case KPROT_EXECUTE_READWRITE:    return RTMEM_PROT_EXEC | RTMEM_PROT_READ | RTMEM_PROT_WRITE;
        default:
            AssertFailed();
            return ~0U;
    }
}


KHLP_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, cb);
    }
    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, cb);
    return 0;
}