688dc8143a43c7fd0b5536ef0139ec212a2705f8
2 * Mesa 3-D graphics library
5 * Copyright (C) 2010 LunarG Inc.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
26 * Chia-I Wu <olv@lunarg.com>
33 #include "u_current.h"
39 #define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
47 /* define public_string_pool and public_stubs */
48 #define MAPI_TMP_PUBLIC_STUBS
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
;
59 static pthread_once_t once
= PTHREAD_ONCE_INIT
;
60 pthread_once(&once
, entry_patch_public
);
71 stub_compare(const void *key
, const void *elem
)
73 const char *name
= (const char *) key
;
74 const struct mapi_stub
*stub
= (const struct mapi_stub
*) elem
;
75 const char *stub_name
;
77 stub_name
= &public_string_pool
[(unsigned long) stub
->name
];
79 return strcmp(name
, stub_name
);
83 * Return the public stub with the given name.
85 const struct mapi_stub
*
86 stub_find_public(const char *name
)
88 return (const struct mapi_stub
*) bsearch(name
, public_stubs
,
89 ARRAY_SIZE(public_stubs
), sizeof(public_stubs
[0]), stub_compare
);
95 static struct mapi_stub
*
96 stub_add_dynamic(const char *name
)
98 struct mapi_stub
*stub
;
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)
106 stub
= &dynamic_stubs
[idx
];
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);
114 stub
->name
= (const void *) name
;
115 /* to be fixed later */
118 num_dynamic_stubs
= idx
+ 1;
124 * Return the dynamic stub with the given name. If no such stub exists and
125 * generate is true, a new stub is generated.
128 stub_find_dynamic(const char *name
, int generate
)
130 u_mutex_declare_static(dynamic_mutex
);
131 struct mapi_stub
*stub
= NULL
;
134 u_mutex_lock(dynamic_mutex
);
137 assert(!stub_find_public(name
));
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
];
147 /* generate a dynamic stub */
148 if (generate
&& !stub
)
149 stub
= stub_add_dynamic(name
);
151 u_mutex_unlock(dynamic_mutex
);
156 static const struct mapi_stub
*
157 search_table_by_slot(const struct mapi_stub
*table
, size_t num_entries
,
161 for (i
= 0; i
< num_entries
; ++i
) {
162 if (table
[i
].slot
== slot
)
168 const struct mapi_stub
*
169 stub_find_by_slot(int slot
)
171 const struct mapi_stub
*stub
=
172 search_table_by_slot(public_stubs
, ARRAY_SIZE(public_stubs
), slot
);
175 return search_table_by_slot(dynamic_stubs
, num_dynamic_stubs
, slot
);
179 stub_fix_dynamic(struct mapi_stub
*stub
, const struct mapi_stub
*alias
)
189 slot
= next_dynamic_slot
++;
191 entry_patch(stub
->addr
, slot
);
196 * Return the name of a stub.
199 stub_get_name(const struct mapi_stub
*stub
)
203 if (stub
>= public_stubs
&&
204 stub
< public_stubs
+ ARRAY_SIZE(public_stubs
))
205 name
= &public_string_pool
[(unsigned long) stub
->name
];
207 name
= (const char *) stub
->name
;
213 * Return the slot of a stub.
216 stub_get_slot(const struct mapi_stub
*stub
)
222 * Return the address of a stub.
225 stub_get_addr(const struct mapi_stub
*stub
)
227 assert(stub
->addr
|| (unsigned int) stub
->slot
< MAPI_TABLE_NUM_STATIC
);
228 return (stub
->addr
) ? stub
->addr
: entry_get_public(stub
->slot
);