VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/darwin/alloc-r0drv-darwin.cpp@ 207

Last change on this file since 207 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:keywords set to Id
File size: 2.9 KB
Line 
1/* $Id: alloc-r0drv-darwin.cpp 1 1970-01-01 00:00:00Z 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 */
36PRTMEMHDR 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 return pHdr;
48}
49
50
51/**
52 * OS specific free function.
53 */
54void rtMemFree(PRTMEMHDR pHdr)
55{
56 pHdr->u32Magic += 1;
57 IOFree(pHdr, pHdr->cb + sizeof(*pHdr));
58}
59
60
61RTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb)
62{
63 /*
64 * validate input.
65 */
66 AssertPtr(pPhys);
67 Assert(cb > 0);
68
69 /*
70 * Allocate the memory and ensure that the API is still providing
71 * memory that's always below 4GB.
72 */
73 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
74 IOPhysicalAddress PhysAddr;
75 void *pv = IOMallocContiguous(cb, PAGE_SIZE, &PhysAddr);
76 if (pv)
77 {
78 if (PhysAddr + (cb - 1) <= (IOPhysicalAddress)0xffffffff)
79 {
80 if (!((uintptr_t)pv & PAGE_OFFSET_MASK))
81 {
82 *pPhys = PhysAddr;
83 return pv;
84 }
85 AssertMsgFailed(("IOMallocContiguous didn't return a page aligned address - %p!\n", pv));
86 }
87 else
88 AssertMsgFailed(("IOMallocContiguous returned high address! PhysAddr=%RX64 cb=%#zx\n", (uint64_t)PhysAddr, cb));
89 IOFreeContiguous(pv, cb);
90 }
91 return NULL;
92}
93
94
95RTR0DECL(void) RTMemContFree(void *pv, size_t cb)
96{
97 if (pv)
98 {
99 Assert(cb > 0);
100 AssertMsg(!((uintptr_t)pv & PAGE_OFFSET_MASK), ("pv=%p\n", pv));
101
102 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
103 IOFreeContiguous(pv, cb);
104 }
105}
106
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette