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>
30 #include <stddef.h> /* for offsetof */
34 #include "u_current.h"
40 #define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
42 /* define public_string_pool and public_stubs */
43 #define MAPI_TMP_PUBLIC_STUBS
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
;
54 static pthread_once_t once
= PTHREAD_ONCE_INIT
;
55 pthread_once(&once
, entry_patch_public
);
66 stub_compare(const void *key
, const void *elem
)
68 const char *name
= (const char *) key
;
69 const struct mapi_stub
*stub
= (const struct mapi_stub
*) elem
;
70 const char *stub_name
;
72 stub_name
= &public_string_pool
[(unsigned long) stub
->name
];
74 return strcmp(name
, stub_name
);
78 * Return the public stub with the given name.
80 const struct mapi_stub
*
81 stub_find_public(const char *name
)
83 return (const struct mapi_stub
*) bsearch(name
, public_stubs
,
84 ARRAY_SIZE(public_stubs
), sizeof(public_stubs
[0]), stub_compare
);
90 static struct mapi_stub
*
91 stub_add_dynamic(const char *name
)
93 struct mapi_stub
*stub
;
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)
101 stub
= &dynamic_stubs
[idx
];
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);
109 stub
->name
= (const void *) name
;
110 /* to be fixed later */
113 num_dynamic_stubs
= idx
+ 1;
119 * Return the dynamic stub with the given name. If no such stub exists and
120 * generate is true, a new stub is generated.
123 stub_find_dynamic(const char *name
, int generate
)
125 u_mutex_declare_static(dynamic_mutex
);
126 struct mapi_stub
*stub
= NULL
;
129 u_mutex_lock(dynamic_mutex
);
132 assert(!stub_find_public(name
));
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
];
142 /* generate a dynamic stub */
143 if (generate
&& !stub
)
144 stub
= stub_add_dynamic(name
);
146 u_mutex_unlock(dynamic_mutex
);
152 stub_fix_dynamic(struct mapi_stub
*stub
, const struct mapi_stub
*alias
)
162 slot
= next_dynamic_slot
++;
164 entry_patch(stub
->addr
, slot
);