688dc8143a43c7fd0b5536ef0139ec212a2705f8
[mesa.git] / src / mapi / mapi / stub.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.9
4 *
5 * Copyright (C) 2010 LunarG Inc.
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 * Authors:
26 * Chia-I Wu <olv@lunarg.com>
27 */
28
29 #include <stdlib.h>
30 #include <string.h>
31 #include <assert.h>
32
33 #include "u_current.h"
34 #include "u_thread.h"
35 #include "entry.h"
36 #include "stub.h"
37 #include "table.h"
38
39 #define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
40
41 struct mapi_stub {
42 const void *name;
43 int slot;
44 mapi_func addr;
45 };
46
47 /* define public_string_pool and public_stubs */
48 #define MAPI_TMP_PUBLIC_STUBS
49 #include "mapi_tmp.h"
50
51 static struct mapi_stub dynamic_stubs[MAPI_TABLE_NUM_DYNAMIC];
52 static int num_dynamic_stubs;
53 static int next_dynamic_slot = MAPI_TABLE_NUM_STATIC;
54
55 void
56 stub_init_once(void)
57 {
58 #ifdef HAVE_PTHREAD
59 static pthread_once_t once = PTHREAD_ONCE_INIT;
60 pthread_once(&once, entry_patch_public);
61 #else
62 static int first = 1;
63 if (first) {
64 first = 0;
65 entry_patch_public();
66 }
67 #endif
68 }
69
70 static int
71 stub_compare(const void *key, const void *elem)
72 {
73 const char *name = (const char *) key;
74 const struct mapi_stub *stub = (const struct mapi_stub *) elem;
75 const char *stub_name;
76
77 stub_name = &public_string_pool[(unsigned long) stub->name];
78
79 return strcmp(name, stub_name);
80 }
81
82 /**
83 * Return the public stub with the given name.
84 */
85 const struct mapi_stub *
86 stub_find_public(const char *name)
87 {
88 return (const struct mapi_stub *) bsearch(name, public_stubs,
89 ARRAY_SIZE(public_stubs), sizeof(public_stubs[0]), stub_compare);
90 }
91
92 /**
93 * Add a dynamic stub.
94 */
95 static struct mapi_stub *
96 stub_add_dynamic(const char *name)
97 {
98 struct mapi_stub *stub;
99 int idx;
100
101 idx = num_dynamic_stubs;
102 /* minus 1 to make sure we can never reach the last slot */
103 if (idx >= MAPI_TABLE_NUM_DYNAMIC - 1)
104 return NULL;
105
106 stub = &dynamic_stubs[idx];
107
108 /* dispatch to the last slot, which is reserved for no-op */
109 stub->addr = entry_generate(
110 MAPI_TABLE_NUM_STATIC + MAPI_TABLE_NUM_DYNAMIC - 1);
111 if (!stub->addr)
112 return NULL;
113
114 stub->name = (const void *) name;
115 /* to be fixed later */
116 stub->slot = -1;
117
118 num_dynamic_stubs = idx + 1;
119
120 return stub;
121 }
122
123 /**
124 * Return the dynamic stub with the given name. If no such stub exists and
125 * generate is true, a new stub is generated.
126 */
127 struct mapi_stub *
128 stub_find_dynamic(const char *name, int generate)
129 {
130 u_mutex_declare_static(dynamic_mutex);
131 struct mapi_stub *stub = NULL;
132 int count, i;
133
134 u_mutex_lock(dynamic_mutex);
135
136 if (generate)
137 assert(!stub_find_public(name));
138
139 count = num_dynamic_stubs;
140 for (i = 0; i < count; i++) {
141 if (strcmp(name, (const char *) dynamic_stubs[i].name) == 0) {
142 stub = &dynamic_stubs[i];
143 break;
144 }
145 }
146
147 /* generate a dynamic stub */
148 if (generate && !stub)
149 stub = stub_add_dynamic(name);
150
151 u_mutex_unlock(dynamic_mutex);
152
153 return stub;
154 }
155
156 static const struct mapi_stub *
157 search_table_by_slot(const struct mapi_stub *table, size_t num_entries,
158 int slot)
159 {
160 size_t i;
161 for (i = 0; i < num_entries; ++i) {
162 if (table[i].slot == slot)
163 return &table[i];
164 }
165 return NULL;
166 }
167
168 const struct mapi_stub *
169 stub_find_by_slot(int slot)
170 {
171 const struct mapi_stub *stub =
172 search_table_by_slot(public_stubs, ARRAY_SIZE(public_stubs), slot);
173 if (stub)
174 return stub;
175 return search_table_by_slot(dynamic_stubs, num_dynamic_stubs, slot);
176 }
177
178 void
179 stub_fix_dynamic(struct mapi_stub *stub, const struct mapi_stub *alias)
180 {
181 int slot;
182
183 if (stub->slot >= 0)
184 return;
185
186 if (alias)
187 slot = alias->slot;
188 else
189 slot = next_dynamic_slot++;
190
191 entry_patch(stub->addr, slot);
192 stub->slot = slot;
193 }
194
195 /**
196 * Return the name of a stub.
197 */
198 const char *
199 stub_get_name(const struct mapi_stub *stub)
200 {
201 const char *name;
202
203 if (stub >= public_stubs &&
204 stub < public_stubs + ARRAY_SIZE(public_stubs))
205 name = &public_string_pool[(unsigned long) stub->name];
206 else
207 name = (const char *) stub->name;
208
209 return name;
210 }
211
212 /**
213 * Return the slot of a stub.
214 */
215 int
216 stub_get_slot(const struct mapi_stub *stub)
217 {
218 return stub->slot;
219 }
220
221 /**
222 * Return the address of a stub.
223 */
224 mapi_func
225 stub_get_addr(const struct mapi_stub *stub)
226 {
227 assert(stub->addr || (unsigned int) stub->slot < MAPI_TABLE_NUM_STATIC);
228 return (stub->addr) ? stub->addr : entry_get_public(stub->slot);
229 }