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