VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/nt/alloc-r0drv-nt.cpp@ 5999

Last change on this file since 5999 was 5999, checked in by vboxsync, 17 years ago

The Giant CDDL Dual-License Header Change.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.8 KB
Line 
1/* $Id: alloc-r0drv-nt.cpp 5999 2007-12-07 15:05:06Z vboxsync $ */
2/** @file
3 * innotek Portable Runtime - Memory Allocation, Ring-0 Driver, NT.
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek 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 (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-nt-kernel.h"
32
33#include <iprt/alloc.h>
34#include <iprt/assert.h>
35#include "r0drv/alloc-r0drv.h"
36
37
38/**
39 * OS specific allocation function.
40 */
41PRTMEMHDR rtMemAlloc(size_t cb, uint32_t fFlags)
42{
43 Assert(cb != sizeof(void *)); /* 99% of pointer sized allocations are wrong. */
44 PRTMEMHDR pHdr = (PRTMEMHDR)ExAllocatePoolWithTag(NonPagedPool, cb + sizeof(*pHdr), IPRT_NT_POOL_TAG);
45 if (pHdr)
46 {
47 pHdr->u32Magic = RTMEMHDR_MAGIC;
48 pHdr->fFlags = fFlags;
49 pHdr->cb = cb;
50 pHdr->u32Padding= 0;
51 }
52 return pHdr;
53}
54
55
56/**
57 * OS specific free function.
58 */
59void rtMemFree(PRTMEMHDR pHdr)
60{
61 pHdr->u32Magic += 1;
62 ExFreePool(pHdr);
63}
64
65
66/**
67 * Allocates physical contiguous memory (below 4GB).
68 * The allocation is page aligned and its contents is undefined.
69 *
70 * @returns Pointer to the memory block. This is page aligned.
71 * @param pPhys Where to store the physical address.
72 * @param cb The allocation size in bytes. This is always
73 * rounded up to PAGE_SIZE.
74 */
75RTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb)
76{
77 /*
78 * validate input.
79 */
80 Assert(VALID_PTR(pPhys));
81 Assert(cb > 0);
82
83 /*
84 * Allocate and get physical address.
85 * Make sure the return is page aligned.
86 */
87 PHYSICAL_ADDRESS MaxPhysAddr;
88 MaxPhysAddr.HighPart = 0;
89 MaxPhysAddr.LowPart = 0xffffffff;
90 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
91 void *pv = MmAllocateContiguousMemory(cb, MaxPhysAddr);
92 if (pv)
93 {
94 if (!((uintptr_t)pv & PAGE_OFFSET_MASK)) /* paranoia */
95 {
96 PHYSICAL_ADDRESS PhysAddr = MmGetPhysicalAddress(pv);
97 if (!PhysAddr.HighPart) /* paranoia */
98 {
99 *pPhys = (RTCCPHYS)PhysAddr.LowPart;
100 return pv;
101 }
102
103 /* failure */
104 AssertMsgFailed(("MMAllocContiguousMemory returned high address! PhysAddr=%RX64\n", (uint64_t)PhysAddr.QuadPart));
105 }
106 else
107 AssertMsgFailed(("MMAllocContiguousMemory didn't return a page aligned address - %p!\n", pv));
108
109 MmFreeContiguousMemory(pv);
110 }
111
112 return NULL;
113}
114
115
116/**
117 * Frees memory allocated ysing RTMemContAlloc().
118 *
119 * @param pv Pointer to return from RTMemContAlloc().
120 * @param cb The cb parameter passed to RTMemContAlloc().
121 */
122RTR0DECL(void) RTMemContFree(void *pv, size_t cb)
123{
124 if (pv)
125 {
126 Assert(cb > 0); NOREF(cb);
127 AssertMsg(!((uintptr_t)pv & PAGE_OFFSET_MASK), ("pv=%p\n", pv));
128 MmFreeContiguousMemory(pv);
129 }
130}
131
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