1 | /* $Id: kHlpPage-iprt.cpp 8155 2008-04-18 15:16:47Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * kHlpPage - Page Memory Allocation, IPRT based implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 | /*******************************************************************************
|
---|
23 | * Header Files *
|
---|
24 | *******************************************************************************/
|
---|
25 | #include <k/kHlpAlloc.h>
|
---|
26 | #include <k/kErrors.h>
|
---|
27 | #include <iprt/mem.h>
|
---|
28 | #include <iprt/assert.h>
|
---|
29 | #include <iprt/err.h>
|
---|
30 |
|
---|
31 |
|
---|
32 |
|
---|
33 | static unsigned kHlpPageProtToNative(KPROT enmProt)
|
---|
34 | {
|
---|
35 | switch (enmProt)
|
---|
36 | {
|
---|
37 | case KPROT_NOACCESS: return RTMEM_PROT_NONE;
|
---|
38 | case KPROT_READONLY: return RTMEM_PROT_READ;
|
---|
39 | case KPROT_READWRITE: return RTMEM_PROT_READ | RTMEM_PROT_WRITE;
|
---|
40 | case KPROT_EXECUTE: return RTMEM_PROT_EXEC;
|
---|
41 | case KPROT_EXECUTE_READ: return RTMEM_PROT_EXEC | RTMEM_PROT_READ;
|
---|
42 | case KPROT_EXECUTE_READWRITE: return RTMEM_PROT_EXEC | RTMEM_PROT_READ | RTMEM_PROT_WRITE;
|
---|
43 | default:
|
---|
44 | AssertFailed();
|
---|
45 | return ~0U;
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 |
|
---|
50 | KHLP_DECL(int) kHlpPageAlloc(void **ppv, KSIZE cb, KPROT enmProt, KBOOL fFixed)
|
---|
51 | {
|
---|
52 | AssertReturn(fFixed, KERR_NOT_IMPLEMENTED);
|
---|
53 |
|
---|
54 | int rc = VINF_SUCCESS;
|
---|
55 | void *pv = RTMemPageAlloc(cb);
|
---|
56 | if (pv)
|
---|
57 | {
|
---|
58 | rc = RTMemProtect(pv, cb, kHlpPageProtToNative(enmProt));
|
---|
59 | if (RT_SUCCESS(rc))
|
---|
60 | *ppv = pv;
|
---|
61 | else
|
---|
62 | RTMemPageFree(pv);
|
---|
63 | }
|
---|
64 | return rc;
|
---|
65 | }
|
---|
66 |
|
---|
67 |
|
---|
68 | KHLP_DECL(int) kHlpPageProtect(void *pv, KSIZE cb, KPROT enmProt)
|
---|
69 | {
|
---|
70 | int rc = RTMemProtect(pv, cb, kHlpPageProtToNative(enmProt));
|
---|
71 | if (!rc)
|
---|
72 | return 0;
|
---|
73 | /** @todo convert iprt status code -> kErrors */
|
---|
74 | return rc;
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | KHLP_DECL(int) kHlpPageFree(void *pv, KSIZE cb)
|
---|
79 | {
|
---|
80 | RTMemPageFree(pv);
|
---|
81 | return 0;
|
---|
82 | }
|
---|
83 |
|
---|