diff options
author | Keith Whitwell <[email protected]> | 2005-10-18 13:29:59 +0000 |
---|---|---|
committer | Keith Whitwell <[email protected]> | 2005-10-18 13:29:59 +0000 |
commit | 005469005df6ba5f80e382d5371c6d069c27738b (patch) | |
tree | 3724212c691417d4dc08213d02b217d346cb25c3 /src/mesa/main/execmem.c | |
parent | 05e1a49ab4b0d34aba4bdf55ed7ffe5b6d4411f8 (diff) |
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.
Diffstat (limited to 'src/mesa/main/execmem.c')
-rw-r--r-- | src/mesa/main/execmem.c | 83 |
1 files changed, 83 insertions, 0 deletions
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 <unistd.h> +#include <sys/mman.h> +#include <mm.h> + +#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 |