VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/posix/rtmempage-exec-mmap-posix.cpp@ 73531

Last change on this file since 73531 was 69111, checked in by vboxsync, 7 years ago

(C) year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.8 KB
Line 
1/* $Id: rtmempage-exec-mmap-posix.cpp 69111 2017-10-17 14:26:02Z vboxsync $ */
2/** @file
3 * IPRT - RTMemPage*, POSIX with mmap only.
4 */
5
6/*
7 * Copyright (C) 2006-2017 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 "internal/iprt.h"
32#include <iprt/mem.h>
33
34#include <iprt/asm.h>
35#include <iprt/assert.h>
36#include <iprt/err.h>
37#include <iprt/param.h>
38#include <iprt/string.h>
39
40#include <stdlib.h>
41#include <errno.h>
42#include <sys/mman.h>
43#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
44# define MAP_ANONYMOUS MAP_ANON
45#endif
46
47
48/**
49 * Allocates memory from the specified heap.
50 *
51 * @returns Address of the allocated memory.
52 * @param cb The number of bytes to allocate.
53 * @param pszTag The tag.
54 * @param fZero Whether to zero the memory or not.
55 * @param fProtExec PROT_EXEC or 0.
56 */
57static void *rtMemPagePosixAlloc(size_t cb, const char *pszTag, bool fZero, int fProtExec)
58{
59 /*
60 * Validate & adjust the input.
61 */
62 Assert(cb > 0);
63 NOREF(pszTag);
64 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
65
66 /*
67 * Do the allocation.
68 */
69 void *pv = mmap(NULL, cb,
70 PROT_READ | PROT_WRITE | fProtExec,
71 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
72 if (pv != MAP_FAILED)
73 {
74 AssertPtr(pv);
75 if (fZero)
76 RT_BZERO(pv, cb);
77 }
78 else
79 pv = NULL;
80
81 return pv;
82}
83
84
85/**
86 * Free memory allocated by rtMemPagePosixAlloc.
87 *
88 * @param pv The address of the memory to free.
89 * @param cb The size.
90 */
91static void rtMemPagePosixFree(void *pv, size_t cb)
92{
93 /*
94 * Validate & adjust the input.
95 */
96 if (!pv)
97 return;
98 AssertPtr(pv);
99 Assert(cb > 0);
100 Assert(!((uintptr_t)pv & PAGE_OFFSET_MASK));
101 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
102
103 /*
104 * Free the memory.
105 */
106 int rc = munmap(pv, cb);
107 AssertMsg(rc == 0, ("rc=%d pv=%p cb=%#zx\n", rc, pv, cb)); NOREF(rc);
108}
109
110
111
112
113
114RTDECL(void *) RTMemPageAllocTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
115{
116 return rtMemPagePosixAlloc(cb, pszTag, false /*fZero*/, 0);
117}
118
119
120RTDECL(void *) RTMemPageAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
121{
122 return rtMemPagePosixAlloc(cb, pszTag, true /*fZero*/, 0);
123}
124
125
126RTDECL(void) RTMemPageFree(void *pv, size_t cb) RT_NO_THROW_DEF
127{
128 return rtMemPagePosixFree(pv, cb);
129}
130
131
132
133
134
135RTDECL(void *) RTMemExecAllocTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
136{
137 return rtMemPagePosixAlloc(cb, pszTag, false /*fZero*/, PROT_EXEC);
138}
139
140
141RTDECL(void) RTMemExecFree(void *pv, size_t cb) RT_NO_THROW_DEF
142{
143 return rtMemPagePosixFree(pv, cb);
144}
145
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