allocex-r3-posix.cpp revision 83de81b6f22c80532a478e79afaee21a0887302a
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync/* $Id$ */
65b0f0cd3c38007138720b7eaa43c963a9ee1244vboxsync/** @file
ea7d33962ceb90ea90e67e427d60b5788f34268avboxsync * IPRT - Memory Allocation, Extended Alloc Workers, posix.
65b0f0cd3c38007138720b7eaa43c963a9ee1244vboxsync */
65b0f0cd3c38007138720b7eaa43c963a9ee1244vboxsync
65b0f0cd3c38007138720b7eaa43c963a9ee1244vboxsync/*
8c48cf39dfb84c1f26e0e7fbd1c407e25a34eef1vboxsync * Copyright (C) 2006-2013 Oracle Corporation
65b0f0cd3c38007138720b7eaa43c963a9ee1244vboxsync *
65b0f0cd3c38007138720b7eaa43c963a9ee1244vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
65b0f0cd3c38007138720b7eaa43c963a9ee1244vboxsync * available from http://www.virtualbox.org. This file is free software;
65b0f0cd3c38007138720b7eaa43c963a9ee1244vboxsync * you can redistribute it and/or modify it under the terms of the GNU
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * General Public License (GPL) as published by the Free Software
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync *
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * The contents of this file may alternatively be used under the terms
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * of the Common Development and Distribution License Version 1.0
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * VirtualBox OSE distribution, in which case the provisions of the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * CDDL are applicable instead of those of the GPL.
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync *
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * You may elect to license modified versions of this file under the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * terms and conditions of either the GPL or the CDDL or both.
65b0f0cd3c38007138720b7eaa43c963a9ee1244vboxsync */
65b0f0cd3c38007138720b7eaa43c963a9ee1244vboxsync
cf57145d4697dceca3f0542b370c20f7a2c5c6e8vboxsync
cf57145d4697dceca3f0542b370c20f7a2c5c6e8vboxsync/*******************************************************************************
65b0f0cd3c38007138720b7eaa43c963a9ee1244vboxsync* Header Files *
cf57145d4697dceca3f0542b370c20f7a2c5c6e8vboxsync*******************************************************************************/
65b0f0cd3c38007138720b7eaa43c963a9ee1244vboxsync#define RTMEM_NO_WRAP_TO_EF_APIS
65b0f0cd3c38007138720b7eaa43c963a9ee1244vboxsync#include <iprt/mem.h>
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync#include "internal/iprt.h"
c0cb47bd8aa02fca08ff72a37ba5c65894241cbfvboxsync
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync#include <iprt/assert.h>
c0cb47bd8aa02fca08ff72a37ba5c65894241cbfvboxsync#include <iprt/string.h>
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync#include "../allocex.h"
c0cb47bd8aa02fca08ff72a37ba5c65894241cbfvboxsync
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync#include <sys/mman.h>
c0cb47bd8aa02fca08ff72a37ba5c65894241cbfvboxsync
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync
c0cb47bd8aa02fca08ff72a37ba5c65894241cbfvboxsyncDECLHIDDEN(int) rtMemAllocEx16BitReach(size_t cbAlloc, uint32_t fFlags, void **ppv)
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync{
c0cb47bd8aa02fca08ff72a37ba5c65894241cbfvboxsync AssertReturn(cbAlloc < _64K, NULL);
e52b0dfe3d9ead70e24f2ce4ed3faa6c79c00618vboxsync
e52b0dfe3d9ead70e24f2ce4ed3faa6c79c00618vboxsync /*
e52b0dfe3d9ead70e24f2ce4ed3faa6c79c00618vboxsync * Try with every possible address hint since the possible range is very limited.
e52b0dfe3d9ead70e24f2ce4ed3faa6c79c00618vboxsync */
e52b0dfe3d9ead70e24f2ce4ed3faa6c79c00618vboxsync int fProt = PROT_READ | PROT_WRITE | (fFlags & RTMEMALLOCEX_FLAGS_EXEC ? PROT_EXEC : 0);
e52b0dfe3d9ead70e24f2ce4ed3faa6c79c00618vboxsync uintptr_t uAddr = 0x1000;
e52b0dfe3d9ead70e24f2ce4ed3faa6c79c00618vboxsync uintptr_t uAddrLast = _64K - uAddr - cbAlloc;
e52b0dfe3d9ead70e24f2ce4ed3faa6c79c00618vboxsync while (uAddr <= uAddrLast)
c320aee99660824809e157bbad368db3542a9e90vboxsync {
c0cb47bd8aa02fca08ff72a37ba5c65894241cbfvboxsync void *pv = mmap((void *)uAddr, cbAlloc, fProt, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
c320aee99660824809e157bbad368db3542a9e90vboxsync if (pv && (uintptr_t)pv <= uAddrLast)
c0cb47bd8aa02fca08ff72a37ba5c65894241cbfvboxsync {
c320aee99660824809e157bbad368db3542a9e90vboxsync *ppv = pv;
c0cb47bd8aa02fca08ff72a37ba5c65894241cbfvboxsync return VINF_SUCCESS;
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync }
c0cb47bd8aa02fca08ff72a37ba5c65894241cbfvboxsync
ea7d33962ceb90ea90e67e427d60b5788f34268avboxsync if (pv)
c0cb47bd8aa02fca08ff72a37ba5c65894241cbfvboxsync {
f9f72da3edbd8434dc722c26ee038e2808d691favboxsync munmap(pv, cbAlloc);
c0cb47bd8aa02fca08ff72a37ba5c65894241cbfvboxsync pv = NULL;
f9f72da3edbd8434dc722c26ee038e2808d691favboxsync }
c0cb47bd8aa02fca08ff72a37ba5c65894241cbfvboxsync uAddr += _4K;
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync }
c0cb47bd8aa02fca08ff72a37ba5c65894241cbfvboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync return VERR_NO_MEMORY;
c0cb47bd8aa02fca08ff72a37ba5c65894241cbfvboxsync}
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
c0cb47bd8aa02fca08ff72a37ba5c65894241cbfvboxsync
f2af1be3e27a2fefe95332260d360f4b26cffbcfvboxsyncDECLHIDDEN(int) rtMemAllocEx32BitReach(size_t cbAlloc, uint32_t fFlags, void **ppv)
f2af1be3e27a2fefe95332260d360f4b26cffbcfvboxsync{
f2af1be3e27a2fefe95332260d360f4b26cffbcfvboxsync int fProt = PROT_READ | PROT_WRITE | (fFlags & RTMEMALLOCEX_FLAGS_EXEC ? PROT_EXEC : 0);
f2af1be3e27a2fefe95332260d360f4b26cffbcfvboxsync#if ARCH_BITS == 32
75d8fea43f2d7474d00172dcf906f7ab598a8c8fvboxsync void *pv = mmap(NULL, cbAlloc, fProt, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
c0cb47bd8aa02fca08ff72a37ba5c65894241cbfvboxsync if (pv)
75d8fea43f2d7474d00172dcf906f7ab598a8c8fvboxsync {
c0cb47bd8aa02fca08ff72a37ba5c65894241cbfvboxsync *ppv = pv;
eaae6c3ce81916ccc4e74af4b55972ba956cf5c8vboxsync return VINF_SUCCESS;
c0cb47bd8aa02fca08ff72a37ba5c65894241cbfvboxsync }
eaae6c3ce81916ccc4e74af4b55972ba956cf5c8vboxsync return VERR_NO_MEMORY;
c0cb47bd8aa02fca08ff72a37ba5c65894241cbfvboxsync
eaae6c3ce81916ccc4e74af4b55972ba956cf5c8vboxsync#elif defined(RT_OS_LINUX)
c0cb47bd8aa02fca08ff72a37ba5c65894241cbfvboxsync# ifdef MAP_32BIT
eaae6c3ce81916ccc4e74af4b55972ba956cf5c8vboxsync void *pv = mmap(NULL, cbAlloc, fProt, MAP_PRIVATE | MAP_ANONYMOUS | MAP_32BIT, -1, 0);
c0cb47bd8aa02fca08ff72a37ba5c65894241cbfvboxsync if (pv)
f1c8438d14b7420eb2b916ef5981ac09faa4f6c4vboxsync {
f1c8438d14b7420eb2b916ef5981ac09faa4f6c4vboxsync *ppv = pv;
f1c8438d14b7420eb2b916ef5981ac09faa4f6c4vboxsync return VINF_SUCCESS;
f1c8438d14b7420eb2b916ef5981ac09faa4f6c4vboxsync }
b6dc9545438e63d05fff621e85ddb7dc39cf14e7vboxsync# endif
c0cb47bd8aa02fca08ff72a37ba5c65894241cbfvboxsync
b6dc9545438e63d05fff621e85ddb7dc39cf14e7vboxsync /** @todo On linux, we need an accurate hint. Since I don't need this branch of
c0cb47bd8aa02fca08ff72a37ba5c65894241cbfvboxsync * the code right now, I won't bother starting to parse
20593760b116c90f3e439552763eef632a3bbb17vboxsync * /proc/curproc/mmap right now... */
c0cb47bd8aa02fca08ff72a37ba5c65894241cbfvboxsync#else
20593760b116c90f3e439552763eef632a3bbb17vboxsync#endif
c0cb47bd8aa02fca08ff72a37ba5c65894241cbfvboxsync return VERR_NOT_SUPPORTED;
46f5824f3e233b92bad1626932d5ce9522886f54vboxsync}
c0cb47bd8aa02fca08ff72a37ba5c65894241cbfvboxsync
46f5824f3e233b92bad1626932d5ce9522886f54vboxsync
46f5824f3e233b92bad1626932d5ce9522886f54vboxsyncDECLHIDDEN(void) rtMemFreeExYyBitReach(void *pv, size_t cb, uint32_t fFlags)
46f5824f3e233b92bad1626932d5ce9522886f54vboxsync{
46f5824f3e233b92bad1626932d5ce9522886f54vboxsync munmap(pv, cb);
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync}
c0cb47bd8aa02fca08ff72a37ba5c65894241cbfvboxsync
6fea4abcc6ee0f2797ac01ef79c374d506aedc02vboxsync