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>
32 #include "u_current.h"
38 #define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
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
;
58 static pthread_once_t once
= PTHREAD_ONCE_INIT
;
59 pthread_once(&once
, entry_patch_public
);
70 stub_compare(const void *key
, const void *elem
)
72 const char *name
= (const char *) key
;
73 const struct mapi_stub
*stub
= (const struct mapi_stub
*) elem
;
74 const char *stub_name
;
76 stub_name
= &public_string_pool
[(unsigned long) stub
->name
];
78 return strcmp(name
, stub_name
);
82 * Return the public stub with the given name.
84 const struct mapi_stub
*
85 stub_find_public(const char *name
)
87 return (const struct mapi_stub
*) bsearch(name
, public_stubs
,
88 ARRAY_SIZE(public_stubs
), sizeof(public_stubs
[0]), stub_compare
);
94 static struct mapi_stub
*
95 stub_add_dynamic(const char *name
)
97 struct mapi_stub
*stub
;
100 idx
= num_dynamic_stubs
;
101 /* minus 1 to make sure we can never reach the last slot */
102 if (idx
>= MAPI_TABLE_NUM_DYNAMIC
- 1)
105 stub
= &dynamic_stubs
[idx
];
107 /* dispatch to the last slot, which is reserved for no-op */
108 stub
->addr
= entry_generate(
109 MAPI_TABLE_NUM_STATIC
+ MAPI_TABLE_NUM_DYNAMIC
- 1);
113 stub
->name
= (const void *) name
;
114 /* to be fixed later */
117 num_dynamic_stubs
= idx
+ 1;
123 * Return the dynamic stub with the given name. If no such stub exists and
124 * generate is true, a new stub is generated.
127 stub_find_dynamic(const char *name
, int generate
)
129 static mtx_t dynamic_mutex
= _MTX_INITIALIZER_NP
;
130 struct mapi_stub
*stub
= NULL
;
133 mtx_lock(&dynamic_mutex
);
136 assert(!stub_find_public(name
));
138 count
= num_dynamic_stubs
;
139 for (i
= 0; i
< count
; i
++) {
140 if (strcmp(name
, (const char *) dynamic_stubs
[i
].name
) == 0) {
141 stub
= &dynamic_stubs
[i
];
146 /* generate a dynamic stub */
147 if (generate
&& !stub
)
148 stub
= stub_add_dynamic(name
);
150 mtx_unlock(&dynamic_mutex
);
155 static const struct mapi_stub
*
156 search_table_by_slot(const struct mapi_stub
*table
, size_t num_entries
,
160 for (i
= 0; i
< num_entries
; ++i
) {
161 if (table
[i
].slot
== slot
)
167 const struct mapi_stub
*
168 stub_find_by_slot(int slot
)
170 const struct mapi_stub
*stub
=
171 search_table_by_slot(public_stubs
, ARRAY_SIZE(public_stubs
), slot
);
174 return search_table_by_slot(dynamic_stubs
, num_dynamic_stubs
, slot
);
178 stub_fix_dynamic(struct mapi_stub
*stub
, const struct mapi_stub
*alias
)
188 slot
= next_dynamic_slot
++;
190 entry_patch(stub
->addr
, slot
);
195 * Return the name of a stub.
198 stub_get_name(const struct mapi_stub
*stub
)
202 if (stub
>= public_stubs
&&
203 stub
< public_stubs
+ ARRAY_SIZE(public_stubs
))
204 name
= &public_string_pool
[(unsigned long) stub
->name
];
206 name
= (const char *) stub
->name
;
212 * Return the slot of a stub.
215 stub_get_slot(const struct mapi_stub
*stub
)
221 * Return the address of a stub.
224 stub_get_addr(const struct mapi_stub
*stub
)
226 assert(stub
->addr
|| (unsigned int) stub
->slot
< MAPI_TABLE_NUM_STATIC
);
227 return (stub
->addr
) ? stub
->addr
: entry_get_public(stub
->slot
);