Path: blob/21.2-virgl/src/gallium/auxiliary/rtasm/rtasm_execmem.c
4561 views
/**************************************************************************1*2* Copyright (C) 1999-2005 Brian Paul All Rights Reserved.3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* the rights to use, copy, modify, merge, publish, distribute, sublicense,8* and/or sell copies of the Software, and to permit persons to whom the9* Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice shall be included12* in all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS15* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR18* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,19* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR20* OTHER DEALINGS IN THE SOFTWARE.21*22**************************************************************************/232425/**26* \file exemem.c27* Functions for allocating executable memory.28*29* \author Keith Whitwell30*/313233#include "pipe/p_compiler.h"34#include "util/u_debug.h"35#include "os/os_thread.h"36#include "util/u_memory.h"3738#include "rtasm_execmem.h"3940#ifndef MAP_ANONYMOUS41#define MAP_ANONYMOUS MAP_ANON42#endif4344#if defined(PIPE_OS_WINDOWS)45#ifndef WIN32_LEAN_AND_MEAN46#define WIN32_LEAN_AND_MEAN 147#endif48#include <windows.h>49#endif5051#if defined(PIPE_OS_UNIX)525354/*55* Allocate a large block of memory which can hold code then dole it out56* in pieces by means of the generic memory manager code.57*/5859#include <unistd.h>60#include <sys/mman.h>61#include "util/u_mm.h"6263#define EXEC_HEAP_SIZE (10*1024*1024)6465static mtx_t exec_mutex = _MTX_INITIALIZER_NP;6667static struct mem_block *exec_heap = NULL;68static unsigned char *exec_mem = NULL;697071static int72init_heap(void)73{74if (!exec_heap)75exec_heap = u_mmInit( 0, EXEC_HEAP_SIZE );7677if (!exec_mem)78exec_mem = (unsigned char *) mmap(0, EXEC_HEAP_SIZE,79PROT_EXEC | PROT_READ | PROT_WRITE,80MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);8182return (exec_mem != MAP_FAILED);83}848586void *87rtasm_exec_malloc(size_t size)88{89struct mem_block *block = NULL;90void *addr = NULL;9192mtx_lock(&exec_mutex);9394if (!init_heap())95goto bail;9697if (exec_heap) {98size = (size + 31) & ~31; /* next multiple of 32 bytes */99block = u_mmAllocMem( exec_heap, size, 5, 0 ); /* 5 -> 32-byte alignment */100}101102if (block)103addr = exec_mem + block->ofs;104else105debug_printf("rtasm_exec_malloc failed\n");106107bail:108mtx_unlock(&exec_mutex);109110return addr;111}112113114void115rtasm_exec_free(void *addr)116{117mtx_lock(&exec_mutex);118119if (exec_heap) {120struct mem_block *block = u_mmFindBlock(exec_heap, (unsigned char *)addr - exec_mem);121122if (block)123u_mmFreeMem(block);124}125126mtx_unlock(&exec_mutex);127}128129130#elif defined(PIPE_OS_WINDOWS)131132133/*134* Avoid Data Execution Prevention.135*/136137void *138rtasm_exec_malloc(size_t size)139{140return VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE);141}142143144void145rtasm_exec_free(void *addr)146{147VirtualFree(addr, 0, MEM_RELEASE);148}149150151#else152153154/*155* Just use regular memory.156*/157158void *159rtasm_exec_malloc(size_t size)160{161return MALLOC( size );162}163164165void166rtasm_exec_free(void *addr)167{168FREE(addr);169}170171172#endif173174175