VirtualBox

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

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

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.2 KB
Line 
1/* $Id: alloc-r0drv-darwin.cpp 28800 2010-04-27 08:22:32Z 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.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#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 PRTMEMHDR pHdr = (PRTMEMHDR)IOMalloc(cb + sizeof(*pHdr));
46 if (pHdr)
47 {
48 pHdr->u32Magic = RTMEMHDR_MAGIC;
49 pHdr->fFlags = fFlags;
50 pHdr->cb = cb;
51 pHdr->cbReq = cb;
52 }
53 else
54 printf("rmMemAlloc(%#zx, %#x) failed\n", cb + sizeof(*pHdr), fFlags);
55 return pHdr;
56}
57
58
59/**
60 * OS specific free function.
61 */
62void rtR0MemFree(PRTMEMHDR pHdr)
63{
64 pHdr->u32Magic += 1;
65 IOFree(pHdr, pHdr->cb + sizeof(*pHdr));
66}
67
68
69RTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb)
70{
71 /*
72 * validate input.
73 */
74 AssertPtr(pPhys);
75 Assert(cb > 0);
76 RT_ASSERT_PREEMPTIBLE();
77
78 /*
79 * Allocate the memory and ensure that the API is still providing
80 * memory that's always below 4GB.
81 */
82 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
83 IOPhysicalAddress PhysAddr;
84 void *pv = IOMallocContiguous(cb, PAGE_SIZE, &PhysAddr);
85 if (pv)
86 {
87 if (PhysAddr + (cb - 1) <= (IOPhysicalAddress)0xffffffff)
88 {
89 if (!((uintptr_t)pv & PAGE_OFFSET_MASK))
90 {
91 *pPhys = PhysAddr;
92 return pv;
93 }
94 AssertMsgFailed(("IOMallocContiguous didn't return a page aligned address - %p!\n", pv));
95 }
96 else
97 AssertMsgFailed(("IOMallocContiguous returned high address! PhysAddr=%RX64 cb=%#zx\n", (uint64_t)PhysAddr, cb));
98 IOFreeContiguous(pv, cb);
99 }
100 return NULL;
101}
102
103
104RTR0DECL(void) RTMemContFree(void *pv, size_t cb)
105{
106 RT_ASSERT_PREEMPTIBLE();
107 if (pv)
108 {
109 Assert(cb > 0);
110 AssertMsg(!((uintptr_t)pv & PAGE_OFFSET_MASK), ("pv=%p\n", pv));
111
112 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
113 IOFreeContiguous(pv, cb);
114 }
115}
116
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