VirtualBox

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

Last change on this file since 45438 was 44528, checked in by vboxsync, 12 years ago

header (C) fixes

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