1 | /* $Id: alloc-r0drv-darwin.cpp 4071 2007-08-07 17:07:59Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * innotek Portable Runtime - Memory Allocation, Ring-0 Driver, Darwin.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek 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 |
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #include "the-darwin-kernel.h"
|
---|
23 |
|
---|
24 | #include <iprt/alloc.h>
|
---|
25 | #include <iprt/assert.h>
|
---|
26 | #include "r0drv/alloc-r0drv.h"
|
---|
27 |
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * OS specific allocation function.
|
---|
31 | */
|
---|
32 | PRTMEMHDR rtMemAlloc(size_t cb, uint32_t fFlags)
|
---|
33 | {
|
---|
34 | Assert(cb != sizeof(void *)); /* 99% of pointer sized allocations are wrong. */
|
---|
35 | PRTMEMHDR pHdr = (PRTMEMHDR)IOMalloc(cb + sizeof(*pHdr));
|
---|
36 | if (pHdr)
|
---|
37 | {
|
---|
38 | pHdr->u32Magic = RTMEMHDR_MAGIC;
|
---|
39 | pHdr->fFlags = fFlags;
|
---|
40 | pHdr->cb = cb;
|
---|
41 | pHdr->u32Padding= 0;
|
---|
42 | }
|
---|
43 | else
|
---|
44 | printf("rmMemAlloc(%#x, %#x) failed\n", cb + sizeof(*pHdr), fFlags);
|
---|
45 | return pHdr;
|
---|
46 | }
|
---|
47 |
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * OS specific free function.
|
---|
51 | */
|
---|
52 | void rtMemFree(PRTMEMHDR pHdr)
|
---|
53 | {
|
---|
54 | pHdr->u32Magic += 1;
|
---|
55 | IOFree(pHdr, pHdr->cb + sizeof(*pHdr));
|
---|
56 | }
|
---|
57 |
|
---|
58 |
|
---|
59 | RTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb)
|
---|
60 | {
|
---|
61 | /*
|
---|
62 | * validate input.
|
---|
63 | */
|
---|
64 | AssertPtr(pPhys);
|
---|
65 | Assert(cb > 0);
|
---|
66 |
|
---|
67 | /*
|
---|
68 | * Allocate the memory and ensure that the API is still providing
|
---|
69 | * memory that's always below 4GB.
|
---|
70 | */
|
---|
71 | cb = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
72 | IOPhysicalAddress PhysAddr;
|
---|
73 | void *pv = IOMallocContiguous(cb, PAGE_SIZE, &PhysAddr);
|
---|
74 | if (pv)
|
---|
75 | {
|
---|
76 | if (PhysAddr + (cb - 1) <= (IOPhysicalAddress)0xffffffff)
|
---|
77 | {
|
---|
78 | if (!((uintptr_t)pv & PAGE_OFFSET_MASK))
|
---|
79 | {
|
---|
80 | *pPhys = PhysAddr;
|
---|
81 | return pv;
|
---|
82 | }
|
---|
83 | AssertMsgFailed(("IOMallocContiguous didn't return a page aligned address - %p!\n", pv));
|
---|
84 | }
|
---|
85 | else
|
---|
86 | AssertMsgFailed(("IOMallocContiguous returned high address! PhysAddr=%RX64 cb=%#zx\n", (uint64_t)PhysAddr, cb));
|
---|
87 | IOFreeContiguous(pv, cb);
|
---|
88 | }
|
---|
89 | return NULL;
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 | RTR0DECL(void) RTMemContFree(void *pv, size_t cb)
|
---|
94 | {
|
---|
95 | if (pv)
|
---|
96 | {
|
---|
97 | Assert(cb > 0);
|
---|
98 | AssertMsg(!((uintptr_t)pv & PAGE_OFFSET_MASK), ("pv=%p\n", pv));
|
---|
99 |
|
---|
100 | cb = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
101 | IOFreeContiguous(pv, cb);
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|