VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/allocex.cpp@ 58029

Last change on this file since 58029 was 57432, checked in by vboxsync, 9 years ago

iprt/cdefs.h,*: Split RT_NO_THROW into prototype and definition macros named RT_NO_THROW_PROTO and RT_NO_THROW_DEF respecitively.

  • 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.cpp 57432 2015-08-18 14:57:46Z vboxsync $ */
2/** @file
3 * IPRT - Memory Allocation, Extended Alloc and Free Functions for Ring-3, posix.
4 */
5
6/*
7 * Copyright (C) 2006-2015 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#include "allocex.h"
39
40
41RTDECL(int) RTMemAllocExTag(size_t cb, size_t cbAlignment, uint32_t fFlags, const char *pszTag, void **ppv) RT_NO_THROW_DEF
42{
43 /*
44 * Validate and adjust input.
45 */
46 AssertMsgReturn(!(fFlags & ~RTMEMALLOCEX_FLAGS_VALID_MASK), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
47 AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
48 AssertReturn(RT_IS_POWER_OF_TWO(cbAlignment), VERR_INVALID_PARAMETER);
49 AssertMsgReturn(cbAlignment <= sizeof(void *), ("%zu (%#x)\n", cbAlignment, cbAlignment), VERR_UNSUPPORTED_ALIGNMENT);
50
51 if (fFlags & RTMEMALLOCEX_FLAGS_ANY_CTX)
52 return VERR_NOT_SUPPORTED;
53
54 /*
55 * Align the request.
56 */
57 size_t cbAligned = cb;
58 if (cbAlignment)
59 cbAligned = RT_ALIGN_Z(cb, cbAlignment);
60 else
61 cbAligned = RT_ALIGN_Z(cb, sizeof(uint64_t));
62 AssertMsgReturn(cbAligned >= cb && cbAligned <= ~(size_t)0, ("cbAligned=%#zx cb=%#zx", cbAligned, cb),
63 VERR_INVALID_PARAMETER);
64
65 /*
66 * Allocate the requested memory.
67 */
68 void *pv;
69 if (fFlags & (RTMEMALLOCEX_FLAGS_16BIT_REACH | RTMEMALLOCEX_FLAGS_32BIT_REACH))
70 {
71 int rc;
72 if (fFlags & RTMEMALLOCEX_FLAGS_16BIT_REACH)
73 rc = rtMemAllocEx16BitReach(cbAligned + sizeof(RTMEMHDRR3), fFlags, &pv);
74 else
75 rc = rtMemAllocEx32BitReach(cbAligned + sizeof(RTMEMHDRR3), fFlags, &pv);
76 if (RT_FAILURE(rc))
77 return rc;
78 }
79 else if (fFlags & RTMEMALLOCEX_FLAGS_EXEC)
80 {
81 pv = RTMemExecAlloc(cbAligned + sizeof(RTMEMHDRR3));
82 if ((fFlags & RTMEMALLOCEX_FLAGS_ZEROED) && pv)
83 RT_BZERO(pv, cbAligned + sizeof(RTMEMHDRR3));
84 }
85 else if (fFlags & RTMEMALLOCEX_FLAGS_ZEROED)
86 pv = RTMemAllocZ(cbAligned + sizeof(RTMEMHDRR3));
87 else
88 pv = RTMemAlloc(cbAligned + sizeof(RTMEMHDRR3));
89 if (!pv)
90 return VERR_NO_MEMORY;
91
92 /*
93 * Fill in the header and return.
94 */
95 PRTMEMHDRR3 pHdr = (PRTMEMHDRR3)pv;
96 pHdr->u32Magic = RTMEMHDR_MAGIC;
97 pHdr->fFlags = fFlags;
98 pHdr->cb = (uint32_t)cbAligned;
99 pHdr->cbReq = (uint32_t)cb;
100
101 *ppv = pHdr + 1;
102 return VINF_SUCCESS;
103}
104RT_EXPORT_SYMBOL(RTMemAllocExTag);
105
106
107RTDECL(void) RTMemFreeEx(void *pv, size_t cb) RT_NO_THROW_DEF
108{
109 if (!pv)
110 return;
111 AssertPtr(pv);
112
113 PRTMEMHDRR3 pHdr = (PRTMEMHDRR3)pv - 1;
114 AssertMsg(pHdr->u32Magic == RTMEMHDR_MAGIC, ("pHdr->u32Magic=%RX32 pv=%p cb=%#x\n", pHdr->u32Magic, pv, cb));
115 pHdr->u32Magic = RTMEMHDR_MAGIC_DEAD;
116 Assert(pHdr->cbReq == cb);
117
118 if (pHdr->fFlags & (RTMEMALLOCEX_FLAGS_16BIT_REACH | RTMEMALLOCEX_FLAGS_32BIT_REACH))
119 rtMemFreeExYyBitReach(pHdr, pHdr->cb + sizeof(*pHdr), pHdr->fFlags);
120 else if (pHdr->fFlags & RTMEMALLOCEX_FLAGS_EXEC)
121 RTMemExecFree(pHdr, pHdr->cb + sizeof(*pHdr));
122 else
123 RTMemFree(pHdr);
124}
125RT_EXPORT_SYMBOL(RTMemFreeEx);
126
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