alloc-win32.cpp revision 677833bc953b6cb418c701facbdcf4aa18d6c44e
0N/A/* $Id$ */
2362N/A/** @file
0N/A * InnoTek Portable Runtime - Memory Allocation, Win32.
0N/A */
0N/A
0N/A/*
0N/A * Copyright (C) 2006 InnoTek Systemberatung GmbH
0N/A *
0N/A * This file is part of VirtualBox Open Source Edition (OSE), as
0N/A * available from http://www.virtualbox.org. This file is free software;
0N/A * you can redistribute it and/or modify it under the terms of the GNU
0N/A * General Public License as published by the Free Software Foundation,
0N/A * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
0N/A * distribution. VirtualBox OSE is distributed in the hope that it will
0N/A * be useful, but WITHOUT ANY WARRANTY of any kind.
0N/A *
0N/A * If you received this file as part of a commercial VirtualBox
0N/A * distribution, then only the terms of your commercial VirtualBox
2362N/A * license agreement apply instead of the previous paragraph.
2362N/A */
2362N/A
0N/A
0N/A/*******************************************************************************
0N/A* Header Files *
0N/A*******************************************************************************/
0N/A#define LOG_GROUP RTLOGGROUP_MEM
0N/A#include <Windows.h>
0N/A
0N/A#include <iprt/alloc.h>
0N/A#include <iprt/assert.h>
0N/A#include <iprt/param.h>
0N/A#include <iprt/string.h>
0N/A#include <iprt/err.h>
0N/A
0N/A#ifndef USE_VIRTUAL_ALLOC
0N/A# include <malloc.h>
0N/A#endif
0N/A
0N/A
0N/A/**
0N/A * Allocates memory which may contain code.
0N/A *
0N/A * @returns Pointer to the allocated memory.
0N/A * @returns NULL on failure.
0N/A * @param cb Size in bytes of the memory block to allocate.
0N/A */
0N/ARTDECL(void *) RTMemExecAlloc(size_t cb)
0N/A{
0N/A /*
0N/A * Allocate first.
0N/A */
0N/A AssertMsg(cb, ("Allocating ZERO bytes is really not a good idea! Good luck with the next assertion!\n"));
0N/A cb = RT_ALIGN_Z(cb, 32);
0N/A void *pv = malloc(cb);
0N/A AssertMsg(pv, ("malloc(%d) failed!!!\n", cb));
0N/A if (pv)
0N/A {
0N/A /*
0N/A * Add PROT_EXEC flag to the page.
0N/A *
0N/A * This is in violation of the SuS where I think it saith that mprotect() shall
0N/A * only be used with mmap()'ed memory. Works on linux and OS/2 LIBC v0.6.
0N/A */
0N/A memset(pv, 0xcc, cb);
0N/A void *pvProt = (void *)((uintptr_t)pv & ~PAGE_OFFSET_MASK);
0N/A size_t cbProt = ((uintptr_t)pv & PAGE_OFFSET_MASK) + cb;
0N/A cbProt = RT_ALIGN_Z(cbProt, PAGE_SIZE);
0N/A DWORD fFlags = 0;
0N/A if (!VirtualProtect(pvProt, cbProt, PAGE_EXECUTE_READWRITE, &fFlags))
0N/A {
0N/A AssertMsgFailed(("VirtualProtect(%p, %#x,,) -> lasterr=%d\n", pvProt, cbProt, GetLastError()));
0N/A free(pv);
0N/A pv = NULL;
0N/A }
0N/A }
0N/A return pv;
0N/A}
0N/A
0N/A
0N/A/**
0N/A * Free executable/read/write memory allocated by RTMemExecAlloc().
0N/A *
0N/A * @param pv Pointer to memory block.
0N/A */
0N/ARTDECL(void) RTMemExecFree(void *pv)
0N/A{
0N/A if (pv)
0N/A free(pv);
0N/A}
0N/A
0N/A
0N/A/**
0N/A * Allocate page aligned memory.
0N/A *
0N/A * @returns Pointer to the allocated memory.
0N/A * @returns NULL if we're out of memory.
0N/A * @param cb Size of the memory block. Will be rounded up to page size.
0N/A */
0N/ARTDECL(void *) RTMemPageAlloc(size_t cb)
0N/A{
0N/A#ifdef USE_VIRTUAL_ALLOC
0N/A void *pv = VirtualAlloc(NULL, RT_ALIGN_Z(cb, PAGE_SIZE), MEM_COMMIT, PAGE_READWRITE);
0N/A#else
0N/A void *pv = _aligned_malloc(cb, PAGE_SIZE);
0N/A#endif
0N/A AssertMsg(pv, ("cb=%d lasterr=%d\n", cb, GetLastError()));
0N/A return pv;
0N/A}
0N/A
0N/A
0N/A/**
0N/A * Allocate zero'ed page aligned memory.
0N/A *
0N/A * @returns Pointer to the allocated memory.
0N/A * @returns NULL if we're out of memory.
0N/A * @param cb Size of the memory block. Will be rounded up to page size.
0N/A */
0N/ARTDECL(void *) RTMemPageAllocZ(size_t cb)
0N/A{
0N/A#ifdef USE_VIRTUAL_ALLOC
0N/A void *pv = VirtualAlloc(NULL, RT_ALIGN_Z(cb, PAGE_SIZE), MEM_COMMIT, PAGE_READWRITE);
0N/A#else
0N/A void *pv = _aligned_malloc(cb, PAGE_SIZE);
0N/A#endif
0N/A if (pv)
0N/A {
0N/A memset(pv, 0, RT_ALIGN_Z(cb, PAGE_SIZE));
0N/A return pv;
0N/A }
0N/A AssertMsgFailed(("cb=%d lasterr=%d\n", cb, GetLastError()));
0N/A return NULL;
0N/A}
0N/A
0N/A
0N/A/**
0N/A * Free a memory block allocated with RTMemPageAlloc() or RTMemPageAllocZ().
0N/A *
5851N/A * @param pv Pointer to the block as it was returned by the allocation function.
5851N/A * NULL will be ignored.
5851N/A */
5851N/ARTDECL(void) RTMemPageFree(void *pv)
5851N/A{
5851N/A if (pv)
5851N/A {
5851N/A#ifdef USE_VIRTUAL_ALLOC
5851N/A if (!VirtualFree(pv, 0, MEM_RELEASE))
5851N/A AssertMsgFailed(("pv=%p lasterr=%d\n", pv, GetLastError()));
5851N/A#else
5851N/A _aligned_free(pv);
5851N/A#endif
0N/A }
0N/A}
0N/A
0N/A
0N/A/**
0N/A * Change the page level protection of a memory region.
0N/A *
0N/A * @returns iprt status code.
0N/A * @param pv Start of the region. Will be rounded down to nearest page boundary.
0N/A * @param cb Size of the region. Will be rounded up to the nearest page boundary.
0N/A * @param fProtect The new protection, a combination of the RTMEM_PROT_* defines.
0N/A */
0N/ARTDECL(int) RTMemProtect(void *pv, size_t cb, unsigned fProtect)
0N/A{
0N/A /*
0N/A * Validate input.
0N/A */
0N/A if (cb == 0)
0N/A {
0N/A AssertMsgFailed(("!cb\n"));
0N/A return VERR_INVALID_PARAMETER;
0N/A }
0N/A if (fProtect & ~(RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC))
0N/A {
0N/A AssertMsgFailed(("fProtect=%#x\n", fProtect));
0N/A return VERR_INVALID_PARAMETER;
0N/A }
0N/A
/*
* Convert the flags.
*/
int fProt;
Assert(!RTMEM_PROT_NONE);
switch (fProtect & (RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC))
{
case RTMEM_PROT_NONE:
fProt = PAGE_NOACCESS;
break;
case RTMEM_PROT_READ:
fProt = PAGE_READONLY;
break;
case RTMEM_PROT_READ | RTMEM_PROT_WRITE:
fProt = PAGE_READWRITE;
break;
case RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC:
fProt = PAGE_EXECUTE_READWRITE;
break;
case RTMEM_PROT_READ | RTMEM_PROT_EXEC:
fProt = PAGE_EXECUTE_READWRITE;
break;
case RTMEM_PROT_WRITE:
fProt = PAGE_READWRITE;
break;
case RTMEM_PROT_WRITE | RTMEM_PROT_EXEC:
fProt = PAGE_EXECUTE_READWRITE;
break;
case RTMEM_PROT_EXEC:
fProt = PAGE_EXECUTE_READWRITE;
break;
/* If the compiler had any brains it would warn about this case. */
default:
AssertMsgFailed(("fProtect=%#x\n", fProtect));
return VERR_INTERNAL_ERROR;
}
/*
* Align the request.
*/
cb += (uintptr_t)pv & PAGE_OFFSET_MASK;
pv = (void *)((uintptr_t)pv & ~PAGE_OFFSET_MASK);
/*
* Change the page attributes.
*/
DWORD fFlags = 0;
if (VirtualProtect(pv, cb, fProt, &fFlags))
return VINF_SUCCESS;
return RTErrConvertFromWin32(GetLastError());
}