2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
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:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
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.
28 * Functions for allocating executable memory.
30 * \author Keith Whitwell
35 #include "util/imports.h"
36 #include "main/glheader.h"
38 #include "c11/threads.h"
41 #if defined(__linux__) || defined(__OpenBSD__) || defined(_NetBSD__) || defined(__sun) || defined(__HAIKU__)
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.
50 #include "util/u_mm.h"
53 #include <selinux/selinux.h>
58 #define MAP_ANONYMOUS MAP_ANON
62 #define EXEC_HEAP_SIZE (10*1024*1024)
64 static mtx_t exec_mutex
= _MTX_INITIALIZER_NP
;
66 static struct mem_block
*exec_heap
= NULL
;
67 static unsigned char *exec_mem
= NULL
;
74 if (is_selinux_enabled()) {
75 if (!security_get_boolean_active("allow_execmem") ||
76 !security_get_boolean_pending("allow_execmem"))
82 exec_heap
= u_mmInit( 0, EXEC_HEAP_SIZE
);
85 exec_mem
= mmap(NULL
, EXEC_HEAP_SIZE
, PROT_EXEC
| PROT_READ
| PROT_WRITE
,
86 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
88 return (exec_mem
!= MAP_FAILED
);
93 _mesa_exec_malloc(unsigned size
)
95 struct mem_block
*block
= NULL
;
98 mtx_lock(&exec_mutex
);
104 size
= (size
+ 31) & ~31;
105 block
= u_mmAllocMem(exec_heap
, size
, 5, 0);
109 addr
= exec_mem
+ block
->ofs
;
111 printf("_mesa_exec_malloc failed\n");
114 mtx_unlock(&exec_mutex
);
121 _mesa_exec_free(void *addr
)
123 mtx_lock(&exec_mutex
);
126 struct mem_block
*block
= u_mmFindBlock(exec_heap
, (unsigned char *)addr
- exec_mem
);
132 mtx_unlock(&exec_mutex
);
139 * Just use regular memory.
143 _mesa_exec_malloc(unsigned size
)
145 return malloc( size
);
150 _mesa_exec_free(void *addr
)