VirtualBox

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

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

IPRT: started on some internal ring-0 alloc api.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.3 KB
Line 
1/* $Id: alloc-r0drv-darwin.cpp 32674 2010-09-21 16:51:50Z vboxsync $ */
2/** @file
3 * IPRT - Memory Allocation, Ring-0 Driver, Darwin.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. 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#include "internal/iprt.h"
33#include <iprt/mem.h>
34
35#include <iprt/assert.h>
36#include <iprt/thread.h>
37#include "r0drv/alloc-r0drv.h"
38
39
40/**
41 * OS specific allocation function.
42 */
43PRTMEMHDR rtR0MemAlloc(size_t cb, uint32_t fFlags)
44{
45 AssertReturn(!(fFlags & RTMEMHDR_FLAG_ANY_CTX), NULL);
46
47 PRTMEMHDR pHdr = (PRTMEMHDR)IOMalloc(cb + sizeof(*pHdr));
48 if (pHdr)
49 {
50 pHdr->u32Magic = RTMEMHDR_MAGIC;
51 pHdr->fFlags = fFlags;
52 pHdr->cb = cb;
53 pHdr->cbReq = cb;
54 }
55 else
56 printf("rmMemAlloc(%#zx, %#x) failed\n", cb + sizeof(*pHdr), fFlags);
57 return pHdr;
58}
59
60
61/**
62 * OS specific free function.
63 */
64void rtR0MemFree(PRTMEMHDR pHdr)
65{
66 pHdr->u32Magic += 1;
67 IOFree(pHdr, pHdr->cb + sizeof(*pHdr));
68}
69
70
71RTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb)
72{
73 /*
74 * validate input.
75 */
76 AssertPtr(pPhys);
77 Assert(cb > 0);
78 RT_ASSERT_PREEMPTIBLE();
79
80 /*
81 * Allocate the memory and ensure that the API is still providing
82 * memory that's always below 4GB.
83 */
84 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
85 IOPhysicalAddress PhysAddr;
86 void *pv = IOMallocContiguous(cb, PAGE_SIZE, &PhysAddr);
87 if (pv)
88 {
89 if (PhysAddr + (cb - 1) <= (IOPhysicalAddress)0xffffffff)
90 {
91 if (!((uintptr_t)pv & PAGE_OFFSET_MASK))
92 {
93 *pPhys = PhysAddr;
94 return pv;
95 }
96 AssertMsgFailed(("IOMallocContiguous didn't return a page aligned address - %p!\n", pv));
97 }
98 else
99 AssertMsgFailed(("IOMallocContiguous returned high address! PhysAddr=%RX64 cb=%#zx\n", (uint64_t)PhysAddr, cb));
100 IOFreeContiguous(pv, cb);
101 }
102 return NULL;
103}
104
105
106RTR0DECL(void) RTMemContFree(void *pv, size_t cb)
107{
108 RT_ASSERT_PREEMPTIBLE();
109 if (pv)
110 {
111 Assert(cb > 0);
112 AssertMsg(!((uintptr_t)pv & PAGE_OFFSET_MASK), ("pv=%p\n", pv));
113
114 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
115 IOFreeContiguous(pv, cb);
116 }
117}
118
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