Fix windows build.
[mesa.git] / src / gallium / auxiliary / rtasm / rtasm_execmem.c
1 /**************************************************************************
2 *
3 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 *
22 **************************************************************************/
23
24
25 /**
26 * \file exemem.c
27 * Functions for allocating executable memory.
28 *
29 * \author Keith Whitwell
30 */
31
32
33 #include "pipe/p_compiler.h"
34 #include "pipe/p_debug.h"
35 #include "pipe/p_thread.h"
36 #include "pipe/p_util.h"
37
38 #include "rtasm_execmem.h"
39
40
41 #if defined(__linux__)
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 #define EXEC_HEAP_SIZE (10*1024*1024)
53
54 _glthread_DECLARE_STATIC_MUTEX(exec_mutex);
55
56 static struct mem_block *exec_heap = NULL;
57 static unsigned char *exec_mem = NULL;
58
59
60 static void
61 init_heap(void)
62 {
63 if (!exec_heap)
64 exec_heap = mmInit( 0, EXEC_HEAP_SIZE );
65
66 if (!exec_mem)
67 exec_mem = (unsigned char *) mmap(0, EXEC_HEAP_SIZE,
68 PROT_EXEC | PROT_READ | PROT_WRITE,
69 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
70 }
71
72
73 void *
74 rtasm_exec_malloc(size_t size)
75 {
76 struct mem_block *block = NULL;
77 void *addr = NULL;
78
79 _glthread_LOCK_MUTEX(exec_mutex);
80
81 init_heap();
82
83 if (exec_heap) {
84 size = (size + 31) & ~31;
85 block = mmAllocMem( exec_heap, size, 32, 0 );
86 }
87
88 if (block)
89 addr = exec_mem + block->ofs;
90 else
91 debug_printf("rtasm_exec_malloc failed\n");
92
93 _glthread_UNLOCK_MUTEX(exec_mutex);
94
95 return addr;
96 }
97
98
99 void
100 rtasm_exec_free(void *addr)
101 {
102 _glthread_LOCK_MUTEX(exec_mutex);
103
104 if (exec_heap) {
105 struct mem_block *block = mmFindBlock(exec_heap, (unsigned char *)addr - exec_mem);
106
107 if (block)
108 mmFreeMem(block);
109 }
110
111 _glthread_UNLOCK_MUTEX(exec_mutex);
112 }
113
114
115 #else
116
117 /*
118 * Just use regular memory.
119 */
120
121 void *
122 rtasm_exec_malloc(size_t size)
123 {
124 return MALLOC( size );
125 }
126
127
128 void
129 rtasm_exec_free(void *addr)
130 {
131 FREE(addr);
132 }
133
134
135 #endif