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
stub_init_once(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
stub_compare(const void * key,const void * elem)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 *
stub_find_public(const char * name)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 *
stub_add_dynamic(const char * name)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 *
stub_find_dynamic(const char * name,int generate)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 void
stub_fix_dynamic(struct mapi_stub * stub,const struct mapi_stub * alias)157 stub_fix_dynamic(struct mapi_stub *stub, const struct mapi_stub *alias)
158 {
159 int slot;
160
161 if (stub->slot >= 0)
162 return;
163
164 if (alias)
165 slot = alias->slot;
166 else
167 slot = next_dynamic_slot++;
168
169 entry_patch(stub->addr, slot);
170 stub->slot = slot;
171 }
172
173 /**
174 * Return the name of a stub.
175 */
176 const char *
stub_get_name(const struct mapi_stub * stub)177 stub_get_name(const struct mapi_stub *stub)
178 {
179 const char *name;
180
181 if (stub >= public_stubs &&
182 stub < public_stubs + ARRAY_SIZE(public_stubs))
183 name = &public_string_pool[(unsigned long) stub->name];
184 else
185 name = (const char *) stub->name;
186
187 return name;
188 }
189
190 /**
191 * Return the slot of a stub.
192 */
193 int
stub_get_slot(const struct mapi_stub * stub)194 stub_get_slot(const struct mapi_stub *stub)
195 {
196 return stub->slot;
197 }
198
199 /**
200 * Return the address of a stub.
201 */
202 mapi_func
stub_get_addr(const struct mapi_stub * stub)203 stub_get_addr(const struct mapi_stub *stub)
204 {
205 assert(stub->addr || (unsigned int) stub->slot < MAPI_TABLE_NUM_STATIC);
206 return (stub->addr) ? stub->addr : entry_get_public(stub->slot);
207 }
208