1
/* $Id: alloc-r0drv-darwin.cpp 4071 2007-08-07 17:07:59Z vboxsync $ */
3
* innotek Portable Runtime - Memory Allocation, Ring-0 Driver, Darwin.
7
* Copyright (C) 2006-2007 innotek GmbH
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.
19
/*******************************************************************************
21
*******************************************************************************/
22
#include "the-darwin-kernel.h"
24
#include <iprt/alloc.h>
25
#include <iprt/assert.h>
26
#include "r0drv/alloc-r0drv.h"
30
* OS specific allocation function.
32
PRTMEMHDR rtMemAlloc(size_t cb, uint32_t fFlags)
34
Assert(cb != sizeof(void *)); /* 99% of pointer sized allocations are wrong. */
35
PRTMEMHDR pHdr = (PRTMEMHDR)IOMalloc(cb + sizeof(*pHdr));
38
pHdr->u32Magic = RTMEMHDR_MAGIC;
39
pHdr->fFlags = fFlags;
44
printf("rmMemAlloc(%#x, %#x) failed\n", cb + sizeof(*pHdr), fFlags);
50
* OS specific free function.
52
void rtMemFree(PRTMEMHDR pHdr)
55
IOFree(pHdr, pHdr->cb + sizeof(*pHdr));
59
RTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb)
68
* Allocate the memory and ensure that the API is still providing
69
* memory that's always below 4GB.
71
cb = RT_ALIGN_Z(cb, PAGE_SIZE);
72
IOPhysicalAddress PhysAddr;
73
void *pv = IOMallocContiguous(cb, PAGE_SIZE, &PhysAddr);
76
if (PhysAddr + (cb - 1) <= (IOPhysicalAddress)0xffffffff)
78
if (!((uintptr_t)pv & PAGE_OFFSET_MASK))
83
AssertMsgFailed(("IOMallocContiguous didn't return a page aligned address - %p!\n", pv));
86
AssertMsgFailed(("IOMallocContiguous returned high address! PhysAddr=%RX64 cb=%#zx\n", (uint64_t)PhysAddr, cb));
87
IOFreeContiguous(pv, cb);
93
RTR0DECL(void) RTMemContFree(void *pv, size_t cb)
98
AssertMsg(!((uintptr_t)pv & PAGE_OFFSET_MASK), ("pv=%p\n", pv));
100
cb = RT_ALIGN_Z(cb, PAGE_SIZE);
101
IOFreeContiguous(pv, cb);