stub.c revision 848b8605
1848b8605Smrg/*
2848b8605Smrg * Mesa 3-D graphics library
3848b8605Smrg *
4848b8605Smrg * Copyright (C) 2010 LunarG Inc.
5848b8605Smrg *
6848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
7848b8605Smrg * copy of this software and associated documentation files (the "Software"),
8848b8605Smrg * to deal in the Software without restriction, including without limitation
9848b8605Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10848b8605Smrg * and/or sell copies of the Software, and to permit persons to whom the
11848b8605Smrg * Software is furnished to do so, subject to the following conditions:
12848b8605Smrg *
13848b8605Smrg * The above copyright notice and this permission notice shall be included
14848b8605Smrg * in all copies or substantial portions of the Software.
15848b8605Smrg *
16848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17848b8605Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18848b8605Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19848b8605Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20848b8605Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21848b8605Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22848b8605Smrg * DEALINGS IN THE SOFTWARE.
23848b8605Smrg *
24848b8605Smrg * Authors:
25848b8605Smrg *    Chia-I Wu <olv@lunarg.com>
26848b8605Smrg */
27848b8605Smrg
28848b8605Smrg#include <stdlib.h>
29848b8605Smrg#include <string.h>
30848b8605Smrg#include <assert.h>
31848b8605Smrg
32848b8605Smrg#include "u_current.h"
33848b8605Smrg#include "u_thread.h"
34848b8605Smrg#include "entry.h"
35848b8605Smrg#include "stub.h"
36848b8605Smrg#include "table.h"
37848b8605Smrg
38848b8605Smrg#define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
39848b8605Smrg
40848b8605Smrgstruct mapi_stub {
41848b8605Smrg   const void *name;
42848b8605Smrg   int slot;
43848b8605Smrg   mapi_func addr;
44848b8605Smrg};
45848b8605Smrg
46848b8605Smrg/* define public_string_pool and public_stubs */
47848b8605Smrg#define MAPI_TMP_PUBLIC_STUBS
48848b8605Smrg#include "mapi_tmp.h"
49848b8605Smrg
50848b8605Smrgstatic struct mapi_stub dynamic_stubs[MAPI_TABLE_NUM_DYNAMIC];
51848b8605Smrgstatic int num_dynamic_stubs;
52848b8605Smrgstatic int next_dynamic_slot = MAPI_TABLE_NUM_STATIC;
53848b8605Smrg
54848b8605Smrgvoid
55848b8605Smrgstub_init_once(void)
56848b8605Smrg{
57848b8605Smrg#ifdef HAVE_PTHREAD
58848b8605Smrg   static pthread_once_t once = PTHREAD_ONCE_INIT;
59848b8605Smrg   pthread_once(&once, entry_patch_public);
60848b8605Smrg#else
61848b8605Smrg   static int first = 1;
62848b8605Smrg   if (first) {
63848b8605Smrg      first = 0;
64848b8605Smrg      entry_patch_public();
65848b8605Smrg   }
66848b8605Smrg#endif
67848b8605Smrg}
68848b8605Smrg
69848b8605Smrgstatic int
70848b8605Smrgstub_compare(const void *key, const void *elem)
71848b8605Smrg{
72848b8605Smrg   const char *name = (const char *) key;
73848b8605Smrg   const struct mapi_stub *stub = (const struct mapi_stub *) elem;
74848b8605Smrg   const char *stub_name;
75848b8605Smrg
76848b8605Smrg   stub_name = &public_string_pool[(unsigned long) stub->name];
77848b8605Smrg
78848b8605Smrg   return strcmp(name, stub_name);
79848b8605Smrg}
80848b8605Smrg
81848b8605Smrg/**
82848b8605Smrg * Return the public stub with the given name.
83848b8605Smrg */
84848b8605Smrgconst struct mapi_stub *
85848b8605Smrgstub_find_public(const char *name)
86848b8605Smrg{
87848b8605Smrg   return (const struct mapi_stub *) bsearch(name, public_stubs,
88848b8605Smrg         ARRAY_SIZE(public_stubs), sizeof(public_stubs[0]), stub_compare);
89848b8605Smrg}
90848b8605Smrg
91848b8605Smrg/**
92848b8605Smrg * Add a dynamic stub.
93848b8605Smrg */
94848b8605Smrgstatic struct mapi_stub *
95848b8605Smrgstub_add_dynamic(const char *name)
96848b8605Smrg{
97848b8605Smrg   struct mapi_stub *stub;
98848b8605Smrg   int idx;
99848b8605Smrg
100848b8605Smrg   idx = num_dynamic_stubs;
101848b8605Smrg   /* minus 1 to make sure we can never reach the last slot */
102848b8605Smrg   if (idx >= MAPI_TABLE_NUM_DYNAMIC - 1)
103848b8605Smrg      return NULL;
104848b8605Smrg
105848b8605Smrg   stub = &dynamic_stubs[idx];
106848b8605Smrg
107848b8605Smrg   /* dispatch to the last slot, which is reserved for no-op */
108848b8605Smrg   stub->addr = entry_generate(
109848b8605Smrg         MAPI_TABLE_NUM_STATIC + MAPI_TABLE_NUM_DYNAMIC - 1);
110848b8605Smrg   if (!stub->addr)
111848b8605Smrg      return NULL;
112848b8605Smrg
113848b8605Smrg   stub->name = (const void *) name;
114848b8605Smrg   /* to be fixed later */
115848b8605Smrg   stub->slot = -1;
116848b8605Smrg
117848b8605Smrg   num_dynamic_stubs = idx + 1;
118848b8605Smrg
119848b8605Smrg   return stub;
120848b8605Smrg}
121848b8605Smrg
122848b8605Smrg/**
123848b8605Smrg * Return the dynamic stub with the given name.  If no such stub exists and
124848b8605Smrg * generate is true, a new stub is generated.
125848b8605Smrg */
126848b8605Smrgstruct mapi_stub *
127848b8605Smrgstub_find_dynamic(const char *name, int generate)
128848b8605Smrg{
129848b8605Smrg   static mtx_t dynamic_mutex = _MTX_INITIALIZER_NP;
130848b8605Smrg   struct mapi_stub *stub = NULL;
131848b8605Smrg   int count, i;
132848b8605Smrg
133848b8605Smrg   mtx_lock(&dynamic_mutex);
134848b8605Smrg
135848b8605Smrg   if (generate)
136848b8605Smrg      assert(!stub_find_public(name));
137848b8605Smrg
138848b8605Smrg   count = num_dynamic_stubs;
139848b8605Smrg   for (i = 0; i < count; i++) {
140848b8605Smrg      if (strcmp(name, (const char *) dynamic_stubs[i].name) == 0) {
141848b8605Smrg         stub = &dynamic_stubs[i];
142848b8605Smrg         break;
143848b8605Smrg      }
144848b8605Smrg   }
145848b8605Smrg
146848b8605Smrg   /* generate a dynamic stub */
147848b8605Smrg   if (generate && !stub)
148848b8605Smrg         stub = stub_add_dynamic(name);
149848b8605Smrg
150848b8605Smrg   mtx_unlock(&dynamic_mutex);
151848b8605Smrg
152848b8605Smrg   return stub;
153848b8605Smrg}
154848b8605Smrg
155848b8605Smrgstatic const struct mapi_stub *
156848b8605Smrgsearch_table_by_slot(const struct mapi_stub *table, size_t num_entries,
157848b8605Smrg                     int slot)
158848b8605Smrg{
159848b8605Smrg   size_t i;
160848b8605Smrg   for (i = 0; i < num_entries; ++i) {
161848b8605Smrg      if (table[i].slot == slot)
162848b8605Smrg         return &table[i];
163848b8605Smrg   }
164848b8605Smrg   return NULL;
165848b8605Smrg}
166848b8605Smrg
167848b8605Smrgconst struct mapi_stub *
168848b8605Smrgstub_find_by_slot(int slot)
169848b8605Smrg{
170848b8605Smrg   const struct mapi_stub *stub =
171848b8605Smrg      search_table_by_slot(public_stubs, ARRAY_SIZE(public_stubs), slot);
172848b8605Smrg   if (stub)
173848b8605Smrg      return stub;
174848b8605Smrg   return search_table_by_slot(dynamic_stubs, num_dynamic_stubs, slot);
175848b8605Smrg}
176848b8605Smrg
177848b8605Smrgvoid
178848b8605Smrgstub_fix_dynamic(struct mapi_stub *stub, const struct mapi_stub *alias)
179848b8605Smrg{
180848b8605Smrg   int slot;
181848b8605Smrg
182848b8605Smrg   if (stub->slot >= 0)
183848b8605Smrg      return;
184848b8605Smrg
185848b8605Smrg   if (alias)
186848b8605Smrg      slot = alias->slot;
187848b8605Smrg   else
188848b8605Smrg      slot = next_dynamic_slot++;
189848b8605Smrg
190848b8605Smrg   entry_patch(stub->addr, slot);
191848b8605Smrg   stub->slot = slot;
192848b8605Smrg}
193848b8605Smrg
194848b8605Smrg/**
195848b8605Smrg * Return the name of a stub.
196848b8605Smrg */
197848b8605Smrgconst char *
198848b8605Smrgstub_get_name(const struct mapi_stub *stub)
199848b8605Smrg{
200848b8605Smrg   const char *name;
201848b8605Smrg
202848b8605Smrg   if (stub >= public_stubs &&
203848b8605Smrg       stub < public_stubs + ARRAY_SIZE(public_stubs))
204848b8605Smrg      name = &public_string_pool[(unsigned long) stub->name];
205848b8605Smrg   else
206848b8605Smrg      name = (const char *) stub->name;
207848b8605Smrg
208848b8605Smrg   return name;
209848b8605Smrg}
210848b8605Smrg
211848b8605Smrg/**
212848b8605Smrg * Return the slot of a stub.
213848b8605Smrg */
214848b8605Smrgint
215848b8605Smrgstub_get_slot(const struct mapi_stub *stub)
216848b8605Smrg{
217848b8605Smrg   return stub->slot;
218848b8605Smrg}
219848b8605Smrg
220848b8605Smrg/**
221848b8605Smrg * Return the address of a stub.
222848b8605Smrg */
223848b8605Smrgmapi_func
224848b8605Smrgstub_get_addr(const struct mapi_stub *stub)
225848b8605Smrg{
226848b8605Smrg   assert(stub->addr || (unsigned int) stub->slot < MAPI_TABLE_NUM_STATIC);
227848b8605Smrg   return (stub->addr) ? stub->addr : entry_get_public(stub->slot);
228848b8605Smrg}
229