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 MAPI_TABLE_FIRST_DYNAMIC \
41 (offsetof(struct mapi_table, dynamic0) / sizeof(mapi_func))
42 #define MAPI_TABLE_NUM_DYNAMIC \
43 ((offsetof(struct mapi_table, last) - \
44 offsetof(struct mapi_table, dynamic0)) / sizeof(mapi_func))
45 #define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
48 * This will define public_string_pool, public_sorted_indices, and
51 #define MAPI_TMP_PUBLIC_STUBS
54 static struct mapi_stub dynamic_stubs
[MAPI_TABLE_NUM_DYNAMIC
];
55 static int num_dynamic_stubs
;
56 static int next_dynamic_slot
= MAPI_TABLE_FIRST_DYNAMIC
;
62 static pthread_once_t once
= PTHREAD_ONCE_INIT
;
63 pthread_once(&once
, entry_patch_public
);
74 stub_compare(const void *key
, const void *elem
)
76 const char *name
= (const char *) key
;
77 const int *index
= (const int *) elem
;
78 const struct mapi_stub
*stub
;
79 const char *stub_name
;
81 stub
= &public_stubs
[*index
];
82 stub_name
= &public_string_pool
[(unsigned long) stub
->name
];
84 return strcmp(name
, stub_name
);
88 * Return the public stub with the given name.
90 const struct mapi_stub
*
91 stub_find_public(const char *name
)
95 index
= (const int *) bsearch(name
, public_sorted_indices
,
96 ARRAY_SIZE(public_sorted_indices
) - 1,
97 sizeof(public_sorted_indices
[0]), stub_compare
);
99 return (index
) ? &public_stubs
[*index
] : NULL
;
103 * Add a dynamic stub.
105 static struct mapi_stub
*
106 stub_add_dynamic(const char *name
)
108 struct mapi_stub
*stub
;
111 idx
= num_dynamic_stubs
;
112 if (idx
>= MAPI_TABLE_NUM_DYNAMIC
)
115 stub
= &dynamic_stubs
[idx
];
117 /* dispatch to mapi_table->last, which is always no-op */
119 entry_generate(MAPI_TABLE_FIRST_DYNAMIC
+ MAPI_TABLE_NUM_DYNAMIC
);
123 stub
->name
= (const void *) name
;
124 /* to be fixed later */
127 num_dynamic_stubs
= idx
+ 1;
133 * Return the dynamic stub with the given name. If no such stub exists and
134 * generate is true, a new stub is generated.
137 stub_find_dynamic(const char *name
, int generate
)
139 u_mutex_declare_static(dynamic_mutex
);
140 struct mapi_stub
*stub
= NULL
;
143 u_mutex_lock(dynamic_mutex
);
146 assert(!stub_find_public(name
));
148 count
= num_dynamic_stubs
;
149 for (i
= 0; i
< count
; i
++) {
150 if (strcmp(name
, (const char *) dynamic_stubs
[i
].name
) == 0) {
151 stub
= &dynamic_stubs
[i
];
156 /* generate a dynamic stub */
157 if (generate
&& !stub
)
158 stub
= stub_add_dynamic(name
);
160 u_mutex_unlock(dynamic_mutex
);
166 stub_fix_dynamic(struct mapi_stub
*stub
, const struct mapi_stub
*alias
)
176 slot
= next_dynamic_slot
++;
178 entry_patch(stub
->addr
, slot
);