1 | /* $Id: alloc-r0drv-darwin.cpp 5999 2007-12-07 15:05:06Z 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 (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 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*******************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *******************************************************************************/
|
---|
31 | #include "the-darwin-kernel.h"
|
---|
32 |
|
---|
33 | #include <iprt/alloc.h>
|
---|
34 | #include <iprt/assert.h>
|
---|
35 | #include "r0drv/alloc-r0drv.h"
|
---|
36 |
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * OS specific allocation function.
|
---|
40 | */
|
---|
41 | PRTMEMHDR rtMemAlloc(size_t cb, uint32_t fFlags)
|
---|
42 | {
|
---|
43 | Assert(cb != sizeof(void *)); /* 99% of pointer sized allocations are wrong. */
|
---|
44 | PRTMEMHDR pHdr = (PRTMEMHDR)IOMalloc(cb + sizeof(*pHdr));
|
---|
45 | if (pHdr)
|
---|
46 | {
|
---|
47 | pHdr->u32Magic = RTMEMHDR_MAGIC;
|
---|
48 | pHdr->fFlags = fFlags;
|
---|
49 | pHdr->cb = cb;
|
---|
50 | pHdr->u32Padding= 0;
|
---|
51 | }
|
---|
52 | else
|
---|
53 | printf("rmMemAlloc(%#x, %#x) failed\n", cb + sizeof(*pHdr), fFlags);
|
---|
54 | return pHdr;
|
---|
55 | }
|
---|
56 |
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * OS specific free function.
|
---|
60 | */
|
---|
61 | void rtMemFree(PRTMEMHDR pHdr)
|
---|
62 | {
|
---|
63 | pHdr->u32Magic += 1;
|
---|
64 | IOFree(pHdr, pHdr->cb + sizeof(*pHdr));
|
---|
65 | }
|
---|
66 |
|
---|
67 |
|
---|
68 | RTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb)
|
---|
69 | {
|
---|
70 | /*
|
---|
71 | * validate input.
|
---|
72 | */
|
---|
73 | AssertPtr(pPhys);
|
---|
74 | Assert(cb > 0);
|
---|
75 |
|
---|
76 | /*
|
---|
77 | * Allocate the memory and ensure that the API is still providing
|
---|
78 | * memory that's always below 4GB.
|
---|
79 | */
|
---|
80 | cb = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
81 | IOPhysicalAddress PhysAddr;
|
---|
82 | void *pv = IOMallocContiguous(cb, PAGE_SIZE, &PhysAddr);
|
---|
83 | if (pv)
|
---|
84 | {
|
---|
85 | if (PhysAddr + (cb - 1) <= (IOPhysicalAddress)0xffffffff)
|
---|
86 | {
|
---|
87 | if (!((uintptr_t)pv & PAGE_OFFSET_MASK))
|
---|
88 | {
|
---|
89 | *pPhys = PhysAddr;
|
---|
90 | return pv;
|
---|
91 | }
|
---|
92 | AssertMsgFailed(("IOMallocContiguous didn't return a page aligned address - %p!\n", pv));
|
---|
93 | }
|
---|
94 | else
|
---|
95 | AssertMsgFailed(("IOMallocContiguous returned high address! PhysAddr=%RX64 cb=%#zx\n", (uint64_t)PhysAddr, cb));
|
---|
96 | IOFreeContiguous(pv, cb);
|
---|
97 | }
|
---|
98 | return NULL;
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 | RTR0DECL(void) RTMemContFree(void *pv, size_t cb)
|
---|
103 | {
|
---|
104 | if (pv)
|
---|
105 | {
|
---|
106 | Assert(cb > 0);
|
---|
107 | AssertMsg(!((uintptr_t)pv & PAGE_OFFSET_MASK), ("pv=%p\n", pv));
|
---|
108 |
|
---|
109 | cb = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
110 | IOFreeContiguous(pv, cb);
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|