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
38 #if defined(__linux__) || defined(__OpenBSD__) || defined(_NetBSD__) || defined(__sun) || defined(__HAIKU__)
41 * Allocate a large block of memory which can hold code then dole it out
42 * in pieces by means of the generic memory manager code.
50 #include <selinux/selinux.h>
55 #define MAP_ANONYMOUS MAP_ANON
59 #define EXEC_HEAP_SIZE (10*1024*1024)
61 static mtx_t exec_mutex
= _MTX_INITIALIZER_NP
;
63 static struct mem_block
*exec_heap
= NULL
;
64 static unsigned char *exec_mem
= NULL
;
71 if (is_selinux_enabled()) {
72 if (!security_get_boolean_active("allow_execmem") ||
73 !security_get_boolean_pending("allow_execmem"))
79 exec_heap
= mmInit( 0, EXEC_HEAP_SIZE
);
82 exec_mem
= mmap(NULL
, EXEC_HEAP_SIZE
, PROT_EXEC
| PROT_READ
| PROT_WRITE
,
83 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
85 return (exec_mem
!= MAP_FAILED
);
90 _mesa_exec_malloc(GLuint size
)
92 struct mem_block
*block
= NULL
;
95 mtx_lock(&exec_mutex
);
101 size
= (size
+ 31) & ~31;
102 block
= mmAllocMem( exec_heap
, size
, 32, 0 );
106 addr
= exec_mem
+ block
->ofs
;
108 printf("_mesa_exec_malloc failed\n");
111 mtx_unlock(&exec_mutex
);
118 _mesa_exec_free(void *addr
)
120 mtx_lock(&exec_mutex
);
123 struct mem_block
*block
= mmFindBlock(exec_heap
, (unsigned char *)addr
- exec_mem
);
129 mtx_unlock(&exec_mutex
);
136 * Just use regular memory.
140 _mesa_exec_malloc(GLuint size
)
142 return malloc( size
);
147 _mesa_exec_free(void *addr
)