Mesa: Use mmap on Haiku for executable memory vs malloc
[mesa.git] / src / mesa / main / remap.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.7
4 *
5 * Copyright (C) 2009 Chia-I Wu <olv@0xlab.org>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 */
25
26
27 /**
28 * \file remap.c
29 * Remap table management.
30 *
31 * Entries in the dispatch table are either static or dynamic. The
32 * dispatch table is shared by mesa core and glapi. When they are
33 * built separately, it is possible that a static entry in mesa core
34 * is dynamic, or assigned a different static offset, in glapi. The
35 * remap table is in charge of mapping a static entry in mesa core to
36 * a dynamic entry, or the corresponding static entry, in glapi.
37 */
38
39 #include "remap.h"
40 #include "imports.h"
41 #include "glapi/glapi.h"
42
43 #define MAX_ENTRY_POINTS 16
44
45 #define need_MESA_remap_table
46 #include "main/remap_helper.h"
47
48
49 /* this is global for quick access */
50 int driDispatchRemapTable[driDispatchRemapTable_size];
51
52
53 /**
54 * Return the spec string associated with the given function index.
55 * The index is available from including remap_helper.h.
56 *
57 * \param func_index an opaque function index.
58 *
59 * \return the spec string associated with the function index, or NULL.
60 */
61 const char *
62 _mesa_get_function_spec(GLint func_index)
63 {
64 if (func_index < Elements(_mesa_function_pool))
65 return _mesa_function_pool + func_index;
66 else
67 return NULL;
68 }
69
70
71 /**
72 * Map a function by its spec. The function will be added to glapi,
73 * and the dispatch offset will be returned.
74 *
75 * \param spec a '\0'-separated string array specifying a function.
76 * It begins with the parameter signature of the function,
77 * followed by the names of the entry points. An empty entry
78 * point name terminates the array.
79 *
80 * \return the offset of the (re-)mapped function in the dispatch
81 * table, or -1.
82 */
83 GLint
84 _mesa_map_function_spec(const char *spec)
85 {
86 const char *signature;
87 const char *names[MAX_ENTRY_POINTS + 1];
88 GLint num_names = 0;
89
90 if (!spec)
91 return -1;
92
93 signature = spec;
94 spec += strlen(spec) + 1;
95
96 /* spec is terminated by an empty string */
97 while (*spec) {
98 names[num_names] = spec;
99 num_names++;
100 if (num_names >= MAX_ENTRY_POINTS)
101 break;
102 spec += strlen(spec) + 1;
103 }
104 if (!num_names)
105 return -1;
106
107 names[num_names] = NULL;
108
109 /* add the entry points to the dispatch table */
110 return _glapi_add_dispatch(names, signature);
111 }
112
113
114 /**
115 * Map an array of functions. This is a convenient function for
116 * use with arrays available from including remap_helper.h.
117 *
118 * Note that the dispatch offsets of the functions are not returned.
119 * If they are needed, _mesa_map_function_spec() should be used.
120 *
121 * \param func_array an array of function remaps.
122 */
123 void
124 _mesa_map_function_array(const struct gl_function_remap *func_array)
125 {
126 GLint i;
127
128 if (!func_array)
129 return;
130
131 for (i = 0; func_array[i].func_index != -1; i++) {
132 const char *spec;
133 GLint offset;
134
135 spec = _mesa_get_function_spec(func_array[i].func_index);
136 if (!spec) {
137 _mesa_problem(NULL, "invalid function index %d",
138 func_array[i].func_index);
139 continue;
140 }
141
142 offset = _mesa_map_function_spec(spec);
143 /* error checks */
144 if (offset < 0) {
145 const char *name = spec + strlen(spec) + 1;
146 _mesa_warning(NULL, "failed to remap %s", name);
147 }
148 else if (func_array[i].dispatch_offset >= 0 &&
149 offset != func_array[i].dispatch_offset) {
150 const char *name = spec + strlen(spec) + 1;
151 _mesa_problem(NULL, "%s should be mapped to %d, not %d",
152 name, func_array[i].dispatch_offset, offset);
153 }
154 }
155 }
156
157
158 /**
159 * Map the functions which are already static.
160 *
161 * When a extension function are incorporated into the ABI, the
162 * extension suffix is usually stripped. Mapping such functions
163 * makes sure the alternative names are available.
164 *
165 * Note that functions mapped by _mesa_init_remap_table() are
166 * excluded.
167 */
168 void
169 _mesa_map_static_functions(void)
170 {
171 /* Remap static functions which have alternative names and are in the ABI.
172 * This is to be on the safe side. glapi should have defined those names.
173 */
174 _mesa_map_function_array(MESA_alt_functions);
175 }
176
177
178 /**
179 * Initialize the remap table. This is called in one_time_init().
180 * The remap table needs to be initialized before calling the
181 * CALL/GET/SET macros defined in main/dispatch.h.
182 */
183 static void
184 _mesa_do_init_remap_table(const char *pool,
185 int size,
186 const struct gl_function_pool_remap *remap)
187 {
188 static GLboolean initialized = GL_FALSE;
189 GLint i;
190
191 if (initialized)
192 return;
193 initialized = GL_TRUE;
194
195 /* initialize the remap table */
196 for (i = 0; i < size; i++) {
197 GLint offset;
198 const char *spec;
199
200 /* sanity check */
201 ASSERT(i == remap[i].remap_index);
202 spec = _mesa_function_pool + remap[i].pool_index;
203
204 offset = _mesa_map_function_spec(spec);
205 /* store the dispatch offset in the remap table */
206 driDispatchRemapTable[i] = offset;
207 if (offset < 0) {
208 const char *name = spec + strlen(spec) + 1;
209 _mesa_warning(NULL, "failed to remap %s", name);
210 }
211 }
212 }
213
214
215 void
216 _mesa_init_remap_table(void)
217 {
218 _mesa_do_init_remap_table(_mesa_function_pool,
219 driDispatchRemapTable_size,
220 MESA_remap_table_functions);
221 }