VirtualBox

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

Last change on this file since 24323 was 22052, checked in by vboxsync, 15 years ago

IPRT: RT_MORE_STRICT for r0rdv and r0drv/darwin.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.4 KB
Line 
1/* $Id: alloc-r0drv-darwin.cpp 22052 2009-08-07 09:45:48Z vboxsync $ */
2/** @file
3 * IPRT - Memory Allocation, Ring-0 Driver, Darwin.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#include "the-darwin-kernel.h"
36#include "internal/iprt.h"
37#include <iprt/mem.h>
38
39#include <iprt/assert.h>
40#include <iprt/thread.h>
41#include "r0drv/alloc-r0drv.h"
42
43
44/**
45 * OS specific allocation function.
46 */
47PRTMEMHDR rtMemAlloc(size_t cb, uint32_t fFlags)
48{
49 PRTMEMHDR pHdr = (PRTMEMHDR)IOMalloc(cb + sizeof(*pHdr));
50 if (pHdr)
51 {
52 pHdr->u32Magic = RTMEMHDR_MAGIC;
53 pHdr->fFlags = fFlags;
54 pHdr->cb = cb;
55 pHdr->cbReq = cb;
56 }
57 else
58 printf("rmMemAlloc(%#zx, %#x) failed\n", cb + sizeof(*pHdr), fFlags);
59 return pHdr;
60}
61
62
63/**
64 * OS specific free function.
65 */
66void rtMemFree(PRTMEMHDR pHdr)
67{
68 pHdr->u32Magic += 1;
69 IOFree(pHdr, pHdr->cb + sizeof(*pHdr));
70}
71
72
73RTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb)
74{
75 /*
76 * validate input.
77 */
78 AssertPtr(pPhys);
79 Assert(cb > 0);
80 RT_ASSERT_PREEMPTIBLE();
81
82 /*
83 * Allocate the memory and ensure that the API is still providing
84 * memory that's always below 4GB.
85 */
86 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
87 IOPhysicalAddress PhysAddr;
88 void *pv = IOMallocContiguous(cb, PAGE_SIZE, &PhysAddr);
89 if (pv)
90 {
91 if (PhysAddr + (cb - 1) <= (IOPhysicalAddress)0xffffffff)
92 {
93 if (!((uintptr_t)pv & PAGE_OFFSET_MASK))
94 {
95 *pPhys = PhysAddr;
96 return pv;
97 }
98 AssertMsgFailed(("IOMallocContiguous didn't return a page aligned address - %p!\n", pv));
99 }
100 else
101 AssertMsgFailed(("IOMallocContiguous returned high address! PhysAddr=%RX64 cb=%#zx\n", (uint64_t)PhysAddr, cb));
102 IOFreeContiguous(pv, cb);
103 }
104 return NULL;
105}
106
107
108RTR0DECL(void) RTMemContFree(void *pv, size_t cb)
109{
110 RT_ASSERT_PREEMPTIBLE();
111 if (pv)
112 {
113 Assert(cb > 0);
114 AssertMsg(!((uintptr_t)pv & PAGE_OFFSET_MASK), ("pv=%p\n", pv));
115
116 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
117 IOFreeContiguous(pv, cb);
118 }
119}
120
Note: See TracBrowser for help on using the repository browser.

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