Merge commit 'origin/master' into gallium-0.2
[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 "util/u_memory.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 "pipe/p_thread.h"
51 #include "util/u_mm.h"
52
53 #define EXEC_HEAP_SIZE (10*1024*1024)
54
55 pipe_static_mutex(exec_mutex);
56
57 static struct mem_block *exec_heap = NULL;
58 static unsigned char *exec_mem = NULL;
59
60
61 static void
62 init_heap(void)
63 {
64 if (!exec_heap)
65 exec_heap = mmInit( 0, EXEC_HEAP_SIZE );
66
67 if (!exec_mem)
68 exec_mem = (unsigned char *) mmap(0, EXEC_HEAP_SIZE,
69 PROT_EXEC | PROT_READ | PROT_WRITE,
70 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
71 }
72
73
74 void *
75 rtasm_exec_malloc(size_t size)
76 {
77 struct mem_block *block = NULL;
78 void *addr = NULL;
79
80 pipe_mutex_lock(exec_mutex);
81
82 init_heap();
83
84 if (exec_heap) {
85 size = (size + 31) & ~31;
86 block = mmAllocMem( exec_heap, size, 32, 0 );
87 }
88
89 if (block)
90 addr = exec_mem + block->ofs;
91 else
92 debug_printf("rtasm_exec_malloc failed\n");
93
94 pipe_mutex_unlock(exec_mutex);
95
96 return addr;
97 }
98
99
100 void
101 rtasm_exec_free(void *addr)
102 {
103 pipe_mutex_lock(exec_mutex);
104
105 if (exec_heap) {
106 struct mem_block *block = mmFindBlock(exec_heap, (unsigned char *)addr - exec_mem);
107
108 if (block)
109 mmFreeMem(block);
110 }
111
112 pipe_mutex_unlock(exec_mutex);
113 }
114
115
116 #else
117
118 /*
119 * Just use regular memory.
120 */
121
122 void *
123 rtasm_exec_malloc(size_t size)
124 {
125 return MALLOC( size );
126 }
127
128
129 void
130 rtasm_exec_free(void *addr)
131 {
132 FREE(addr);
133 }
134
135
136 #endif