From 005469005df6ba5f80e382d5371c6d069c27738b Mon Sep 17 00:00:00 2001 From: Keith Whitwell Date: Tue, 18 Oct 2005 13:29:59 +0000 Subject: [PATCH] Add _mesa_exec_malloc() and _mesa_exec_free() for allocating executable memory. Based on Thomas Hellstrom's patch. TODO: glapi.c also needs this, but cannot access this code. --- src/mesa/drivers/dri/Makefile.template | 1 - src/mesa/drivers/dri/r200/r200_vtxfmt.c | 2 +- src/mesa/drivers/dri/r200/r200_vtxfmt.h | 2 +- src/mesa/drivers/dri/radeon/radeon_vtxfmt.c | 2 +- src/mesa/drivers/dri/radeon/radeon_vtxfmt.h | 2 +- src/mesa/main/execmem.c | 83 +++++++++++++++++++++++++++++ src/mesa/main/imports.h | 9 ++++ src/mesa/{drivers/dri/common => main}/mm.c | 15 ++++++ src/mesa/{drivers/dri/common => main}/mm.h | 7 +++ src/mesa/sources | 2 + src/mesa/x86/rtasm/x86sse.c | 4 +- 11 files changed, 122 insertions(+), 7 deletions(-) create mode 100644 src/mesa/main/execmem.c rename src/mesa/{drivers/dri/common => main}/mm.c (95%) rename src/mesa/{drivers/dri/common => main}/mm.h (94%) diff --git a/src/mesa/drivers/dri/Makefile.template b/src/mesa/drivers/dri/Makefile.template index 7c217f3..35f7fd8 100644 --- a/src/mesa/drivers/dri/Makefile.template +++ b/src/mesa/drivers/dri/Makefile.template @@ -4,7 +4,6 @@ MESA_MODULES = $(TOP)/src/mesa/libmesa.a COMMON_SOURCES = \ ../../common/driverfuncs.c \ - ../common/mm.c \ ../common/utils.c \ ../common/texmem.c \ ../common/vblank.c \ diff --git a/src/mesa/drivers/dri/r200/r200_vtxfmt.c b/src/mesa/drivers/dri/r200/r200_vtxfmt.c index d9af6bf..61a757c 100644 --- a/src/mesa/drivers/dri/r200/r200_vtxfmt.c +++ b/src/mesa/drivers/dri/r200/r200_vtxfmt.c @@ -1172,7 +1172,7 @@ static void free_funcs( struct dynfn *l ) struct dynfn *f, *tmp; foreach_s (f, tmp, l) { remove_from_list( f ); - ALIGN_FREE( f->code ); + _mesa_exec_free( f->code ); FREE( f ); } } diff --git a/src/mesa/drivers/dri/r200/r200_vtxfmt.h b/src/mesa/drivers/dri/r200/r200_vtxfmt.h index 82cbda2..32a21c4 100644 --- a/src/mesa/drivers/dri/r200/r200_vtxfmt.h +++ b/src/mesa/drivers/dri/r200/r200_vtxfmt.h @@ -59,7 +59,7 @@ do { \ insert_at_head( &CACHE, dfn ); \ dfn->key[0] = key[0]; \ dfn->key[1] = key[1]; \ - dfn->code = ALIGN_MALLOC( end - start, 16 ); \ + dfn->code = _mesa_exec_malloc( end - start ); \ memcpy (dfn->code, start, end - start); \ } \ while ( 0 ) diff --git a/src/mesa/drivers/dri/radeon/radeon_vtxfmt.c b/src/mesa/drivers/dri/radeon/radeon_vtxfmt.c index c734ab8..537a1c0 100644 --- a/src/mesa/drivers/dri/radeon/radeon_vtxfmt.c +++ b/src/mesa/drivers/dri/radeon/radeon_vtxfmt.c @@ -1038,7 +1038,7 @@ static void free_funcs( struct dynfn *l ) struct dynfn *f, *tmp; foreach_s (f, tmp, l) { remove_from_list( f ); - ALIGN_FREE( f->code ); + _mesa_exec_free( f->code ); FREE( f ); } } diff --git a/src/mesa/drivers/dri/radeon/radeon_vtxfmt.h b/src/mesa/drivers/dri/radeon/radeon_vtxfmt.h index 950d530..a7d5b83 100644 --- a/src/mesa/drivers/dri/radeon/radeon_vtxfmt.h +++ b/src/mesa/drivers/dri/radeon/radeon_vtxfmt.h @@ -56,7 +56,7 @@ do { \ char *end = (char *)&FUNC##_end; \ insert_at_head( &CACHE, dfn ); \ dfn->key = key; \ - dfn->code = ALIGN_MALLOC( end - start, 16 ); \ + dfn->code = _mesa_exec_malloc( end - start ); \ memcpy (dfn->code, start, end - start); \ } \ while ( 0 ) diff --git a/src/mesa/main/execmem.c b/src/mesa/main/execmem.c new file mode 100644 index 0000000..17ad06c --- /dev/null +++ b/src/mesa/main/execmem.c @@ -0,0 +1,83 @@ +#include "imports.h" +#include "glthread.h" + + + +#ifdef __linux__ + +#include +#include +#include + +#define EXEC_HEAP_SIZE (128*1024) + +_glthread_DECLARE_STATIC_MUTEX(exec_mutex); + +static memHeap_t *exec_heap = NULL; +static unsigned char *exec_mem = NULL; + +static void init_heap( void ) +{ + if (!exec_heap) + exec_heap = mmInit( 0, EXEC_HEAP_SIZE ); + + if (!exec_mem) + exec_mem = (unsigned char *) mmap(0, EXEC_HEAP_SIZE, + PROT_EXEC | PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); +} + + +void * +_mesa_exec_malloc( GLuint size ) +{ + PMemBlock block = NULL; + void *addr = NULL; + + _glthread_LOCK_MUTEX(exec_mutex); + + init_heap(); + + if (exec_heap) { + size = (size + 31) & ~31; + block = mmAllocMem( exec_heap, size, 32, 0 ); + } + + if (block) + addr = exec_mem + block->ofs; + + _glthread_UNLOCK_MUTEX(exec_mutex); + + return addr; +} + +void +_mesa_exec_free(void *addr) +{ + _glthread_LOCK_MUTEX(exec_mutex); + + if (exec_heap) { + PMemBlock block = mmFindBlock(exec_heap, (unsigned char *)addr - exec_mem); + + if (block) + mmFreeMem(block); + } + + _glthread_UNLOCK_MUTEX(exec_mutex); +} + +#else + +void * +_mesa_exec_malloc( GLuint size ) +{ + return _mesa_malloc( size ); +} + +void +_mesa_exec_free(void *addr) +{ + _mesa_free(addr); +} + +#endif diff --git a/src/mesa/main/imports.h b/src/mesa/main/imports.h index 56ba114..2484933 100644 --- a/src/mesa/main/imports.h +++ b/src/mesa/main/imports.h @@ -723,6 +723,15 @@ _mesa_exit( int status ); extern void _mesa_init_default_imports( __GLimports *imports, void *driverCtx ); +/* Allocate executable memory for codegen: + */ +extern void * +_mesa_exec_malloc( GLuint size ); + +extern void +_mesa_exec_free( void *addr ); + + #ifdef __cplusplus } diff --git a/src/mesa/drivers/dri/common/mm.c b/src/mesa/main/mm.c similarity index 95% rename from src/mesa/drivers/dri/common/mm.c rename to src/mesa/main/mm.c index 5781d99..14563b2 100644 --- a/src/mesa/drivers/dri/common/mm.c +++ b/src/mesa/main/mm.c @@ -136,6 +136,21 @@ PMemBlock mmAllocMem( memHeap_t *heap, int size, int align2, int startSearch) return p; } +PMemBlock mmFindBlock( memHeap_t *heap, int start) +{ + TMemBlock *p = (TMemBlock *)heap; + + while (p) { + if (p->ofs == start && p->free) + return p; + + p = p->next; + } + + return NULL; +} + + static __inline__ int Join2Blocks(TMemBlock *p) { if (p->free && p->next && p->next->free) { diff --git a/src/mesa/drivers/dri/common/mm.h b/src/mesa/main/mm.h similarity index 94% rename from src/mesa/drivers/dri/common/mm.h rename to src/mesa/main/mm.h index 176910d..83a4ac8 100644 --- a/src/mesa/drivers/dri/common/mm.h +++ b/src/mesa/main/mm.h @@ -72,6 +72,13 @@ PMemBlock mmAllocMem( memHeap_t *heap, int size, int align2, int mmFreeMem( PMemBlock b ); /* + * Free block starts at offset + * input: pointer to a heap, start offset + * return: pointer to a block + */ +PMemBlock mmFindBlock( memHeap_t *heap, int start); + +/* * destroy MM */ void mmDestroy( memHeap_t *mmInit ); diff --git a/src/mesa/sources b/src/mesa/sources index 5d7df41..8b2819b 100644 --- a/src/mesa/sources +++ b/src/mesa/sources @@ -21,6 +21,7 @@ MAIN_SOURCES = \ main/enable.c \ main/enums.c \ main/eval.c \ + main/execmem.c \ main/extensions.c \ main/fbobject.c \ main/feedback.c \ @@ -36,6 +37,7 @@ MAIN_SOURCES = \ main/light.c \ main/lines.c \ main/matrix.c \ + main/mm.c \ main/occlude.c \ main/pixel.c \ main/points.c \ diff --git a/src/mesa/x86/rtasm/x86sse.c b/src/mesa/x86/rtasm/x86sse.c index 40ffc03..0c9ffe2 100644 --- a/src/mesa/x86/rtasm/x86sse.c +++ b/src/mesa/x86/rtasm/x86sse.c @@ -971,13 +971,13 @@ struct x86_reg x86_fn_arg( struct x86_function *p, void x86_init_func( struct x86_function *p ) { - p->store = _mesa_malloc(1024); + p->store = _mesa_exec_malloc(1024); p->csr = p->store; } void x86_release_func( struct x86_function *p ) { - _mesa_free(p->store); + _mesa_exec_free(p->store); } -- 2.7.4