summaryrefslogtreecommitdiffstats
path: root/src/mesa/main/execmem.c
blob: 17ad06c90a4e7e5e2eaac1da5056bee16b727587 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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