2
* Mesa 3-D graphics library
4
* Copyright (C) 2010 LunarG Inc.
6
* Permission is hereby granted, free of charge, to any person obtaining a
7
* copy of this software and associated documentation files (the "Software"),
8
* to deal in the Software without restriction, including without limitation
9
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
10
* and/or sell copies of the Software, and to permit persons to whom the
11
* Software is furnished to do so, subject to the following conditions:
13
* The above copyright notice and this permission notice shall be included
14
* in all copies or substantial portions of the Software.
16
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
* DEALINGS IN THE SOFTWARE.
25
* Chia-I Wu <olv@lunarg.com>
31
#include "c11/threads.h"
33
#include "util/macros.h"
34
#include "u_current.h"
46
/* define public_string_pool and public_stubs */
47
#define MAPI_TMP_PUBLIC_STUBS
50
static struct mapi_stub dynamic_stubs[MAPI_TABLE_NUM_DYNAMIC];
51
static int num_dynamic_stubs;
52
static int next_dynamic_slot = MAPI_TABLE_NUM_STATIC;
57
static once_flag flag = ONCE_FLAG_INIT;
58
call_once(&flag, entry_patch_public);
62
stub_compare(const void *key, const void *elem)
64
const char *name = (const char *) key;
65
const struct mapi_stub *stub = (const struct mapi_stub *) elem;
66
const char *stub_name;
68
stub_name = &public_string_pool[(size_t) stub->name];
70
return strcmp(name, stub_name);
74
* Return the public stub with the given name.
76
const struct mapi_stub *
77
stub_find_public(const char *name)
79
return (const struct mapi_stub *) bsearch(name, public_stubs,
80
ARRAY_SIZE(public_stubs), sizeof(public_stubs[0]), stub_compare);
86
static struct mapi_stub *
87
stub_add_dynamic(const char *name)
89
struct mapi_stub *stub;
92
idx = num_dynamic_stubs;
93
/* minus 1 to make sure we can never reach the last slot */
94
if (idx >= MAPI_TABLE_NUM_DYNAMIC - 1)
97
stub = &dynamic_stubs[idx];
99
/* dispatch to the last slot, which is reserved for no-op */
100
stub->addr = entry_generate(
101
MAPI_TABLE_NUM_STATIC + MAPI_TABLE_NUM_DYNAMIC - 1);
105
stub->name = (const void *) strdup(name);
106
/* to be fixed later */
109
num_dynamic_stubs = idx + 1;
115
* Return the dynamic stub with the given name. If no such stub exists and
116
* generate is true, a new stub is generated.
119
stub_find_dynamic(const char *name, int generate)
121
static mtx_t dynamic_mutex = _MTX_INITIALIZER_NP;
122
struct mapi_stub *stub = NULL;
125
mtx_lock(&dynamic_mutex);
128
assert(!stub_find_public(name));
130
count = num_dynamic_stubs;
131
for (i = 0; i < count; i++) {
132
if (strcmp(name, (const char *) dynamic_stubs[i].name) == 0) {
133
stub = &dynamic_stubs[i];
138
/* generate a dynamic stub */
139
if (generate && !stub)
140
stub = stub_add_dynamic(name);
142
mtx_unlock(&dynamic_mutex);
147
static const struct mapi_stub *
148
search_table_by_slot(const struct mapi_stub *table, size_t num_entries,
152
for (i = 0; i < num_entries; ++i) {
153
if (table[i].slot == slot)
159
const struct mapi_stub *
160
stub_find_by_slot(int slot)
162
const struct mapi_stub *stub =
163
search_table_by_slot(public_stubs, ARRAY_SIZE(public_stubs), slot);
166
return search_table_by_slot(dynamic_stubs, num_dynamic_stubs, slot);
170
stub_fix_dynamic(struct mapi_stub *stub, const struct mapi_stub *alias)
180
slot = next_dynamic_slot++;
182
entry_patch(stub->addr, slot);
187
* Return the name of a stub.
190
stub_get_name(const struct mapi_stub *stub)
194
if (stub >= public_stubs &&
195
stub < public_stubs + ARRAY_SIZE(public_stubs))
196
name = &public_string_pool[(size_t) stub->name];
198
name = (const char *) stub->name;
204
* Return the slot of a stub.
207
stub_get_slot(const struct mapi_stub *stub)
213
* Return the address of a stub.
216
stub_get_addr(const struct mapi_stub *stub)
218
assert(stub->addr || (unsigned int) stub->slot < MAPI_TABLE_NUM_STATIC);
219
return (stub->addr) ? stub->addr : entry_get_public(stub->slot);