move mesa32 over to new dir
[reactos.git] / reactos / lib / mesa32 / src / main / execmem.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5
4 *
5 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 /**
27 * \file exemem.c
28 * Functions for allocating executable memory.
29 *
30 * \author Keith Whitwell
31 */
32
33
34 #include "imports.h"
35 #include "glthread.h"
36
37
38
39 #if defined(__linux__) && !defined(XFree86Server)
40
41 /*
42 * Allocate a large block of memory which can hold code then dole it out
43 * in pieces by means of the generic memory manager code.
44 */
45
46
47 #include <unistd.h>
48 #include <sys/mman.h>
49 #include "mm.h"
50
51 #define EXEC_HEAP_SIZE (128*1024)
52
53 _glthread_DECLARE_STATIC_MUTEX(exec_mutex);
54
55 static memHeap_t *exec_heap = NULL;
56 static unsigned char *exec_mem = NULL;
57
58
59 static void
60 init_heap(void)
61 {
62 if (!exec_heap)
63 exec_heap = mmInit( 0, EXEC_HEAP_SIZE );
64
65 if (!exec_mem)
66 exec_mem = (unsigned char *) mmap(0, EXEC_HEAP_SIZE,
67 PROT_EXEC | PROT_READ | PROT_WRITE,
68 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
69 }
70
71
72 void *
73 _mesa_exec_malloc(GLuint size)
74 {
75 PMemBlock block = NULL;
76 void *addr = NULL;
77
78 _glthread_LOCK_MUTEX(exec_mutex);
79
80 init_heap();
81
82 if (exec_heap) {
83 size = (size + 31) & ~31;
84 block = mmAllocMem( exec_heap, size, 32, 0 );
85 }
86
87 if (block)
88 addr = exec_mem + block->ofs;
89
90 _glthread_UNLOCK_MUTEX(exec_mutex);
91
92 return addr;
93 }
94
95
96 void
97 _mesa_exec_free(void *addr)
98 {
99 _glthread_LOCK_MUTEX(exec_mutex);
100
101 if (exec_heap) {
102 PMemBlock block = mmFindBlock(exec_heap, (unsigned char *)addr - exec_mem);
103
104 if (block)
105 mmFreeMem(block);
106 }
107
108 _glthread_UNLOCK_MUTEX(exec_mutex);
109 }
110
111
112 #else
113
114 /*
115 * Just use regular memory.
116 */
117
118 void *
119 _mesa_exec_malloc(GLuint size)
120 {
121 return _mesa_malloc( size );
122 }
123
124
125 void
126 _mesa_exec_free(void *addr)
127 {
128 _mesa_free(addr);
129 }
130
131
132 #endif