mapi: Rewrite mapi_abi.py to get rid of preprocessor magic.
[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 <stddef.h> /* for offsetof */
31 #include <string.h>
32 #include <assert.h>
33
34 #include "u_current.h"
35 #include "u_thread.h"
36 #include "entry.h"
37 #include "stub.h"
38 #include "table.h"
39
40 #define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
41
42 /* define public_string_pool and public_stubs */
43 #define MAPI_TMP_PUBLIC_STUBS
44 #include "mapi_tmp.h"
45
46 static struct mapi_stub dynamic_stubs[MAPI_TABLE_NUM_DYNAMIC];
47 static int num_dynamic_stubs;
48 static int next_dynamic_slot = MAPI_TABLE_NUM_STATIC;
49
50 void
51 stub_init_once(void)
52 {
53 #ifdef PTHREADS
54 static pthread_once_t once = PTHREAD_ONCE_INIT;
55 pthread_once(&once, entry_patch_public);
56 #else
57 static int first = 1;
58 if (first) {
59 first = 0;
60 entry_patch_public();
61 }
62 #endif
63 }
64
65 static int
66 stub_compare(const void *key, const void *elem)
67 {
68 const char *name = (const char *) key;
69 const struct mapi_stub *stub = (const struct mapi_stub *) elem;
70 const char *stub_name;
71
72 stub_name = &public_string_pool[(unsigned long) stub->name];
73
74 return strcmp(name, stub_name);
75 }
76
77 /**
78 * Return the public stub with the given name.
79 */
80 const struct mapi_stub *
81 stub_find_public(const char *name)
82 {
83 return (const struct mapi_stub *) bsearch(name, public_stubs,
84 ARRAY_SIZE(public_stubs), sizeof(public_stubs[0]), stub_compare);
85 }
86
87 /**
88 * Add a dynamic stub.
89 */
90 static struct mapi_stub *
91 stub_add_dynamic(const char *name)
92 {
93 struct mapi_stub *stub;
94 int idx;
95
96 idx = num_dynamic_stubs;
97 /* minus 1 to make sure we can never reach the last slot */
98 if (idx >= MAPI_TABLE_NUM_DYNAMIC - 1)
99 return NULL;
100
101 stub = &dynamic_stubs[idx];
102
103 /* dispatch to the last slot, which is reserved for no-op */
104 stub->addr = entry_generate(
105 MAPI_TABLE_NUM_STATIC + MAPI_TABLE_NUM_DYNAMIC - 1);
106 if (!stub->addr)
107 return NULL;
108
109 stub->name = (const void *) name;
110 /* to be fixed later */
111 stub->slot = -1;
112
113 num_dynamic_stubs = idx + 1;
114
115 return stub;
116 }
117
118 /**
119 * Return the dynamic stub with the given name. If no such stub exists and
120 * generate is true, a new stub is generated.
121 */
122 struct mapi_stub *
123 stub_find_dynamic(const char *name, int generate)
124 {
125 u_mutex_declare_static(dynamic_mutex);
126 struct mapi_stub *stub = NULL;
127 int count, i;
128
129 u_mutex_lock(dynamic_mutex);
130
131 if (generate)
132 assert(!stub_find_public(name));
133
134 count = num_dynamic_stubs;
135 for (i = 0; i < count; i++) {
136 if (strcmp(name, (const char *) dynamic_stubs[i].name) == 0) {
137 stub = &dynamic_stubs[i];
138 break;
139 }
140 }
141
142 /* generate a dynamic stub */
143 if (generate && !stub)
144 stub = stub_add_dynamic(name);
145
146 u_mutex_unlock(dynamic_mutex);
147
148 return stub;
149 }
150
151 void
152 stub_fix_dynamic(struct mapi_stub *stub, const struct mapi_stub *alias)
153 {
154 int slot;
155
156 if (stub->slot >= 0)
157 return;
158
159 if (alias)
160 slot = alias->slot;
161 else
162 slot = next_dynamic_slot++;
163
164 entry_patch(stub->addr, slot);
165 stub->slot = slot;
166 }