VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/generic/allocex-r3-generic.cpp@ 46567

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

RTMemAllocEx for ring-3.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.5 KB
Line 
1/* $Id: allocex-r3-generic.cpp 46567 2013-06-14 16:12:24Z vboxsync $ */
2/** @file
3 * IPRT - Memory Allocation, Extended Alloc and Free Functions for Ring-3, generic.
4 */
5
6/*
7 * Copyright (C) 2006-2013 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#define RTMEM_NO_WRAP_TO_EF_APIS
32#include <iprt/mem.h>
33#include "internal/iprt.h"
34
35#include <iprt/assert.h>
36#include <iprt/string.h>
37#include "internal/magics.h"
38
39
40/*******************************************************************************
41* Structures and Typedefs *
42*******************************************************************************/
43/**
44 * Heading for extended memory allocations in ring-3.
45 */
46typedef struct RTMEMHDRR3
47{
48 /** Magic (RTMEMHDR_MAGIC). */
49 uint32_t u32Magic;
50 /** Block flags (RTMEMALLOCEX_FLAGS_*). */
51 uint32_t fFlags;
52 /** The actual size of the block, header not included. */
53 uint32_t cb;
54 /** The requested allocation size. */
55 uint32_t cbReq;
56} RTMEMHDRR3;
57/** Pointer to a ring-3 extended memory header. */
58typedef RTMEMHDRR3 *PRTMEMHDRR3;
59
60
61
62RTDECL(int) RTMemAllocExTag(size_t cb, size_t cbAlignment, uint32_t fFlags, const char *pszTag, void **ppv) RT_NO_THROW
63{
64 /*
65 * Validate and adjust input.
66 */
67 AssertMsgReturn(!(fFlags & ~RTMEMALLOCEX_FLAGS_VALID_MASK), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
68 AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
69 AssertReturn(RT_IS_POWER_OF_TWO(cbAlignment), VERR_INVALID_PARAMETER);
70 AssertMsgReturn(cbAlignment <= sizeof(void *), ("%zu (%#x)\n", cbAlignment, cbAlignment), VERR_UNSUPPORTED_ALIGNMENT);
71
72 if (fFlags & (RTMEMALLOCEX_FLAGS_16BIT_REACH | RTMEMALLOCEX_FLAGS_32BIT_REACH | RTMEMALLOCEX_FLAGS_ANY_CTX))
73 return VERR_NOT_SUPPORTED;
74
75 /*
76 * Align the request.
77 */
78 size_t cbAligned = cb;
79 if (cbAlignment)
80 cbAligned = RT_ALIGN_Z(cb, cbAlignment);
81 else
82 cbAligned = RT_ALIGN_Z(cb, sizeof(uint64_t));
83 AssertMsgReturn(cbAligned >= cb && cbAligned <= ~(size_t)0 / 2U, ("cbAligned=%#zx cb=%#zx", cbAligned, cb),
84 VERR_INVALID_PARAMETER);
85
86 /*
87 * Allocate the requested memory.
88 */
89 void *pv;
90 if (fFlags & RTMEMALLOCEX_FLAGS_EXEC)
91 {
92 pv = RTMemExecAlloc(cbAligned + sizeof(RTMEMHDRR3));
93 if ((fFlags & RTMEMALLOCEX_FLAGS_ZEROED) && pv)
94 RT_BZERO(pv, cbAligned + sizeof(RTMEMHDRR3));
95 }
96 else if (fFlags & RTMEMALLOCEX_FLAGS_ZEROED)
97 pv = RTMemAllocZ(cbAligned + sizeof(RTMEMHDRR3));
98 else
99 pv = RTMemAlloc(cbAligned + sizeof(RTMEMHDRR3));
100 if (!pv)
101 return VERR_NO_MEMORY;
102
103 /*
104 * Fill in the header and return.
105 */
106 PRTMEMHDRR3 pHdr = (PRTMEMHDRR3)pv;
107 pHdr->u32Magic = RTMEMHDR_MAGIC;
108 pHdr->fFlags = fFlags;
109 pHdr->cb = cbAligned;
110 pHdr->cbReq = cb;
111
112 *ppv = pHdr + 1;
113 return VINF_SUCCESS;
114}
115RT_EXPORT_SYMBOL(RTMemAllocExTag);
116
117
118RTDECL(void) RTMemFreeEx(void *pv, size_t cb) RT_NO_THROW
119{
120 if (!pv)
121 return;
122 AssertPtr(pv);
123
124 PRTMEMHDRR3 pHdr = (PRTMEMHDRR3)pv - 1;
125 AssertMsg(pHdr->u32Magic == RTMEMHDR_MAGIC, ("pHdr->u32Magic=%RX32 pv=%p cb=%#x\n", pHdr->u32Magic, pv, cb));
126 pHdr->u32Magic = RTMEMHDR_MAGIC_DEAD;
127 Assert(pHdr->cbReq == cb);
128
129 if (pHdr->fFlags & RTMEMALLOCEX_FLAGS_EXEC)
130 RTMemExecFree(pHdr, pHdr->cb + sizeof(*pHdr));
131 else
132 RTMemFree(pHdr);
133}
134RT_EXPORT_SYMBOL(RTMemFreeEx);
135
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