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 7c217f3d1d7..35f7fd8a129 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 d9af6bf5f85..61a757c870d 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 82cbda2742b..32a21c48977 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 c734ab8d5ab..537a1c03520 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 950d530ae55..a7d5b8334df 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 00000000000..17ad06c90a4 --- /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 56ba114d9aa..24849330254 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 5781d9936c2..14563b2591c 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 176910d70b6..83a4ac8c74d 100644 --- a/src/mesa/drivers/dri/common/mm.h +++ b/src/mesa/main/mm.h @@ -71,6 +71,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 */ diff --git a/src/mesa/sources b/src/mesa/sources index 5d7df417893..8b2819b0e7e 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 40ffc03204e..0c9ffe25fa2 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.30.2