mesa: optimize initialization of new VAOs
[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 "util/imports.h"
36 #include "main/glheader.h"
37 #include "execmem.h"
38 #include "c11/threads.h"
39
40
41 #if defined(__linux__) || defined(__OpenBSD__) || defined(_NetBSD__) || defined(__sun) || defined(__HAIKU__)
42
43 /*
44 * Allocate a large block of memory which can hold code then dole it out
45 * in pieces by means of the generic memory manager code.
46 */
47
48 #include <unistd.h>
49 #include <sys/mman.h>
50 #include "util/u_mm.h"
51
52 #ifdef MESA_SELINUX
53 #include <selinux/selinux.h>
54 #endif
55
56
57 #ifndef MAP_ANONYMOUS
58 #define MAP_ANONYMOUS MAP_ANON
59 #endif
60
61
62 #define EXEC_HEAP_SIZE (10*1024*1024)
63
64 static mtx_t exec_mutex = _MTX_INITIALIZER_NP;
65
66 static struct mem_block *exec_heap = NULL;
67 static unsigned char *exec_mem = NULL;
68
69
70 static int
71 init_heap(void)
72 {
73 #ifdef MESA_SELINUX
74 if (is_selinux_enabled()) {
75 if (!security_get_boolean_active("allow_execmem") ||
76 !security_get_boolean_pending("allow_execmem"))
77 return 0;
78 }
79 #endif
80
81 if (!exec_heap)
82 exec_heap = u_mmInit( 0, EXEC_HEAP_SIZE );
83
84 if (!exec_mem)
85 exec_mem = mmap(NULL, EXEC_HEAP_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE,
86 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
87
88 return (exec_mem != MAP_FAILED);
89 }
90
91
92 void *
93 _mesa_exec_malloc(unsigned size)
94 {
95 struct mem_block *block = NULL;
96 void *addr = NULL;
97
98 mtx_lock(&exec_mutex);
99
100 if (!init_heap())
101 goto bail;
102
103 if (exec_heap) {
104 size = (size + 31) & ~31;
105 block = u_mmAllocMem(exec_heap, size, 5, 0);
106 }
107
108 if (block)
109 addr = exec_mem + block->ofs;
110 else
111 printf("_mesa_exec_malloc failed\n");
112
113 bail:
114 mtx_unlock(&exec_mutex);
115
116 return addr;
117 }
118
119
120 void
121 _mesa_exec_free(void *addr)
122 {
123 mtx_lock(&exec_mutex);
124
125 if (exec_heap) {
126 struct mem_block *block = u_mmFindBlock(exec_heap, (unsigned char *)addr - exec_mem);
127
128 if (block)
129 u_mmFreeMem(block);
130 }
131
132 mtx_unlock(&exec_mutex);
133 }
134
135
136 #else
137
138 /*
139 * Just use regular memory.
140 */
141
142 void *
143 _mesa_exec_malloc(unsigned size)
144 {
145 return malloc( size );
146 }
147
148
149 void
150 _mesa_exec_free(void *addr)
151 {
152 free(addr);
153 }
154
155
156 #endif