1 | /* $Id: alloc-r0drv-darwin.cpp 395 2007-01-28 00:03:10Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * InnoTek Portable Runtime - Memory Allocation, Ring-0 Driver, Darwin.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006 InnoTek Systemberatung GmbH
|
---|
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 as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * If you received this file as part of a commercial VirtualBox
|
---|
18 | * distribution, then only the terms of your commercial VirtualBox
|
---|
19 | * license agreement apply instead of the previous paragraph.
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 | #include "the-darwin-kernel.h"
|
---|
27 |
|
---|
28 | #include <iprt/alloc.h>
|
---|
29 | #include <iprt/assert.h>
|
---|
30 | #include "r0drv/alloc-r0drv.h"
|
---|
31 |
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * OS specific allocation function.
|
---|
35 | */
|
---|
36 | PRTMEMHDR rtMemAlloc(size_t cb, uint32_t fFlags)
|
---|
37 | {
|
---|
38 | Assert(cb != sizeof(void *)); /* 99% of pointer sized allocations are wrong. */
|
---|
39 | PRTMEMHDR pHdr = (PRTMEMHDR)IOMalloc(cb + sizeof(*pHdr));
|
---|
40 | if (pHdr)
|
---|
41 | {
|
---|
42 | pHdr->u32Magic = RTMEMHDR_MAGIC;
|
---|
43 | pHdr->fFlags = fFlags;
|
---|
44 | pHdr->cb = cb;
|
---|
45 | pHdr->u32Padding= 0;
|
---|
46 | }
|
---|
47 | else
|
---|
48 | printf("rmMemAlloc(%#x, %#x) failed\n", cb + sizeof(*pHdr), fFlags);
|
---|
49 | return pHdr;
|
---|
50 | }
|
---|
51 |
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * OS specific free function.
|
---|
55 | */
|
---|
56 | void rtMemFree(PRTMEMHDR pHdr)
|
---|
57 | {
|
---|
58 | pHdr->u32Magic += 1;
|
---|
59 | IOFree(pHdr, pHdr->cb + sizeof(*pHdr));
|
---|
60 | }
|
---|
61 |
|
---|
62 |
|
---|
63 | RTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb)
|
---|
64 | {
|
---|
65 | /*
|
---|
66 | * validate input.
|
---|
67 | */
|
---|
68 | AssertPtr(pPhys);
|
---|
69 | Assert(cb > 0);
|
---|
70 |
|
---|
71 | /*
|
---|
72 | * Allocate the memory and ensure that the API is still providing
|
---|
73 | * memory that's always below 4GB.
|
---|
74 | */
|
---|
75 | cb = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
76 | IOPhysicalAddress PhysAddr;
|
---|
77 | void *pv = IOMallocContiguous(cb, PAGE_SIZE, &PhysAddr);
|
---|
78 | if (pv)
|
---|
79 | {
|
---|
80 | if (PhysAddr + (cb - 1) <= (IOPhysicalAddress)0xffffffff)
|
---|
81 | {
|
---|
82 | if (!((uintptr_t)pv & PAGE_OFFSET_MASK))
|
---|
83 | {
|
---|
84 | *pPhys = PhysAddr;
|
---|
85 | return pv;
|
---|
86 | }
|
---|
87 | AssertMsgFailed(("IOMallocContiguous didn't return a page aligned address - %p!\n", pv));
|
---|
88 | }
|
---|
89 | else
|
---|
90 | AssertMsgFailed(("IOMallocContiguous returned high address! PhysAddr=%RX64 cb=%#zx\n", (uint64_t)PhysAddr, cb));
|
---|
91 | IOFreeContiguous(pv, cb);
|
---|
92 | }
|
---|
93 | return NULL;
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | RTR0DECL(void) RTMemContFree(void *pv, size_t cb)
|
---|
98 | {
|
---|
99 | if (pv)
|
---|
100 | {
|
---|
101 | Assert(cb > 0);
|
---|
102 | AssertMsg(!((uintptr_t)pv & PAGE_OFFSET_MASK), ("pv=%p\n", pv));
|
---|
103 |
|
---|
104 | cb = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
105 | IOFreeContiguous(pv, cb);
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|